diff --git a/akarin b/akarin index 6255e9f4f..07de0c936 100755 --- a/akarin +++ b/akarin @@ -2,52 +2,38 @@ # exit immediately if a command exits with a non-zero status set -e -# get base dir regardless of execution location -SOURCE="${BASH_SOURCE[0]}" -while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink - DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )" - SOURCE="$(readlink "$SOURCE")" - [[ "$SOURCE" != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located -done -SOURCE=$([[ "$SOURCE" = /* ]] && echo "$SOURCE" || echo "$PWD/${SOURCE#./}") -basedir=$(dirname "$SOURCE") -. "$basedir"/scripts/init.sh -paperstash() { - STASHED=$(git stash) -} +gitcmd="git -c commit.gpgsign=false" -paperunstash() { - if [[ "$STASHED" != "No local changes to save" ]] ; then - git stash pop - fi -} +source "./scripts/functions.sh" +failed=0 case "$1" in "rb" | "rbp" | "rebuild") ( set -e cd "$basedir" - scripts/rebuildpatches.sh "$basedir" - ) + scripts/rebuildPatches.sh "$basedir" $2 || exit 1 + ) || failed=1 ;; "p" | "patch" | "apply") ( set -e cd "$basedir" if [ "$2" != "fast" ]; then - scripts/upstream.sh + scripts/upstream.sh || exit 1 fi - scripts/apply.sh "$basedir" - ) + scripts/apply.sh "$basedir" || exit 1 + ) || failed=1 ;; "b" | "bu" | "build") ( - basedir - mvn -N install + set -e + cd "$basedir" + mvn -N install || exit 1 cd ${FORK_NAME}-API - mvn -e clean install && cd ../${FORK_NAME}-Server && mvn -e clean install - ) + mvn -e clean install && cd ../${FORK_NAME}-Server && mvn -e clean install || exit 1 + ) || failed=1 ;; "jar" | "paperclip") ( @@ -88,19 +74,68 @@ case "$1" in "s" | "server") cd "$basedir/Akarin-Server" ;; + "c" | "clean") + rm -rf Akarin-API + rm -rf Akarin-Server + rm -rf work + echo "Cleaned build files" + ;; + "e" | "edit") + case "$2" in + "s" | "server") + export AKARIN_LAST_EDIT="$basedir/Akarin-Server" + cd "$basedir/Akarin-Server" + ( + set -e + + gitstash + $gitcmd rebase -i upstream/upstream + gitunstash + ) + ;; + "a" | "api") + export AKARIN_LAST_EDIT="$basedir/Akarin-API" + cd "$basedir/Akarin-API" + ( + set -e + + gitstash + $gitcmd rebase -i upstream/upstream + gitunstash + ) + ;; + "c" | "continue") + cd "$AKARIN_LAST_EDIT" + unset AKARIN_LAST_EDIT + ( + set -e + + $gitcmd add . + $gitcmd commit --amend + $gitcmd rebase --continue + + cd "$basedir" + scripts/rebuildPatches.sh "$basedir" + ) + ;; + *) + echo "You must edit either the api or server." + ;; + esac + ;; "setup") if [[ -f ~/.bashrc ]] ; then - NAME="ec" + NAME="akarin" if [[ ! -z "${2+x}" ]] ; then NAME="$2" fi (grep "alias $NAME=" ~/.bashrc > /dev/null) && (sed -i "s|alias $NAME=.*|alias $NAME='. $SOURCE'|g" ~/.bashrc) || (echo "alias $NAME='. $SOURCE'" >> ~/.bashrc) alias "$NAME=. $SOURCE" - echo "You can now just type '$NAME' at any time to access the paper tool." + echo "You can now just type '$NAME' at any time to access the akarin tool." fi ;; *) - echo "Akarin build tool command. This provides a variety of commands to build and manage the PaperMC build" + echo "Akarin build tool command. This provides a variety of commands to build and manage the Akarin build" echo "environment. For all of the functionality of this command to be available, you must first run the" echo "'setup' command. View below for details. For essential building and patching, you do not need to do the setup." echo "" @@ -108,13 +143,13 @@ case "$1" in echo " * rb, rebuild | Rebuild patches, can be called from anywhere." echo " * p, patch | Apply all patches to top of Paper without building it. Can be run from anywhere." echo " * up, upstream | Build Paper upstream, pass arg up to update paper. Can be run from anywhere." - echo " * b, build | Build API and Server but no deploy. Can be ran anywhere." - echo " * d, deploy | Build and Deploy API jar and build Server. Can be ran anywhere." + echo " * b, build | Build the API and the server project without deploying. Can be ran anywhere." + echo " * d, deploy | Build and deploy jars of the API and the server project. Can be ran anywhere." echo "" echo " These commands require the setup command before use:" echo " * r, root | Change directory to the root of the project." - echo " * a. api | Move to the Paper-API directory." - echo " * s, server | Move to the Paper-Server directory." + echo " * a. api | Move to the Akarin-API directory." + echo " * s, server | Move to the Akarin-Server directory." echo " * e, edit | Use to edit a specific patch, give it the argument \"server\" or \"api\"" echo " | respectively to edit the correct project. Use the argument \"continue\" after" echo " | the changes have been made to finish and rebuild patches. Can be called from anywhere." @@ -129,5 +164,17 @@ case "$1" in ;; esac -unset -f paperstash -unset -f paperunstash +unset RCPATH +unset SOURCE +unset basedir +unset -f color +unset -f colorend +unset -f gitstash +unset -f gitunstash +if [[ "$failed" == "1" ]]; then + unset failed + false +else + unset failed + true +fi \ No newline at end of file diff --git a/scripts/functions.sh b/scripts/functions.sh new file mode 100644 index 000000000..dbb7a9414 --- /dev/null +++ b/scripts/functions.sh @@ -0,0 +1,96 @@ +#!/usr/bin/env bash +# CONFIG set +FORK_NAME="Akarin" +API_REPO="" +SERVER_REPO="" +PAPER_API_REPO="" +PAPER_SERVER_REPO="" +MCDEV_REPO="" + +# DIR configure +# resolve shell-specifics +case "$(echo "$SHELL" | sed -E 's|/usr(/local)?||g')" in + "/bin/zsh") + RCPATH="$HOME/.zshrc" + SOURCE="${BASH_SOURCE[0]:-${(%):-%N}}" + ;; + *) + RCPATH="$HOME/.bashrc" + if [[ -f "$HOME/.bash_aliases" ]]; then + RCPATH="$HOME/.bash_aliases" + fi + SOURCE="${BASH_SOURCE[0]}" + ;; +esac + +while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink + DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )" + SOURCE="$(readlink "$SOURCE")" + [[ "$SOURCE" != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located +done +SOURCE=$([[ "$SOURCE" = /* ]] && echo "$SOURCE" || echo "$PWD/${SOURCE#./}") +basedir=$(dirname "$SOURCE") + +function basedir { + cd "$basedir" +} + +# COLOUR functions +color() { + if [ $2 ]; then + echo -e "\e[$1;$2m" + else + echo -e "\e[$1m" + fi +} + +colorend() { + echo -e "\e[m" +} + +# GIT functions +gitstash() { + STASHED=$($gitcmd stash 2>/dev/null|| return 0) # errors are ok +} + +gitunstash() { + if [[ "$STASHED" != "No local changes to save" ]] ; then + $gitcmd stash pop 2>/dev/null|| return 0 # errors are ok + fi +} + +function gethead { + cd "$1" + git log -1 --oneline +} + +function gitpush { + if [ "$(git config minecraft.push-${FORK_NAME})" == "1" ]; then + echo "Push - $1 ($3) to $2" + ( + cd "$1" + git remote rm script-push > /dev/null 2>&1 + git remote add script-push $2 >/dev/null 2>&1 + git push script-push $3 -f + ) + fi +} + +# PATCH functions +function cleanupPatches { + cd "$1" + for patch in *.patch; do + gitver=$(tail -n 2 $patch | grep -ve "^$" | tail -n 1) + diffs=$(git diff --staged $patch | grep -E "^(\+|\-)" | grep -Ev "(From [a-z0-9]{32,}|\-\-\- a|\+\+\+ b|.index|Date\: )") + + testver=$(echo "$diffs" | tail -n 2 | grep -ve "^$" | tail -n 1 | grep "$gitver") + if [ "x$testver" != "x" ]; then + diffs=$(echo "$diffs" | tail -n +3) + fi + + if [ "x$diffs" == "x" ] ; then + git reset HEAD $patch >/dev/null + git checkout -- $patch >/dev/null + fi + done +} diff --git a/scripts/init.sh b/scripts/init.sh deleted file mode 100755 index 9d6780a08..000000000 --- a/scripts/init.sh +++ /dev/null @@ -1,65 +0,0 @@ -#!/usr/bin/env bash -# BEGIN config -FORK_NAME="Akarin" -API_REPO="" -SERVER_REPO="" -PAPER_API_REPO="" -PAPER_SERVER_REPO="" -MCDEV_REPO="" -# END config - -sourceBase=$(dirname $SOURCE)/../ -cd "${basedir:-$sourceBase}" - -basedir=$(pwd -P) -cd - - - -function bashColor { -if [ $2 ]; then - echo -e "\e[$1;$2m" -else - echo -e "\e[$1m" -fi -} -function bashColorReset { - echo -e "\e[m" -} - -function cleanupPatches { - cd "$1" - for patch in *.patch; do - gitver=$(tail -n 2 $patch | grep -ve "^$" | tail -n 1) - diffs=$(git diff --staged $patch | grep -E "^(\+|\-)" | grep -Ev "(From [a-z0-9]{32,}|\-\-\- a|\+\+\+ b|.index|Date\: )") - - testver=$(echo "$diffs" | tail -n 2 | grep -ve "^$" | tail -n 1 | grep "$gitver") - if [ "x$testver" != "x" ]; then - diffs=$(echo "$diffs" | tail -n +3) - fi - - if [ "x$diffs" == "x" ] ; then - git reset HEAD $patch >/dev/null - git checkout -- $patch >/dev/null - fi - done -} -function pushRepo { - if [ "$(git config minecraft.push-${FORK_NAME})" == "1" ]; then - echo "Pushing - $1 ($3) to $2" - ( - cd "$1" - git remote rm emc-push > /dev/null 2>&1 - git remote add emc-push $2 >/dev/null 2>&1 - git push emc-push $3 -f - ) - fi -} -function basedir { - cd "$basedir" -} -function gethead { - ( - cd "$1" - git log -1 --oneline - ) -}