-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(script): add
initool-overwrite.*
scripts
- Loading branch information
Showing
3 changed files
with
121 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
@echo off | ||
setlocal enabledelayedexpansion | ||
|
||
for %%a in (%*) do ( | ||
if "%%a"=="-h" ( | ||
goto :help | ||
) | ||
if "%%a"=="--help" ( | ||
goto :help | ||
) | ||
) | ||
|
||
if "%~2"=="" ( | ||
goto :error_usage | ||
) | ||
|
||
set "command=%~1" | ||
set "file=%~2" | ||
set status=0 | ||
set overwrite=1 | ||
|
||
if "%command%"=="e" ( | ||
set overwrite=0 | ||
) | ||
if "%command%"=="exists" ( | ||
set overwrite=0 | ||
) | ||
|
||
if "%file%"=="-" ( | ||
echo file must not be "-" 1>&2 | ||
exit /b 2 | ||
) | ||
|
||
:create_temp | ||
set "temp=%temp%\initool-in-place-%random%.tmp" | ||
if exist "!temp!" goto :create_temp | ||
|
||
if defined INITOOL ( | ||
"!INITOOL!" %* > "!temp!" | ||
) else ( | ||
initool %* > "!temp!" | ||
) | ||
|
||
set status=%errorlevel% | ||
if %status% equ 0 ( | ||
if %overwrite% equ 1 ( | ||
copy /y "!temp!" "%file%" > nul | ||
) | ||
) | ||
|
||
:cleanup | ||
del "%temp%" | ||
exit /b %status% | ||
|
||
:error_usage | ||
echo usage: %~nx0 command file [arg ...] 1>&2 | ||
exit /b 2 | ||
|
||
:help | ||
echo Modify a file in place with initool. | ||
echo. | ||
echo usage: %~nx0 command file [arg ...] | ||
echo. | ||
echo You can give the path to initool in the environment variable "INITOOL". | ||
exit /b |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#! /bin/sh | ||
set -eu | ||
|
||
usage() { | ||
echo "usage: $(basename "$0") command file [arg ...]" | ||
} | ||
|
||
help() { | ||
printf 'Modify a file in place with initool.\n\n' | ||
usage | ||
printf '\nYou can give the path to initool in the environment variable "INITOOL".\n' | ||
} | ||
|
||
for arg in "$@"; do | ||
if [ "$arg" = "-h" ] || [ "$arg" = "--help" ]; then | ||
help | ||
exit 0 | ||
fi | ||
done | ||
|
||
if [ $# -lt 2 ]; then | ||
usage >/dev/stderr | ||
exit 2 | ||
fi | ||
|
||
command=$1 | ||
file=$2 | ||
replace_original=1 | ||
|
||
if [ "$command" = e ] || [ "$command" = exists ]; then | ||
replace_original=0 | ||
fi | ||
|
||
if [ "$file" = - ]; then | ||
echo 'file must not be "-"' >/dev/stderr | ||
exit 2 | ||
fi | ||
|
||
temp=$(mktemp) | ||
clean_up() { | ||
rm "$temp" 2>/dev/null || true | ||
} | ||
trap clean_up EXIT | ||
|
||
"${INITOOL:-initool}" "$@" >"$temp" | ||
status=$? | ||
|
||
if [ "$status" -eq 0 ] && [ "$replace_original" -eq 1 ]; then | ||
mv "$temp" "$file" | ||
else | ||
exit "$status" | ||
fi |