-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall.lua
executable file
·185 lines (160 loc) · 4.7 KB
/
install.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#!/usr/bin/env lua
test = false
basedir = os.getenv('PWD') ..'/'.. debug.getinfo(1).source:match("@?(.*/)")
dofile(basedir .. 'config.lua') -- global variables: links, scripts, copies
function run_scripts()
-- run scripts
for i = 1,#scripts do
local script = basedir .. scripts[i]
if is_file(script) then
os.execute(script)
else
print('Script: `' .. script.. '` not exists')
end
end
end
function run_links(forced)
for i = 1,#links do
links[i].src = basedir .. links[i].src
if test then
links[i].dst = basedir .. links[i].dst
end
end
link(links,forced)
end
function run_copies()
for i = 1,#copies do
copies[i].src = basedir .. copies[i].src
if test then
copies[i].dst = basedir .. copies[i].dst
end
end
copy(copies)
end
function dirname(s)
return is_dir(s) and (s:match("(.*)/") or s) or (s:match("(.*)/.*") or '.')
end
function link_sf(src, dst); os.execute('ln -sf ' .. src .. ' ' .. dst); end
function cp_r(src, dst); os.execute('cp -r ' .. src .. ' ' .. dst); end
function exists(name); return os.execute('[ -e ' ..name.. ' ]'); end
function is_file(name); return os.execute('[ -f ' ..name.. ' ]'); end
function is_link(name); return os.execute('[ -L ' ..name.. ' ]'); end
function is_dir(name); return os.execute('[ -d ' ..name.. ' ]'); end
function is_link_or_not_exists_but_parent_exists(name); return ( (not exists(name) and exists(dirname(name))) or is_link(name) ); end
function mkdir(name); os.execute('mkdir -p ' .. name); end
function rm(name); os.execute('rm -rf ' .. name); end
function childjobs(s, d)
if not is_dir(d) or not is_dir(s) then
print(d..' or '..s.. ' is not a directory.')
return
end
local src, dst = s..'/', d..'/'
local i, children, popen = 0, {}, io.popen
local pfile = popen('ls ' ..src)
for filename in pfile:lines() do
i = i + 1
local filefullname = src..filename
if is_file(filefullname) then
children[i] = {src=src..filename, dst=dst..filename}
elseif is_dir(filefullname) then
children[i] = {src=src..filename, dst=dst..filename, mode='children'}
else
print('? => s='..src..filename..', d='..dst..filename)
end
end
return children
end
function copy(jobs)
for i = 1, #jobs do
local item = jobs[i]
local src, dst = item.src, item.dst
if item.mode == 'overwrite' then
if exists(dst) then
rm(dst)
end
cp_r(src, dst)
end
end
end
function link(jobs, forced)
for i = 1, #jobs do
local item = jobs[i]
local src, dst = item.src, item.dst
if is_file(src) then
print('link ' .. src .. ' <- ' .. dst)
if is_link_or_not_exists_but_parent_exists(dst) then
-- a link, or not exists but dir exists
rm(dst)
link_sf(src, dst)
elseif not exists(dirname(dst)) then
-- parent dir not exists
print('Parent dir: `' ..dirname(dst).. '` not exists')
else
-- file exists, not a link
if forced then
link_sf(src, dst)
else
print('File: `' ..dst.. '` exists. Use `-f` to override it.')
end
end
elseif is_dir(src) then
if item.mode == 'children' then
if is_link(dst) then
os.execute('rm ' .. dst)
end
if not exists(dst) then
print('Dir: `' ..dst.. '` not exists, with mode `children`.')
else
children = childjobs(src, dst)
link(children, forced)
end
elseif item.mode == 'sub' then
if is_link_or_not_exists_but_parent_exists(dst) then
-- a link, or not exists but dir exists
rm(dst)
link_sf(src,dst)
elseif not exists(dirname(dst)) then
-- parent dir not exists
print('Parent dir: `' ..dirname(dst).. '` not exists')
else
-- dir exists, not a link
if forced then
rm(dst)
link_sf(src,dst)
else
print('Dir: `'..dst..'` exists. Use `-f` to override it.')
end
end
else
print('Mode of dir: ' ..dst.. ' unrecognized.')
end
else
print('Source: ' ..src.. ' not found.')
end
end
end
function has(l, el)
for i = 0,#l do
if el == l[i] then
return true
end
end
return false
end
function arghas(el); return has(arg,el); end
function main()
local forced = arghas('-f')
print('force mode: ' .. tostring(forced))
if arghas('all') then
run_links(forced)
run_scripts()
run_copies()
elseif arghas('links') and not arghas('scripts') then
run_links(forced)
elseif arghas('scripts') and not arghas('links') then
run_scripts()
else
print('Nothing Executed, check your params: `all` | `links` | `scripts`')
end
end
main()