From a3a97d3025822c38256a560d1eb83695e2b589f1 Mon Sep 17 00:00:00 2001
From: Ben Meadors <benmmeadors@gmail.com>
Date: Sun, 6 Oct 2024 05:24:57 -0500
Subject: [PATCH] Start of generating json manifest of macros in userPrefs.h
 (#4946)

* Start of generating json manifest for userPrefs.h

* Just trunk this for now

* Add automatic generation of json manifest in GH action

* Trunk
---
 .github/workflows/generate-userprefs.yml | 33 ++++++++++++++++
 bin/build-userprefs-json.py              | 48 ++++++++++++++++++++++++
 userPrefs.json                           | 16 ++++++++
 3 files changed, 97 insertions(+)
 create mode 100644 .github/workflows/generate-userprefs.yml
 create mode 100644 bin/build-userprefs-json.py
 create mode 100644 userPrefs.json

diff --git a/.github/workflows/generate-userprefs.yml b/.github/workflows/generate-userprefs.yml
new file mode 100644
index 000000000..9c4c75569
--- /dev/null
+++ b/.github/workflows/generate-userprefs.yml
@@ -0,0 +1,33 @@
+on:
+  push:
+    paths:
+      - userPrefs.h
+    branches:
+      - master
+
+jobs:
+  generate-userprefs:
+    runs-on: ubuntu-latest
+    steps:
+      - name: Checkout code
+        uses: actions/checkout@v4
+
+      - name: Install Clang
+        run: sudo apt-get install -y clang
+
+      - name: Install trunk
+        run: curl https://get.trunk.io -fsSL | bash
+
+      - name: Generate userPrefs.jsom
+        run: python3 ./bin/build-userprefs-json.py
+
+      - name: Trunk format json
+        run: trunk format userPrefs.json
+
+      - name: Commit userPrefs.json
+        run: |
+          git config --global user.email "actions@github.com"
+          git config --global user.name "GitHub Actions"
+          git add userPrefs.json
+          git commit -m "Update userPrefs.json"
+          git push
diff --git a/bin/build-userprefs-json.py b/bin/build-userprefs-json.py
new file mode 100644
index 000000000..58f460bcf
--- /dev/null
+++ b/bin/build-userprefs-json.py
@@ -0,0 +1,48 @@
+import json
+import subprocess
+import re
+
+def get_macros_from_header(header_file):
+    # Run clang to preprocess the header file and capture the output
+    result = subprocess.run(['clang', '-E', '-dM', header_file], capture_output=True, text=True)
+    if result.returncode != 0:
+        raise RuntimeError(f"Clang preprocessing failed: {result.stderr}")
+
+    # Extract macros from the output
+    macros = {}
+    macro_pattern = re.compile(r'#define\s+(\w+)\s+(.*)')
+    for line in result.stdout.splitlines():
+        match = macro_pattern.match(line) 
+        if match and 'USERPREFS_' in line and '_USERPREFS_' not in line:
+            macros[match.group(1)] = match.group(2)
+
+    return macros
+
+def write_macros_to_json(macros, output_file):
+    with open(output_file, 'w') as f:
+        json.dump(macros, f, indent=4)
+
+def main():
+    header_file = 'userPrefs.h'
+    output_file = 'userPrefs.json'
+    # Uncomment all macros in the header file
+    with open(header_file, 'r') as file:
+        lines = file.readlines()
+
+    uncommented_lines = []
+    for line in lines:
+        stripped_line = line.strip().replace('/*', '').replace('*/', '')
+        if stripped_line.startswith('//') and 'USERPREFS_' in stripped_line:
+            # Replace "//"
+            stripped_line = stripped_line.replace('//', '')
+        uncommented_lines.append(stripped_line + '\n')
+
+    with open(header_file, 'w') as file:
+        for line in uncommented_lines:
+                file.write(line)
+    macros = get_macros_from_header(header_file)
+    write_macros_to_json(macros, output_file)
+    print(f"Macros have been written to {output_file}")
+
+if __name__ == "__main__":
+    main()
\ No newline at end of file
diff --git a/userPrefs.json b/userPrefs.json
new file mode 100644
index 000000000..bc62602be
--- /dev/null
+++ b/userPrefs.json
@@ -0,0 +1,16 @@
+{
+  "USERPREFS_CHANNEL_0_NAME": "\"DEFCONnect\"",
+  "USERPREFS_CHANNEL_0_PRECISION": "14",
+  "USERPREFS_CHANNEL_0_PSK": "{ 0x38, 0x4b, 0xbc, 0xc0, 0x1d, 0xc0, 0x22, 0xd1, 0x81, 0xbf, 0x36, 0xb8, 0x61, 0x21, 0xe1, 0xfb, 0x96, 0xb7, 0x2e, 0x55, 0xbf, 0x74, 0x22, 0x7e, 0x9d, 0x6a, 0xfb, 0x48, 0xd6, 0x4c, 0xb1, 0xa1 }",
+  "USERPREFS_CONFIG_LORA_IGNORE_MQTT": "true",
+  "USERPREFS_CONFIG_LORA_REGION": "meshtastic_Config_LoRaConfig_RegionCode_US",
+  "USERPREFS_CONFIG_OWNER_LONG_NAME": "\"My Long Name\"",
+  "USERPREFS_CONFIG_OWNER_SHORT_NAME": "\"MLN\"",
+  "USERPREFS_EVENT_MODE": "1",
+  "USERPREFS_HAS_SPLASH": "",
+  "USERPREFS_LORACONFIG_CHANNEL_NUM": "31",
+  "USERPREFS_LORACONFIG_MODEM_PRESET": "meshtastic_Config_LoRaConfig_ModemPreset_SHORT_FAST",
+  "USERPREFS_SPLASH_TITLE": "\"DEFCONtastic\"",
+  "USERPREFS_TZ_STRING": "\"tzplaceholder                                         \"",
+  "USERPREFS_USE_ADMIN_KEY": "1"
+}