mor

Cross platform dependency manager

commit 89da507fb6ea9e377a639e78f9a02b107dd4d469
parent 1bcb09084f9273d61d7fce267be54f49344f0185
Author: Bharatvaj Hemanth <bharatvaj@yahoo.com>
Date: Thu, 18 Apr 2024 01:44:21 +0530

Add LICENSE

Remove CHANGELOG in favour of git-notes

Add FAQ section in README

Add Status Messages to mor for better UX

Fix hangup while downloading with bitsadmin

Add code to cleanup bitsadmin stray jobs after download
4 files changed, 92 insertions(+), 18 deletions(-)
D
CHANGELOG
|
4
----
A
LICENSE
|
21
+++++++++++++++++++++
M
README
|
28
++++++++++++++++++++++++++--
M
mor.cmd
|
57
+++++++++++++++++++++++++++++++++++++++++++++------------
diff --git a/CHANGELOG b/CHANGELOG
@@ -1,4 +0,0 @@
-mor v0.2
---------
-- Handle urls with '&'
-- Add support for extraction using 'tar' (available in windows 10 by default)
diff --git a/LICENSE b/LICENSE
@@ -0,0 +1,21 @@
+MIT License
+
+Copyright (c) 2024 Bharatvaj Hemanth <bharatvaj@getsh.org>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
diff --git a/README b/README
@@ -12,13 +12,16 @@ Some might even say it is a red-pill way of dependency management.
 mor searches for requirements.ini file in the current folder, and uses it's
 content to download files.
 
-GOALS
+Goals
 -----
 * Should work standalone
 * Cross-platform
 * Tiny - mor.cmd for windows, mor.sh for *NIX
 
-NOTE: Check Windows section if you are using the mor.cmd
+Note
+----
+- Read the FAQ section
+- Check the Windows section below if you're using mor.cmd
 
 Config File
 ------------

@@ -100,6 +103,14 @@ Variables in mor has a specific rule and follows the following regex,
 	; arbitrary variables
 	github=https://github.com
 
+Status Messages
+---------------
+Mor uses the following symbols to denote the status to the user,
+
+v - Download
+| - Archive Extraction
+g - Git
+! - Error
 
 Example
 -------

@@ -152,6 +163,19 @@ This is a bug with how the cmd process arguments. Use -Dvar="" instead.
 - TODO Add test cases
 - TODO Merge if two section with same names are present
 
+FAQ
+---
+> Why the weird symbols ($/, $<, $>) instead of simple clean $root, $include, etc in [$]?
+I wanted mor to be as language agnostic as possible and I wanted the core variables to be
+ASCII clean. This is also the reason why the status messages are mostly made up of symbols.
+
+> Where are the damn CHANGELOGs?
+For the complete changelog,
+        git notes show v0.2~1..
+
+For a specific version,
+        git notes show v0.2
+
 Credits
 -------
 The graphics is a derivative of "butler" by jgs.
diff --git a/mor.cmd b/mor.cmd
@@ -1,7 +1,7 @@
 @echo off
 setlocal EnableDelayedExpansion
 set mor_version=0.2
-set root_dir=%cd%\out\
+set root_dir=%cd%\out
 for /f %%a in ('copy /Z "%~dpf0" nul') do set "CR=%%a"
 
 rem default values

@@ -49,6 +49,7 @@ for /f "usebackq delims=: tokens=1,*" %%l in ( `findstr /n /v ^; %~1` ) do (
 
 for /f "usebackq delims==[ tokens=1,*" %%l in (`set [`) do (
 	call :prime_download %%l %%~m 
+	if %ERRORLEVEL% equ 1 exit /b %ERRORLEVEL%
 )
 endlocal
 goto :eof

@@ -65,7 +66,9 @@ for /f %%a in ("%targets%") do (
 		:key_value
 		if "%~1" == "" goto :eof
 		for %%i in (%2) do set ext=%%~xi
-		call :download_archive !current_target_dir! %1 %2 !ext!
+		call :download_archive !current_target_dir! %1 %2 !ext! || (
+			exit /b %ERRORLEVEL%
+		)
 		call :unzip_archive !current_target_dir! %1 !ext!
 		shift
 		shift

@@ -75,14 +78,14 @@ for /f %%a in ("%targets%") do (
 endlocal
 goto :eof
 
-:unzip_archive
+:unzip_archive <download_dir> <file_name> <file_extension>
 setlocal
-	echo x [%2]
+	echo ^| [%2%3] %~1\
 	tar xzf "%1\%2%3" -C %1
 endlocal
 goto :eof
 
-:download_archive
+: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 (

@@ -93,14 +96,42 @@ setlocal
 		bitsadmin /setnoprogresstimout "%%i" 30 >>mor.log
 		bitsadmin /resume "%%i"  >>mor.log
 	:mor_download_start
-		for /f %%j in ('bitsadmin /info %job_id% ^| findstr TRANSFERRED') do (
-			goto :mor_download_end
+		if "%job_id%" == "" (
+			timeout /t 2 >nul
+			goto :mor_download_start
+		)
+		for /f %%f in ('bitsadmin /rawreturn /getstate %job_id%') do (
+			set dstate=%%f
 		)
-		for /f %%b in ('bitsadmin /rawreturn /getbytestransferred "%job_id%"') do (
-			<nul set /p"=%%b!CR!"
+		goto :BITS_%dstate% || (
+			goto :BITS_TRANSFERRING
 		)
-		timeout /t 2 >nul
-		goto :mor_download_start
+
+		:BITS_CANCELED
+		:BITS_SUSPENDED
+		:BITS_TRANSIENT_ERROR
+		:BITS_ERROR
+		:BITS_Unable
+			bitsadmin /rawreturn /cancel %job_id% >>mor.log
+			setlocal DisableDelayedExpansion
+			echo ! Error
+			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
+			goto :mor_download_end
 	:mor_download_end
 		bitsadmin /rawreturn /complete "%job_id%" >>mor.log
 	)

@@ -135,7 +166,7 @@ if "%~1" == "-v" (
 	echo mor: invalid argument '-'
 ) else (
 	set targets=%~1
-	echo %targets%
+	echo !targets!:
 )
 if "%arg:~0,1%" == "=" echo "= command"
 

@@ -143,7 +174,9 @@ shift
 goto parse
 :main_continue
 call :read_ini "%config_file%"
+if %ERRORLEVEL% equ 1 exit /b %ERRORLEVEL%
 endlocal
 goto :eof
 
 endlocal
+:mor_end_program