1
0

new argument parser and some features (not tested)

This commit is contained in:
GenZmeY 2022-02-14 02:11:02 +03:00
parent 015239e34a
commit 5c3a7d78a9

307
builder
View File

@ -54,12 +54,27 @@ KFPublishLocalization="$KFPublish/Localization"
MutWsInfo="$KFDoc/wsinfo.txt" MutWsInfo="$KFDoc/wsinfo.txt"
KFEditorConfBackup="$KFEditorConf.backup" KFEditorConfBackup="$KFEditorConf.backup"
# Args
ArgInitBuild="false"
ArgInitTest="false"
ArgCompile="false"
ArgBrew="false"
ArgBrewManual="false"
ArgUpload="false"
ArgTest="false"
ArgVersion="false"
ArgHelp="false"
ArgDebug="false"
ArgQuiet="false"
ArgWarnings="false"
ArgNoColors="false"
function reg_readkey () # $1: path, $2: key function reg_readkey () # $1: path, $2: key
{ {
cygpath -u $( cygpath -u "$(
reg query "$1" //v "$2" | \ reg query "$1" //v "$2" | \
grep -F "$2" | \ grep -F "$2" | \
awk '{ $1=$2=""; print $0 }' ) awk '{ $1=$2=""; print $0 }')"
} }
function is_true () # $1: Bool arg to check function is_true () # $1: Bool arg to check
@ -70,7 +85,8 @@ function is_true () # $1: Bool arg to check
function get_latest () # $1: Reponame, $2: filename, $3: output filename function get_latest () # $1: Reponame, $2: filename, $3: output filename
{ {
local ApiUrl="https://api.github.com/repos/$1/releases/latest" local ApiUrl="https://api.github.com/repos/$1/releases/latest"
local LatestTag=$(curl --silent "$ApiUrl" | grep -Po '"tag_name": "\K.*?(?=")') local LatestTag=""
LatestTag=$(curl --silent "$ApiUrl" | grep -Po '"tag_name": "\K.*?(?=")')
local DownloadUrl="https://github.com/$1/releases/download/$LatestTag/$2" local DownloadUrl="https://github.com/$1/releases/download/$LatestTag/$2"
mkdir -p "$(dirname "$3")/" mkdir -p "$(dirname "$3")/"
@ -79,66 +95,112 @@ function get_latest () # $1: Reponame, $2: filename, $3: output filename
function get_latest_multini () # $1: file to save function get_latest_multini () # $1: file to save
{ {
msg "download latest multini"
get_latest "GenZmeY/multini" "multini-windows-amd64.exe" "$1" get_latest "GenZmeY/multini" "multini-windows-amd64.exe" "$1"
} }
function get_latest_kfeditor_patcher () # $1: file to save function get_latest_kfeditor_patcher () # $1: file to save
{ {
msg "download latest kfeditor-patcher"
get_latest "notpeelz/kfeditor-patcher" "kfeditor_patcher.exe" "$1" get_latest "notpeelz/kfeditor-patcher" "kfeditor_patcher.exe" "$1"
} }
function show_help () function setup_colors ()
{ {
cat <<EOF if [[ -t 2 ]] && ! is_true "$ArgNoColors" && [[ "${TERM-}" != "dumb" ]]; then
Usage: $0 OPTION # shellcheck disable=SC2034
WHT='\e[37m'
Build, pack, test and upload your kf2 packages to the Steam Workshop. RED='\e[31m'
# shellcheck disable=SC2034
Available options: GRN='\e[32m'
-ib, --init-build generate $(basename "$MutBuildConfig") with build parameters # shellcheck disable=SC2034
-it, --init-test generate $(basename "$MutTestConfig") with test parameters YLW='\e[33m'
-i, --init the same as "./$ScriptName --init-build; ./$ScriptName --init-test" DEF='\e[0m'
-c, --compile build package(s) BLD='\e[1m'
-b, --brew compress *.upk and place inside *.u else
-bm, --brew-manual the same (almost) as above, but with patched kfeditor by @notpeelz # shellcheck disable=SC2034
-u, --upload upload package(s) to the Steam Workshop WHT=''
-t, --test run local single player test with $(basename "$MutTestConfig") parameters RED=''
-v, --version show version # shellcheck disable=SC2034
-h, --help show this help GRN=''
# shellcheck disable=SC2034
Shortcuts for multiple options: YLW=''
-cb compile, brew DEF=''
-cu compile, upload BLD=''
-cbm compile, brew_manual fi
-cbu compile, brew, upload
-cbmu compile, brew_manual, upload
-ct compile, run_test
-cbt compile, brew, run_test
-cbmt compile, brew_manual, run_test
EOF
} }
function show_version () function err () # $1: String
{ {
cat <<EOF if ! is_true "$ArgQuiet"; then
$ScriptName $(git describe 2> /dev/null) echo -e "${RED}${1-}${DEF}" >&2
EOF fi
}
function msg () # $1: String
{
if ! is_true "$ArgQuiet"; then
echo -e "${DEF}${1-}" >&1
fi
}
function die () # $1: String, $2: Exit code
{
err "${1-}"
exit "${2-3}"
}
function usage ()
{
msg "${BLD}Usage:${DEF} $0 OPTIONS"
msg ""
msg "Build, pack, test and upload your kf2 packages to the Steam Workshop."
msg ""
msg "${BLD}Available options:${DEF}"
msg " -ib, --init-build generate $(basename "$MutBuildConfig") with build parameters"
msg " -it, --init-test generate $(basename "$MutTestConfig") with test parameters"
msg " -i, --init the same as \"./$ScriptName --init-build; ./$ScriptName --init-test\""
msg " -c, --compile build package(s)"
msg " -b, --brew compress *.upk and place inside *.u"
msg " -bm, --brew-manual the same (almost) as above, but with patched kfeditor by @notpeelz"
msg " -u, --upload upload package(s) to the Steam Workshop"
msg " -t, --test run local single player test with $(basename "$MutTestConfig") parameters"
msg " -q, --quiet run without output"
msg " -w, --warnings do not close kf2editor automatically (to be able to read warnings)"
msg " -nc, --no-colors do not use color output"
msg " -d, --debug print every executed command (script debug)"
msg " -v, --version show version"
msg " -h, --help show this help"
msg ""
msg "${BLD}Short options can be combined, examples:${DEF}"
msg " -cbu compile, brew, upload"
msg " -cbmt compile, brew_manual, run_test"
msg " -wcb compile and brew without closing kf2editor"
msg " etc..."
}
function version ()
{
msg "${BLD}$ScriptName $(git describe 2> /dev/null)${DEF}"
} }
function cleanup() function cleanup()
{ {
trap - SIGINT SIGTERM ERR EXIT trap - SIGINT SIGTERM ERR EXIT
msg "cleanup..."
restore_kfeditorconf restore_kfeditorconf
} }
function backup_kfeditorconf () function backup_kfeditorconf ()
{ {
msg "backup $KFEditorConf"
cp -f "$KFEditorConf" "$KFEditorConfBackup" cp -f "$KFEditorConf" "$KFEditorConfBackup"
} }
function restore_kfeditorconf () function restore_kfeditorconf ()
{ {
if [[ -f "$KFEditorConfBackup" ]]; then if [[ -f "$KFEditorConfBackup" ]]; then
msg "restore $KFEditorConf from backup"
mv -f "$KFEditorConfBackup" "$KFEditorConf" mv -f "$KFEditorConfBackup" "$KFEditorConf"
fi fi
} }
@ -147,6 +209,8 @@ function init_build ()
{ {
local PackageList="" local PackageList=""
msg "create new build config ($MutBuildConfig)"
:> "$MutBuildConfig" :> "$MutBuildConfig"
while read -r Package while read -r Package
@ -186,8 +250,7 @@ function read_build_settings ()
# shellcheck source=./.shellcheck/build.cfg # shellcheck source=./.shellcheck/build.cfg
source "$MutBuildConfig" source "$MutBuildConfig"
else else
echo "$MutBuildConfig broken! Check this file before continue or create new one using $0 --init-build" die "$MutBuildConfig broken! Check this file before continue or create new one using --init-build option" 2
return 1
fi fi
} }
@ -199,7 +262,7 @@ function read_test_settings ()
# shellcheck source=./.shellcheck/test.cfg # shellcheck source=./.shellcheck/test.cfg
source "$MutTestConfig" source "$MutTestConfig"
else else
echo "$MutTestConfig broken! Check this file before continue or create new one using $0 --init-test" die "$MutTestConfig broken! Check this file before continue or create new one using --init-test option" 2
return 1 return 1
fi fi
} }
@ -210,6 +273,11 @@ function merge_package () # $1: What, $2: Where
local ModificationTimeNew="" local ModificationTimeNew=""
local PID="" local PID=""
msg "merge $1 to $2"
if is_true "$ArgWarnings"; then
CMD //C "cd /D $(cygpath -w "$KFWin64") && $(basename "$KFEditorMergePackages") make $1 $2"
else
ModificationTime=$(stat -c %y "$KFWin64/$2") ModificationTime=$(stat -c %y "$KFWin64/$2")
CMD //C "cd /D $(cygpath -w "$KFWin64") && $(basename "$KFEditorMergePackages") make $1 $2" & CMD //C "cd /D $(cygpath -w "$KFWin64") && $(basename "$KFEditorMergePackages") make $1 $2" &
PID="$!" PID="$!"
@ -231,12 +299,15 @@ function merge_package () # $1: What, $2: Where
fi fi
sleep 1 sleep 1
done done
fi
rm -f "$KFWin64/$1" # cleanup (manual) rm -f "$KFWin64/$1" # cleanup (manual)
} }
function merge_packages () # $1: Mutator name function merge_packages () # $1: Mutator name
{ {
msg "merge packages for $1.u"
cp -f "$KFUnpublishScript/$1.u" "$KFWin64" cp -f "$KFUnpublishScript/$1.u" "$KFWin64"
while read -r Upk while read -r Upk
@ -261,6 +332,8 @@ function compile ()
local StripSourceArg="" local StripSourceArg=""
local PID="" local PID=""
msg "compilation"
read_build_settings read_build_settings
if ! command -v multini &> /dev/null; then if ! command -v multini &> /dev/null; then
@ -297,6 +370,12 @@ function compile ()
if is_true "$StripSource"; then StripSourceArg="-stripsource"; fi if is_true "$StripSource"; then StripSourceArg="-stripsource"; fi
if is_true "$ArgWarnings"; then
CMD //C "$(cygpath -w "$KFEditor") make $StripSourceArg -useunpublished"
if ! compiled; then
die "compilation failed, details in Launch.log"
fi
else
CMD //C "$(cygpath -w "$KFEditor") make $StripSourceArg -useunpublished" & CMD //C "$(cygpath -w "$KFEditor") make $StripSourceArg -useunpublished" &
PID="$!" PID="$!"
while ps -p "$PID" &> /dev/null while ps -p "$PID" &> /dev/null
@ -304,6 +383,7 @@ function compile ()
if compiled; then kill "$PID"; break; fi if compiled; then kill "$PID"; break; fi
sleep 1 sleep 1
done done
fi
find "$KFUnpublish" -type d -empty -delete find "$KFUnpublish" -type d -empty -delete
@ -350,17 +430,25 @@ function brew ()
{ {
local PID="" local PID=""
msg "brew"
read_build_settings read_build_settings
if ! compiled ; then if ! compiled ; then
echo "You must compile packages before brewing. Use $0 --compile for this." die "You must compile packages before brewing. Use --compile option for this." 2
exit 1
fi fi
rm -rf "$KFPublish" rm -rf "$KFPublish"
mkdir -p "$KFPublishBrewedPC" mkdir -p "$KFPublishBrewedPC"
if is_true "$ArgWarnings"; then
CMD //C "cd /D $(cygpath -w "$KFWin64") && $(basename "$KFEditor") brewcontent -platform=PC $PackageUpload -useunpublished"
if ! brewed; then
brew_cleanup
die "brewing failed, details in Launch.log"
fi
else
CMD //C "cd /D $(cygpath -w "$KFWin64") && $(basename "$KFEditor") brewcontent -platform=PC $PackageUpload -useunpublished" & CMD //C "cd /D $(cygpath -w "$KFWin64") && $(basename "$KFEditor") brewcontent -platform=PC $PackageUpload -useunpublished" &
PID="$!" PID="$!"
while ps -p "$PID" &> /dev/null while ps -p "$PID" &> /dev/null
@ -368,6 +456,7 @@ function brew ()
if brewed; then kill "$PID"; break; fi if brewed; then kill "$PID"; break; fi
sleep 1 sleep 1
done done
fi
publish_common publish_common
brew_cleanup brew_cleanup
@ -377,11 +466,12 @@ function brew ()
function brew_manual () function brew_manual ()
{ {
msg "manual brew"
read_build_settings read_build_settings
if ! compiled ; then if ! compiled ; then
echo "You must compile packages before brewing. Use $0 --compile for this." die "You must compile packages before brewing. Use --compile option for this." 2
exit 1
fi fi
rm -rf "$KFPublish" rm -rf "$KFPublish"
@ -425,11 +515,12 @@ function upload ()
{ {
local PreparedWsDir="" local PreparedWsDir=""
msg "upload to steam workshop"
read_build_settings read_build_settings
if ! compiled ; then if ! compiled ; then
echo "You must compile packages before uploading. Use $0 --compile for this." die "You must compile packages before uploading. Use --compile option for this." 2
exit 1
fi fi
if ! [[ -d "$KFPublish" ]]; then if ! [[ -d "$KFPublish" ]]; then
@ -462,6 +553,8 @@ function init_test ()
local AviableMutators="" local AviableMutators=""
local AviableGamemodes="" local AviableGamemodes=""
msg "create new test config ($MutTestConfig)"
read_build_settings read_build_settings
for Package in $PackageUpload for Package in $PackageUpload
@ -530,6 +623,8 @@ function run_test ()
{ {
local UseUnpublished="" local UseUnpublished=""
msg "run test..."
read_build_settings read_build_settings
read_test_settings read_test_settings
@ -538,54 +633,86 @@ function run_test ()
CMD //C "$(cygpath -w "$KFGame") $Map?Difficulty=$Difficulty?GameLength=$GameLength?Game=$Game?Mutator=$Mutators?$Args $UseUnpublished" -log CMD //C "$(cygpath -w "$KFGame") $Map?Difficulty=$Difficulty?GameLength=$GameLength?Game=$Game?Mutator=$Mutators?$Args $UseUnpublished" -log
} }
function debug () function parse_combined_params () # $1: Combined short parameters
{ {
set -o xtrace local Param="${1}"
local Length="${#Param}"
local Position=1
while true
do
if [[ $((Position + 2)) -gt "$Length" ]]; then break; fi
case "${Param:$Position:2}" in
ib ) ((Position+=2)); ArgInitBuild="true" ;;
it ) ((Position+=2)); ArgInitTest="true" ;;
bm ) ((Position+=2)); ArgBrewManual="true" ;;
nc ) ((Position+=2)); ArgNoColors="true" ;;
esac
if [[ $((Position + 1)) -gt "$Length" ]]; then break; fi
case "${Param:$Position:1}" in
h ) ((Position+=1)); ArgHelp="true" ;;
v ) ((Position+=1)); ArgVersion="true" ;;
i ) ((Position+=1)); ArgInitBuild="true"; ArgInitTest="true" ;;
c ) ((Position+=1)); ArgCompile="true" ;;
b ) ((Position+=1)); ArgBrew="true" ;;
u ) ((Position+=1)); ArgUpload="true" ;;
t ) ((Position+=1)); ArgTest="true" ;;
d ) ((Position+=1)); ArgDebug="true" ;;
q ) ((Position+=1)); ArgQuiet="true" ;;
* ) die "Unknown short option: -${Param:$Position:1}" 1 ;;
esac
done
} }
function parse_params () # $@: Args
{
while true
do
case "${1-}" in
-h | --help ) ArgHelp="true" ;;
-v | --version ) ArgVersion="true" ;;
-ib | --init-build ) ArgInitBuild="true" ;;
-it | --init-test ) ArgInitTest="true" ;;
-i | --init ) ArgInitBuild="true"; ArgInitTest="true" ;;
-c | --compile ) ArgCompile="true" ;;
-b | --brew ) ArgBrew="true" ;;
-bm | --brew-manual ) ArgBrewManual="true" ;;
-u | --upload ) ArgUpload="true" ;;
-t | --test ) ArgTest="true" ;;
-d | --debug ) ArgDebug="true" ;;
-q | --quiet ) ArgQuiet="true" ;;
-w | --warnings ) ArgWarnings="true" ;;
-nc | --no-color ) ArgNoColors="true" ;;
--* ) die "Unknown option: ${1}" 1 ;;
-* ) parse_combined_params "${1}" ;;
* ) if [[ -n "${1-}" ]]; then die "Unknown option: ${1-}" 1; fi; break ;;
esac
shift
done
}
function main ()
{
if [[ $# -eq 0 ]]; then usage; die "" 0; fi
parse_params "$@"
setup_colors
export PATH="$PATH:$ThirdPartyBin" export PATH="$PATH:$ThirdPartyBin"
if [[ $# -eq 0 ]]; then show_help; exit 0; fi # Modifiers
case $1 in if is_true "$ArgDebug"; then set -o xtrace; fi
# Options
-h|--help ) show_help ;; # Actions
-v|--version ) show_version ;; if is_true "$ArgVersion"; then version; die "" 0; fi
-ib|--init-build ) init_build ;; if is_true "$ArgHelp"; then usage; die "" 0; fi
-it|--init-test ) init_test ;; if is_true "$ArgInitBuild"; then init_build; fi
-i|--init ) init_build; init_test ;; if is_true "$ArgInitTest"; then init_test; fi
-c|--compile ) compile ;; if is_true "$ArgCompile"; then compile; fi
-b|--brew ) brew ;; if is_true "$ArgBrew"; then brew; fi
-bm|--brew-manual ) brew_manual ;; if is_true "$ArgBrewManual"; then brew_manual; fi
-u|--upload ) upload ;; if is_true "$ArgUpload"; then upload; fi
-t|--test ) run_test ;; if is_true "$ArgTest"; then run_test; fi
# Shortcuts }
-cb ) compile; brew ;;
-cu ) compile; upload ;; main "$@"
-cbm ) compile; brew_manual ;;
-cbu ) compile; brew; upload ;;
-cbmu ) compile; brew_manual; upload ;;
-ct ) compile; run_test ;;
-cbt ) compile; brew; run_test ;;
-cbmt ) compile; brew_manual; run_test ;;
# Debug
-dh ) debug; show_help ;;
-dv ) debug; show_version ;;
-dib ) debug; init_build ;;
-dit ) debug; init_test ;;
-di ) debug; init_build; init_test ;;
-dc ) debug; compile ;;
-db ) debug; brew ;;
-dbm ) debug; brew_manual ;;
-du ) debug; upload ;;
-dt ) debug; run_test ;;
-dcb ) debug; compile; brew ;;
-dcu ) debug; compile; upload ;;
-dcbm ) debug; compile; brew_manual ;;
-dcbu ) debug; compile; brew; upload ;;
-dcbmu ) debug; compile; brew_manual; upload ;;
-dct ) debug; compile; run_test ;;
-dcbt ) debug; compile; brew; run_test ;;
-dcbmt ) debug; compile; brew_manual; run_test ;;
# Other
* ) echo "Command not recognized: $1"; exit 1 ;;
esac