mirror of
https://github.com/edera-dev/sprout.git
synced 2026-11-04 19:02:20 +00:00
feat(sprout): generator variants (#105)
Generators can now declare variant axes. Every generated entry is multiplied by each combination of choices, one per axis. A choice sets the axis name to the choice name and inserts its own values, and the entry name is suffixed with the choice names. Generators can also declare exclude rules, which remove generated entries whose values match every glob pattern in a rule. This covers unwanted variant combinations and entries like BLS rescue kernels. Entry matching for default-entry and the bootloader interface now accepts `*` anywhere in the pattern instead of only at the end, so generated entry names can be matched regardless of the kernel version in the middle of the name. The BLS generator gains a pin-names option (default true, the existing behavior). Two BLS generators reading the same directory previously produced entries with identical names; disabling pin-names prefixes them with the generator name instead. Sprout now warns when more than one entry has the same name. * docs(config): note that variants suffix pinned BLS entry names Pinned BLS entry names match the BLS entry ids, but variants still append their choice names, so those entries need a pattern like <id>-* to be selected.
This commit is contained in:
9 files changed
+419
-26
No files matched your search
@@ -81,6 +81,7 @@ pub fn scan(
|
||||
..Default::default()
|
||||
},
|
||||
path: format!("{}\\loader", root),
|
||||
pin_names: true,
|
||||
};
|
||||
|
||||
// Generate a unique name for the BLS generator and insert the generator into the configuration.
|
||||
|
||||
+22
-10
@@ -2,6 +2,7 @@ use crate::context::SproutContext;
|
||||
use alloc::rc::Rc;
|
||||
use alloc::string::{String, ToString};
|
||||
use edera_sprout_config::entries::EntryDeclaration;
|
||||
use edera_sprout_parsing::glob_match;
|
||||
|
||||
/// Represents an entry that is stamped and ready to be booted.
|
||||
#[derive(Clone)]
|
||||
@@ -105,18 +106,29 @@ impl BootableEntry {
|
||||
}
|
||||
|
||||
/// Determine if this entry matches `needle` by comparing to the name or title of the entry.
|
||||
/// If `needle` ends with *, we will match a partial match.
|
||||
/// The `needle` is a glob pattern, where `*` matches any sequence of characters.
|
||||
/// A `needle` without any `*` must equal the name or title exactly.
|
||||
pub fn is_match(&self, needle: &str) -> bool {
|
||||
// If the needle ends with '*', we will accept a partial match.
|
||||
if needle.ends_with("*") {
|
||||
// Strip off any '*' at the end.
|
||||
let partial = needle.trim_end_matches("*");
|
||||
// Check if the name or title start with the partial match.
|
||||
return self.name.starts_with(partial) || self.title.starts_with(partial);
|
||||
}
|
||||
glob_match(needle, &self.name) || glob_match(needle, &self.title)
|
||||
}
|
||||
|
||||
// Standard quality matching rules.
|
||||
self.name == needle || self.title == needle
|
||||
/// Create a variant of this entry, with the name suffixed by `suffix` and using `context`.
|
||||
/// The actions of the entry are stamped with `context`, the same as the list generator.
|
||||
/// Variants of the same entry keep the sort key of this entry, so they stay grouped
|
||||
/// together in the order they were created in.
|
||||
pub fn variant(&self, suffix: &str, context: Rc<SproutContext>) -> Self {
|
||||
let mut entry = self.clone();
|
||||
entry.name.push_str(suffix);
|
||||
entry.declaration.actions = context
|
||||
.stamp_iter(entry.declaration.actions.iter())
|
||||
.collect();
|
||||
// Without any sort key, the name is used to sort, which differs between variants.
|
||||
// Pin the sort key to the name of this entry to keep the variants together.
|
||||
if entry.sort_key.is_none() && entry.declaration.sort_key.is_none() {
|
||||
entry.sort_key = Some(self.name.clone());
|
||||
}
|
||||
entry.context = context;
|
||||
entry
|
||||
}
|
||||
|
||||
/// Set the sort key of the entry. This is used to sort entries via version comparison.
|
||||
|
||||
@@ -1,10 +1,14 @@
|
||||
use crate::context::SproutContext;
|
||||
use crate::entries::BootableEntry;
|
||||
use alloc::collections::BTreeSet;
|
||||
use alloc::format;
|
||||
use alloc::rc::Rc;
|
||||
use alloc::string::String;
|
||||
use alloc::vec::Vec;
|
||||
use anyhow::Result;
|
||||
use anyhow::bail;
|
||||
use edera_sprout_config::generators::GeneratorDeclaration;
|
||||
use edera_sprout_parsing::{build_variants, rule_matches};
|
||||
|
||||
/// The BLS generator.
|
||||
pub mod bls;
|
||||
@@ -15,10 +19,8 @@ pub mod list;
|
||||
/// The matrix generator.
|
||||
pub mod matrix;
|
||||
|
||||
/// Runs the generator specified by the `generator` option.
|
||||
/// It uses the specified `context` as the parent context for
|
||||
/// the generated entries, injecting more values if needed.
|
||||
pub fn generate(
|
||||
/// Runs the generator kind specified by the `generator` option.
|
||||
fn generate_base(
|
||||
context: Rc<SproutContext>,
|
||||
generator: &GeneratorDeclaration,
|
||||
) -> Result<Vec<BootableEntry>> {
|
||||
@@ -32,3 +34,92 @@ pub fn generate(
|
||||
bail!("unknown generator configuration");
|
||||
}
|
||||
}
|
||||
|
||||
/// Multiplies `entries` by every combination of the variants in `generator`.
|
||||
fn apply_variants(
|
||||
entries: Vec<BootableEntry>,
|
||||
generator: &GeneratorDeclaration,
|
||||
) -> Result<Vec<BootableEntry>> {
|
||||
// Without any variants, the entries are used as-is.
|
||||
if generator.variants.is_empty() {
|
||||
return Ok(entries);
|
||||
}
|
||||
|
||||
// Validate the axes, as an axis without choices would silently produce no entries,
|
||||
// and duplicate choice names would produce entries with the same name.
|
||||
let mut axes = Vec::new();
|
||||
for (axis, choices) in &generator.variants {
|
||||
if choices.is_empty() {
|
||||
bail!("variant axis '{}' has no choices", axis);
|
||||
}
|
||||
let mut seen = BTreeSet::new();
|
||||
for choice in choices {
|
||||
if !seen.insert(choice.name.as_str()) {
|
||||
bail!(
|
||||
"variant axis '{}' has more than one choice named '{}'",
|
||||
axis,
|
||||
choice.name
|
||||
);
|
||||
}
|
||||
}
|
||||
axes.push((
|
||||
axis.clone(),
|
||||
choices
|
||||
.iter()
|
||||
.map(|choice| (choice.name.clone(), choice.values.clone()))
|
||||
.collect(),
|
||||
));
|
||||
}
|
||||
|
||||
let combinations = build_variants(&axes);
|
||||
let mut result = Vec::with_capacity(entries.len() * combinations.len());
|
||||
for entry in &entries {
|
||||
for combination in &combinations {
|
||||
let mut context = entry.context().fork();
|
||||
context.insert(&combination.values);
|
||||
let suffix: String = combination
|
||||
.names
|
||||
.iter()
|
||||
.map(|name| format!("-{}", name))
|
||||
.collect();
|
||||
result.push(entry.variant(&suffix, context.freeze()));
|
||||
}
|
||||
}
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
/// Runs the generator specified by the `generator` option, named `name`.
|
||||
/// It uses the specified `context` as the parent context for
|
||||
/// the generated entries, injecting more values if needed.
|
||||
/// Entry names are prefixed with the generator name unless pinned,
|
||||
/// then variants are applied, and then excluded entries are removed.
|
||||
pub fn generate(
|
||||
name: &str,
|
||||
context: Rc<SproutContext>,
|
||||
generator: &GeneratorDeclaration,
|
||||
) -> Result<Vec<BootableEntry>> {
|
||||
let mut entries = generate_base(context, generator)?;
|
||||
|
||||
// We will prefix all entries with [name]-, provided the name is not pinned.
|
||||
let prefix = format!("{}-", name);
|
||||
for entry in &mut entries {
|
||||
if !entry.is_pin_name() {
|
||||
entry.prepend_name_prefix(&prefix);
|
||||
}
|
||||
}
|
||||
|
||||
let mut entries = apply_variants(entries, generator)?;
|
||||
|
||||
// Remove any entries that match an exclusion rule.
|
||||
if !generator.exclude.is_empty() {
|
||||
entries.retain(|entry| {
|
||||
let values = entry.context().all_values();
|
||||
!generator
|
||||
.exclude
|
||||
.iter()
|
||||
.any(|rule| rule_matches(rule, &values))
|
||||
});
|
||||
}
|
||||
|
||||
Ok(entries)
|
||||
}
|
||||
@@ -206,7 +206,9 @@ pub fn generate(context: Rc<SproutContext>, bls: &BlsConfiguration) -> Result<Ve
|
||||
// Pin the entry name to prevent prefixing.
|
||||
// This is needed as the bootloader interface requires the name to be
|
||||
// the same as the entry file name, minus the .conf extension.
|
||||
boot.mark_pin_name();
|
||||
if bls.pin_names {
|
||||
boot.mark_pin_name();
|
||||
}
|
||||
|
||||
// Add the BLS entry to the list, along with the bootable entry.
|
||||
entries.push((entry, boot));
|
||||
|
||||
+18
-10
@@ -9,7 +9,12 @@ use crate::{
|
||||
options::SproutOptions,
|
||||
phases::phase,
|
||||
};
|
||||
use alloc::{collections::BTreeMap, format, string::ToString, vec::Vec};
|
||||
use alloc::{
|
||||
collections::{BTreeMap, BTreeSet},
|
||||
format,
|
||||
string::ToString,
|
||||
vec::Vec,
|
||||
};
|
||||
use anyhow::{Context, Result, bail};
|
||||
use core::{ops::Deref, time::Duration};
|
||||
use edera_sprout_bls::compare_versions;
|
||||
@@ -245,18 +250,21 @@ fn run() -> Result<()> {
|
||||
for (name, generator) in config.generators {
|
||||
let context = context.fork().freeze();
|
||||
|
||||
// We will prefix all entries with [name]-, provided the name is not pinned.
|
||||
let prefix = format!("{}-", name);
|
||||
|
||||
// Add all the entries generated by the generator to the entry list.
|
||||
// The generator specifies the context associated with the entry.
|
||||
for mut entry in generators::generate(context.clone(), &generator)? {
|
||||
// If the entry name is not pinned, prepend the name prefix.
|
||||
if !entry.is_pin_name() {
|
||||
entry.prepend_name_prefix(&prefix);
|
||||
}
|
||||
entries.extend(generators::generate(&name, context, &generator)?);
|
||||
}
|
||||
|
||||
entries.push(entry);
|
||||
// Entry names are used to select entries, so duplicates make the selection ambiguous.
|
||||
// This can happen when two generators pin names, like two BLS generators reading the
|
||||
// same BLS directory.
|
||||
let mut seen_names = BTreeSet::new();
|
||||
for entry in &entries {
|
||||
if !seen_names.insert(entry.name()) {
|
||||
warn!(
|
||||
"more than one entry is named {}, selecting it by name is ambiguous",
|
||||
entry.name()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user