mor

Cross platform dependency manager

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 
290 
291 
292 
293 
294 
295 
296 
297 
@echo off
setlocal
set mor_version=0.8
set root_dir=%cd%
for /f %%a in ('copy /Z "%~dpf0" nul') do set "CR=%%a"

rem default values
set /a is_logi=0
set config_file=requirements.ini
set MOR_EXTS_TAR=.tar.gz,.tgz,.zip

if "%~1" == "" if not exist %config_file% goto print_usage
goto :main

:logi
	if %is_logi% equ 1 echo %*
goto :eof

:print_usage
	echo Usage: mor [ -c requirements.ini] [-d] [-Dvar1=value1 ...] [[@]target1, ...]
goto :eof

:read_ini <config_file.ini>
setlocal EnableDelayedExpansion
	set /a section_count=0
	set current_section=[

	rem locally remove env variables starting with '['
	set [ 2>nul && for /f "usebackq delims== tokens=1" %%l in ( `set [` ) do (
		set %%l=
	)

	for /f "eol=; usebackq delims==] tokens=1,*" %%a in (%~1) do (
		set tok=%%~a
		set val=%%~b
		if "!tok:~0,1!" == "[" (
			set current_section=!tok!
			call :logi #!current_section!
		) else (
			set MOR_V_ 2>NUL >NUL
			if not ERRORLEVEL 1 (
				for /f "usebackq delims== tokens=1*" %%s in (`set MOR_V_`) do (
					set varname=%%s
					set varname=!varname:MOR_V_=!
					call set val=%%val:$!varname!=%%t%%
				)
			)
			if "!current_section:~1!" == "$" (
				if "!tok!"=="/" (
					set root_dir=!val!
				) else (
					set MOR_V_!tok!=!val!
				)
			) else (
				set key=!current_section![!tok!]
				set !key!=!val!
				call :logi # 	[!tok!] "!val!"
			)
		)
	)

	set wtargets=%targets%
:MOR_TARGETS_START
	for /f "tokens=1*" %%a in ("%wtargets%") do (
		set target=%%~a

		set [!target![ 2>NUL >NUL
		if ERRORLEVEL 1 (
			echo ^> Cannot find target '%%~a'
			exit /b 1
		)

		echo !target!:
		for /f "usebackq tokens=1,2* delims=[]=" %%l in (`set  [!target![`) do (
			if "!target:~0,1!"=="#" (
				echo ^> Target definitions ^(i.e #targets^) cannot be invoked
				exit /b 1
			) else if "!target:~0,1!"=="@" (
				set section=%%~l
				set section=!section:~1!
				call :parse_target !section! %%~m %%~n
				if ERRORLEVEL 1 exit /b !ERRORLEVEL!
			) else (
				call :prime_download %%~l %%~m %%~n
				if ERRORLEVEL 1 exit /b !ERRORLEVEL!
			)
		)

		set wtargets=%%b
		if not [!wtargets!] == [] goto :MOR_TARGETS_START
	)
endlocal DisableDelayedExpansion
goto :eof

:parse_target <section> <target_definition> <value>
setlocal EnableDelayedExpansion
	set section=%1
	set wsections=![#%section%[/]!
:MOR_PARSE_TARGET_START
	for /f "usebackq tokens=1* delims= " %%a in ('!wsections!') do (
		set [%%a[ 2>NUL >NUL
		if ERRORLEVEL 1 (
			echo ^> Cannot find target '%%~a'
			exit /b 1
		)

		for /f "usebackq tokens=1,2,3* delims=[]=" %%e in (`set  [%%a[`) do (
			if "%%~f"=="%~2-%~3" (
				if not defined %%~f_done (
					set %%~f_done=1
					call :prime_download %%e %%~f %%~g || goto :eof
					exit /b 0
				)
			)
		)

		set wsections=%%b
		if not [%%b] == [] goto :MOR_PARSE_TARGET_START
	)
	echo ^> Cannot find key '%~2-%~3'
	exit /b 1
endlocal DisableDelayedExpansion
goto :eof

:prime_download <section> <target> <url>
setlocal EnableDelayedExpansion
	set current_target_dir="!root_dir!\%1"
	shift
	call :logi Current Target Dir: !current_target_dir!
	if not exist "!current_target_dir!" mkdir "!current_target_dir!"
	:MOR_KEY_VAULE
	if "%~1" == "" goto :eof
	rem %ext% will have the last '.' splitted word
	for %%i in (%2) do set ext=%%~xi

	set repo_url=%~2
	rem the 'y' is there to prevent %ext% from taking over the script
	if not y%ext:git@=%==y%ext% (
		rem Splitting a string using string
		set i=1
		set "fn!i!=%repo_url:.git@=" & set /A i+=1 & set "fn!i!=%"
		set repo_url=!fn1!
		set repo_branch=!fn2!
	)

	if not "!repo_branch!"=="" (
		call :clone_git "!current_target_dir!" %1 "!repo_url!" "!repo_branch!"
	) else (
		where /q curl
		if ERRORLEVEL 1 (
			call :download_archive !current_target_dir! %1 %2 !ext!
			if ERRORLEVEL 1 exit /b !ERRORLEVEL!
		) else (
			call :download_archive_curl !current_target_dir! %1 %2 !ext!
			if ERRORLEVEL 1 exit /b !ERRORLEVEL!
		)
	)


	for %%x in (%MOR_EXTS_TAR%) do (
		if "!ext!"=="%%x" (
			call :unzip_archive !current_target_dir! %1 !ext!
			if ERRORLEVEL 1 exit /b !ERRORLEVEL!
			goto :MOR_AFTER_EXTRACT
		)
	)

	:MOR_AFTER_EXTRACT
	shift
	shift
	goto :MOR_KEY_VAULE
endlocal
goto :eof

:unzip_archive <download_dir> <file_name> <file_extension>
setlocal
	echo ^| [%~2%~3] %~1\
	tar xzf "%~1\%~2%~3" -C "%~1"
	exit /b %ERRORLEVEL%
endlocal
goto :eof

:download_archive_curl <download_dir> <file_name> <url> <file_extension>
setlocal
	echo v [%2] %3
	curl -Lf "%~3" -o "%~1\%~2%~4" 2>>mor.log
	exit /b %ERRORLEVEL%
endlocal
goto :eof

:download_archive <download_dir> <file_name> <url> <file_extension>
setlocal
	echo v [%2] %3
	for /f "usebackq" %%i in (`bitsadmin /rawreturn /create "mor:%2"`) do (
		set job_id=%%i
		bitsadmin /rawreturn /addfile "%%i" %3 "%~1\%2%4" >>mor.log
		bitsadmin /setsecurityflags "%%i" 0x0000 >>mor.log
		bitsadmin /setpriority "%%i" HIGH >>mor.log
		bitsadmin /setnoprogresstimeout "%%i" 30 >>mor.log
		bitsadmin /resume "%%i"  >>mor.log
	:MOR_DOWNLOAD_START
		if "%job_id%" == "" (
			timeout /t 2 >nul
			goto :MOR_DOWNLOAD_START
		)
		for /f %%f in ('bitsadmin /rawreturn /getstate %job_id%') do (
			set dstate=%%f
		)
		goto :BITS_%dstate% || (
			goto :BITS_TRANSFERRING
		)

		:BITS_CANCELED
		:BITS_SUSPENDED
		:BITS_TRANSIENT_ERROR
		:BITS_ERROR
		:BITS_Unable
			bitsadmin /rawreturn /cancel %job_id% >>mor.log
			setlocal DisableDelayedExpansion
			exit /b 1
		goto :eof
		:BITS_TRANSFERRING
		:BITS_CONNECTING
			for /f %%b in ('bitsadmin /rawreturn /getbytestransferred "%job_id%"') do (
				<nul set /p"=%%b!CR!"
			)
		:BITS_CONNECTING
		:BITS_ACKNOWLEDGED
		:BITS_QUEUED
		:BITS_Wait
		:BITS_Wait2
		:BITS_ERWait
			timeout /t 1 >nul
			goto:MOR_DOWNLOAD_START
		:BITS_TRANSFERRED

		bitsadmin /rawreturn /complete "%job_id%" >>mor.log
	)
endlocal
goto :eof

:clone_git <directory> <name> <repo> <branch>
setlocal EnableDelayedExpansion
	echo G %~3 (%~4)
	set dest_dir="%~1\%~2"
	if exist !dest_dir! (
		call :logi "!dest_dir! Alreay exists"
	) else (
		git clone "%~3" -b "%~4" !dest_dir! 2>>mor.log
	)
	exit /b %ERRORLEVEL%
endlocal DisableDelayedExpansion
goto :eof

:main
setlocal EnableDelayedExpansion
:parse
set arg=%~1
if "%~1" == "" goto :MOR_MAIN_CONTINUE
if "%~1" == "-v" (
	echo mor v%mor_version%
	goto :eof
) else if "%arg%" == "-c" (
	set config_file="%~2"
	call :logi -c !config_file!
	shift
) else if "%arg%" == "-d" (
	set /a is_logi=1
) else if "%arg%" == "-D" (
	set d=%~2
	call :logi -D !d!=%~3
	shift
	shift
) else if "%arg:~0,2%" == "-D" (
	set arg=%~1
	set d=%arg:~2%
	call :logi -D!d!=%~2
	shift
) else if "%arg%" == "-" (
	echo mor: invalid argument '-'
) else (
	set targets=%targets% %~1
)
if "%arg:~0,1%" == "=" echo "= command"

shift
goto :parse
:MOR_MAIN_CONTINUE
call :read_ini "%config_file%"
if ERRORLEVEL 1 (
	echo ^^^! Error
	exit /b !ERRORLEVEL!
)
endlocal DisableDelayedExpansion
goto :eof

endlocal