Skip to content

Commit

Permalink
add pre script to check if webapp changed and auto compile
Browse files Browse the repository at this point in the history
  • Loading branch information
spcqike authored and schlimmchen committed Sep 5, 2024
1 parent a87f9fa commit ba78923
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
platformio-device-monitor*.log
logs/device-monitor*.log
platformio_override.ini
.webapp_hashes.pkl
.DS_Store
76 changes: 76 additions & 0 deletions pio-scripts/compile_webapp.py
Original file line number Diff line number Diff line change
@@ -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)
1 change: 1 addition & 0 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ba78923

Please sign in to comment.