From 5335386926d26936a7e88277fdf2e163e03ed593 Mon Sep 17 00:00:00 2001 From: Joel Holdsworth Date: Sat, 6 Oct 2018 15:18:47 +0100 Subject: [PATCH 01/23] Compose Arduino download url only once --- marlintool.sh | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/marlintool.sh b/marlintool.sh index 4990b81..ecbdef5 100755 --- a/marlintool.sh +++ b/marlintool.sh @@ -26,10 +26,12 @@ checkTools() getArduinoToolchain() { echo -e "\nDownloading Arduino environment ...\n" + + local url=http://downloads-02.arduino.cc/"$arduinoToolchainArchive" if [ "$os" == "Darwin" ]; then - curl -o "$arduinoToolchainArchive" http://downloads-02.arduino.cc/"$arduinoToolchainArchive" + curl -o "$arduinoToolchainArchive" $url else - wget http://downloads-02.arduino.cc/"$arduinoToolchainArchive" + wget $url fi mkdir -p "$arduinoDir/portable" echo -e "\nUnpacking Arduino environment. This might take a while ...\n" From 8d4d51c5fb46a5f94b09c19d90f5628c0d6cd49b Mon Sep 17 00:00:00 2001 From: Joel Holdsworth Date: Sat, 6 Oct 2018 15:53:23 +0100 Subject: [PATCH 02/23] Tidied indentation --- marlintool.sh | 309 ++++++++++++++++++++++++++------------------------ 1 file changed, 160 insertions(+), 149 deletions(-) diff --git a/marlintool.sh b/marlintool.sh index ecbdef5..36a6136 100755 --- a/marlintool.sh +++ b/marlintool.sh @@ -25,103 +25,102 @@ checkTools() ## Download the toolchain and unpack it getArduinoToolchain() { - echo -e "\nDownloading Arduino environment ...\n" - - local url=http://downloads-02.arduino.cc/"$arduinoToolchainArchive" - if [ "$os" == "Darwin" ]; then - curl -o "$arduinoToolchainArchive" $url - else - wget $url - fi - mkdir -p "$arduinoDir/portable" - echo -e "\nUnpacking Arduino environment. This might take a while ...\n" - if [ "$os" == "Darwin" ]; then - unzip -q "$arduinoToolchainArchive" -d "$arduinoDir" - else - tar -xf "$arduinoToolchainArchive" -C "$arduinoDir" --strip 1 - fi - rm -R "$arduinoToolchainArchive" + echo -e "\nDownloading Arduino environment ...\n" + + local url=http://downloads-02.arduino.cc/"$arduinoToolchainArchive" + if [ "$os" == "Darwin" ]; then + curl -o "$arduinoToolchainArchive" $url + else + wget $url + fi + mkdir -p "$arduinoDir/portable" + echo -e "\nUnpacking Arduino environment. This might take a while ...\n" + if [ "$os" == "Darwin" ]; then + unzip -q "$arduinoToolchainArchive" -d "$arduinoDir" + else + tar -xf "$arduinoToolchainArchive" -C "$arduinoDir" --strip 1 + fi + rm -R "$arduinoToolchainArchive" } ## Get dependencies and move them in place getDependencies() { - echo -e "\nDownloading libraries ...\n" - - for library in ${marlinDependencies[@]}; do - IFS=',' read libName libUrl libDir <<< "$library" - git clone "$libUrl" "$libName" - rm -rf "$arduinoLibrariesDir"/"$libName" - mv -f "$libName"/"$libDir" "$arduinoLibrariesDir"/"$libName" - rm -rf "$libName" - done + echo -e "\nDownloading libraries ...\n" + + for library in ${marlinDependencies[@]}; do + IFS=',' read libName libUrl libDir <<< "$library" + git clone "$libUrl" "$libName" + rm -rf "$arduinoLibrariesDir"/"$libName" + mv -f "$libName"/"$libDir" "$arduinoLibrariesDir"/"$libName" + rm -rf "$libName" + done } ## Clone Marlin getMarlin() { - echo -e "\nCloning Marlin \"$marlinRepositoryUrl\" ...\n" + echo -e "\nCloning Marlin \"$marlinRepositoryUrl\" ...\n" - if [ "$marlinRepositoryBranch" != "" ]; then - git clone -b "$marlinRepositoryBranch" --single-branch "$marlinRepositoryUrl" "$marlinDir" - else - git clone "$marlinRepositoryUrl" "$marlinDir" - fi + if [ "$marlinRepositoryBranch" != "" ]; then + git clone -b "$marlinRepositoryBranch" --single-branch "$marlinRepositoryUrl" "$marlinDir" + else + git clone "$marlinRepositoryUrl" "$marlinDir" + fi - exit + exit } ## Update an existing Marlin clone checkoutMarlin() { - date=`date +%Y-%m-%d-%H-%M-%S` + date=`date +%Y-%m-%d-%H-%M-%S` - # backup configuration - backupMarlinConfiguration $date + # backup configuration + backupMarlinConfiguration $date - cd $marlinDir + cd $marlinDir - echo -e "\nFetching most recent Marlin from \"$marlinRepositoryUrl\" ...\n" + echo -e "\nFetching most recent Marlin from \"$marlinRepositoryUrl\" ...\n" - git fetch - git checkout - git reset origin/`git rev-parse --abbrev-ref HEAD` --hard + git fetch + git checkout + git reset origin/`git rev-parse --abbrev-ref HEAD` --hard - echo -e "\n" + echo -e "\n" - cd .. + cd .. - restoreMarlinConfiguration $date - exit + restoreMarlinConfiguration $date + exit } ## Get the toolchain and Marlin, install board definition setupEnvironment() { - echo -e "\nSetting up build environment in \"$arduinoDir\" ...\n" - getArduinoToolchain - getDependencies - getHardwareDefinition - exit + echo -e "\nSetting up build environment in \"$arduinoDir\" ...\n" + getArduinoToolchain + getDependencies + getHardwareDefinition + exit } ## Fetch and install anet board hardware definition getHardwareDefinition() { - if [ "$hardwareDefinitionRepo" != "" ]; then - - echo -e "\nCloning board hardware definition from \"$hardwareDefinitionRepo\" ... \n" - git clone "$hardwareDefinitionRepo" - - echo -e "\nMoving board hardware definition into arduino directory ... \n" - - repoName=$(basename "$hardwareDefinitionRepo" ".${hardwareDefinitionRepo##*.}") - - mv -f $repoName/hardware/* "$arduinoHardwareDir" - rm -rf $repoName - fi + if [ "$hardwareDefinitionRepo" != "" ]; then + echo -e "\nCloning board hardware definition from \"$hardwareDefinitionRepo\" ... \n" + git clone "$hardwareDefinitionRepo" + + echo -e "\nMoving board hardware definition into arduino directory ... \n" + + repoName=$(basename "$hardwareDefinitionRepo" ".${hardwareDefinitionRepo##*.}") + + mv -f $repoName/hardware/* "$arduinoHardwareDir" + rm -rf $repoName + fi } @@ -129,102 +128,102 @@ getHardwareDefinition() ## param #1 backup name backupMarlinConfiguration() { - echo -e "\nSaving Marlin configuration\n" - echo -e " \"Configuration.h\"" - echo -e " \"Configuration_adv.h\"" - echo -e "\nto \"./configuration/$1/\"\n" + echo -e "\nSaving Marlin configuration\n" + echo -e " \"Configuration.h\"" + echo -e " \"Configuration_adv.h\"" + echo -e "\nto \"./configuration/$1/\"\n" - mkdir -p configuration/$1 + mkdir -p configuration/$1 - cp "$marlinDir"/Marlin/Configuration.h configuration/"$1" - cp "$marlinDir"/Marlin/Configuration_adv.h configuration/"$1" + cp "$marlinDir"/Marlin/Configuration.h configuration/"$1" + cp "$marlinDir"/Marlin/Configuration_adv.h configuration/"$1" } ## Restore Marlin Configuration from backup ## param #1 backup name restoreMarlinConfiguration() { - if [ -d "configuration/$1" ]; then - echo -e "Restoring Marlin configuration\n" - echo -e " \"Configuration.h\"" - echo -e " \"Configuration_adv.h\"" - echo -e "\nfrom \"./configuration/$1/\"\n" - - cp configuration/"$1"/Configuration.h "$marlinDir"/Marlin/ - cp configuration/"$1"/Configuration_adv.h "$marlinDir"/Marlin/ - else - echo -e "\nBackup configuration/$1 not found!\n" - fi - exit + if [ -d "configuration/$1" ]; then + echo -e "Restoring Marlin configuration\n" + echo -e " \"Configuration.h\"" + echo -e " \"Configuration_adv.h\"" + echo -e "\nfrom \"./configuration/$1/\"\n" + + cp configuration/"$1"/Configuration.h "$marlinDir"/Marlin/ + cp configuration/"$1"/Configuration_adv.h "$marlinDir"/Marlin/ + else + echo -e "\nBackup configuration/$1 not found!\n" + fi + exit } ## Build Marlin verifyBuild() { - echo -e "\nVerifying build ...\n" + echo -e "\nVerifying build ...\n" - "$arduinoExecutable" --verify --verbose --board "$boardString" "$marlinDir"/Marlin/Marlin.ino --pref build.path="$buildDir" - exit + "$arduinoExecutable" --verify --verbose --board "$boardString" "$marlinDir"/Marlin/Marlin.ino --pref build.path="$buildDir" + exit } ## Build Marlin and upload buildAndUpload() { - echo -e "\nBuilding and uploading Marlin build from \"$buildDir\" ...\n" + echo -e "\nBuilding and uploading Marlin build from \"$buildDir\" ...\n" - "$arduinoExecutable" --upload --port "$port" --verbose --board "$boardString" "$marlinDir"/Marlin/Marlin.ino --pref build.path="$buildDir" - exit + "$arduinoExecutable" --upload --port "$port" --verbose --board "$boardString" "$marlinDir"/Marlin/Marlin.ino --pref build.path="$buildDir" + exit } ## Delete everything that was downloaded cleanEverything() { - rm -Rf "$arduinoDir" - rm -Rf "$marlinDir" - rm -Rf "$buildDir" + rm -Rf "$arduinoDir" + rm -Rf "$marlinDir" + rm -Rf "$buildDir" } ## Print help printDocu() { - echo "Usage:" - echo " $scriptName ARGS" - echo - echo "Builds an installs Marlin 3D printer firmware." - echo - echo "Options:" - echo - echo " -s, --setup Download and configure the toolchain and the" - echo " necessary libraries for building Marlin." - echo " -m, --marlin Download Marlin sources." - echo " -f, --fetch Update an existing Marlin clone." - echo " -v, --verify Build without uploading." - echo " -u, --upload Build and upload Marlin." - echo " -b, --backupConfig [name] Backup the Marlin configuration to the named backup." - echo " -r, --restoreConfig [name] Restore the given configuration into the Marlin directory." - echo " Rename to Configuration.h implicitly." - echo " -c, --clean Cleanup everything. Remove Marlin sources and Arduino toolchain" - echo " -p, --port [port] Set the serialport for uploading the firmware." - echo " Overrides the default in the script." - echo " -h, --help Show this doc." - echo - exit + echo "Usage:" + echo " $scriptName ARGS" + echo + echo "Builds an installs Marlin 3D printer firmware." + echo + echo "Options:" + echo + echo " -s, --setup Download and configure the toolchain and the" + echo " necessary libraries for building Marlin." + echo " -m, --marlin Download Marlin sources." + echo " -f, --fetch Update an existing Marlin clone." + echo " -v, --verify Build without uploading." + echo " -u, --upload Build and upload Marlin." + echo " -b, --backupConfig [name] Backup the Marlin configuration to the named backup." + echo " -r, --restoreConfig [name] Restore the given configuration into the Marlin directory." + echo " Rename to Configuration.h implicitly." + echo " -c, --clean Cleanup everything. Remove Marlin sources and Arduino toolchain" + echo " -p, --port [port] Set the serialport for uploading the firmware." + echo " Overrides the default in the script." + echo " -h, --help Show this doc." + echo + exit } # Check for parameters file and source it if available if [ -f $defaultParametersFile ]; then - source "$defaultParametersFile" + source "$defaultParametersFile" else - echo -e "\n ===================================================================" - echo -e "\n Can't find $defaultParametersFile!" - echo -e "\n Please rename the \"$defaultParametersFile.example\" file placed in the" - echo -e " same directory as this script to \"$defaultParametersFile\" and edit" - echo -e " if neccessary.\n" - echo -e " ===================================================================\n\n" - exit 1 + echo -e "\n ===================================================================" + echo -e "\n Can't find $defaultParametersFile!" + echo -e "\n Please rename the \"$defaultParametersFile.example\" file placed in the" + echo -e " same directory as this script to \"$defaultParametersFile\" and edit" + echo -e " if neccessary.\n" + echo -e " ===================================================================\n\n" + exit 1 fi # Toolchain architecture @@ -258,36 +257,48 @@ fi checkTools "$tools" -if [ "$1" = "" ]; then printDocu; exit 1; fi +if [ "$1" = "" ]; then + printDocu; exit 1; fi while [ "$1" != "" ]; do - case $1 in - -p | --port ) shift - port=$1 - ;; - -s | --setup ) setupEnvironment - ;; - -m | --marlin ) getMarlin - ;; - -f | --fetch ) checkoutMarlin - ;; - -v | --verify ) verifyBuild - ;; - -u | --upload ) buildAndUpload - ;; - -b | --backupConfig ) shift - backupMarlinConfiguration $1 exit - ;; - -r | --restoreConfig ) shift - restoreMarlinConfiguration $1 - ;; - -c | --clean ) shift - cleanEverything - ;; - -h | --help ) printDocu - ;; - * ) printDocu - exit 1 - esac - shift + case $1 in + -p | --port ) + shift + port=$1 + ;; + -s | --setup ) + setupEnvironment + ;; + -m | --marlin ) + getMarlin + ;; + -f | --fetch ) + checkoutMarlin + ;; + -v | --verify ) + verifyBuild + ;; + -u | --upload ) + buildAndUpload + ;; + -b | --backupConfig ) + shift + backupMarlinConfiguration $1 exit + ;; + -r | --restoreConfig ) + shift + restoreMarlinConfiguration $1 + ;; + -c | --clean ) + shift + cleanEverything + ;; + -h | --help ) + printDocu + ;; + * ) + printDocu + exit 1 + esac + shift done From 60f4df73f2e42b79b3ccc7ec2da994150ff0f312 Mon Sep 17 00:00:00 2001 From: Joel Holdsworth Date: Sat, 6 Oct 2018 15:29:52 +0100 Subject: [PATCH 03/23] Simplify checkTools --- marlintool.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/marlintool.sh b/marlintool.sh index 36a6136..943381c 100755 --- a/marlintool.sh +++ b/marlintool.sh @@ -12,7 +12,7 @@ scriptName=$0 checkTools() { for cmd in "$@"; do - type -p $cmd >/dev/null || [ -x /usr/bin/$cmd ] || [ -x /bin/$cmd ] || [ -x /sbin/$cmd ] || { + command -v $cmd >/dev/null || { >&2 echo "The following tools must be installed:" >&2 echo " $@" >&2 echo " Failed to find $cmd" From 6f5ffed1f1a51d1f81d19651d199980628b7fcd1 Mon Sep 17 00:00:00 2001 From: Joel Holdsworth Date: Sat, 6 Oct 2018 15:32:29 +0100 Subject: [PATCH 04/23] Don't assume curl or wget are available on Linux --- marlintool.sh | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/marlintool.sh b/marlintool.sh index 943381c..b34ba6a 100755 --- a/marlintool.sh +++ b/marlintool.sh @@ -22,16 +22,25 @@ checkTools() done } +checkCurlWget() +{ + if ! curl=$(command -v curl) && ! wget=$(command -v wget); then + >&2 echo "Neither curl nor wget were found installed" + >&2 echo + exit 1 + fi +} + ## Download the toolchain and unpack it getArduinoToolchain() { echo -e "\nDownloading Arduino environment ...\n" local url=http://downloads-02.arduino.cc/"$arduinoToolchainArchive" - if [ "$os" == "Darwin" ]; then - curl -o "$arduinoToolchainArchive" $url + if [ "$curl" != "" ]; then + $curl -o "$arduinoToolchainArchive" $url else - wget $url + $wget $url fi mkdir -p "$arduinoDir/portable" echo -e "\nUnpacking Arduino environment. This might take a while ...\n" @@ -241,13 +250,13 @@ esac # Operating system specific values os=$(uname -s) if [ "$os" == "Darwin" ]; then - tools="git unzip curl" + tools="git unzip" arduinoToolchainArchive="arduino-$arduinoToolchainVersion-macosx.zip" arduinoExecutable="$arduinoDir/Arduino.app/Contents/MacOS/Arduino" arduinoHardwareDir="$arduinoDir/Arduino.app/Contents/Java/hardware" arduinoLibrariesDir="$arduinoDir/Arduino.app/Contents/Java/libraries" else - tools="git tar wget" + tools="git tar" arduinoToolchainArchive="arduino-$arduinoToolchainVersion-$arduinoToolchainArchitecture.tar.xz" arduinoExecutable="$arduinoDir/arduino" arduinoHardwareDir="$arduinoDir/hardware" @@ -256,6 +265,7 @@ fi checkTools "$tools" +checkCurlWget if [ "$1" = "" ]; then printDocu; exit 1; fi From 7eccc3525f47ead40e780955eb82dcb0d5e07a9e Mon Sep 17 00:00:00 2001 From: Joel Holdsworth Date: Sun, 14 Oct 2018 15:00:15 +0100 Subject: [PATCH 05/23] Check number of arguments --- marlintool.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/marlintool.sh b/marlintool.sh index b34ba6a..ebb7812 100755 --- a/marlintool.sh +++ b/marlintool.sh @@ -267,7 +267,7 @@ fi checkTools "$tools" checkCurlWget -if [ "$1" = "" ]; then +if [ $# -lt 1 ]; then printDocu; exit 1; fi while [ "$1" != "" ]; do From fc43b91aeee6fd38c9e8e34366f8d5232a51ef75 Mon Sep 17 00:00:00 2001 From: Joel Holdsworth Date: Sat, 6 Oct 2018 15:39:29 +0100 Subject: [PATCH 06/23] Exit on failure --- marlintool.sh | 3 +++ 1 file changed, 3 insertions(+) diff --git a/marlintool.sh b/marlintool.sh index ebb7812..ac96d75 100755 --- a/marlintool.sh +++ b/marlintool.sh @@ -3,6 +3,9 @@ # by mmone with contribution by jhol, tssva # on github at https://github.com/mmone/marlintool +set -e +set -u + # The default config file to look for defaultParametersFile="marlintool.params" From 3410072978e8742334ba9926c701d797e2e64e19 Mon Sep 17 00:00:00 2001 From: Joel Holdsworth Date: Sat, 6 Oct 2018 15:56:08 +0100 Subject: [PATCH 07/23] Wrapped curl/wget into downloadFile function --- marlintool.sh | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/marlintool.sh b/marlintool.sh index ac96d75..7883d87 100755 --- a/marlintool.sh +++ b/marlintool.sh @@ -34,17 +34,24 @@ checkCurlWget() fi } -## Download the toolchain and unpack it -getArduinoToolchain() +downloadFile() { - echo -e "\nDownloading Arduino environment ...\n" + local url=$1 + local file=$2 - local url=http://downloads-02.arduino.cc/"$arduinoToolchainArchive" if [ "$curl" != "" ]; then - $curl -o "$arduinoToolchainArchive" $url + $curl -o "$file" "$url" else - $wget $url + $wget -O "$file" "$url" fi +} + +## Download the toolchain and unpack it +getArduinoToolchain() +{ + echo -e "\nDownloading Arduino environment ...\n" + + downloadFile http://downloads-02.arduino.cc/"$arduinoToolchainArchive" $arduinoToolchainArchive mkdir -p "$arduinoDir/portable" echo -e "\nUnpacking Arduino environment. This might take a while ...\n" if [ "$os" == "Darwin" ]; then From bfe555e4ff9817fc47e499d73f0116a843036e14 Mon Sep 17 00:00:00 2001 From: Joel Holdsworth Date: Sat, 6 Oct 2018 19:25:38 +0100 Subject: [PATCH 08/23] Renamed printDocu to printUsage --- marlintool.sh | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/marlintool.sh b/marlintool.sh index 7883d87..2e3b951 100755 --- a/marlintool.sh +++ b/marlintool.sh @@ -205,7 +205,7 @@ cleanEverything() } ## Print help -printDocu() +printUsage() { echo "Usage:" echo " $scriptName ARGS" @@ -277,8 +277,8 @@ fi checkTools "$tools" checkCurlWget -if [ $# -lt 1 ]; then - printDocu; exit 1; fi +if [ "$1" = "" ]; then + printUsage; exit 1; fi while [ "$1" != "" ]; do case $1 in @@ -314,10 +314,10 @@ while [ "$1" != "" ]; do cleanEverything ;; -h | --help ) - printDocu + printUsage ;; * ) - printDocu + printUsage exit 1 esac shift From 08fba3bbbc243472dabfef268bed7150d362e9b2 Mon Sep 17 00:00:00 2001 From: Joel Holdsworth Date: Sat, 6 Oct 2018 19:26:59 +0100 Subject: [PATCH 09/23] Print usage text to stderr when printing due to errors --- marlintool.sh | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/marlintool.sh b/marlintool.sh index 2e3b951..33aa442 100755 --- a/marlintool.sh +++ b/marlintool.sh @@ -277,8 +277,10 @@ fi checkTools "$tools" checkCurlWget -if [ "$1" = "" ]; then - printUsage; exit 1; fi +if [ $# -lt 1 ]; then + printUsage >&2 + exit 1 +fi while [ "$1" != "" ]; do case $1 in @@ -317,7 +319,7 @@ while [ "$1" != "" ]; do printUsage ;; * ) - printUsage + printUsage >&2 exit 1 esac shift From 3d66e414677d7a126ab47052a75dece0b71e2dad Mon Sep 17 00:00:00 2001 From: Joel Holdsworth Date: Sun, 14 Oct 2018 15:49:44 +0100 Subject: [PATCH 10/23] Replaced command line options with verbs and options --- marlintool.sh | 148 ++++++++++++++++++++++++++++++-------------------- 1 file changed, 89 insertions(+), 59 deletions(-) diff --git a/marlintool.sh b/marlintool.sh index 33aa442..5700f55 100755 --- a/marlintool.sh +++ b/marlintool.sh @@ -94,10 +94,10 @@ getMarlin() ## Update an existing Marlin clone checkoutMarlin() { - date=`date +%Y-%m-%d-%H-%M-%S` + backupName=`date +%Y-%m-%d-%H-%M-%S` # backup configuration - backupMarlinConfiguration $date + backupMarlinConfiguration cd $marlinDir @@ -111,7 +111,7 @@ checkoutMarlin() cd .. - restoreMarlinConfiguration $date + restoreMarlinConfiguration exit } @@ -150,28 +150,28 @@ backupMarlinConfiguration() echo -e "\nSaving Marlin configuration\n" echo -e " \"Configuration.h\"" echo -e " \"Configuration_adv.h\"" - echo -e "\nto \"./configuration/$1/\"\n" + echo -e "\nto \"./configuration/$backupName/\"\n" - mkdir -p configuration/$1 + mkdir -p configuration/$backupName - cp "$marlinDir"/Marlin/Configuration.h configuration/"$1" - cp "$marlinDir"/Marlin/Configuration_adv.h configuration/"$1" + cp "$marlinDir"/Marlin/Configuration.h configuration/"$backupName" + cp "$marlinDir"/Marlin/Configuration_adv.h configuration/"$backupName" } ## Restore Marlin Configuration from backup ## param #1 backup name restoreMarlinConfiguration() { - if [ -d "configuration/$1" ]; then + if [ -d "configuration/$backupName" ]; then echo -e "Restoring Marlin configuration\n" echo -e " \"Configuration.h\"" echo -e " \"Configuration_adv.h\"" - echo -e "\nfrom \"./configuration/$1/\"\n" + echo -e "\nfrom \"./configuration/$backupName/\"\n" - cp configuration/"$1"/Configuration.h "$marlinDir"/Marlin/ - cp configuration/"$1"/Configuration_adv.h "$marlinDir"/Marlin/ + cp configuration/"$backupName"/Configuration.h "$marlinDir"/Marlin/ + cp configuration/"$backupName"/Configuration_adv.h "$marlinDir"/Marlin/ else - echo -e "\nBackup configuration/$1 not found!\n" + echo -e "\nBackup configuration/$backupName not found!\n" fi exit } @@ -208,25 +208,26 @@ cleanEverything() printUsage() { echo "Usage:" - echo " $scriptName ARGS" + echo " $scriptName []" echo echo "Builds an installs Marlin 3D printer firmware." echo - echo "Options:" + echo "Commands:" + echo " setup Download and configure the toolchain and the" + echo " necessary libraries for building Marlin." + echo " get-marlin Download Marlin sources." + echo " update-marlin Update existing Marlin source." echo - echo " -s, --setup Download and configure the toolchain and the" - echo " necessary libraries for building Marlin." - echo " -m, --marlin Download Marlin sources." - echo " -f, --fetch Update an existing Marlin clone." - echo " -v, --verify Build without uploading." - echo " -u, --upload Build and upload Marlin." - echo " -b, --backupConfig [name] Backup the Marlin configuration to the named backup." - echo " -r, --restoreConfig [name] Restore the given configuration into the Marlin directory." - echo " Rename to Configuration.h implicitly." - echo " -c, --clean Cleanup everything. Remove Marlin sources and Arduino toolchain" - echo " -p, --port [port] Set the serialport for uploading the firmware." - echo " Overrides the default in the script." - echo " -h, --help Show this doc." + echo " build Build Marlin without uploading." + echo " build-upload Build Marlin and upload." + echo " -p, --port [port] Set the device serial port." + echo + echo " backup-config Backup the Marlin configuration to a named backup." + echo " restore-config Restore the Marlin given configuration." + echo + echo " clean Remove Arduino tool-chain and Marlin sources." + echo + echo " help Show help." echo exit } @@ -282,45 +283,74 @@ if [ $# -lt 1 ]; then exit 1 fi -while [ "$1" != "" ]; do +parseBackupRestoreArgument() { + if [ $# -eq 0 ]; then + >&2 echo "No backup path given" + >&2 echo + exit 1 + else + echo $1 + fi +} + +verb='' + +while [ "x$verb" = "x" ]; do case $1 in - -p | --port ) + setup) verb=setupEnvironment;; + get-marlin) verb=getMarlin;; + update-marlin) verb=checkoutMarlin;; + build) verb=verifyBuild;; + build-upload) verb=buildAndUpload;; + backup-config) + verb=backupMarlinConfiguration shift - port=$1 - ;; - -s | --setup ) - setupEnvironment - ;; - -m | --marlin ) - getMarlin - ;; - -f | --fetch ) - checkoutMarlin - ;; - -v | --verify ) - verifyBuild - ;; - -u | --upload ) - buildAndUpload + backupName=$(parseBackupRestoreArgument $@) ;; - -b | --backupConfig ) + restore-config) + verb=restoreMarlinConfiguration shift - backupMarlinConfiguration $1 exit + backupName=$(parseBackupRestoreArgument $@) ;; - -r | --restoreConfig ) - shift - restoreMarlinConfiguration $1 - ;; - -c | --clean ) - shift - cleanEverything - ;; - -h | --help ) - printUsage - ;; - * ) + clean) verb=cleanEverything;; + help|-h|--help) verb=printUsage;; + *) printUsage >&2 exit 1 + ;; esac shift done + +case $verb in + buildAndUpload) + while [ $# -gt 0 ]; do + case $1 in + -p|--port) + shift + port=$1 + ;; + -q|--quiet) quiet=y;; + -v|--verbose) verbose=y;; + *) + printUsage >&2 + exit 1 + esac + shift + done + ;; + *) + while [ $# -gt 0 ]; do + case $1 in + -q|--quiet) quiet=y;; + -v|--verbose) verbose=y;; + *) + printUsage >&2 + exit 1 + esac + shift + done + ;; +esac + +$verb From 78a1701983628b5b719f3b2ad01ced94043609df Mon Sep 17 00:00:00 2001 From: Joel Holdsworth Date: Sat, 6 Oct 2018 19:27:57 +0100 Subject: [PATCH 11/23] Allow status message to be quietened and print error messages to stderr --- marlintool.sh | 61 ++++++++++++++++++++++++++++----------------------- 1 file changed, 34 insertions(+), 27 deletions(-) diff --git a/marlintool.sh b/marlintool.sh index 5700f55..f04f034 100755 --- a/marlintool.sh +++ b/marlintool.sh @@ -49,11 +49,11 @@ downloadFile() ## Download the toolchain and unpack it getArduinoToolchain() { - echo -e "\nDownloading Arduino environment ...\n" + >&$l echo -e "\nDownloading Arduino environment ...\n" downloadFile http://downloads-02.arduino.cc/"$arduinoToolchainArchive" $arduinoToolchainArchive mkdir -p "$arduinoDir/portable" - echo -e "\nUnpacking Arduino environment. This might take a while ...\n" + >&$l echo -e "\nUnpacking Arduino environment. This might take a while ...\n" if [ "$os" == "Darwin" ]; then unzip -q "$arduinoToolchainArchive" -d "$arduinoDir" else @@ -66,7 +66,7 @@ getArduinoToolchain() ## Get dependencies and move them in place getDependencies() { - echo -e "\nDownloading libraries ...\n" + >&$l echo -e "\nDownloading libraries ...\n" for library in ${marlinDependencies[@]}; do IFS=',' read libName libUrl libDir <<< "$library" @@ -80,7 +80,7 @@ getDependencies() ## Clone Marlin getMarlin() { - echo -e "\nCloning Marlin \"$marlinRepositoryUrl\" ...\n" + >&$l echo -e "\nCloning Marlin \"$marlinRepositoryUrl\" ...\n" if [ "$marlinRepositoryBranch" != "" ]; then git clone -b "$marlinRepositoryBranch" --single-branch "$marlinRepositoryUrl" "$marlinDir" @@ -101,13 +101,13 @@ checkoutMarlin() cd $marlinDir - echo -e "\nFetching most recent Marlin from \"$marlinRepositoryUrl\" ...\n" + >&$l echo -e "\nFetching most recent Marlin from \"$marlinRepositoryUrl\" ...\n" git fetch git checkout git reset origin/`git rev-parse --abbrev-ref HEAD` --hard - echo -e "\n" + >&$l echo -e "\n" cd .. @@ -119,7 +119,7 @@ checkoutMarlin() ## Get the toolchain and Marlin, install board definition setupEnvironment() { - echo -e "\nSetting up build environment in \"$arduinoDir\" ...\n" + >&$l echo -e "\nSetting up build environment in \"$arduinoDir\" ...\n" getArduinoToolchain getDependencies getHardwareDefinition @@ -130,10 +130,10 @@ setupEnvironment() getHardwareDefinition() { if [ "$hardwareDefinitionRepo" != "" ]; then - echo -e "\nCloning board hardware definition from \"$hardwareDefinitionRepo\" ... \n" + >&$l echo -e "\nCloning board hardware definition from \"$hardwareDefinitionRepo\" ... \n" git clone "$hardwareDefinitionRepo" - echo -e "\nMoving board hardware definition into arduino directory ... \n" + >&$l echo -e "\nMoving board hardware definition into arduino directory ... \n" repoName=$(basename "$hardwareDefinitionRepo" ".${hardwareDefinitionRepo##*.}") @@ -147,10 +147,10 @@ getHardwareDefinition() ## param #1 backup name backupMarlinConfiguration() { - echo -e "\nSaving Marlin configuration\n" - echo -e " \"Configuration.h\"" - echo -e " \"Configuration_adv.h\"" - echo -e "\nto \"./configuration/$backupName/\"\n" + >&$l echo -e "\nSaving Marlin configuration\n" + >&$l echo -e " \"Configuration.h\"" + >&$l echo -e " \"Configuration_adv.h\"" + >&$l echo -e "\nto \"./configuration/$backupName/\"\n" mkdir -p configuration/$backupName @@ -163,15 +163,15 @@ backupMarlinConfiguration() restoreMarlinConfiguration() { if [ -d "configuration/$backupName" ]; then - echo -e "Restoring Marlin configuration\n" - echo -e " \"Configuration.h\"" - echo -e " \"Configuration_adv.h\"" - echo -e "\nfrom \"./configuration/$backupName/\"\n" + >&$l echo -e "Restoring Marlin configuration\n" + >&$l echo -e " \"Configuration.h\"" + >&$l echo -e " \"Configuration_adv.h\"" + >&$l echo -e "\nfrom \"./configuration/$backupName/\"\n" cp configuration/"$backupName"/Configuration.h "$marlinDir"/Marlin/ cp configuration/"$backupName"/Configuration_adv.h "$marlinDir"/Marlin/ else - echo -e "\nBackup configuration/$backupName not found!\n" + >&2 echo -e "\nBackup configuration/$backupName not found!\n" fi exit } @@ -179,7 +179,7 @@ restoreMarlinConfiguration() ## Build Marlin verifyBuild() { - echo -e "\nVerifying build ...\n" + >&$l echo -e "\nVerifying build ...\n" "$arduinoExecutable" --verify --verbose --board "$boardString" "$marlinDir"/Marlin/Marlin.ino --pref build.path="$buildDir" exit @@ -189,7 +189,7 @@ verifyBuild() ## Build Marlin and upload buildAndUpload() { - echo -e "\nBuilding and uploading Marlin build from \"$buildDir\" ...\n" + >&$l echo -e "\nBuilding and uploading Marlin build from \"$buildDir\" ...\n" "$arduinoExecutable" --upload --port "$port" --verbose --board "$boardString" "$marlinDir"/Marlin/Marlin.ino --pref build.path="$buildDir" exit @@ -208,10 +208,13 @@ cleanEverything() printUsage() { echo "Usage:" - echo " $scriptName []" + echo " $scriptName [-q] []" echo echo "Builds an installs Marlin 3D printer firmware." echo + echo "Options:" + echo " -q, --quiet Don't print status messages." + echo echo "Commands:" echo " setup Download and configure the toolchain and the" echo " necessary libraries for building Marlin." @@ -237,12 +240,12 @@ printUsage() if [ -f $defaultParametersFile ]; then source "$defaultParametersFile" else - echo -e "\n ===================================================================" - echo -e "\n Can't find $defaultParametersFile!" - echo -e "\n Please rename the \"$defaultParametersFile.example\" file placed in the" - echo -e " same directory as this script to \"$defaultParametersFile\" and edit" - echo -e " if neccessary.\n" - echo -e " ===================================================================\n\n" + >&2 echo -e "\n ===================================================================" + >&2 echo -e "\n Can't find $defaultParametersFile!" + >&2 echo -e "\n Please rename the \"$defaultParametersFile.example\" file placed in the" + >&2 echo -e " same directory as this script to \"$defaultParametersFile\" and edit" + >&2 echo -e " if neccessary.\n" + >&2 echo -e " ===================================================================\n\n" exit 1 fi @@ -293,6 +296,7 @@ parseBackupRestoreArgument() { fi } +quiet= verb='' while [ "x$verb" = "x" ]; do @@ -314,6 +318,7 @@ while [ "x$verb" = "x" ]; do ;; clean) verb=cleanEverything;; help|-h|--help) verb=printUsage;; + -q|--quiet) quiet=y;; *) printUsage >&2 exit 1 @@ -353,4 +358,6 @@ case $verb in ;; esac +[ "x$quiet" = "xy" ] && exec {l}>/dev/null || exec {l}>&1 + $verb From 3ef6d9823bc49f87f5c72600ae5e56530fe24a39 Mon Sep 17 00:00:00 2001 From: Joel Holdsworth Date: Sat, 6 Oct 2018 19:34:24 +0100 Subject: [PATCH 12/23] Added unpackArchive helper --- marlintool.sh | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/marlintool.sh b/marlintool.sh index f04f034..cfb11af 100755 --- a/marlintool.sh +++ b/marlintool.sh @@ -46,6 +46,21 @@ downloadFile() fi } +unpackArchive() +{ + local archive=$1 + local dir=$2 + + case $archive in + *.zip) + unzip -q "$archive" -d "$dir" + ;; + *.tar.*) + tar -xf "$archive" -C "$dir" --strip 1 + ;; + esac +} + ## Download the toolchain and unpack it getArduinoToolchain() { @@ -54,11 +69,7 @@ getArduinoToolchain() downloadFile http://downloads-02.arduino.cc/"$arduinoToolchainArchive" $arduinoToolchainArchive mkdir -p "$arduinoDir/portable" >&$l echo -e "\nUnpacking Arduino environment. This might take a while ...\n" - if [ "$os" == "Darwin" ]; then - unzip -q "$arduinoToolchainArchive" -d "$arduinoDir" - else - tar -xf "$arduinoToolchainArchive" -C "$arduinoDir" --strip 1 - fi + unpackArchive "$arduinoToolchainArchive" "$arduinoDir" rm -R "$arduinoToolchainArchive" } @@ -264,13 +275,11 @@ esac # Operating system specific values os=$(uname -s) if [ "$os" == "Darwin" ]; then - tools="git unzip" arduinoToolchainArchive="arduino-$arduinoToolchainVersion-macosx.zip" arduinoExecutable="$arduinoDir/Arduino.app/Contents/MacOS/Arduino" arduinoHardwareDir="$arduinoDir/Arduino.app/Contents/Java/hardware" arduinoLibrariesDir="$arduinoDir/Arduino.app/Contents/Java/libraries" else - tools="git tar" arduinoToolchainArchive="arduino-$arduinoToolchainVersion-$arduinoToolchainArchitecture.tar.xz" arduinoExecutable="$arduinoDir/arduino" arduinoHardwareDir="$arduinoDir/hardware" @@ -278,7 +287,7 @@ else fi -checkTools "$tools" +checkTools git tar unzip checkCurlWget if [ $# -lt 1 ]; then From ab6af9bc21a9fafee7984715abdcfe0b3ea87446 Mon Sep 17 00:00:00 2001 From: Joel Holdsworth Date: Sat, 6 Oct 2018 19:37:55 +0100 Subject: [PATCH 13/23] Silence command output by default --- marlintool.sh | 46 +++++++++++++++++++++++++++++++++------------- 1 file changed, 33 insertions(+), 13 deletions(-) diff --git a/marlintool.sh b/marlintool.sh index cfb11af..f3cd569 100755 --- a/marlintool.sh +++ b/marlintool.sh @@ -40,9 +40,9 @@ downloadFile() local file=$2 if [ "$curl" != "" ]; then - $curl -o "$file" "$url" + >&$o $curl -o "$file" "$url" else - $wget -O "$file" "$url" + >&$o $wget -O "$file" "$url" fi } @@ -53,10 +53,10 @@ unpackArchive() case $archive in *.zip) - unzip -q "$archive" -d "$dir" + >&$o unzip -q "$archive" -d "$dir" ;; *.tar.*) - tar -xf "$archive" -C "$dir" --strip 1 + >&$o tar -xf "$archive" -C "$dir" --strip 1 ;; esac } @@ -81,7 +81,7 @@ getDependencies() for library in ${marlinDependencies[@]}; do IFS=',' read libName libUrl libDir <<< "$library" - git clone "$libUrl" "$libName" + >&$o git clone "$libUrl" "$libName" rm -rf "$arduinoLibrariesDir"/"$libName" mv -f "$libName"/"$libDir" "$arduinoLibrariesDir"/"$libName" rm -rf "$libName" @@ -94,9 +94,9 @@ getMarlin() >&$l echo -e "\nCloning Marlin \"$marlinRepositoryUrl\" ...\n" if [ "$marlinRepositoryBranch" != "" ]; then - git clone -b "$marlinRepositoryBranch" --single-branch "$marlinRepositoryUrl" "$marlinDir" + >&$o git clone -b "$marlinRepositoryBranch" --single-branch "$marlinRepositoryUrl" "$marlinDir" else - git clone "$marlinRepositoryUrl" "$marlinDir" + >&$o git clone "$marlinRepositoryUrl" "$marlinDir" fi exit @@ -114,9 +114,9 @@ checkoutMarlin() >&$l echo -e "\nFetching most recent Marlin from \"$marlinRepositoryUrl\" ...\n" - git fetch - git checkout - git reset origin/`git rev-parse --abbrev-ref HEAD` --hard + >&$o git fetch + >&$o git checkout + >&$o git reset origin/`git rev-parse --abbrev-ref HEAD` --hard >&$l echo -e "\n" @@ -142,7 +142,7 @@ getHardwareDefinition() { if [ "$hardwareDefinitionRepo" != "" ]; then >&$l echo -e "\nCloning board hardware definition from \"$hardwareDefinitionRepo\" ... \n" - git clone "$hardwareDefinitionRepo" + >&$o git clone "$hardwareDefinitionRepo" >&$l echo -e "\nMoving board hardware definition into arduino directory ... \n" @@ -192,7 +192,15 @@ verifyBuild() { >&$l echo -e "\nVerifying build ...\n" - "$arduinoExecutable" --verify --verbose --board "$boardString" "$marlinDir"/Marlin/Marlin.ino --pref build.path="$buildDir" + if >&$o "$arduinoExecutable" --verify --verbose --board "$boardString" "$marlinDir"/Marlin/Marlin.ino \ + --pref build.path="$buildDir" 2>&1 ; then + >&$l echo "Build successful." + else + >&2 echo "Build failed." + >&2 echo + exit 1 + fi + exit } @@ -202,7 +210,15 @@ buildAndUpload() { >&$l echo -e "\nBuilding and uploading Marlin build from \"$buildDir\" ...\n" - "$arduinoExecutable" --upload --port "$port" --verbose --board "$boardString" "$marlinDir"/Marlin/Marlin.ino --pref build.path="$buildDir" + if >&$o "$arduinoExecutable" --upload --port "$port" --verbose --board "$boardString" \ + "$marlinDir"/Marlin/Marlin.ino --pref build.path="$buildDir" 2>&1 ; then + >&$l echo "Build and upload successful." + else + >&2 echo "Build and upload failed." + >&2 echo + exit 1 + fi + exit } @@ -225,6 +241,7 @@ printUsage() echo echo "Options:" echo " -q, --quiet Don't print status messages." + echo " -v, --verbose Print the output of sub-processes." echo echo "Commands:" echo " setup Download and configure the toolchain and the" @@ -306,6 +323,7 @@ parseBackupRestoreArgument() { } quiet= +verbose= verb='' while [ "x$verb" = "x" ]; do @@ -328,6 +346,7 @@ while [ "x$verb" = "x" ]; do clean) verb=cleanEverything;; help|-h|--help) verb=printUsage;; -q|--quiet) quiet=y;; + -v|--verbose) verbose=y;; *) printUsage >&2 exit 1 @@ -368,5 +387,6 @@ case $verb in esac [ "x$quiet" = "xy" ] && exec {l}>/dev/null || exec {l}>&1 +[ "x$verbose" = "xy" ] && exec {o}>&1 || exec {o}>/dev/null $verb From c8be8c1982be624e72a85f2b6908478deed1cb67 Mon Sep 17 00:00:00 2001 From: Joel Holdsworth Date: Sat, 6 Oct 2018 16:26:02 +0100 Subject: [PATCH 14/23] Store download files in a cache --- marlintool.sh | 65 +++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 50 insertions(+), 15 deletions(-) diff --git a/marlintool.sh b/marlintool.sh index f3cd569..8d4b8fb 100755 --- a/marlintool.sh +++ b/marlintool.sh @@ -9,6 +9,8 @@ set -u # The default config file to look for defaultParametersFile="marlintool.params" +cacheDir=$PWD/.cache + scriptName=$0 ## Checks that the tools listed in arguments are all installed. @@ -34,16 +36,29 @@ checkCurlWget() fi } -downloadFile() +getFile() { local url=$1 local file=$2 + local cacheFile=$cacheDir/$file + + if [ ! -f $cacheFile ]; then + >&$l echo " Downloading from $url..." - if [ "$curl" != "" ]; then - >&$o $curl -o "$file" "$url" + local downloadTmpFile=$tmpDir/download + if [ "$curl" != "" ]; then + >&$o $curl -o "$downloadTmpFile" "$url" + else + >&$o $wget -O "$downloadTmpFile" "$url" + fi + + mkdir -p $cacheDir + mv $downloadTmpFile $cacheFile else - >&$o $wget -O "$file" "$url" + >&$l echo " Retrieving $(basename $file) from cache..." fi + + echo $cacheFile } unpackArchive() @@ -64,13 +79,16 @@ unpackArchive() ## Download the toolchain and unpack it getArduinoToolchain() { - >&$l echo -e "\nDownloading Arduino environment ...\n" + >&$l echo -e "\nGetting Arduino environment..." - downloadFile http://downloads-02.arduino.cc/"$arduinoToolchainArchive" $arduinoToolchainArchive + local archive=$(getFile http://downloads-02.arduino.cc/"$arduinoToolchainArchive" $arduinoToolchainArchive) mkdir -p "$arduinoDir/portable" - >&$l echo -e "\nUnpacking Arduino environment. This might take a while ...\n" - unpackArchive "$arduinoToolchainArchive" "$arduinoDir" - rm -R "$arduinoToolchainArchive" + >&$l echo " Unpacking (this might take a while)..." + if [ "$os" == "Darwin" ]; then + >&$o unzip -q "$archive" -d "$arduinoDir" + else + >&$o tar -xf "$archive" -C "$arduinoDir" --strip 1 + fi } @@ -231,6 +249,12 @@ cleanEverything() rm -Rf "$buildDir" } +## Delete everything that was downloaded +cleanCache() +{ + rm -Rf "$cacheDir" +} + ## Print help printUsage() { @@ -257,26 +281,36 @@ printUsage() echo " restore-config Restore the Marlin given configuration." echo echo " clean Remove Arduino tool-chain and Marlin sources." + echo " clean-cache Clean up the download cache." echo echo " help Show help." echo exit } +onExit() +{ + rm -rf $tmpDir +} + # Check for parameters file and source it if available if [ -f $defaultParametersFile ]; then source "$defaultParametersFile" else - >&2 echo -e "\n ===================================================================" - >&2 echo -e "\n Can't find $defaultParametersFile!" - >&2 echo -e "\n Please rename the \"$defaultParametersFile.example\" file placed in the" - >&2 echo -e " same directory as this script to \"$defaultParametersFile\" and edit" - >&2 echo -e " if neccessary.\n" - >&2 echo -e " ===================================================================\n\n" + echo -e "\n ===================================================================" + echo -e "\n Can't find $defaultParametersFile!" + echo -e "\n Please rename the \"$defaultParametersFile.example\" file placed in the" + echo -e " same directory as this script to \"$defaultParametersFile\" and edit" + echo -e " if neccessary.\n" + echo -e " ===================================================================\n\n" exit 1 fi +# Temporary directory +tmpDir=$(mktemp -d "${TMPDIR:-/tmp}/marlintool.XXXXXX") +trap "onExit" EXIT + # Toolchain architecture arch=$(uname -m) case $arch in @@ -344,6 +378,7 @@ while [ "x$verb" = "x" ]; do backupName=$(parseBackupRestoreArgument $@) ;; clean) verb=cleanEverything;; + clean-cache) verb=cleanCache;; help|-h|--help) verb=printUsage;; -q|--quiet) quiet=y;; -v|--verbose) verbose=y;; From a7eef99b1c75c5e42c5eb237a5f55aa6561009fc Mon Sep 17 00:00:00 2001 From: Joel Holdsworth Date: Sat, 6 Oct 2018 18:11:49 +0100 Subject: [PATCH 15/23] Store git clones in the cache --- marlintool.sh | 88 +++++++++++++++++++++++++++++++++------------------ 1 file changed, 57 insertions(+), 31 deletions(-) diff --git a/marlintool.sh b/marlintool.sh index 8d4b8fb..aa07b25 100755 --- a/marlintool.sh +++ b/marlintool.sh @@ -36,6 +36,11 @@ checkCurlWget() fi } +gitRepoName() +{ + basename "$1" ".${1##*.}" +} + getFile() { local url=$1 @@ -61,6 +66,33 @@ getFile() echo $cacheFile } +getGitRepo() +{ + local url=$1 + local branch=$2 + + local repoName=$(gitRepoName $url) + local repoDir=$tmpDir/$repoName + local cacheRepo=$cacheDir/$repoName + + if [ -d $cacheRepo ]; then + >&$l echo " Updating repository from $url..." + (cd $cacheRepo; 2>&$o git fetch -p origin) + else + >&$l echo " Cloning repository from $url..." + >&$o git clone --bare $url $cacheRepo 2>&1 + fi + + >&$l echo " Copying from cache..." + if [ "$marlinRepositoryBranch" != "" ]; then + >&$o git clone -b "$branch" --single-branch $cacheRepo $repoDir 2>&1 + else + >&$o git clone $cacheRepo $repoDir 2>&1 + fi + + echo $repoDir +} + unpackArchive() { local archive=$1 @@ -95,50 +127,46 @@ getArduinoToolchain() ## Get dependencies and move them in place getDependencies() { - >&$l echo -e "\nDownloading libraries ...\n" + >&$l echo -e "\nGetting libraries..." for library in ${marlinDependencies[@]}; do IFS=',' read libName libUrl libDir <<< "$library" - >&$o git clone "$libUrl" "$libName" - rm -rf "$arduinoLibrariesDir"/"$libName" - mv -f "$libName"/"$libDir" "$arduinoLibrariesDir"/"$libName" - rm -rf "$libName" + local libRepo=$(getGitRepo "$libUrl" "") + mv -f "$libRepo/$libDir" "$arduinoLibrariesDir/$libName" + rm -rf "$libRepo" done } ## Clone Marlin getMarlin() { - >&$l echo -e "\nCloning Marlin \"$marlinRepositoryUrl\" ...\n" - - if [ "$marlinRepositoryBranch" != "" ]; then - >&$o git clone -b "$marlinRepositoryBranch" --single-branch "$marlinRepositoryUrl" "$marlinDir" - else - >&$o git clone "$marlinRepositoryUrl" "$marlinDir" - fi - + >&$l echo -e "\nGetting Marlin..." + local repoDir=$(getGitRepo "$marlinRepositoryUrl" "$marlinRepositoryBranch") + rm -rf "$marlinDir" + mv "$repoDir" "$marlinDir" exit } ## Update an existing Marlin clone checkoutMarlin() { - backupName=`date +%Y-%m-%d-%H-%M-%S` + local cacheRepo=$cacheDir/Marlin - # backup configuration + backupName=`date +%Y-%m-%d-%H-%M-%S` backupMarlinConfiguration - cd $marlinDir - >&$l echo -e "\nFetching most recent Marlin from \"$marlinRepositoryUrl\" ...\n" - >&$o git fetch - >&$o git checkout - >&$o git reset origin/`git rev-parse --abbrev-ref HEAD` --hard - - >&$l echo -e "\n" - - cd .. + ( + cd $cacheRepo + 2>&$o git fetch -p origin + ) + ( + cd $marlinDir + >&$o git fetch -p origin + >&$o git checkout + >&$o git reset origin/`git rev-parse --abbrev-ref HEAD` --hard + ) restoreMarlinConfiguration exit @@ -159,15 +187,13 @@ setupEnvironment() getHardwareDefinition() { if [ "$hardwareDefinitionRepo" != "" ]; then - >&$l echo -e "\nCloning board hardware definition from \"$hardwareDefinitionRepo\" ... \n" - >&$o git clone "$hardwareDefinitionRepo" - - >&$l echo -e "\nMoving board hardware definition into arduino directory ... \n" + >&$l echo -e "\nGetting board hardware definition..." + local repoPath=$(getGitRepo "$hardwareDefinitionRepo" "") - repoName=$(basename "$hardwareDefinitionRepo" ".${hardwareDefinitionRepo##*.}") + >&$l echo " Moving board hardware definition into arduino directory..." - mv -f $repoName/hardware/* "$arduinoHardwareDir" - rm -rf $repoName + mv -f $repoPath/hardware/* "$arduinoHardwareDir" + rm -rf $repoPath fi } From 33954da49765dfdf4b913e280194882dd2354fb9 Mon Sep 17 00:00:00 2001 From: Joel Holdsworth Date: Sat, 6 Oct 2018 18:13:45 +0100 Subject: [PATCH 16/23] Remove arduino/ directory before set-up --- marlintool.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/marlintool.sh b/marlintool.sh index aa07b25..3f2726c 100755 --- a/marlintool.sh +++ b/marlintool.sh @@ -114,6 +114,7 @@ getArduinoToolchain() >&$l echo -e "\nGetting Arduino environment..." local archive=$(getFile http://downloads-02.arduino.cc/"$arduinoToolchainArchive" $arduinoToolchainArchive) + rm -rf "$arduinoDir" mkdir -p "$arduinoDir/portable" >&$l echo " Unpacking (this might take a while)..." if [ "$os" == "Darwin" ]; then From f925ea684670859385acbefa3ba285e2ed3b6cef Mon Sep 17 00:00:00 2001 From: Joel Holdsworth Date: Sat, 6 Oct 2018 18:14:02 +0100 Subject: [PATCH 17/23] Store getDependencies variables in locals --- marlintool.sh | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/marlintool.sh b/marlintool.sh index 3f2726c..fac1abe 100755 --- a/marlintool.sh +++ b/marlintool.sh @@ -131,6 +131,10 @@ getDependencies() >&$l echo -e "\nGetting libraries..." for library in ${marlinDependencies[@]}; do + local libName= + local libUrl= + local libDir= + IFS=',' read libName libUrl libDir <<< "$library" local libRepo=$(getGitRepo "$libUrl" "") mv -f "$libRepo/$libDir" "$arduinoLibrariesDir/$libName" From e974d8c80b55ffd954ab20eb5362d68ccb99c895 Mon Sep 17 00:00:00 2001 From: Joel Holdsworth Date: Sun, 14 Oct 2018 20:28:33 +0100 Subject: [PATCH 18/23] Renamed verifyBuild to build --- marlintool.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/marlintool.sh b/marlintool.sh index fac1abe..0ddd453 100755 --- a/marlintool.sh +++ b/marlintool.sh @@ -237,9 +237,9 @@ restoreMarlinConfiguration() } ## Build Marlin -verifyBuild() +build() { - >&$l echo -e "\nVerifying build ...\n" + >&$l echo -e "\nBuilding Marlin...\n" if >&$o "$arduinoExecutable" --verify --verbose --board "$boardString" "$marlinDir"/Marlin/Marlin.ino \ --pref build.path="$buildDir" 2>&1 ; then @@ -396,7 +396,7 @@ while [ "x$verb" = "x" ]; do setup) verb=setupEnvironment;; get-marlin) verb=getMarlin;; update-marlin) verb=checkoutMarlin;; - build) verb=verifyBuild;; + build) verb=build;; build-upload) verb=buildAndUpload;; backup-config) verb=backupMarlinConfiguration From b2d268ead3a7d8ea88eaa8f570f638612bcc8a3f Mon Sep 17 00:00:00 2001 From: Joel Holdsworth Date: Thu, 18 Oct 2018 13:53:58 +0100 Subject: [PATCH 19/23] Renamed checkoutMarlin to updateMarlin --- marlintool.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/marlintool.sh b/marlintool.sh index 0ddd453..824cf0d 100755 --- a/marlintool.sh +++ b/marlintool.sh @@ -153,7 +153,7 @@ getMarlin() } ## Update an existing Marlin clone -checkoutMarlin() +updateMarlin() { local cacheRepo=$cacheDir/Marlin @@ -395,7 +395,7 @@ while [ "x$verb" = "x" ]; do case $1 in setup) verb=setupEnvironment;; get-marlin) verb=getMarlin;; - update-marlin) verb=checkoutMarlin;; + update-marlin) verb=updateMarlin;; build) verb=build;; build-upload) verb=buildAndUpload;; backup-config) From 5a09ec11b0b3cc709d5a9e6243e7b785790a6875 Mon Sep 17 00:00:00 2001 From: Joel Holdsworth Date: Sun, 14 Oct 2018 20:41:51 +0100 Subject: [PATCH 20/23] Check marlin and tool-chain are installed before building --- marlintool.sh | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/marlintool.sh b/marlintool.sh index 824cf0d..25d1450 100755 --- a/marlintool.sh +++ b/marlintool.sh @@ -36,6 +36,24 @@ checkCurlWget() fi } +checkToolChain() +{ + if [ ! -f $arduinoExecutable ]; then + >&2 echo "Arduino tool-chain has not been set up. Run $0 setup" + >&2 echo + exit 1 + fi +} + +checkMarlin() +{ + if [ ! -f $marlinDir/Marlin/Marlin.ino ]; then + >&2 echo "Marlin has not been set up. Run $0 get-marlin" + >&2 echo + exit 1 + fi +} + gitRepoName() { basename "$1" ".${1##*.}" @@ -241,6 +259,9 @@ build() { >&$l echo -e "\nBuilding Marlin...\n" + checkToolChain + checkMarlin + if >&$o "$arduinoExecutable" --verify --verbose --board "$boardString" "$marlinDir"/Marlin/Marlin.ino \ --pref build.path="$buildDir" 2>&1 ; then >&$l echo "Build successful." @@ -259,6 +280,9 @@ buildAndUpload() { >&$l echo -e "\nBuilding and uploading Marlin build from \"$buildDir\" ...\n" + checkToolChain + checkMarlin + if >&$o "$arduinoExecutable" --upload --port "$port" --verbose --board "$boardString" \ "$marlinDir"/Marlin/Marlin.ino --pref build.path="$buildDir" 2>&1 ; then >&$l echo "Build and upload successful." From 560161ec4f80bcf117b3ae7d5bab5f102e1c930e Mon Sep 17 00:00:00 2001 From: Joel Holdsworth Date: Thu, 18 Oct 2018 14:02:59 +0100 Subject: [PATCH 21/23] Removed unneeded exit commands --- marlintool.sh | 8 -------- 1 file changed, 8 deletions(-) diff --git a/marlintool.sh b/marlintool.sh index 25d1450..8e4f3bc 100755 --- a/marlintool.sh +++ b/marlintool.sh @@ -167,7 +167,6 @@ getMarlin() local repoDir=$(getGitRepo "$marlinRepositoryUrl" "$marlinRepositoryBranch") rm -rf "$marlinDir" mv "$repoDir" "$marlinDir" - exit } ## Update an existing Marlin clone @@ -192,7 +191,6 @@ updateMarlin() ) restoreMarlinConfiguration - exit } @@ -203,7 +201,6 @@ setupEnvironment() getArduinoToolchain getDependencies getHardwareDefinition - exit } ## Fetch and install anet board hardware definition @@ -251,7 +248,6 @@ restoreMarlinConfiguration() else >&2 echo -e "\nBackup configuration/$backupName not found!\n" fi - exit } ## Build Marlin @@ -270,8 +266,6 @@ build() >&2 echo exit 1 fi - - exit } @@ -291,8 +285,6 @@ buildAndUpload() >&2 echo exit 1 fi - - exit } From 70ce5a923f5242a552ca9c2560fc3d7bf6e67577 Mon Sep 17 00:00:00 2001 From: Joel Holdsworth Date: Thu, 18 Oct 2018 14:13:24 +0100 Subject: [PATCH 22/23] Tidied up backup reporting --- marlintool.sh | 53 ++++++++++++++++++++++++++++++++------------------- 1 file changed, 33 insertions(+), 20 deletions(-) diff --git a/marlintool.sh b/marlintool.sh index 8e4f3bc..8bb469a 100755 --- a/marlintool.sh +++ b/marlintool.sh @@ -175,9 +175,9 @@ updateMarlin() local cacheRepo=$cacheDir/Marlin backupName=`date +%Y-%m-%d-%H-%M-%S` - backupMarlinConfiguration + doBackup $backupName - >&$l echo -e "\nFetching most recent Marlin from \"$marlinRepositoryUrl\" ...\n" + >&$l echo "Fetching most recent Marlin from \"$marlinRepositoryUrl\"..." ( cd $cacheRepo @@ -190,7 +190,7 @@ updateMarlin() >&$o git reset origin/`git rev-parse --abbrev-ref HEAD` --hard ) - restoreMarlinConfiguration + doRestore $backupName } @@ -217,20 +217,38 @@ getHardwareDefinition() fi } +## Reports the backup paths +reportBackupFiles() +{ + >&$l echo " \"Configuration.h\"" + >&$l echo " \"Configuration_adv.h\"" + >&$l echo "$1 \"./configuration/$backupName/\"" + >&$l echo +} + +## Performs the backup operation +doBackup() +{ + >&$l echo "Saving Marlin configuration..." + mkdir -p configuration/$0 + cp "$marlinDir"/Marlin/Configuration.h configuration/"$0" + cp "$marlinDir"/Marlin/Configuration_adv.h configuration/"$0" +} + +doRestore() +{ + >&$l echo "Restoring Marlin configuration..." + cp configuration/"$0"/Configuration.h "$marlinDir"/Marlin/ + cp configuration/"$0"/Configuration_adv.h "$marlinDir"/Marlin/ +} + ## Backup Marlin configuration ## param #1 backup name backupMarlinConfiguration() { - >&$l echo -e "\nSaving Marlin configuration\n" - >&$l echo -e " \"Configuration.h\"" - >&$l echo -e " \"Configuration_adv.h\"" - >&$l echo -e "\nto \"./configuration/$backupName/\"\n" - - mkdir -p configuration/$backupName - - cp "$marlinDir"/Marlin/Configuration.h configuration/"$backupName" - cp "$marlinDir"/Marlin/Configuration_adv.h configuration/"$backupName" + doBackup $backupName + reportBackupFiles 'to' } ## Restore Marlin Configuration from backup @@ -238,15 +256,10 @@ backupMarlinConfiguration() restoreMarlinConfiguration() { if [ -d "configuration/$backupName" ]; then - >&$l echo -e "Restoring Marlin configuration\n" - >&$l echo -e " \"Configuration.h\"" - >&$l echo -e " \"Configuration_adv.h\"" - >&$l echo -e "\nfrom \"./configuration/$backupName/\"\n" - - cp configuration/"$backupName"/Configuration.h "$marlinDir"/Marlin/ - cp configuration/"$backupName"/Configuration_adv.h "$marlinDir"/Marlin/ + doRestore $backupName + reportBackupFiles 'from' else - >&2 echo -e "\nBackup configuration/$backupName not found!\n" + >&2 echo "Backup configuration/$backupName not found!" fi } From 39c0d010ef38c62ca808a16b0c4aa15ec11f3466 Mon Sep 17 00:00:00 2001 From: Joel Holdsworth Date: Thu, 18 Oct 2018 14:21:38 +0100 Subject: [PATCH 23/23] Tidied status output --- marlintool.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/marlintool.sh b/marlintool.sh index 8bb469a..898a124 100755 --- a/marlintool.sh +++ b/marlintool.sh @@ -129,7 +129,7 @@ unpackArchive() ## Download the toolchain and unpack it getArduinoToolchain() { - >&$l echo -e "\nGetting Arduino environment..." + >&$l echo "Getting Arduino environment..." local archive=$(getFile http://downloads-02.arduino.cc/"$arduinoToolchainArchive" $arduinoToolchainArchive) rm -rf "$arduinoDir" @@ -266,7 +266,7 @@ restoreMarlinConfiguration() ## Build Marlin build() { - >&$l echo -e "\nBuilding Marlin...\n" + >&$l echo "Building Marlin...\n" checkToolChain checkMarlin @@ -285,7 +285,7 @@ build() ## Build Marlin and upload buildAndUpload() { - >&$l echo -e "\nBuilding and uploading Marlin build from \"$buildDir\" ...\n" + echo "Building and uploading Marlin..." checkToolChain checkMarlin