mor

Cross platform dependency manager
git clone git@getsh.org:mor.git
Log | Files | Refs | README | LICENSE

mor.cmd (6846B)


      1 @echo off
      2 setlocal
      3 set mor_version=0.8
      4 set root_dir=%cd%
      5 for /f %%a in ('copy /Z "%~dpf0" nul') do set "CR=%%a"
      6 
      7 rem default values
      8 set /a is_logi=0
      9 set config_file=requirements.ini
     10 set MOR_EXTS_TAR=.tar.gz,.tgz,.zip
     11 
     12 if "%~1" == "" if not exist %config_file% goto print_usage
     13 goto :main
     14 
     15 :logi
     16 	if %is_logi% equ 1 echo %*
     17 goto :eof
     18 
     19 :print_usage
     20 	echo Usage: mor [ -c requirements.ini] [-d] [-Dvar1=value1 ...] [[@]target1, ...]
     21 goto :eof
     22 
     23 :read_ini <config_file.ini>
     24 setlocal EnableDelayedExpansion
     25 	set /a section_count=0
     26 	set current_section=[
     27 
     28 	rem locally remove env variables starting with '['
     29 	set [ 2>nul && for /f "usebackq delims== tokens=1" %%l in ( `set [` ) do (
     30 		set %%l=
     31 	)
     32 
     33 	for /f "eol=; usebackq delims==] tokens=1,*" %%a in (%~1) do (
     34 		set tok=%%~a
     35 		set val=%%~b
     36 		if "!tok:~0,1!" == "[" (
     37 			set current_section=!tok!
     38 			call :logi #!current_section!
     39 		) else (
     40 			set MOR_V_ 2>NUL >NUL
     41 			if not ERRORLEVEL 1 (
     42 				for /f "usebackq delims== tokens=1*" %%s in (`set MOR_V_`) do (
     43 					set varname=%%s
     44 					set varname=!varname:MOR_V_=!
     45 					call set val=%%val:$!varname!=%%t%%
     46 				)
     47 			)
     48 			if "!current_section:~1!" == "$" (
     49 				if "!tok!"=="/" (
     50 					set root_dir=!val!
     51 				) else (
     52 					set MOR_V_!tok!=!val!
     53 				)
     54 			) else (
     55 				set key=!current_section![!tok!]
     56 				set !key!=!val!
     57 				call :logi # 	[!tok!] "!val!"
     58 			)
     59 		)
     60 	)
     61 
     62 	set wtargets=%targets%
     63 :MOR_TARGETS_START
     64 	for /f "tokens=1*" %%a in ("%wtargets%") do (
     65 		set target=%%~a
     66 
     67 		set [!target![ 2>NUL >NUL
     68 		if ERRORLEVEL 1 (
     69 			echo ^> Cannot find target '%%~a'
     70 			exit /b 1
     71 		)
     72 
     73 		echo !target!:
     74 		for /f "usebackq tokens=1,2* delims=[]=" %%l in (`set  [!target![`) do (
     75 			if "!target:~0,1!"=="#" (
     76 				echo ^> Target definitions ^(i.e #targets^) cannot be invoked
     77 				exit /b 1
     78 			) else if "!target:~0,1!"=="@" (
     79 				set section=%%~l
     80 				set section=!section:~1!
     81 				call :parse_target !section! %%~m %%~n
     82 				if ERRORLEVEL 1 exit /b !ERRORLEVEL!
     83 			) else (
     84 				call :prime_download %%~l %%~m %%~n
     85 				if ERRORLEVEL 1 exit /b !ERRORLEVEL!
     86 			)
     87 		)
     88 
     89 		set wtargets=%%b
     90 		if not [!wtargets!] == [] goto :MOR_TARGETS_START
     91 	)
     92 endlocal DisableDelayedExpansion
     93 goto :eof
     94 
     95 :parse_target <section> <target_definition> <value>
     96 setlocal EnableDelayedExpansion
     97 	set section=%1
     98 	set wsections=![#%section%[/]!
     99 :MOR_PARSE_TARGET_START
    100 	for /f "usebackq tokens=1* delims= " %%a in ('!wsections!') do (
    101 		set [%%a[ 2>NUL >NUL
    102 		if ERRORLEVEL 1 (
    103 			echo ^> Cannot find target '%%~a'
    104 			exit /b 1
    105 		)
    106 
    107 		for /f "usebackq tokens=1,2,3* delims=[]=" %%e in (`set  [%%a[`) do (
    108 			if "%%~f"=="%~2-%~3" (
    109 				if not defined %%~f_done (
    110 					set %%~f_done=1
    111 					call :prime_download %%e %%~f %%~g || goto :eof
    112 					exit /b 0
    113 				)
    114 			)
    115 		)
    116 
    117 		set wsections=%%b
    118 		if not [%%b] == [] goto :MOR_PARSE_TARGET_START
    119 	)
    120 	echo ^> Cannot find key '%~2-%~3'
    121 	exit /b 1
    122 endlocal DisableDelayedExpansion
    123 goto :eof
    124 
    125 :prime_download <section> <target> <url>
    126 setlocal EnableDelayedExpansion
    127 	set current_target_dir="!root_dir!\%1"
    128 	shift
    129 	call :logi Current Target Dir: !current_target_dir!
    130 	if not exist "!current_target_dir!" mkdir "!current_target_dir!"
    131 	:MOR_KEY_VAULE
    132 	if "%~1" == "" goto :eof
    133 	rem %ext% will have the last '.' splitted word
    134 	for %%i in (%2) do set ext=%%~xi
    135 
    136 	set repo_url=%~2
    137 	rem the 'y' is there to prevent %ext% from taking over the script
    138 	if not y%ext:git@=%==y%ext% (
    139 		rem Splitting a string using string
    140 		set i=1
    141 		set "fn!i!=%repo_url:.git@=" & set /A i+=1 & set "fn!i!=%"
    142 		set repo_url=!fn1!
    143 		set repo_branch=!fn2!
    144 	)
    145 
    146 	if not "!repo_branch!"=="" (
    147 		call :clone_git "!current_target_dir!" %1 "!repo_url!" "!repo_branch!"
    148 	) else (
    149 		where /q curl
    150 		if ERRORLEVEL 1 (
    151 			call :download_archive !current_target_dir! %1 %2 !ext!
    152 			if ERRORLEVEL 1 exit /b !ERRORLEVEL!
    153 		) else (
    154 			call :download_archive_curl !current_target_dir! %1 %2 !ext!
    155 			if ERRORLEVEL 1 exit /b !ERRORLEVEL!
    156 		)
    157 	)
    158 
    159 
    160 	for %%x in (%MOR_EXTS_TAR%) do (
    161 		if "!ext!"=="%%x" (
    162 			call :unzip_archive !current_target_dir! %1 !ext!
    163 			if ERRORLEVEL 1 exit /b !ERRORLEVEL!
    164 			goto :MOR_AFTER_EXTRACT
    165 		)
    166 	)
    167 
    168 	:MOR_AFTER_EXTRACT
    169 	shift
    170 	shift
    171 	goto :MOR_KEY_VAULE
    172 endlocal
    173 goto :eof
    174 
    175 :unzip_archive <download_dir> <file_name> <file_extension>
    176 setlocal
    177 	echo ^| [%~2%~3] %~1\
    178 	tar xzf "%~1\%~2%~3" -C "%~1"
    179 	exit /b %ERRORLEVEL%
    180 endlocal
    181 goto :eof
    182 
    183 :download_archive_curl <download_dir> <file_name> <url> <file_extension>
    184 setlocal
    185 	echo v [%2] %3
    186 	curl -Lf "%~3" -o "%~1\%~2%~4" 2>>mor.log
    187 	exit /b %ERRORLEVEL%
    188 endlocal
    189 goto :eof
    190 
    191 :download_archive <download_dir> <file_name> <url> <file_extension>
    192 setlocal
    193 	echo v [%2] %3
    194 	for /f "usebackq" %%i in (`bitsadmin /rawreturn /create "mor:%2"`) do (
    195 		set job_id=%%i
    196 		bitsadmin /rawreturn /addfile "%%i" %3 "%~1\%2%4" >>mor.log
    197 		bitsadmin /setsecurityflags "%%i" 0x0000 >>mor.log
    198 		bitsadmin /setpriority "%%i" HIGH >>mor.log
    199 		bitsadmin /setnoprogresstimeout "%%i" 30 >>mor.log
    200 		bitsadmin /resume "%%i"  >>mor.log
    201 	:MOR_DOWNLOAD_START
    202 		if "%job_id%" == "" (
    203 			timeout /t 2 >nul
    204 			goto :MOR_DOWNLOAD_START
    205 		)
    206 		for /f %%f in ('bitsadmin /rawreturn /getstate %job_id%') do (
    207 			set dstate=%%f
    208 		)
    209 		goto :BITS_%dstate% || (
    210 			goto :BITS_TRANSFERRING
    211 		)
    212 
    213 		:BITS_CANCELED
    214 		:BITS_SUSPENDED
    215 		:BITS_TRANSIENT_ERROR
    216 		:BITS_ERROR
    217 		:BITS_Unable
    218 			bitsadmin /rawreturn /cancel %job_id% >>mor.log
    219 			setlocal DisableDelayedExpansion
    220 			exit /b 1
    221 		goto :eof
    222 		:BITS_TRANSFERRING
    223 		:BITS_CONNECTING
    224 			for /f %%b in ('bitsadmin /rawreturn /getbytestransferred "%job_id%"') do (
    225 				<nul set /p"=%%b!CR!"
    226 			)
    227 		:BITS_CONNECTING
    228 		:BITS_ACKNOWLEDGED
    229 		:BITS_QUEUED
    230 		:BITS_Wait
    231 		:BITS_Wait2
    232 		:BITS_ERWait
    233 			timeout /t 1 >nul
    234 			goto:MOR_DOWNLOAD_START
    235 		:BITS_TRANSFERRED
    236 
    237 		bitsadmin /rawreturn /complete "%job_id%" >>mor.log
    238 	)
    239 endlocal
    240 goto :eof
    241 
    242 :clone_git <directory> <name> <repo> <branch>
    243 setlocal EnableDelayedExpansion
    244 	echo G %~3 (%~4)
    245 	set dest_dir="%~1\%~2"
    246 	if exist !dest_dir! (
    247 		call :logi "!dest_dir! Alreay exists"
    248 	) else (
    249 		git clone "%~3" -b "%~4" !dest_dir! 2>>mor.log
    250 	)
    251 	exit /b %ERRORLEVEL%
    252 endlocal DisableDelayedExpansion
    253 goto :eof
    254 
    255 :main
    256 setlocal EnableDelayedExpansion
    257 :parse
    258 set arg=%~1
    259 if "%~1" == "" goto :MOR_MAIN_CONTINUE
    260 if "%~1" == "-v" (
    261 	echo mor v%mor_version%
    262 	goto :eof
    263 ) else if "%arg%" == "-c" (
    264 	set config_file="%~2"
    265 	call :logi -c !config_file!
    266 	shift
    267 ) else if "%arg%" == "-d" (
    268 	set /a is_logi=1
    269 ) else if "%arg%" == "-D" (
    270 	set d=%~2
    271 	call :logi -D !d!=%~3
    272 	shift
    273 	shift
    274 ) else if "%arg:~0,2%" == "-D" (
    275 	set arg=%~1
    276 	set d=%arg:~2%
    277 	call :logi -D!d!=%~2
    278 	shift
    279 ) else if "%arg%" == "-" (
    280 	echo mor: invalid argument '-'
    281 ) else (
    282 	set targets=%targets% %~1
    283 )
    284 if "%arg:~0,1%" == "=" echo "= command"
    285 
    286 shift
    287 goto :parse
    288 :MOR_MAIN_CONTINUE
    289 call :read_ini "%config_file%"
    290 if ERRORLEVEL 1 (
    291 	echo ^^^! Error
    292 	exit /b !ERRORLEVEL!
    293 )
    294 endlocal DisableDelayedExpansion
    295 goto :eof
    296 
    297 endlocal