From e778b02c0cc11b36b2b54e1c38bc8a4555117a14 Mon Sep 17 00:00:00 2001 From: Woody Date: Tue, 19 Dec 2023 22:23:01 +0100 Subject: [PATCH 1/4] Automatically build UI before compiling --- pio-scripts/build_ui.py | 3 +++ platformio.ini | 1 + 2 files changed, 4 insertions(+) create mode 100644 pio-scripts/build_ui.py diff --git a/pio-scripts/build_ui.py b/pio-scripts/build_ui.py new file mode 100644 index 000000000..f3688a5d4 --- /dev/null +++ b/pio-scripts/build_ui.py @@ -0,0 +1,3 @@ +Import('env') + +env.Execute("npm run build") \ No newline at end of file diff --git a/platformio.ini b/platformio.ini index fa6bf3bb8..c319e4b05 100644 --- a/platformio.ini +++ b/platformio.ini @@ -155,6 +155,7 @@ extra_scripts = post:pio-scripts/output_bins.py post:pio-scripts/strip-floats.py pre:pio-scripts/user_config_copy.py + pre:pio-scripts/build_ui.py # ------------------------------------------------------------------------------ # COMMON SETTINGS: From 0a9145cd3cf95a17f596cdbc4a11548f44575250 Mon Sep 17 00:00:00 2001 From: Woody Date: Sun, 7 Jan 2024 00:55:56 +0100 Subject: [PATCH 2/4] add caching for cdata.js --- .gitignore | 2 ++ tools/cdata.js | 46 ++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index c85fae0c2..e461ebbda 100644 --- a/.gitignore +++ b/.gitignore @@ -22,3 +22,5 @@ wled-update.sh /wled00/my_config.h /wled00/Release /wled00/wled00.ino.cpp + +/tools/cdataCache.json \ No newline at end of file diff --git a/tools/cdata.js b/tools/cdata.js index fabe7a5bd..b4a0fff1b 100644 --- a/tools/cdata.js +++ b/tools/cdata.js @@ -22,6 +22,36 @@ const CleanCSS = require("clean-css"); const MinifyHTML = require("html-minifier-terser").minify; const packageJson = require("../package.json"); +const CACHE_FILE = __dirname + "/cdataCache.json"; +const cache = loadCache(); +cache.version = 1; + +function loadCache() { + try { + return JSON.parse(fs.readFileSync(CACHE_FILE)); + } catch (e) { + return {}; + } +} + +function saveCache(file) { + const stat = fs.statSync(file); + cache[file] = { + mtime: stat.mtimeMs, + size: stat.size, + }; + fs.writeFileSync(CACHE_FILE, JSON.stringify(cache)); +} + +function isCached(file) { + const stat = fs.statSync(file); + const cached = cache[file]; + if (cached && cached.mtime >= stat.mtimeMs && cached.size == stat.size) { + return true; + } + return false; +} + /** * */ @@ -110,6 +140,10 @@ function filter(str, type) { } function writeHtmlGzipped(sourceFile, resultFile, page) { + if (isCached(sourceFile)) { + console.info(`Skipping ${resultFile} as it is cached`); + return; + } console.info("Reading " + sourceFile); new inliner(sourceFile, function (error, html) { console.info("Inlined " + html.length + " characters"); @@ -146,6 +180,7 @@ ${array} `; console.info("Writing " + resultFile); fs.writeFileSync(resultFile, src); + saveCache(sourceFile); }); }); } @@ -196,6 +231,11 @@ ${result} } function writeChunks(srcDir, specs, resultFile) { + if (specs.every(s => isCached(srcDir + "/" + s.file))) { + console.info(`Skipping ${resultFile} as all files are cached`); + return; + } + let src = `/* * More web UI HTML source arrays. * This file is auto generated, please don't make any changes manually. @@ -204,12 +244,14 @@ function writeChunks(srcDir, specs, resultFile) { */ `; specs.forEach((s) => { + const file = srcDir + "/" + s.file; try { - console.info("Reading " + srcDir + "/" + s.file + " as " + s.name); + console.info("Reading " + file + " as " + s.name); src += specToChunk(srcDir, s); + saveCache(file); } catch (e) { console.warn( - "Failed " + s.name + " from " + srcDir + "/" + s.file, + "Failed " + s.name + " from " + file, e.message.length > 60 ? e.message.substring(0, 60) : e.message ); } From 21de9e938af43e6d68c2e375b4638d9fd81f0e79 Mon Sep 17 00:00:00 2001 From: Woody Date: Sun, 7 Jan 2024 22:06:20 +0100 Subject: [PATCH 3/4] Rebuild if command line argument is set --- tools/cdata.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tools/cdata.js b/tools/cdata.js index b4a0fff1b..a73e2fa3f 100644 --- a/tools/cdata.js +++ b/tools/cdata.js @@ -44,6 +44,10 @@ function saveCache(file) { } function isCached(file) { + // If command line argument is set, always rebuild + if (process.argv[2] == "--force" || process.argv[2] == "-f") { + return false; + } const stat = fs.statSync(file); const cached = cache[file]; if (cached && cached.mtime >= stat.mtimeMs && cached.size == stat.size) { From 74a5528f17bd671c4ffb0d5ed03d1f9b7dbe0f05 Mon Sep 17 00:00:00 2001 From: Woody Date: Sun, 7 Jan 2024 23:14:24 +0100 Subject: [PATCH 4/4] Small refactor of isCached function in cdata.js --- tools/cdata.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/tools/cdata.js b/tools/cdata.js index a73e2fa3f..5d9965634 100644 --- a/tools/cdata.js +++ b/tools/cdata.js @@ -50,10 +50,7 @@ function isCached(file) { } const stat = fs.statSync(file); const cached = cache[file]; - if (cached && cached.mtime >= stat.mtimeMs && cached.size == stat.size) { - return true; - } - return false; + return cached && cached.mtime == stat.mtimeMs && cached.size == stat.size; } /**