From 00ad483228f6c4d98c333924e3d3c3ef38106a0f Mon Sep 17 00:00:00 2001 From: Lars Berger Date: Fri, 27 Sep 2024 22:30:30 +0800 Subject: [PATCH 1/2] feat: rename boilerplate example dirs --- .../example.zebar.json} | 0 .../react-buildless => boilerplate-react-buildless}/index.html | 0 .../react-buildless => boilerplate-react-buildless}/styles.css | 0 .../{boilerplates/solid-ts => boilerplate-solid-ts}/.gitignore | 0 .../{boilerplates/solid-ts => boilerplate-solid-ts}/README.md | 0 .../solid-ts => boilerplate-solid-ts}/dist/index.html | 0 .../example.zebar.json} | 0 .../{boilerplates/solid-ts => boilerplate-solid-ts}/index.html | 0 .../{boilerplates/solid-ts => boilerplate-solid-ts}/package.json | 0 .../{boilerplates/solid-ts => boilerplate-solid-ts}/src/index.css | 0 .../{boilerplates/solid-ts => boilerplate-solid-ts}/src/index.tsx | 0 .../{boilerplates/solid-ts => boilerplate-solid-ts}/tsconfig.json | 0 .../solid-ts => boilerplate-solid-ts}/vite.config.ts | 0 13 files changed, 0 insertions(+), 0 deletions(-) rename examples/{boilerplates/react-buildless/my-window.zebar.json => boilerplate-react-buildless/example.zebar.json} (100%) rename examples/{boilerplates/react-buildless => boilerplate-react-buildless}/index.html (100%) rename examples/{boilerplates/react-buildless => boilerplate-react-buildless}/styles.css (100%) rename examples/{boilerplates/solid-ts => boilerplate-solid-ts}/.gitignore (100%) rename examples/{boilerplates/solid-ts => boilerplate-solid-ts}/README.md (100%) rename examples/{boilerplates/solid-ts => boilerplate-solid-ts}/dist/index.html (100%) rename examples/{boilerplates/solid-ts/my-window.zebar.json => boilerplate-solid-ts/example.zebar.json} (100%) rename examples/{boilerplates/solid-ts => boilerplate-solid-ts}/index.html (100%) rename examples/{boilerplates/solid-ts => boilerplate-solid-ts}/package.json (100%) rename examples/{boilerplates/solid-ts => boilerplate-solid-ts}/src/index.css (100%) rename examples/{boilerplates/solid-ts => boilerplate-solid-ts}/src/index.tsx (100%) rename examples/{boilerplates/solid-ts => boilerplate-solid-ts}/tsconfig.json (100%) rename examples/{boilerplates/solid-ts => boilerplate-solid-ts}/vite.config.ts (100%) diff --git a/examples/boilerplates/react-buildless/my-window.zebar.json b/examples/boilerplate-react-buildless/example.zebar.json similarity index 100% rename from examples/boilerplates/react-buildless/my-window.zebar.json rename to examples/boilerplate-react-buildless/example.zebar.json diff --git a/examples/boilerplates/react-buildless/index.html b/examples/boilerplate-react-buildless/index.html similarity index 100% rename from examples/boilerplates/react-buildless/index.html rename to examples/boilerplate-react-buildless/index.html diff --git a/examples/boilerplates/react-buildless/styles.css b/examples/boilerplate-react-buildless/styles.css similarity index 100% rename from examples/boilerplates/react-buildless/styles.css rename to examples/boilerplate-react-buildless/styles.css diff --git a/examples/boilerplates/solid-ts/.gitignore b/examples/boilerplate-solid-ts/.gitignore similarity index 100% rename from examples/boilerplates/solid-ts/.gitignore rename to examples/boilerplate-solid-ts/.gitignore diff --git a/examples/boilerplates/solid-ts/README.md b/examples/boilerplate-solid-ts/README.md similarity index 100% rename from examples/boilerplates/solid-ts/README.md rename to examples/boilerplate-solid-ts/README.md diff --git a/examples/boilerplates/solid-ts/dist/index.html b/examples/boilerplate-solid-ts/dist/index.html similarity index 100% rename from examples/boilerplates/solid-ts/dist/index.html rename to examples/boilerplate-solid-ts/dist/index.html diff --git a/examples/boilerplates/solid-ts/my-window.zebar.json b/examples/boilerplate-solid-ts/example.zebar.json similarity index 100% rename from examples/boilerplates/solid-ts/my-window.zebar.json rename to examples/boilerplate-solid-ts/example.zebar.json diff --git a/examples/boilerplates/solid-ts/index.html b/examples/boilerplate-solid-ts/index.html similarity index 100% rename from examples/boilerplates/solid-ts/index.html rename to examples/boilerplate-solid-ts/index.html diff --git a/examples/boilerplates/solid-ts/package.json b/examples/boilerplate-solid-ts/package.json similarity index 100% rename from examples/boilerplates/solid-ts/package.json rename to examples/boilerplate-solid-ts/package.json diff --git a/examples/boilerplates/solid-ts/src/index.css b/examples/boilerplate-solid-ts/src/index.css similarity index 100% rename from examples/boilerplates/solid-ts/src/index.css rename to examples/boilerplate-solid-ts/src/index.css diff --git a/examples/boilerplates/solid-ts/src/index.tsx b/examples/boilerplate-solid-ts/src/index.tsx similarity index 100% rename from examples/boilerplates/solid-ts/src/index.tsx rename to examples/boilerplate-solid-ts/src/index.tsx diff --git a/examples/boilerplates/solid-ts/tsconfig.json b/examples/boilerplate-solid-ts/tsconfig.json similarity index 100% rename from examples/boilerplates/solid-ts/tsconfig.json rename to examples/boilerplate-solid-ts/tsconfig.json diff --git a/examples/boilerplates/solid-ts/vite.config.ts b/examples/boilerplate-solid-ts/vite.config.ts similarity index 100% rename from examples/boilerplates/solid-ts/vite.config.ts rename to examples/boilerplate-solid-ts/vite.config.ts From 1d8d134fa0e2b957d8d3738b9bea73a50b2c53f0 Mon Sep 17 00:00:00 2001 From: Lars Berger Date: Sat, 28 Sep 2024 00:30:23 +0800 Subject: [PATCH 2/2] feat: scan 2nd-level of config directory --- packages/desktop/src/config.rs | 121 +++++++++++++++++-------------- packages/desktop/tauri.conf.json | 2 +- 2 files changed, 69 insertions(+), 54 deletions(-) diff --git a/packages/desktop/src/config.rs b/packages/desktop/src/config.rs index b3f05e29..39c0eff0 100644 --- a/packages/desktop/src/config.rs +++ b/packages/desktop/src/config.rs @@ -256,67 +256,82 @@ impl Config { Ok(()) } - /// Recursively aggregates all valid widget configs in the given - /// directory. + /// Aggregates all valid widget configs at the 2nd-level of the given + /// directory (i.e. `/*/*.zebar.json`). /// - /// Returns a list of `ConfigEntry` instances. + /// Returns a vector of `WidgetConfigEntry` instances. fn read_widget_configs( dir: &PathBuf, ) -> anyhow::Result> { - let mut configs = Vec::new(); - - let entries = fs::read_dir(dir).with_context(|| { + let dir_entries = fs::read_dir(dir).with_context(|| { format!("Failed to read directory: {}", dir.display()) })?; - for entry in entries { - let entry = entry?; - let path = entry.path(); - - if path.is_dir() { - // Recursively aggregate configs in subdirectories. - configs.extend(Self::read_widget_configs(&path)?); - } else if has_extension(&path, ".zebar.json") { - let parse_res = read_and_parse_json::(&path) - .and_then(|config| { - let config_path = path.to_absolute()?; - - let html_path = path - .parent() - .and_then(|parent| { - parent.join(&config.html_path).to_absolute().ok() - }) - .with_context(|| { - format!( - "HTML file not found at {} for config {}.", - config.html_path.display(), - config_path.display() - ) - })?; - - Ok(WidgetConfigEntry { - config, - config_path, - html_path, + // Scan the 2nd-level of the directory for config files. + let config_files = dir_entries + .into_iter() + .filter_map(|entry| { + let path = entry.ok()?.path(); + if path.is_dir() { + Some(fs::read_dir(path).ok()?) + } else { + None + } + }) + .flatten() + .filter_map(|entry| { + let path = entry.ok()?.path(); + if path.is_file() && has_extension(&path, ".zebar.json") { + Some(path) + } else { + None + } + }) + .collect::>(); + + let mut configs = Vec::new(); + + // Parse the found config files. + for path in config_files { + let parse_res = + read_and_parse_json::(&path).and_then(|config| { + let config_path = path.to_absolute()?; + + let html_path = path + .parent() + .and_then(|parent| { + parent.join(&config.html_path).to_absolute().ok() }) - }); - - match parse_res { - Ok(config) => { - info!( - "Found valid widget config at: {}", - config.config_path.display() - ); - - configs.push(config); - } - Err(err) => { - error!( - "Failed to parse config at {}: {:?}", - path.display(), - err - ); - } + .with_context(|| { + format!( + "HTML file not found at {} for config {}.", + config.html_path.display(), + config_path.display() + ) + })?; + + Ok(WidgetConfigEntry { + config, + config_path, + html_path, + }) + }); + + match parse_res { + Ok(config) => { + info!( + "Found valid widget config at: {}", + config.config_path.display() + ); + + configs.push(config); + } + Err(err) => { + error!( + "Failed to parse config at {}: {:?}", + path.display(), + err + ); } } } diff --git a/packages/desktop/tauri.conf.json b/packages/desktop/tauri.conf.json index b67d233e..a800b0d0 100644 --- a/packages/desktop/tauri.conf.json +++ b/packages/desktop/tauri.conf.json @@ -19,7 +19,7 @@ "shortDescription": "Zebar", "category": "Utility", "publisher": "Glzr Software Pte. Ltd.", - "resources": ["resources/*", "../../examples/*"], + "resources": ["resources/*", "../../examples/**/*"], "targets": ["deb", "appimage", "msi", "dmg"], "windows": { "signCommand": "powershell -ExecutionPolicy Bypass -File ./resources/scripts/sign.ps1 -FilePath %1",