Skip to content

Commit

Permalink
+ New: hardlink-runner.cmd for easy hardlinking multiple runners.
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-south-guo committed Feb 15, 2020
1 parent ab9f83e commit 92e62f6
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 1 deletion.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ A multicall Windows batch file (the _runner_) to provide a transparent method to
- An tiny open-source utility to run commands as admin, from [here](http://code.kliu.org/misc/elevate/).
- This file is **optional**. You don't need it if you are not planning to use the "Run as admin automatically" feature. (You can always start the _runner_ as admin manually.)
- It can be in the same directory as the _runner_, or in the `PATH`.
- `hardlink-runner.cmd`
- A utility script to hardlink one _runner_ to multiple _runners_ for a set of sh-scripts, so that you don't have to do it one-by-one.
- If running it without parameters, it will create _runners_ for all the sh-script in the same directory, by hard-linking from `sh-runner.cmd`.
- Use option `-h` to show help messages for more usage details.
- This file is **optional**.

### Example files

Expand All @@ -42,7 +47,7 @@ A multicall Windows batch file (the _runner_) to provide a transparent method to

## FAQ

### Will this work with WSL (Windows Subsystem for Linux)?
### Will this work with WSL, Cygwin, MSYS, etc.?

I don't know. Maybe you can try it and let me know?

Expand Down
102 changes: 102 additions & 0 deletions hardlink-runner.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
:::: Hardlink runner for multiple sh-scripts
::
:: See `:showHelp` section at the bottom for details.

@echo off

setlocal EnableDelayedExpansion

set "_SCRIPT_NAME=%~n0"
set "_SCRIPT_DIR=%~dp0"

:parseArg
if "%1"=="" goto :parseArgEnd

if "%1"=="-o" (
set "optOverwrite=yes"
shift
) else if "%1"=="-r" (
set "RUNNER_TARGET=%2"
shift & shift
) else if "%1"=="-s" (
set "SH_PATTERN=%2"
shift & shift
) else if "%1"=="-h" (
goto :showHelp
) else (
echo "Unknow argument: %1"
exit /b
)
goto :parseArg
:parseArgEnd

:: Default values
if "%RUNNER_TARGET%"=="" set "RUNNER_TARGET=%_SCRIPT_DIR%sh-runner.cmd"
if "%SH_PATTERN%"=="" set "SH_PATTERN=%_SCRIPT_DIR%*.sh"
if "%optOverwrite%"=="" set "optOverwrite=no"

echo Hardlink runner
echo ===============
echo.
echo runner target : %RUNNER_TARGET%
echo sh pattern : %SH_PATTERN%
echo overwriting : %optOverwrite%
echo.

if not exist "%RUNNER_TARGET%" (
echo "%RUNNER_TARGET%" does not exist. Abort.
exit /b
)

pause
echo.

for %%I in (%SH_PATTERN%) do (
set "skipThisItem=no"
if not exist "%%I" (
echo No sh-script matching "%SH_PATTERN%" is found.
exit /b
)
set "RUNNER_LINK=%%~dpnI.cmd"

if "!RUNNER_LINK!"=="%RUNNER_TARGET%" (
echo Will not link "%RUNNER_TARGET%" to itself.
set "skipThisItem=yes"
)

if exist "!RUNNER_LINK!" (
if "%optOverwrite%"=="yes" (
echo File exist, overwriting: "!RUNNER_LINK!"
del "!RUNNER_LINK!"
) else (
echo File exist, skipping: "!RUNNER_LINK!"
set "skipThisItem=yes"
)
)

if "!skipThisItem!"=="no" (
mklink /h "!RUNNER_LINK!" "%RUNNER_TARGET%"
)
)
exit /b

:showHelp
echo Usage:
echo %_SCRIPT_NAME% -h
echo %_SCRIPT_NAME% ^[-o^] ^[-r RUNNER_TARGET^] ^[-s SH_PATTERN^]
echo.
echo Create runners for multiple sh-scripts matching SH_PATTERN, by hardlinking from RUNNER_TARGET.
echo.
echo RUNNER_TARGET The runner script to link from. Default is "./sh-runner.cmd"
echo SH_PATTERN The pattern to match for sh-script. Default is "./*.sh"
echo.
echo OPTIONS:
echo.
echo -o Overwrite if the linking runner file exists.
echo -h Show this help.
echo.
echo Example:
echo.
echo %_SCRIPT_NAME% -o -r x:/dir1/sh-runner.cmd -s y:/dir2/*.sh

endlocal

0 comments on commit 92e62f6

Please sign in to comment.