Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: scan 2nd-level of config dir for widget configs #111

Merged
merged 2 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 68 additions & 53 deletions packages/desktop/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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. `<CONFIG_DIR>/*/*.zebar.json`).
///
/// Returns a list of `ConfigEntry` instances.
/// Returns a vector of `WidgetConfigEntry` instances.
fn read_widget_configs(
dir: &PathBuf,
) -> anyhow::Result<Vec<WidgetConfigEntry>> {
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::<WidgetConfig>(&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::<Vec<PathBuf>>();

let mut configs = Vec::new();

// Parse the found config files.
for path in config_files {
let parse_res =
read_and_parse_json::<WidgetConfig>(&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
);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/desktop/tauri.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down