1
0

Merge pull request #3 from GenZmeY/dev

New arguments parser, output system, new parameters: -w -q -nc
This commit is contained in:
GenZmeY 2022-02-14 05:16:14 +03:00 committed by GitHub
commit 0dd0011190
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

348
builder
View File

@ -1,5 +1,21 @@
#!/bin/bash
# Copyright (C) 2022 GenZmeY
# mailto: genzmey@gmail.com
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
# Requirements: git-bash
# https://git-scm.com/download/win
@ -7,6 +23,15 @@ set -Eeuo pipefail
trap cleanup SIGINT SIGTERM ERR EXIT
function reg_readkey () # $1: path, $2: key
{
cygpath -u "$(
reg query "$1" //v "$2" | \
grep -F "$2" | \
awk '{ $1=$2=""; print $0 }' | \
sed -r 's|^\s*(.+)\s*|\1|g')"
}
# Whoami
ScriptFullname=$(readlink -e "$0")
ScriptName=$(basename "$0")
@ -54,13 +79,28 @@ KFPublishLocalization="$KFPublish/Localization"
MutWsInfo="$KFDoc/wsinfo.txt"
KFEditorConfBackup="$KFEditorConf.backup"
function reg_readkey () # $1: path, $2: key
{
cygpath -u $(
reg query "$1" //v "$2" | \
grep -F "$2" | \
awk '{ $1=$2=""; print $0 }' )
}
# 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"
# Colors
RED=''
GRN=''
YLW=''
BLU=''
DEF=''
BLD=''
function is_true () # $1: Bool arg to check
{
@ -70,11 +110,14 @@ function is_true () # $1: Bool arg to check
function get_latest () # $1: Reponame, $2: filename, $3: output filename
{
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"
msg "download $2 ($LatestTag)"
mkdir -p "$(dirname "$3")/"
curl -LJs "$DownloadUrl" -o "$3"
msg "${GRN}successfully downloaded${DEF}"
}
function get_latest_multini () # $1: file to save
@ -87,14 +130,52 @@ function get_latest_kfeditor_patcher () # $1: file to save
get_latest "notpeelz/kfeditor-patcher" "kfeditor_patcher.exe" "$1"
}
function show_help ()
function setup_colors ()
{
cat <<EOF
Usage: $0 OPTION
if [[ -t 2 ]] && ! is_true "$ArgNoColors" && [[ "${TERM-}" != "dumb" ]]; then
RED='\e[31m'
GRN='\e[32m'
YLW='\e[33m'
BLU='\e[34m'
DEF='\e[0m'
BLD='\e[1m'
fi
}
function err () # $1: String
{
if ! is_true "$ArgQuiet"; then
echo -e "${RED}${1-}${DEF}" >&2
fi
}
function msg () # $1: String
{
if ! is_true "$ArgQuiet"; then
if is_true "$ArgDebug"; then
echo -e "${BLU}${1-}${DEF}" >&1
else
echo -e "${DEF}${1-}${DEF}" >&1
fi
fi
}
function die () # $1: String, $2: Exit code
{
err "${1-}"
exit "${2-3}"
}
function usage ()
{
local HelpMessage=""
HelpMessage=$(cat <<EOF
${BLD}Usage:${DEF} $0 OPTIONS
Build, pack, test and upload your kf2 packages to the Steam Workshop.
Available options:
${BLD}Available options:${DEF}
-ib, --init-build generate $(basename "$MutBuildConfig") with build parameters
-it, --init-test generate $(basename "$MutTestConfig") with test parameters
-i, --init the same as "./$ScriptName --init-build; ./$ScriptName --init-test"
@ -103,26 +184,26 @@ Available options:
-bm, --brew-manual the same (almost) as above, but with patched kfeditor by @notpeelz
-u, --upload upload package(s) to the Steam Workshop
-t, --test run local single player test with $(basename "$MutTestConfig") parameters
-q, --quiet run without output
-w, --warnings do not close kf2editor automatically (to be able to read warnings)
-nc, --no-colors do not use color output
-d, --debug print every executed command (script debug)
-v, --version show version
-h, --help show this help
Shortcuts for multiple options:
-cb compile, brew
-cu compile, upload
-cbm compile, brew_manual
${BLD}Short options can be combined, examples:${DEF}
-cbu compile, brew, upload
-cbmu compile, brew_manual, upload
-ct compile, run_test
-cbt compile, brew, run_test
-cbmt compile, brew_manual, run_test
-wcb compile and brew without closing kf2editor
etc...
EOF
)
msg "$HelpMessage"
}
function show_version ()
function version ()
{
cat <<EOF
$ScriptName $(git describe 2> /dev/null)
EOF
msg "${BLD}$ScriptName $(git describe 2> /dev/null)${DEF}"
}
function cleanup()
@ -133,12 +214,14 @@ function cleanup()
function backup_kfeditorconf ()
{
msg "backup $(basename "$KFEditorConf") to $(basename "$KFEditorConfBackup")"
cp -f "$KFEditorConf" "$KFEditorConfBackup"
}
function restore_kfeditorconf ()
{
if [[ -f "$KFEditorConfBackup" ]]; then
msg "restore $(basename "$KFEditorConf") from backup"
mv -f "$KFEditorConfBackup" "$KFEditorConf"
fi
}
@ -147,6 +230,8 @@ function init_build ()
{
local PackageList=""
msg "creating new build config"
:> "$MutBuildConfig"
while read -r Package
@ -158,6 +243,8 @@ function init_build ()
fi
done < <(find "$MutSource" -mindepth 2 -maxdepth 2 -type d -ipath '*/Classes' | sed -r 's|.+/([^/]+)/[^/]+|\1|' | sort)
msg "packages found: $PackageList"
cat > "$MutBuildConfig" <<EOF
# Build parameters
@ -176,6 +263,8 @@ PackageBuildOrder="$PackageList"
# The order doesn't matter
PackageUpload="$PackageList"
EOF
msg "${GRN}$(basename "$MutBuildConfig") created${DEF}"
}
function read_build_settings ()
@ -186,8 +275,7 @@ function read_build_settings ()
# shellcheck source=./.shellcheck/build.cfg
source "$MutBuildConfig"
else
echo "$MutBuildConfig broken! Check this file before continue or create new one using $0 --init-build"
return 1
die "$MutBuildConfig broken! Check this file before continue or create new one using --init-build option" 2
fi
}
@ -199,7 +287,7 @@ function read_test_settings ()
# shellcheck source=./.shellcheck/test.cfg
source "$MutTestConfig"
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
fi
}
@ -210,6 +298,11 @@ function merge_package () # $1: What, $2: Where
local ModificationTimeNew=""
local PID=""
msg "merge $1 into $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")
CMD //C "cd /D $(cygpath -w "$KFWin64") && $(basename "$KFEditorMergePackages") make $1 $2" &
PID="$!"
@ -231,12 +324,15 @@ function merge_package () # $1: What, $2: Where
fi
sleep 1
done
fi
rm -f "$KFWin64/$1" # cleanup (manual)
}
function merge_packages () # $1: Mutator name
{
msg "merge packages for $1.u"
cp -f "$KFUnpublishScript/$1.u" "$KFWin64"
while read -r Upk
@ -297,13 +393,27 @@ function compile ()
if is_true "$StripSource"; then StripSourceArg="-stripsource"; fi
msg "compilation"
if is_true "$ArgWarnings"; then
CMD //C "$(cygpath -w "$KFEditor") make $StripSourceArg -useunpublished"
if ! compiled; then
die "compilation failed"
fi
msg "${GRN}successfully compiled${DEF}"
else
CMD //C "$(cygpath -w "$KFEditor") make $StripSourceArg -useunpublished" &
PID="$!"
while ps -p "$PID" &> /dev/null
do
if compiled; then kill "$PID"; break; fi
if compiled; then
kill "$PID"
msg "${GRN}successfully compiled${DEF}"
break
fi
sleep 1
done
fi
find "$KFUnpublish" -type d -empty -delete
@ -350,24 +460,38 @@ function brew ()
{
local PID=""
msg "brewing"
read_build_settings
if ! compiled ; then
echo "You must compile packages before brewing. Use $0 --compile for this."
exit 1
die "You must compile packages before brewing. Use --compile option for this." 2
fi
rm -rf "$KFPublish"
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"
fi
msg "${GRN}successfully brewed${DEF}"
else
CMD //C "cd /D $(cygpath -w "$KFWin64") && $(basename "$KFEditor") brewcontent -platform=PC $PackageUpload -useunpublished" &
PID="$!"
while ps -p "$PID" &> /dev/null
do
if brewed; then kill "$PID"; break; fi
if brewed; then
kill "$PID"
msg "${GRN}successfully brewed${DEF}"
break
fi
sleep 1
done
fi
publish_common
brew_cleanup
@ -377,11 +501,12 @@ function brew ()
function brew_manual ()
{
msg "manual brewing"
read_build_settings
if ! compiled ; then
echo "You must compile packages before brewing. Use $0 --compile for this."
exit 1
die "You must compile packages before brewing. Use --compile option for this." 2
fi
rm -rf "$KFPublish"
@ -392,7 +517,9 @@ function brew_manual ()
get_latest_kfeditor_patcher "$KFEditorPatcher"
fi
msg "patching $(basename "$KFEditor")"
CMD //C "cd /D $(cygpath -w "$KFWin64") && $(basename "$KFEditorPatcher")"
msg "${GRN}successfully patched${DEF}"
for Package in $PackageUpload
do
@ -400,14 +527,17 @@ function brew_manual ()
mv "$KFWin64/$Package.u" "$KFPublishBrewedPC"
done
msg "${GRN}successfully brewed${DEF}"
publish_common
find "$KFPublish" -type d -empty -delete
}
# Uploading without brewing
function publish_unpublished ()
{
msg "${YLW}warn: uploading without brewing${DEF}"
mkdir -p "$KFPublishBrewedPC" "$KFPublishScript" "$KFPublishPackages"
for Package in $PackageUpload
@ -428,8 +558,7 @@ function upload ()
read_build_settings
if ! compiled ; then
echo "You must compile packages before uploading. Use $0 --compile for this."
exit 1
die "You must compile packages before uploading. Use --compile option for this." 2
fi
if ! [[ -d "$KFPublish" ]]; then
@ -451,7 +580,13 @@ EOF
cp -rf "$KFPublish" "$PreparedWsDir"
msg "upload to steam workshop"
if is_true "$ArgQuiet"; then
CMD //C "$(cygpath -w "$KFWorkshop") $(basename "$MutWsInfo")" &>/dev/null
else
CMD //C "$(cygpath -w "$KFWorkshop") $(basename "$MutWsInfo")"
fi
msg "${GRN}successfully uploaded to steam workshop${DEF}"
rm -rf "$PreparedWsDir"
rm -f "$MutWsInfo"
@ -462,6 +597,8 @@ function init_test ()
local AviableMutators=""
local AviableGamemodes=""
msg "creating new test config"
read_build_settings
for Package in $PackageUpload
@ -487,8 +624,14 @@ function init_test ()
done < <(grep -rihPo '\s.+extends\sKFGameInfo_' "$MutSource/$Package" | awk '{ print $1 }')
done
if [[ -n "$AviableMutators" ]]; then
msg "mutators found: $AviableMutators"
fi
if [[ -z "$AviableGamemodes" ]]; then
AviableGamemodes="KFGameContent.KFGameInfo_Survival"
else
msg "custom gamemodes found: $AviableGamemodes"
fi
cat > "$MutTestConfig" <<EOF
@ -524,6 +667,8 @@ Mutators="$AviableMutators"
# Additional parameters
Args=""
EOF
msg "${GRN}$(basename "$MutTestConfig") created${DEF}"
}
function run_test ()
@ -533,59 +678,96 @@ function run_test ()
read_build_settings
read_test_settings
if ! brewed; then UseUnpublished="-useunpublished"; fi
if ! brewed; then
UseUnpublished="-useunpublished"
msg "run test (unpublished)"
else
msg "run test (brewed)"
fi
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" -ge "$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" -ge "$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" ;;
w ) ((Position+=1)); ArgWarnings="true" ;;
* ) die "Unknown short option: -${Param:$Position:1}" 1 ;;
esac
done
}
export PATH="$PATH:$ThirdPartyBin"
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
}
if [[ $# -eq 0 ]]; then show_help; exit 0; fi
case $1 in
# Options
-h|--help ) show_help ;;
-v|--version ) show_version ;;
-ib|--init-build ) init_build ;;
-it|--init-test ) init_test ;;
-i|--init ) init_build; init_test ;;
-c|--compile ) compile ;;
-b|--brew ) brew ;;
-bm|--brew-manual ) brew_manual ;;
-u|--upload ) upload ;;
-t|--test ) run_test ;;
# Shortcuts
-cb ) compile; brew ;;
-cu ) compile; upload ;;
-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
function main ()
{
if [[ $# -eq 0 ]]; then usage; die "" 0; fi
parse_params "$@"
setup_colors
export PATH="$PATH:$ThirdPartyBin"
# Modifiers
if is_true "$ArgDebug"; then set -o xtrace; fi
# Actions
if is_true "$ArgVersion"; then version; die "" 0; fi
if is_true "$ArgHelp"; then usage; die "" 0; fi
if is_true "$ArgInitBuild"; then init_build; fi
if is_true "$ArgInitTest"; then init_test; fi
if is_true "$ArgCompile"; then compile; fi
if is_true "$ArgBrew"; then brew; fi
if is_true "$ArgBrewManual"; then brew_manual; fi
if is_true "$ArgUpload"; then upload; fi
if is_true "$ArgTest"; then run_test; fi
}
main "$@"