From 418bcdcc053214598518f37309cf258847e35d6d Mon Sep 17 00:00:00 2001 From: elral <3263285+elral@users.noreply.github.com> Date: Wed, 12 Feb 2025 12:39:08 +0100 Subject: [PATCH] Set version in json using release information (#354) * add function to python script for setting the FW version * fixed wrong variable type for path * revert path definition partly back --- .../Atmel/Board_Mega/arduino_mega.board.json | 2 +- .../Atmel/Board_Nano/arduino_nano.board.json | 2 +- .../Board_ProMicro/arduino_micro.board.json | 2 +- .../Atmel/Board_Uno/arduino_uno.board.json | 2 +- .../Pico/raspberrypi_pico.board.json | 2 +- copy_fw_files.py | 26 ++++++++++++++++--- 6 files changed, 28 insertions(+), 8 deletions(-) diff --git a/_Boards/Atmel/Board_Mega/arduino_mega.board.json b/_Boards/Atmel/Board_Mega/arduino_mega.board.json index b921a599..8af0c1b1 100644 --- a/_Boards/Atmel/Board_Mega/arduino_mega.board.json +++ b/_Boards/Atmel/Board_Mega/arduino_mega.board.json @@ -37,7 +37,7 @@ "DelayAfterFirmwareUpdate": 0, "FirmwareBaseName": "mobiflight_mega", "FirmwareExtension": "hex", - "LatestFirmwareVersion": "3.0.0", + "LatestFirmwareVersion": "0.0.1", "FriendlyName": "Arduino Mega 2560", "MobiFlightType": "MobiFlight Mega", "ResetFirmwareFile": "reset.arduino_mega_1_0_2.hex", diff --git a/_Boards/Atmel/Board_Nano/arduino_nano.board.json b/_Boards/Atmel/Board_Nano/arduino_nano.board.json index 1b5e5b41..cb4593eb 100644 --- a/_Boards/Atmel/Board_Nano/arduino_nano.board.json +++ b/_Boards/Atmel/Board_Nano/arduino_nano.board.json @@ -33,7 +33,7 @@ "FirmwareBaseName": "mobiflight_nano", "FirmwareExtension": "hex", "FriendlyName": "Arduino Nano", - "LatestFirmwareVersion": "3.0.0", + "LatestFirmwareVersion": "0.0.1", "MobiFlightType": "MobiFlight Nano", "ResetFirmwareFile": "reset.arduino_uno_1_0_2.hex", "Community": { diff --git a/_Boards/Atmel/Board_ProMicro/arduino_micro.board.json b/_Boards/Atmel/Board_ProMicro/arduino_micro.board.json index 7e454d68..3b4f2f29 100644 --- a/_Boards/Atmel/Board_ProMicro/arduino_micro.board.json +++ b/_Boards/Atmel/Board_ProMicro/arduino_micro.board.json @@ -27,7 +27,7 @@ "FirmwareBaseName": "mobiflight_micro", "FirmwareExtension": "hex", "FriendlyName": "Arduino Pro Micro", - "LatestFirmwareVersion": "3.0.0", + "LatestFirmwareVersion": "0.0.1", "MobiFlightType": "MobiFlight Micro", "ResetFirmwareFile": "reset.arduino_promicro_1_0_2.hex", "Community": { diff --git a/_Boards/Atmel/Board_Uno/arduino_uno.board.json b/_Boards/Atmel/Board_Uno/arduino_uno.board.json index 438fb340..a03476f1 100644 --- a/_Boards/Atmel/Board_Uno/arduino_uno.board.json +++ b/_Boards/Atmel/Board_Uno/arduino_uno.board.json @@ -32,7 +32,7 @@ "FriendlyName": "Arduino Uno", "FirmwareBaseName": "mobiflight_uno", "FirmwareExtension": "hex", - "LatestFirmwareVersion": "3.0.0", + "LatestFirmwareVersion": "0.0.1", "MobiFlightType": "MobiFlight Uno", "ResetFirmwareFile": "reset.arduino_uno_1_0_2.hex", "Community": { diff --git a/_Boards/RaspberryPi/Pico/raspberrypi_pico.board.json b/_Boards/RaspberryPi/Pico/raspberrypi_pico.board.json index 6bbaeb08..20e3e6a0 100644 --- a/_Boards/RaspberryPi/Pico/raspberrypi_pico.board.json +++ b/_Boards/RaspberryPi/Pico/raspberrypi_pico.board.json @@ -20,7 +20,7 @@ "FirmwareBaseName": "mobiflight_raspberrypico", "FirmwareExtension": "uf2", "FriendlyName": "Raspberry Pico", - "LatestFirmwareVersion": "3.0.0", + "LatestFirmwareVersion": "0.0.1", "MobiFlightType": "MobiFlight RaspiPico", "ResetFirmwareFile": "reset.raspberry_pico_flash_nuke.uf2" }, diff --git a/copy_fw_files.py b/copy_fw_files.py index dd4eaa32..46b434f0 100644 --- a/copy_fw_files.py +++ b/copy_fw_files.py @@ -1,5 +1,6 @@ Import("env") import os, zipfile, shutil +import fileinput from pathlib import Path # Get the version number from the build environment. @@ -10,9 +11,9 @@ firmware_version = firmware_version.strip(".") zip_file_name = 'Mobiflight-Connector' -build_path = './_build' -build_path_fw = build_path + '/firmware' -build_path_json = build_path + '/Boards' +build_path = Path('./_build') +build_path_fw = build_path / 'firmware' +build_path_json = build_path / 'Boards' distrubution_path = './_dist' board_folder = ['./_Boards/Atmel', './_Boards/RaspberryPi'] @@ -38,6 +39,13 @@ def copy_fw_files (source, target, env): file_extension = '.json' copy_files_by_extension(board_folder, build_path_json, file_extension) + # set FW version within boad.json files + replacements = { + "0.0.1": firmware_version + } + for file_path in build_path_json.rglob("*.json"): + replace_in_file(file_path, replacements) + # Create ZIP file and add files from distrubution folder zip_file_path = distrubution_path + '/' + zip_file_name + '_' + firmware_version + '.zip' createZIP(build_path, zip_file_path, zip_file_name) @@ -65,5 +73,17 @@ def createZIP(original_folder_path, zip_file_path, new_folder_name): zipf.write(os.path.join(root, file), new_path) +def replace_in_file(file_path, replacements): + """Replace all keys in `replacements` with their values in the given file.""" + with open(file_path, "r", encoding="utf-8") as file: + content = file.read() + + for old, new in replacements.items(): + content = content.replace(old, new) + + with open(file_path, "w", encoding="utf-8") as file: + file.write(content) + + env.AddPostAction("$BUILD_DIR/${PROGNAME}.hex", copy_fw_files) env.AddPostAction("$BUILD_DIR/${PROGNAME}.bin", copy_fw_files)