Allow local sources

This makes the source section of recipe.toml optional, so that one can
simply create a recipe with a build recipe and a source folder and not
have to worry about git at all just to get a program running in redox.

I also ran into a situation where a source directory without a git
repository in it caused cookbook to reset it's own origin URL to the URL
specified in the recipe for that source directory. I added a check to
prevent that happening to anybody else.
This commit is contained in:
Wesley Hershberger 2021-04-25 14:16:49 -04:00
parent 8b9d3788c4
commit 0c0aa91cab
2 changed files with 27 additions and 9 deletions

View File

@ -159,10 +159,10 @@ fn run_command_stdin(mut command: process::Command, stdin_data: &[u8]) -> Result
Ok(()) Ok(())
} }
fn fetch(recipe_dir: &Path, source: &SourceRecipe) -> Result<PathBuf, String> { fn fetch(recipe_dir: &Path, source: &Option<SourceRecipe>) -> Result<PathBuf, String> {
let source_dir = recipe_dir.join("source"); let source_dir = recipe_dir.join("source");
match source { match source {
SourceRecipe::Git { git, upstream, branch, rev } => { Some(SourceRecipe::Git { git, upstream, branch, rev }) => {
//TODO: use libgit? //TODO: use libgit?
if ! source_dir.is_dir() { if ! source_dir.is_dir() {
// Create source.tmp // Create source.tmp
@ -181,6 +181,15 @@ fn fetch(recipe_dir: &Path, source: &SourceRecipe) -> Result<PathBuf, String> {
// Move source.tmp to source atomically // Move source.tmp to source atomically
rename(&source_dir_tmp, &source_dir)?; rename(&source_dir_tmp, &source_dir)?;
} else { } else {
// Don't let this code reset the origin for the cookbook repo
let source_git_dir = source_dir.join(".git");
if ! source_git_dir.is_dir() {
return Err(format!(
"'{}' is not a git repository, but recipe indicated git source",
source_dir.display(),
));
}
// Reset origin // Reset origin
let mut command = Command::new("git"); let mut command = Command::new("git");
command.arg("-C").arg(&source_dir); command.arg("-C").arg(&source_dir);
@ -240,7 +249,7 @@ fi"#);
command.arg("submodule").arg("update").arg("--init").arg("--recursive"); command.arg("submodule").arg("update").arg("--init").arg("--recursive");
run_command(command)?; run_command(command)?;
}, },
SourceRecipe::Tar { tar, blake3, sha256, patches, script } => { Some(SourceRecipe::Tar { tar, blake3, sha256, patches, script }) => {
if ! source_dir.is_dir() { if ! source_dir.is_dir() {
// Download tar // Download tar
//TODO: replace wget //TODO: replace wget
@ -350,7 +359,16 @@ fi"#);
// Move source.tmp to source atomically // Move source.tmp to source atomically
rename(&source_dir_tmp, &source_dir)?; rename(&source_dir_tmp, &source_dir)?;
} }
} },
// Local Sources
None => {
if ! source_dir.is_dir() {
return Err(format!(
"Recipe without source section expected source dir at '{}'",
source_dir.display(),
))
}
},
} }
Ok(source_dir) Ok(source_dir)

View File

@ -69,7 +69,7 @@ pub struct PackageRecipe {
#[derive(Debug, Deserialize, PartialEq, Serialize)] #[derive(Debug, Deserialize, PartialEq, Serialize)]
pub struct Recipe { pub struct Recipe {
/// Specifies how to donload the source for this recipe /// Specifies how to donload the source for this recipe
pub source: SourceRecipe, pub source: Option<SourceRecipe>,
/// Specifies how to build this recipe /// Specifies how to build this recipe
pub build: BuildRecipe, pub build: BuildRecipe,
/// Specifies how to package this recipe /// Specifies how to package this recipe
@ -95,12 +95,12 @@ mod tests {
"#).unwrap(); "#).unwrap();
assert_eq!(recipe, Recipe { assert_eq!(recipe, Recipe {
source: SourceRecipe::Git { source: Some(SourceRecipe::Git {
git: "https://gitlab.redox-os.org/redox-os/acid.git".to_string(), git: "https://gitlab.redox-os.org/redox-os/acid.git".to_string(),
upstream: None, upstream: None,
branch: Some("master".to_string()), branch: Some("master".to_string()),
rev: Some("06344744d3d55a5ac9a62a6059cb363d40699bbc".to_string()), rev: Some("06344744d3d55a5ac9a62a6059cb363d40699bbc".to_string()),
}, }),
build: BuildRecipe { build: BuildRecipe {
kind: BuildKind::Cargo, kind: BuildKind::Cargo,
dependencies: Vec::new(), dependencies: Vec::new(),
@ -126,13 +126,13 @@ mod tests {
"#).unwrap(); "#).unwrap();
assert_eq!(recipe, Recipe { assert_eq!(recipe, Recipe {
source: SourceRecipe::Tar { source: Some(SourceRecipe::Tar {
tar: "http://downloads.xiph.org/releases/ogg/libogg-1.3.3.tar.xz".to_string(), tar: "http://downloads.xiph.org/releases/ogg/libogg-1.3.3.tar.xz".to_string(),
blake3: None, blake3: None,
sha256: Some("4f3fc6178a533d392064f14776b23c397ed4b9f48f5de297aba73b643f955c08".to_string()), sha256: Some("4f3fc6178a533d392064f14776b23c397ed4b9f48f5de297aba73b643f955c08".to_string()),
patches: Vec::new(), patches: Vec::new(),
script: None, script: None,
}, }),
build: BuildRecipe { build: BuildRecipe {
kind: BuildKind::Custom { kind: BuildKind::Custom {
script: "make".to_string() script: "make".to_string()