change rollback time because of start http

master
Jan Umbach 2023-08-15 10:31:55 +02:00
parent 30a12b8aa6
commit e9fa3a1423
4 changed files with 348 additions and 1 deletions

@ -1 +1 @@
Subproject commit 3c8fca81162a6f5deebd8caa7702202d64b4e7c2
Subproject commit 8dab6c3de4a2b1f301a780315bc5f2d03286b399

345
script.sh Normal file
View File

@ -0,0 +1,345 @@
#!/bin/bash
cd "../../"
# echo Project path
echo "Project Path: $(pwd)"
# Set the IDF_PATH environment variable to point to the ESP-IDF installation
export IDF_PATH=~/esp/esp-idf
version_file="version.txt"
initial_version="1.0"
initProject="false"
firmware_server_path="./firmwareserver"
upload_server="$2"
firmware_server="$3"
# Function to translate text using translate-shell
translate() {
text="$1"
lang="$2"
translation=$(trans -b :"$lang" "$text")
echo "$translation"
}
urlencode() {
# Usage: urlencode "string"
local data
data="$(curl -Go /dev/null --data-urlencode "$1" "")"
data=${data#?} # remove leading "?"
echo "$data"
}
# check if backup file exists and restore it
if [ -f "$version_file.backup" ]; then
mv "$version_file.backup" "$version_file"
fi
# Check if version file exists
if [ -f "$version_file" ]; then
current_version=$(cat "$version_file")
else
echo ""
echo ""
read -p "Version file not found. Create version.txt with initial version $initial_version? (y/n) " create_new
if [ "$create_new" = "y" ]; then
echo "$initial_version" >"$version_file"
current_version="$initial_version"
initProject="true"
else
echo "Cancelling script."
exit 0
fi
fi
# check if project is initialized
if [ "$initProject" = "true" ]; then
echo ""
echo "###############################################"
echo ""
echo "Project initialized with version $initial_version"
echo ""
echo "###############################################"
echo ""
else
# create backup file from version.txt
cp "$version_file" "$version_file.backup"
# Ask user for input
echo ""
echo ""
read -p "Is this a bugfix update (y/n)? [y]" bug_fixes
if [ "$bug_fixes" = "y" ] || [ "$bug_fixes" = "" ]; then
# current version string are two numbers separated by a dot (e.g. 1.0) so split to two variables
major_version=$(echo "$current_version" | cut -d. -f1)
minor_version=$(echo "$current_version" | cut -d. -f2)
# increment minor version
new_version="$major_version.$(echo "$minor_version + 1" | bc)"
else
echo ""
echo ""
read -p "Are there major changes (y/n)? [n]" major_changes
# Determine the version increment based on user input
if [ "$major_changes" = "y" ]; then
# current version string are two numbers separated by a dot (e.g. 1.0) so split to two variables
major_version=$(echo "$current_version" | cut -d. -f1)
minor_version=$(echo "$current_version" | cut -d. -f2)
# increment major version
new_version="$(echo "$major_version + 1" | bc).0"
else
echo ""
echo "###############################################"
echo ""
echo "No version change needed."
echo ""
echo "###############################################"
exit 0
fi
fi
# Update version in the file
echo "$new_version" >"$version_file"
echo ""
echo "###############################################"
echo ""
echo "Update and publish firmware to version: $new_version ..."
echo ""
echo "###############################################"
echo ""
fi
# get project name in cmake file but the file contains multiple project texts
project_name=$(grep -oP '(?<=project\().*(?=\))' CMakeLists.txt)
# git get current branch name
branch_name=$(git rev-parse --abbrev-ref HEAD)
# make curl get request to $3 url and set response to $response
response=$(curl -s "$firmware_server/getVersion?device=$project_name&branch=$branch_name")
# response is {"error":"Device not found"}
# response is {"version":"1.0"}
# check if response contains error
if echo "$response" | grep -q "error"; then
echo ""
echo "############### WARNING ####################"
echo ""
echo "Device '$project_name' on branch '$branch_name' not found in firmware server!"
echo ""
read -p "Create device in firmware server? (y/n) " create_device
if [ "$create_device" = "y" ]; then
echo ""
echo "###############################################"
echo ""
echo "Device '$project_name' on branch '$branch_name' will be created in firmware server."
echo ""
echo "###############################################"
echo ""
else
echo "Cancelling script."
exit 1
fi
fi
oldversion="none"
# check if response contains version
if echo "$response" | grep -q "version"; then
# get version from response
oldversion=$(echo "$response" | grep -oP '(?<="version":").*(?=")')
#echo ""
#echo "###############################################"
#echo ""
#echo "Device $project_name found in firmware server with version $oldversion"
#echo ""
#echo "###############################################"
#echo ""
fi
. ${IDF_PATH}/export.sh >/dev/null 2>&1
# Clean the build directory
# ask user if clean build directory
#echo ""
#echo "###############################################"
echo ""
read -p "Clean build directory? (y/n) [n]" clean_build
if [ "$clean_build" = "y" ]; then
echo "cleaning project..."
echo ""
"$IDF_PATH/tools/idf.py" fullclean
fi
echo "building project..."
echo ""
"$IDF_PATH/tools/idf.py" build
# Check if the build was successful
if [ $? -eq 0 ]; then
echo "project name: $project_name"
# check if project_name.bin file in ./build directory exists
if [ -f "./build/$project_name.bin" ]; then
echo "firmware file found: $project_name.bin"
else
echo "firmware file not found: $project_name.bin"
exit 1
fi
# check if ./changelog_en.txt exists If not exists create empty file
if [ ! -f "./changelog_en.txt" ]; then
echo "creating changelog_en.txt..."
echo "" >"./changelog_en.txt"
fi
# read changelog_en.txt
changes_en=$(cat "./changelog_en.txt")
# check if changes_en is empty or contains no letters or numbers
echo ""
echo "###############################################"
echo ""
if [ -z "$changes_en" ] || [ ! -n "$(echo "$changes_en" | sed -e 's/[[:space:]]//g')" ]; then
echo "Enter quick changes:"
echo ""
read -p "English: " changes_en
echo ""
echo "###############################################"
echo ""
else
changesFromFile="true"
fi
echo "English changes:"
echo "$changes_en"
changes_de=$(translate "$changes_en" de)
echo ""
echo "German changes:"
echo "$changes_de"
echo ""
echo "###############################################"
echo ""
# check if var "changesFromFile" is true
if [ "$changesFromFile" = "true" ]; then
# ask to agree or edit changes
read -p "Do you agree with the changes? (y/n) [y]" agree_changes
if [ "$agree_changes" != "y" ] && [ "$agree_changes" != "" ]; then
echo "Enter changes:"
echo ""
read -p "English: " changes_en
echo ""
echo "###############################################"
echo ""
echo "English changes:"
echo "$changes_en"
echo ""
changes_de=$(translate "$changes_en" de)
echo "German changes:"
echo "$changes_de"
echo ""
changesFromFile="false"
fi
fi
myChangelogs=""
# if changes entered ask to agree or discard
if [ ! -z "$changes_de" ] || [ ! -z "$changes_en" ]; then
if [ "$changesFromFile" != "true" ]; then
read -p "Do you agree with the changes? (y/n) [y]" agree_changes
fi
if [ "$changesFromFile" = "true" ] || [ "$agree_changes" = "y" ] || [ "$agree_changes" = "" ]; then
# escape to json in $changes_de and $changes_en
changes_de_escaped=$(echo "$changes_de" | sed ':a;N;$!ba;s/\n/\\n/g')
changes_en_escaped=$(echo "$changes_en" | sed ':a;N;$!ba;s/\n/\\n/g')
myChangelogs="{\"changelog\":{\"de\": \"$changes_de_escaped\", \"en\": \"$changes_en_escaped\"}}"
#create changelogs folder if not exists
if [ ! -d "./changelogs" ]; then
mkdir "./changelogs"
fi
if [ "$changesFromFile" = "true" ]; then
mv "changelog_en.txt" "./changelogs/$project_name-$new_version-en.txt"
touch "changelog_en.txt"
else
# write changes_en to "./changelogs/$project_name-$new_version-en.txt"
echo "$changes_en" >"./changelogs/$project_name-$new_version-en.txt"
fi
echo "sending changelog to firmware server..."
response=$(curl -s -G "$upload_server/updateChangelog?key=$1&device=$project_name&branch=$branch_name&version=$new_version" --data-urlencode "changelogs=$myChangelogs")
echo "$response"
echo ""
else
echo "Cancelling script."
exit 1
fi
fi
# upload firmware file to upload server
echo "uploading firmware file to firmware server..."
response=$(curl -F "file=@./build/$project_name.bin" "$upload_server/upload?key=$1&device=$project_name&branch=$branch_name&version=$new_version")
echo "$response"
#remove old version file
rm "$version_file.backup"
echo ""
echo "###############################################"
echo ""
echo "Build successful :-) Firmware published and updated from $oldversion to $new_version !"
exit 0
else
# Restore the version file from backup
mv "$version_file.backup" "$version_file"
echo ""
echo ""
echo "###############################################"
echo ""
echo "Build failed. Firmware will NOT be published!"
echo ""
echo "###############################################"
exit 1
fi

1
version.txt Normal file
View File

@ -0,0 +1 @@
1.0

1
version.txt.backup Normal file
View File

@ -0,0 +1 @@
1.0