From ba78923b93e8cdd52acfc64b315327383a244b0e Mon Sep 17 00:00:00 2001 From: spcqike Date: Sat, 24 Aug 2024 12:09:47 +0200 Subject: [PATCH] add pre script to check if webapp changed and auto compile --- .gitignore | 1 + pio-scripts/compile_webapp.py | 76 +++++++++++++++++++++++++++++++++++ platformio.ini | 1 + 3 files changed, 78 insertions(+) create mode 100644 pio-scripts/compile_webapp.py diff --git a/.gitignore b/.gitignore index c29e72f47..483246013 100644 --- a/.gitignore +++ b/.gitignore @@ -7,4 +7,5 @@ platformio-device-monitor*.log logs/device-monitor*.log platformio_override.ini +.webapp_hashes.pkl .DS_Store diff --git a/pio-scripts/compile_webapp.py b/pio-scripts/compile_webapp.py new file mode 100644 index 000000000..7a978a928 --- /dev/null +++ b/pio-scripts/compile_webapp.py @@ -0,0 +1,76 @@ +import os +import hashlib +import pickle +import subprocess + +# Funktion zur Berechnung des Hash-Werts einer Datei +def calculate_hash(file_path): + hasher = hashlib.md5() + with open(file_path, 'rb') as f: + buf = f.read() + hasher.update(buf) + return hasher.hexdigest() + +# Funktion zur Überprüfung der Dateien in einem Ordner +def check_files(directory, hash_file): + file_hashes = {} + + # Alle Dateien im Ordner und Unterordner durchsuchen + for root, dirs, files in os.walk(directory): + for file in files: + file_path = os.path.join(root, file) + file_hashes[file_path] = calculate_hash(file_path) + + # Laden der vorherigen Hash-Werte + if os.path.exists(hash_file): + with open(hash_file, 'rb') as f: + old_file_hashes = pickle.load(f) + else: + old_file_hashes = {} + + changed = False + # Vergleich der aktuellen Hash-Werte mit den vorherigen + for file_path, file_hash in file_hashes.items(): + if file_path not in old_file_hashes or old_file_hashes[file_path] != file_hash: + changed = True + break + + if not changed: + print("No changes detected.") + else: + print(f"webapp changed.") + execute_command(directory) + + # Speichern der aktuellen Hash-Werte + with open(hash_file, 'wb') as f: + pickle.dump(file_hashes, f) + +# Funktion zur Ausführung eines Befehls +def execute_command(directory): + print("Webapp changed, rebuilding...") + # change working directory + print(f"Changing directory to: {directory}") + os.chdir(directory) + # Run yarn install + result = subprocess.run(["yarn", "install"], shell=True) + if result.returncode != 0: + print("Error during yarn install.") + return + # Run yarn build + result = subprocess.run(["yarn", "build"], shell=True, capture_output=True, text=True) + if result.returncode != 0: + print("Error during yarn build:") + print(result.stdout) + print(result.stderr) + else: + print("Build completed successfully.") + # move back to old directory + os.chdir("..") + +# Pfad zum Ordner und zur Hash-Datei +directory = 'webapp' +hash_file = ".webapp_hashes.pkl" + +# Überprüfung der Dateien +print("checke webapp") +check_files(directory, hash_file) diff --git a/platformio.ini b/platformio.ini index f9435984c..b7917a10e 100644 --- a/platformio.ini +++ b/platformio.ini @@ -50,6 +50,7 @@ lib_deps = plerup/EspSoftwareSerial @ ^8.2.0 extra_scripts = + pre:pio-scripts/compile_webapp.py pre:pio-scripts/auto_firmware_version.py pre:pio-scripts/patch_apply.py post:pio-scripts/create_factory_bin.py