Skip to content

Commit

Permalink
Update dependencies
Browse files Browse the repository at this point in the history
Fix warnings
Improve testing of build script in Query phase
  • Loading branch information
hunger committed Feb 21, 2021
1 parent 20c5368 commit 7031911
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 52 deletions.
30 changes: 5 additions & 25 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 26 additions & 1 deletion gng-build-agent/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,26 @@ impl EngineBuilder {
};

let script = format!(
"PKG = {}\n",
r#"pkg_defaults = {{
bootstrap = false,
build_dependencies = {{}},
check_dependencies = {{}},
prepare = function() end,
build = function() end,
check = function() end,
install = function() end,
polish = function() end,
}}
PKG = {}
for k, v in pairs(pkg_defaults) do
if PKG[k] == nil then
PKG[k] = v
end
end"#,
std::fs::read_to_string(build_file).map_err(|e| gng_shared::Error::Script {
message: format!("Failed to read build script: {}", e),
})?
Expand Down Expand Up @@ -208,4 +227,10 @@ impl Engine {
})
.map_err(|e| map_error(&e))
}

/// Query whether a function is defined.
pub fn has_function(&mut self, name: &str) -> bool {
self.evaluate::<bool>(&format!("type(PKG.{}) == 'function'", name))
.unwrap_or(false)
}
}
24 changes: 12 additions & 12 deletions gng-build-agent/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,17 @@ use structopt::StructOpt;
)]
enum Args {
/// query packet definition file
QUERY,
Query,
/// prepare the sources for the build
PREPARE,
Prepare,
/// run the actual build process
BUILD,
Build,
/// Run tests and other checks
CHECK,
Check,
/// move the build results to their final location in the filesystem
INSTALL,
Install,
/// polish up the filesystem before putting all the files into a packet
POLISH,
Polish,
}

fn get_env(key: &str, default: &str) -> String {
Expand Down Expand Up @@ -159,11 +159,11 @@ fn main() -> Result<()> {
let mut ctx = Context { engine };

match args {
Args::QUERY => Ok(()),
Args::PREPARE => prepare(&mut ctx),
Args::BUILD => build(&mut ctx),
Args::CHECK => check(&mut ctx),
Args::INSTALL => install(&mut ctx),
Args::POLISH => polish(&mut ctx),
Args::Query => Ok(()),
Args::Prepare => prepare(&mut ctx),
Args::Build => build(&mut ctx),
Args::Check => check(&mut ctx),
Args::Install => install(&mut ctx),
Args::Polish => polish(&mut ctx),
}
}
58 changes: 45 additions & 13 deletions gng-build-agent/src/source_packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

use gng_shared::{Name, Version};

use std::convert::TryFrom;
// This does not use Serde since the error reporting is not that good there!

// - Helpers:
// ----------------------------------------------------------------------
Expand All @@ -23,14 +23,40 @@ fn map_error(e: gng_shared::Error, expression: &str) -> gng_shared::Error {
}
}

fn name_from_expression(
fn from_expression<T: serde::de::DeserializeOwned>(
engine: &mut crate::engine::Engine,
expression: &str,
) -> gng_shared::Result<Name> {
let name = engine
.evaluate::<String>("PKG.name")
.map_err(|e| map_error(e, expression))?;
Name::try_from(name).map_err(|e| map_error(e, expression))
) -> gng_shared::Result<T> {
engine
.evaluate::<T>(expression)
.map_err(|e| map_error(e, expression))
}

fn converted_expression<T: std::convert::TryFrom<String, Error = gng_shared::Error>>(
engine: &mut crate::engine::Engine,
expression: &str,
) -> gng_shared::Result<T> {
let name = from_expression::<String>(engine, expression)?;
T::try_from(name)
}

fn url_option(engine: &mut crate::engine::Engine, expression: &str) -> Option<String> {
let url = from_expression::<String>(engine, expression).unwrap_or_else(|_| String::new());
if url.is_empty() {
None
} else {
Some(url)
}
}

fn has_function(engine: &mut crate::engine::Engine, name: &str) -> gng_shared::Result<()> {
if engine.has_function(name) {
Ok(())
} else {
Err(gng_shared::Error::Script {
message: format!("Function \"{}\" is missing.", name),
})
}
}

/// Create a `SourcePacket` from an `Engine`
Expand All @@ -40,13 +66,19 @@ fn name_from_expression(
pub fn from_engine(
engine: &mut crate::engine::Engine,
) -> gng_shared::Result<gng_build_shared::SourcePacket> {
has_function(engine, "prepare")?;
has_function(engine, "build")?;
has_function(engine, "check")?;
has_function(engine, "install")?;
has_function(engine, "polish")?;

Ok(gng_build_shared::SourcePacket {
name: name_from_expression(engine, "PKG.name")?,
version: Version::new(0, "foo", "bar")?,
license: "".to_string(),
url: None,
bug_url: None,
bootstrap: false,
name: converted_expression::<Name>(engine, "PKG.name")?,
version: converted_expression::<Version>(engine, "PKG.version")?,
license: from_expression::<String>(engine, "PKG.license")?,
url: url_option(engine, "PKG.url"),
bug_url: url_option(engine, "PKG.bug_url"),
bootstrap: from_expression::<bool>(engine, "PKG.bootstrap").unwrap_or(false),
build_dependencies: vec![],
check_dependencies: vec![],
sources: vec![],
Expand Down
2 changes: 1 addition & 1 deletion gng-build/src/message_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ impl SourceHandler {
}

fn store_sources(&mut self, source_packet: SourcePacket) -> Result<()> {
let mut normalizer = UrlNormalizer::new(&self.pkgsrc_directory)?;
let mut _normalizer = UrlNormalizer::new(&self.pkgsrc_directory)?;

// for s in source_packet.sources {
// let source_url = normalizer.normalize(&s.url)?;
Expand Down
1 change: 1 addition & 0 deletions rust-toolchain
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
[toolchain]
channel = "nightly"
targets = [ "x86_64-unknown-linux-musl" ]

0 comments on commit 7031911

Please sign in to comment.