Merge branch 'improve-recipes-packages-api' into 'master'

Improve package and recipes API

See merge request redox-os/cookbook!534
This commit is contained in:
Jeremy Soller 2025-07-08 06:06:33 -06:00
commit 9403e65845
4 changed files with 62 additions and 58 deletions

4
Cargo.lock generated
View File

@ -1832,7 +1832,7 @@ dependencies = [
[[package]] [[package]]
name = "redox-pkg" name = "redox-pkg"
version = "0.2.5" version = "0.2.7"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"ignore", "ignore",
@ -1867,7 +1867,7 @@ dependencies = [
"pkgar 0.1.17", "pkgar 0.1.17",
"pkgar-core 0.1.17", "pkgar-core 0.1.17",
"pkgar-keys 0.1.17", "pkgar-keys 0.1.17",
"redox-pkg 0.2.5", "redox-pkg 0.2.7",
"redoxer", "redoxer",
"serde", "serde",
"tempfile", "tempfile",

View File

@ -219,7 +219,20 @@ fn fetch_offline(recipe_dir: &Path, source: &Option<SourceRecipe>) -> Result<Pat
Some(SourceRecipe::SameAs { same_as: _ }) | Some(SourceRecipe::Path { path: _ }) | None => { Some(SourceRecipe::SameAs { same_as: _ }) | Some(SourceRecipe::Path { path: _ }) | None => {
return fetch(recipe_dir, source); return fetch(recipe_dir, source);
} }
Some(SourceRecipe::Git { git: _, upstream: _, branch: _, rev: _, patches: _, script: _ }) | Some(SourceRecipe::Tar { tar: _, blake3: _, patches: _, script: _ }) => { Some(SourceRecipe::Git {
git: _,
upstream: _,
branch: _,
rev: _,
patches: _,
script: _,
})
| Some(SourceRecipe::Tar {
tar: _,
blake3: _,
patches: _,
script: _,
}) => {
if !source_dir.is_dir() { if !source_dir.is_dir() {
return Err(format!( return Err(format!(
"'{dir}' is not exist and unable to continue in offline mode", "'{dir}' is not exist and unable to continue in offline mode",
@ -596,12 +609,7 @@ fn auto_deps(
if let Ok(relative_path) = path.strip_prefix(stage_dir) { if let Ok(relative_path) = path.strip_prefix(stage_dir) {
eprintln!("DEBUG: {} needs {}", relative_path.display(), name); eprintln!("DEBUG: {} needs {}", relative_path.display(), name);
} }
if let Ok(name) = PackageName::new(name) { needed.insert(name.to_string());
needed.insert(name);
} else {
// AFAICT this shouldn't ever happen.
eprintln!("ERROR: `{name}` is an invalid package name; please report");
}
} }
} }
} }
@ -1131,7 +1139,8 @@ fn cook(
let source_dir = match is_offline { let source_dir = match is_offline {
true => fetch_offline(recipe_dir, &recipe.source), true => fetch_offline(recipe_dir, &recipe.source),
false => fetch(recipe_dir, &recipe.source), false => fetch(recipe_dir, &recipe.source),
}.map_err(|err| format!("failed to fetch: {}", err))?; }
.map_err(|err| format!("failed to fetch: {}", err))?;
if fetch_only { if fetch_only {
return Ok(()); return Ok(());

View File

@ -1,18 +1,22 @@
use std::{env::args, process::ExitCode}; use std::env::args;
use pkg::package::Package; use pkg::{
package::{Package, PackageError},
PackageName,
};
use cookbook::WALK_DEPTH; use cookbook::WALK_DEPTH;
fn main() -> ExitCode { fn main() -> Result<(), PackageError> {
let names = args().skip(1).collect::<Vec<String>>(); let names: Vec<_> = args()
// TODO: Ugly vec .skip(1)
let names: Vec<&str> = names.iter().map(|s| s.as_str()).collect(); .map(PackageName::new)
let packages = Package::new_recursive(&names, WALK_DEPTH).expect("package not found"); .collect::<Result<_, _>>()?;
let packages = Package::new_recursive(&names, WALK_DEPTH)?;
for package in packages { for package in packages {
println!("{}", package.name); println!("{}", package.name);
} }
ExitCode::SUCCESS Ok(())
} }

View File

@ -1,7 +1,10 @@
use std::{convert::TryInto, fs, path::PathBuf}; use std::{convert::TryInto, fs, path::PathBuf};
use pkg::{recipes, PackageName}; use pkg::{package::PackageError, recipes, PackageName};
use serde::{Deserialize, Serialize}; use serde::{
de::{value::Error as DeError, Error as DeErrorT},
Deserialize, Serialize,
};
/// Specifies how to download the source for a recipe /// Specifies how to download the source for a recipe
#[derive(Debug, Deserialize, PartialEq, Serialize)] #[derive(Debug, Deserialize, PartialEq, Serialize)]
@ -108,48 +111,33 @@ pub struct CookRecipe {
} }
impl CookRecipe { impl CookRecipe {
pub fn new(name: &str) -> Result<Self, String> { pub fn new(
let name: PackageName = name name: impl TryInto<PackageName, Error = PackageError>,
.try_into() ) -> Result<Self, PackageError> {
.map_err(|e| format!("Invalid package name: {e}"))?; let name: PackageName = name.try_into()?;
let dir = recipes::find(name.as_str()); let dir = recipes::find(name.as_str())
if dir.is_none() { .ok_or_else(|| PackageError::PackageNotFound(name.clone()))?;
return Err(format!("failed to find recipe directory '{}'", name));
}
let dir = dir.unwrap();
let file = dir.join("recipe.toml"); let file = dir.join("recipe.toml");
if !file.is_file() { if !file.is_file() {
return Err(format!("failed to find recipe file '{}'", file.display())); return Err(PackageError::FileMissing(file));
} }
let toml = fs::read_to_string(&file).map_err(|err| { let toml = fs::read_to_string(&file)
format!( .map_err(|err| PackageError::Parse(DeError::custom(err), Some(file.clone())))?;
"failed to read recipe file '{}': {}\n{:#?}",
file.display(),
err,
err
)
})?;
let recipe: Recipe = toml::from_str(&toml).map_err(|err| { let recipe: Recipe = toml::from_str(&toml)
format!( .map_err(|err| PackageError::Parse(DeError::custom(err), Some(file)))?;
"failed to parse recipe file '{}': {}\n{:#?}",
file.display(),
err,
err
)
})?;
let dir = dir.to_path_buf(); let dir = dir.to_path_buf();
Ok(Self { name, dir, recipe }) Ok(Self { name, dir, recipe })
} }
pub fn new_recursive(names: &[PackageName], recursion: usize) -> Result<Vec<Self>, String> { pub fn new_recursive(
names: &[PackageName],
recursion: usize,
) -> Result<Vec<Self>, PackageError> {
if recursion == 0 { if recursion == 0 {
return Err(format!( return Err(PackageError::Recursion(Default::default()));
"recursion limit while processing build dependencies: {:#?}",
names
));
} }
let mut recipes = Vec::new(); let mut recipes = Vec::new();
@ -158,7 +146,10 @@ impl CookRecipe {
let dependencies = let dependencies =
Self::new_recursive(&recipe.recipe.build.dependencies, recursion - 1).map_err( Self::new_recursive(&recipe.recipe.build.dependencies, recursion - 1).map_err(
|err| format!("{}: failed on loading build dependencies:\n{}", name, err), |mut err| {
err.append_recursion(name);
err
},
)?; )?;
for dependency in dependencies { for dependency in dependencies {
@ -178,12 +169,9 @@ impl CookRecipe {
pub fn get_package_deps_recursive( pub fn get_package_deps_recursive(
names: &[PackageName], names: &[PackageName],
recursion: usize, recursion: usize,
) -> Result<Vec<PackageName>, String> { ) -> Result<Vec<PackageName>, PackageError> {
if recursion == 0 { if recursion == 0 {
return Err(format!( return Err(PackageError::Recursion(Default::default()));
"recursion limit while processing package dependencies: {:#?}",
names
));
} }
let mut recipes: Vec<PackageName> = Vec::new(); let mut recipes: Vec<PackageName> = Vec::new();
@ -194,7 +182,10 @@ impl CookRecipe {
&recipe.recipe.package.dependencies, &recipe.recipe.package.dependencies,
recursion - 1, recursion - 1,
) )
.map_err(|err| format!("{}: failed on loading package dependencies:\n{}", name, err))?; .map_err(|mut err| {
err.append_recursion(name);
err
})?;
for dependency in dependencies { for dependency in dependencies {
if !recipes.contains(&dependency) { if !recipes.contains(&dependency) {