diff --git a/sf-app/src/manifest.rs b/sf-app/src/manifest.rs index 6733d10..fe9bf36 100644 --- a/sf-app/src/manifest.rs +++ b/sf-app/src/manifest.rs @@ -23,6 +23,10 @@ pub struct AppManifest { pub id: String, pub name: String, pub kind: AppKind, + /// Package version (semver). Published as `sf-app-` when it changes; + /// the newer of a factory and an installed copy wins. + #[serde(default = "default_version")] + pub version: String, /// Short label above the title in the hero ("Saját app", "Castolás"...). #[serde(default)] pub category: String, @@ -50,6 +54,11 @@ pub struct AppManifest { pub web: WebOptions, #[serde(default)] pub dial: Option, + /// Background receiver (AirPlay, Miracast): sf-core keeps it running all + /// the time and it shows its own window when something connects. The + /// launcher tile only opens the details sheet. + #[serde(default)] + pub service: bool, /// Folder the manifest was loaded from (not serialized). #[serde(skip)] pub dir: PathBuf, @@ -74,6 +83,10 @@ pub struct DialOptions { pub name: String, } +fn default_version() -> String { + "0.0.0".into() +} + fn default_accent() -> String { "#ffffff".into() } @@ -99,10 +112,46 @@ pub fn load_apps(dir: &Path) -> Result> { Err(e) => log::warn!("skipping {}: {e:#}", path.display()), } } - apps.sort_by(|a, b| a.order.cmp(&b.order).then_with(|| a.name.cmp(&b.name))); + sort_apps(&mut apps); Ok(apps) } +/// Loads apps from several folders (factory apps, then installed packages). +/// Missing folders are skipped; of two apps with the same id the higher +/// `version` wins, on a tie the later folder. +pub fn load_apps_from(dirs: &[PathBuf]) -> Result> { + let mut apps: Vec = Vec::new(); + let mut found = false; + for dir in dirs { + if !dir.is_dir() { + continue; + } + found = true; + for app in load_apps(dir)? { + match apps.iter_mut().find(|a| a.id == app.id) { + Some(old) if version_cmp(&app.version, &old.version).is_ge() => *old = app, + Some(_) => {} + None => apps.push(app), + } + } + } + anyhow::ensure!(found, "none of the app folders exist: {dirs:?}"); + sort_apps(&mut apps); + Ok(apps) +} + +fn sort_apps(apps: &mut [AppManifest]) { + apps.sort_by(|a, b| a.order.cmp(&b.order).then_with(|| a.name.cmp(&b.name))); +} + +/// Compares dotted numeric versions ("1.10.0" > "1.9.2"); anything that +/// isn't a number compares as 0. +pub fn version_cmp(a: &str, b: &str) -> std::cmp::Ordering { + let parts = |v: &str| -> Vec { v.split(['.', '-', '+']).take(3).map(|p| p.parse().unwrap_or(0)).collect() }; + let (a, b) = (parts(a), parts(b)); + (0..3).map(|i| a.get(i).unwrap_or(&0).cmp(b.get(i).unwrap_or(&0))).find(|o| o.is_ne()).unwrap_or(std::cmp::Ordering::Equal) +} + fn load_one(path: &Path) -> Result { let text = std::fs::read_to_string(path)?; let mut app: AppManifest = toml::from_str(&text)?; @@ -133,5 +182,14 @@ mod tests { let mut s = Settings::default(); s.values.insert("netflex_server".into(), "http://10.0.0.5:7880".into()); assert_eq!(app.resolved_url(&s).unwrap(), "http://10.0.0.5:7880/"); + assert_eq!(app.version, "0.0.0"); + } + + #[test] + fn compares_versions() { + use std::cmp::Ordering::*; + assert_eq!(version_cmp("1.10.0", "1.9.2"), Greater); + assert_eq!(version_cmp("0.2", "0.2.0"), Equal); + assert_eq!(version_cmp("0.1.9", "0.2.0"), Less); } }