Add Recipe Subfolders

This commit is contained in:
joshua Williams 2023-08-09 21:15:42 +00:00 committed by Jeremy Soller
parent 300e99bf52
commit 73c662b9da
13 changed files with 154 additions and 20 deletions

View File

@ -5,7 +5,7 @@ source config.sh
if [ $# = 0 ]
then
recipes="$(ls -1 recipes)"
recipes="$(target/release/list_recipes)"
else
recipes="$@"
fi

View File

@ -368,9 +368,10 @@ function op {
if [ -n "$1" ]
then
if [ -d "$ROOT/recipes/$1" ]
recipe_path=`target/release/find_recipe $1`
if [ -d "$ROOT/$recipe_path" ]
then
export COOKBOOK_RECIPE="${ROOT}/recipes/$1"
export COOKBOOK_RECIPE="${ROOT}/$recipe_path"
TARGET_DIR="${COOKBOOK_RECIPE}/target/${TARGET}"
mkdir -p "${TARGET_DIR}"

View File

@ -5,14 +5,15 @@ source config.sh
if [ $# = 0 ]
then
recipes="$(ls -1 recipes)"
recipes="$(target/release/list_recipes)"
else
recipes="$@"
fi
for recipe in $recipes
do
if [ -e "recipes/$recipe/recipe.toml" ]
recipe_path=`target/release/find_recipe $recipe`
if [ -e "$recipe_path/recipe.toml" ]
then
target/release/cook --fetch-only "$recipe"
continue

View File

@ -17,12 +17,14 @@ done
if [ "$recipes" == "" ]
then
recipes="$(ls -1 recipes)"
recipes="$(target/release/list_recipes)"
fi
for recipe in $recipes
do
COOKBOOK_RECIPE="recipes/$recipe"
recipe_path=`target/release/find_recipe $recipe`
echo recipe path is $recipe_path
COOKBOOK_RECIPE="$recipe_path"
TARGET_DIR="${COOKBOOK_RECIPE}/target/${TARGET}"
COOKBOOK_BUILD="${TARGET_DIR}/build"
COOKBOOK_STAGE="${TARGET_DIR}/stage"
@ -113,7 +115,8 @@ mkdir -p "$REPO"
for recipe in $recipes
do
COOKBOOK_RECIPE="recipes/$recipe"
recipe_path=`target/release/find_recipe $recipe`
COOKBOOK_RECIPE="$recipe_path"
TARGET_DIR="${COOKBOOK_RECIPE}/target/${TARGET}"
COOKBOOK_STAGE="${TARGET_DIR}/stage"

View File

@ -1,6 +1,7 @@
use cookbook::blake3::blake3_progress;
use cookbook::recipe::{Recipe, SourceRecipe, BuildKind, BuildRecipe, PackageRecipe};
use cookbook::sha256::sha256_progress;
use cookbook::recipe_find::recipe_find;
use std::{
env,
fs,
@ -399,7 +400,14 @@ fn build(recipe_dir: &Path, source_dir: &Path, target_dir: &Path, build: &BuildR
for dependency in build.dependencies.iter() {
let public_path = "build/id_ed25519.pub.toml";
//TODO: sanitize name
let archive_path = format!("recipes/{}/target/{}/stage.pkgar", dependency, redoxer::target());
let dependency_dir = recipe_find(dependency, Path::new("recipes"))?;
if dependency_dir.is_none() {
return Err(format!(
"failed to find recipe directory '{}'",
dependency
));
}
let archive_path = format!("{}/target/{}/stage.pkgar", dependency_dir.unwrap().display(), redoxer::target());
pkgar::extract(
public_path,
&archive_path,
@ -668,14 +676,14 @@ pub struct CookRecipe {
impl CookRecipe {
pub fn new(name: String) -> Result<Self, String> {
//TODO: sanitize recipe name?
let dir = Path::new("recipes").join(&name);
if ! dir.is_dir() {
let dir = recipe_find(&name, Path::new("recipes"))?;
if dir.is_none() {
return Err(format!(
"failed to find recipe directory '{}'",
dir.display()
name
));
}
let dir = dir.unwrap();
let file = dir.join("recipe.toml");
if ! file.is_file() {
return Err(format!(

26
src/bin/find_recipe.rs Normal file
View File

@ -0,0 +1,26 @@
use cookbook::recipe_find::recipe_find;
use std::env::args;
use std::path::Path;
use std::process::exit;
// use clap::Parser;
fn usage() {
println!("Usage: find_recipe recipe_name");
}
fn main() {
if args().len() != 2 {
usage();
exit(2);
}
let result = recipe_find(&args().last().unwrap(), Path::new("recipes"));
if result.is_err() {
eprintln!("{}", result.err().unwrap());
exit(2);
} else if result.as_ref().unwrap().is_none() {
eprintln!("recipe {} not found", &args().last().unwrap());
exit(1);
} else {
println!("{}", result.unwrap().unwrap().display());
exit(0);
}
}

19
src/bin/list_recipes.rs Normal file
View File

@ -0,0 +1,19 @@
use cookbook::recipe_find::list_recipes;
use std::path::Path;
use std::process::exit;
// use clap::Parser;
fn main() {
let result = list_recipes( Path::new("recipes"));
if result.is_err() {
eprintln!("{}", result.err().unwrap());
exit(2);
} else if result.as_ref().unwrap().is_empty() {
eprintln!("recipes not found");
exit(1);
} else {
result.unwrap().iter().for_each(|recipe| println!("{}", recipe));
exit(0);
}
}

View File

@ -1,5 +1,6 @@
pub mod blake3;
pub mod recipe;
pub mod sha256;
pub mod recipe_find;
mod progress_bar;

72
src/recipe_find.rs Normal file
View File

@ -0,0 +1,72 @@
use std::fs::{self};
use std::path::{Path, PathBuf};
pub fn recipe_find(recipe: &str, dir: &Path) -> Result<Option<PathBuf>, String> {
let mut recipe_path = None;
if !dir.is_dir() {
return Ok(None);
}
for entry in fs::read_dir(dir).map_err(|e| e.to_string())? {
let entry = entry.map_err(|e| e.to_string())?;
if entry.file_name().to_string_lossy() == "recipe.sh"
|| entry.file_name().to_string_lossy() == "recipe.toml"
{
// println!("recipe is {:?}", dir.file_name());
if dir.file_name().unwrap().to_string_lossy() != recipe {
return Ok(None);
} else {
return Ok(Some(dir.to_path_buf()));
}
}
}
for entry in fs::read_dir(dir).map_err(|e| e.to_string())? {
let entry = entry.map_err(|e| e.to_string())?;
if !entry.file_type().map_err(|e| e.to_string())?.is_dir() {
continue;
}
let found = recipe_find(recipe, entry.path().as_path())?;
if found.is_none() {
continue;
}
if recipe_path.is_none() {
recipe_path = found;
} else {
return Err(format!(
"recipe {} has two or more entries {}, {}",
recipe,
recipe_path.unwrap().display(),
found.unwrap().display()
));
}
}
Ok(recipe_path)
}
pub fn list_recipes(dir: &Path) -> Result<Vec<String>, String> {
let mut recipes = Vec::<String>::new();
if !dir.is_dir() {
return Ok(recipes);
}
for entry in fs::read_dir(dir).map_err(|e| e.to_string())? {
let entry = entry.map_err(|e| e.to_string())?;
if entry.file_name().to_string_lossy() == "recipe.sh"
|| entry.file_name().to_string_lossy() == "recipe.toml"
{
recipes.push(dir.file_name().ok_or(format!("could not unwrap the filename for {:?}", dir))?.to_string_lossy().to_string());
return Ok(recipes);
}
}
for entry in fs::read_dir(dir).map_err(|e| e.to_string())? {
let entry = entry.map_err(|e| e.to_string())?;
if !entry.file_type().map_err(|e| e.to_string())?.is_dir() {
continue;
}
let mut found = list_recipes(entry.path().as_path())?;
recipes.append(&mut found);
}
recipes.sort();
Ok(recipes)
}

View File

@ -5,14 +5,15 @@ source config.sh
if [ $# = 0 ]
then
recipes="$(ls -1 recipes)"
recipes="$(target/release/list_recipes)"
else
recipes="$@"
fi
for recipe in $recipes
do
if [ -d "recipes/$recipe/source" ]
recipe_path=`target/release/find_recipe $recipe`
if [ -d "$recipe_path/source" ]
then
status="$(COOK_QUIET=1 ./cook.sh "$recipe" status)"

View File

@ -5,14 +5,15 @@ source config.sh
if [ $# = 0 ]
then
recipes="$(ls -1 recipes)"
recipes="$(target/release/list_recipes)"
else
recipes="$@"
fi
for recipe in $recipes
do
if [ -d "recipes/$recipe/source" ]
recipe_path=`target/release/find_recipe $recipe`
if [ -d "$recipe_path/source" ]
then
status="$(COOK_QUIET=1 ./cook.sh "$recipe" status_origin)"

View File

@ -5,14 +5,15 @@ source config.sh
if [ $# = 0 ]
then
recipes="$(ls -1 recipes)"
recipes="$(target/release/list_recipes)"
else
recipes="$@"
fi
for recipe in $recipes
do
if [ -d "recipes/$recipe/source" ]
recipe_path=`target/release/find_recipe $recipe`
if [ -d "$recipe_path/source" ]
then
status="$(COOK_QUIET=1 ./cook.sh "$recipe" status_upstream)"

View File

@ -5,7 +5,7 @@ source config.sh
if [ $# = 0 ]
then
recipes="$(ls -1 recipes)"
recipes="$(target/release/list_recipes)"
else
recipes="$@"
fi