tool: always include version in PRETTY_NAME

... to give a consistent user experience in systemd-boot.

Fixes #220.
This commit is contained in:
Julian Stecklina 2023-10-20 11:29:28 +02:00
parent 3da3049bef
commit ec05d707f3
3 changed files with 30 additions and 9 deletions

View File

@ -67,7 +67,16 @@ impl Generation {
} }
} }
/// Describe the generation in a single line. /// A helper for describe functions below.
fn describe_specialisation(&self) -> String {
if let Some(specialization) = &self.specialisation_name {
format!("-{specialization}")
} else {
"".to_string()
}
}
/// Describe the generation in a single line for humans.
/// ///
/// Emulates how NixOS's current systemd-boot-builder.py describes generations so that the user /// Emulates how NixOS's current systemd-boot-builder.py describes generations so that the user
/// interface remains similar. /// interface remains similar.
@ -83,14 +92,15 @@ impl Generation {
format!( format!(
"Generation {}{}, {}", "Generation {}{}, {}",
self.version, self.version,
if let Some(specialization) = &self.specialisation_name { self.describe_specialisation(),
format!("-{specialization}")
} else {
"".to_string()
},
build_time build_time
) )
} }
/// A unique short identifier.
pub fn version_tag(&self) -> String {
format!("{}{}", self.version, self.describe_specialisation(),)
}
} }
impl fmt::Display for Generation { impl fmt::Display for Generation {

View File

@ -26,10 +26,21 @@ impl OsRelease {
// Because the ID field here does not have the same meaning as in a real os-release file, // Because the ID field here does not have the same meaning as in a real os-release file,
// it is fine to use a dummy value. // it is fine to use a dummy value.
map.insert("ID".into(), String::from("lanza")); map.insert("ID".into(), String::from("lanza"));
// systemd-boot will only show VERSION_ID when PRETTY_NAME is not unique. This is
// confusing to users. Make sure that our PRETTY_NAME is unique, so we get a consistent
// user experience.
//
// See #220.
map.insert( map.insert(
"PRETTY_NAME".into(), "PRETTY_NAME".into(),
generation.spec.bootspec.bootspec.label.clone(), format!(
"{} ({})",
generation.spec.bootspec.bootspec.label,
generation.describe()
),
); );
map.insert("VERSION_ID".into(), generation.describe()); map.insert("VERSION_ID".into(), generation.describe());
Ok(Self(map)) Ok(Self(map))

View File

@ -27,8 +27,8 @@ fn generate_expected_os_release() -> Result<()> {
let expected = expect![[r#" let expected = expect![[r#"
ID=lanza ID=lanza
PRETTY_NAME=LanzaOS PRETTY_NAME=LanzaOS (Generation 1, 1970-01-01)
VERSION_ID=Generation 1, Built on 1970-01-01 VERSION_ID=Generation 1, 1970-01-01
"#]]; "#]];
expected.assert_eq(&String::from_utf8(os_release_section)?); expected.assert_eq(&String::from_utf8(os_release_section)?);