-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathscript_actions.rb
289 lines (243 loc) · 4.4 KB
/
script_actions.rb
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
class ScriptActions
attr_accessor :download_app
@download_app = 'auto'
@@subclasses = {}
def self.create(type)
c = @@subclasses[type.to_sym]
if c
c.new
else
raise "Bad script file type: #{type}"
end
end
def self.register_script(name)
@@subclasses[name] = self
end
def file_header
''
end
def begin_lines
''
end
def end_lines
comment('End of script')
end
def comment_prefix
raise 'Not Implemented!'
end
def comment(str)
comment_prefix + ' ' + str
end
def mkdir(dir)
raise 'Not Implemented!'
end
def rmdir(dir)
raise 'Not Implemented!'
end
def rm(file)
raise 'Not Implemented!'
end
def variable(var, value)
comment_prefix + " #{var}=#{value}"
end
def parse_variable(line)
m = /#{comment_prefix}([^=]+)=(.*)$/.match(line)
unless m.nil? || m.length < 2
{ m[1].strip.to_sym => m[2].strip}
end
end
def curl_update(src, dst)
"curl -# -L -z \"#{dst}\" -o \"#{dst}\" \"#{src}\""
end
def curl_replace(src, dst)
"curl -# -L -o \"#{dst}\" \"#{src}\""
end
def wget_update(src, dst)
"wget -q -L -N \"#{src}\""
end
def unzip(zip_file, dst)
"unzip -uqo #{zip_file} -d \"#{dst}\""
end
end
class BashScriptActions < ScriptActions
def file_header
'#!/bin/bash'
end
def begin_lines
<<-eos
cd "$(dirname "$0")"
#{comment("*** Functions ***")}
#{functions}
eos
end
def functions
<<-eos
force=0
clean=0
while getopts fc opt; do
case $opt in
f) force=1 ;;
c) clean=1 ;;
esac
done
shift $((OPTIND - 1))
copy_auto() {
if [ "$clean" == "1" ]
then
echo cleaning $2
rm -f ""$2""
else
where_curl=$(type -P curl)
where_wget=$(type -P wget)
if [ "$where_curl" != "" ]
then
copy_curl "$1" "$2"
elif [ "$where_wget" != "" ]
then
copy_wget "$1" "$2"
else
echo "Missing curl or wget"
exit 1
fi
fi
}
copy_curl() {
echo "curl: $2 <= $1"
if [ -e "$2" ] && [ "$force" != "1" ]
then
#{curl_update('$1', '$2')}
else
#{curl_replace('$1', '$2')}
fi
}
copy_wget() {
echo "wget: $2 <= $1"
f1=$(basename $1)
f2=$(basename $2)
cd $(dirname $2)
#{wget_update('$1', '$f2')}
#{comment("wget has no true equivalent of curl's -o option.")}
#{comment("Different versions of wget handle (or not) % escaping differently.")}
#{comment("A URL query is the only reason why $f1 and $f2 should differ.")}
if [ "$f1" != "$f2" ]; then mv $f2\\?* $f2; fi
cd -
}
eos
end
def comment_prefix
'#'
end
def unix_path(dir)
dir.gsub!('\\','/')
unless dir[/\s+/].nil?
dir = "\"#{dir}\""
end
dir
end
def mkdir(dir)
"mkdir -p #{unix_path(dir)}"
end
def rmdir(dir)
"rm -rf #{unix_path(dir)}"
end
def rm(file)
"rm -rf #{unix_path(file)}"
end
def download(src,dst)
"copy_#{@download_app} #{src} #{unix_path(dst)}"
end
register_script :sh
end
class CmdScriptActions < ScriptActions
def file_header
'@echo off'
end
def begin_lines
<<-eos
setlocal EnableDelayedExpansion
pushd "%~dp0"
:getopts
if "%~1" == "-f" SET FORCE_DOWNLOAD=1
if "%~1" == "-c" SET CLEAN_DOWNLOAD=1
shift
if not "%~1" == "" goto getopts
eos
end
def end_lines
"endlocal\npopd\ngoto:eof\n\n" + functions + comment('End of Script')
end
def functions
<<-eos
:copy_auto
if "!CLEAN_DOWNLOAD!" == "1" (
echo. cleaning %2
DEL /F %2
) ELSE (
if "!USE_CURL!!USE_WGET!" == "" (
curl --help >nul 2>&1
if !errorlevel! == 0 (
SET USE_CURL=1
) ELSE (
wget --help >nul 2>&1
if !errorlevel! == 0 (
SET USE_WGET=1
) ELSE (
echo. curl and wget are missing!
exit /b
)
)
)
if !USE_CURL! == 1 (
call :copy_curl %1 %2
) ELSE (
IF !USE_WGET! == 1 (
call :copy_wget %1 %2
)
)
)
goto:eof
:copy_curl
echo. %~2
echo. %~1
if exist %~2 if "!FORCE_DOWNLOAD!" == "" (
#{curl_update('%~1', '%~2')}
) else (
#{curl_replace('%~1', '%~2')}
)
goto:eof
:copy_wget
echo. %~2
echo. %~1
pushd %~2\\..\\
#{wget_update('%~1', '%~2')}
popd
goto:eof
eos
end
def comment_prefix
'::'
end
def windows_path(dir)
dir.gsub!('/', '\\')
unless dir[/\s+/].nil?
dir = "\"#{dir}\""
end
dir
end
def mkdir(dir)
win_dir = windows_path(dir)
"if not exist #{win_dir}\\nul mkdir #{win_dir}"
end
def rmdir(dir)
win_dir = windows_path(dir)
"del /f/s/q #{win_dir}"
"rmdir #{win_dir}"
end
def rm(file)
"del /f/s/q #{windows_path(file)}"
end
def download(src,dst)
"call:copy_#{@download_app} #{src} #{windows_path(dst)}"
end
register_script :bat
end