Skip to content

Instantly share code, notes, and snippets.

@iComputerfreak
Last active January 27, 2022 22:48
Show Gist options
  • Save iComputerfreak/9f0843270dba65402f9e23a19e9db3ff to your computer and use it in GitHub Desktop.
Save iComputerfreak/9f0843270dba65402f9e23a19e9db3ff to your computer and use it in GitHub Desktop.
Marlin Update and Configuration Override
#!/bin/zsh
set -e
# This script assumes the following folder structure:
# .
# ├── .current_version
# ├── Configurations <- clone of https://github.com/MarlinFirmware/Configurations
# ├── Marlin <- clone of https://github.com/MarlinFirmware/Marlin
# ├── Overrides <- Configuration.h and Configuration_adv.h with the overriding values (all lines without any indentation)
# └── update.sh <- this script
# This script checks if the latest Marlin version is newer than the current version stored in the file .current_version.
# If there is a newer Marlin version available, the script pulls both the Marlin updates and the Configuration updates.
# It then copies the configuration files into the Marlin repository and applies any overrides specified.
# Use "./update.sh -f" to skip the version check and update anyways
config_path="config/examples/AnyCubic/Mega Zero 2.0/Anycubic V1/"
cd "$(dirname "$0")"
echo "Checking for updates..."
current_version=$(head .current_version)
echo "Current version: $current_version"
cd Marlin
git fetch --tags
latest_version=$(git tag | tail -1)
echo "Latest version: $latest_version"
if [[ "$1" != "-f" ]]; then
# If there are no updates, exit
autoload is-at-least
# If the installed version is at least the latest version (cannot be newer so this is an equal check), exit
is-at-least "$current_version" "$latest_version" && echo "No updates available." && exit 0 || echo "New version available!"
fi
echo
echo "Updating Marlin"
git reset --hard
git clean -fd
git checkout -
git pull
git checkout tags/"$latest_version"
cd ..
echo
echo "Updating Configurations"
cd Configurations
git reset --hard
git clean -fd
git checkout -
git pull
git checkout tags/"$latest_version"
cd ..
cp "Configurations/$config_path/Configuration.h" "Marlin/Marlin/"
cp "Configurations/$config_path/Configuration_adv.h" "Marlin/Marlin/"
# For each line in the override files, replace the line in the real configuration, keeping the identation
cd Overrides
echo
echo "Writing overrides..."
for filename in Configuration.h Configuration_adv.h
do
echo " Configuring ${filename}..."
while IFS='' read -r line || [ -n "${line}" ]
do
if [[ -n "$line" ]]; then
# "#define PARAMETER"
echo " $line"
# Escape the line's contents to prevent regex conflicts
line_clean=$(echo "$line" | sed 's/\//\\\//g')
query=$(echo "$line_clean" | cut -d ' ' -f1-2)
# Remove leading // if present (query should not be commented out; leading // are already escaped)
query="${query##\\/\\/}"
# For all lines that match the regex "^( *)(\/\/)?($query) .*" (indentation, comment?, query, value)
# Replace the match "(\/\/)?($query) .*" (comment?, query, value)
# With $line_clean (whatever is in the overrides file)
#echo " Query: $query"
#echo " Replc: $line_clean"
sed -i "" -E "/^( *)(\/\/)?($query)( |$).*/s/(\/\/)?($query)( |$).*/$line_clean/g" "../Marlin/Marlin/${filename}"
fi
done < "$filename"
done
cd ..
echo "$latest_version" > .current_version
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment