Skip to content

Commit

Permalink
twoliter: remove release.toml requirement
Browse files Browse the repository at this point in the history
It is no longer required to have a Release.toml file in the project.

Co-authored-by: Ethan Pullen <[email protected]>
Co-authored-by: Matthew James Briggs <[email protected]>
  • Loading branch information
ecpullen and webern committed Nov 30, 2023
1 parent 66fc9f3 commit 2495717
Show file tree
Hide file tree
Showing 15 changed files with 100 additions and 17 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 0 additions & 1 deletion tests/projects/project1/Release.toml

This file was deleted.

8 changes: 0 additions & 8 deletions tests/projects/project1/variants/Cargo.lock

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

1 change: 1 addition & 0 deletions twoliter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ hex = "0.4"
log = "0.4"
non-empty-string = { version = "0.2", features = [ "serde" ] }
serde = { version = "1", features = ["derive"] }
serde_json = "1"
sha2 = "0.10"
tar = "0.4"
tempfile = "3"
Expand Down
4 changes: 3 additions & 1 deletion twoliter/embedded/Makefile.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ BUILDSYS_VERSION_BUILD = { script = ["git describe --always --dirty --exclude '*
# later in this section. You have to edit the path here in Makefile.toml to
# use a different Release.toml.
BUILDSYS_RELEASE_CONFIG_PATH = "${BUILDSYS_ROOT_DIR}/Release.toml"
BUILDSYS_VERSION_IMAGE = { script = ["awk -F '[ =\"]+' '$1 == \"version\" {print $2}' ${BUILDSYS_RELEASE_CONFIG_PATH}"] }

# This can be overridden with -e to build a different variant from the variants/ directory
BUILDSYS_VARIANT = { script = ['echo "${BUILDSYS_VARIANT:-aws-k8s-1.24}"'] }
# Product name used for file and directory naming
Expand Down Expand Up @@ -137,6 +137,8 @@ TESTSYS_LOG_LEVEL = "info"
# Certain variables are defined here to allow us to override a component value
# on the command line.

BUILDSYS_VERSION_IMAGE = { script = ["awk -F '[ =\"]+' '$1 == \"version\" {print $2}' ${BUILDSYS_RELEASE_CONFIG_PATH}"], condition = { env_not_set = ["BUILDSYS_VERSION_IMAGE"]}}

# Depends on ${BUILDSYS_JOBS}.
CARGO_MAKE_CARGO_LIMIT_JOBS = "--jobs ${BUILDSYS_JOBS}"
CARGO_MAKE_CARGO_ARGS = "--offline --locked"
Expand Down
4 changes: 4 additions & 0 deletions twoliter/src/cmd/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::cargo_make::CargoMake;
use crate::docker::DockerContainer;
use crate::project;
use crate::tools::{install_tools, tools_tempdir};
use crate::variant::variant_version;
use anyhow::{Context, Result};
use clap::Parser;
use log::debug;
Expand Down Expand Up @@ -50,6 +51,8 @@ impl BuildVariant {
.context("Unable to create a tempdir for Twoliter's build")?;
let packages_dir = build_temp_dir.path().join("sdk_rpms");
fs::create_dir_all(&packages_dir)?;
let variant_version =
variant_version(&project.project_dir().join("variants"), &self.variant).await?;

let sdk_container = DockerContainer::new(
format!("sdk-{}", token),
Expand Down Expand Up @@ -107,6 +110,7 @@ impl BuildVariant {
.env("BUILDSYS_ARCH", &self.arch)
.env("BUILDSYS_VARIANT", &self.variant)
.env("BUILDSYS_SBKEYS_DIR", sbkeys_dir.display().to_string())
.env("BUILDSYS_VERSION_IMAGE", variant_version)
.makefile(makefile_path)
.project_dir(project.project_dir())
.exec("build")
Expand Down
16 changes: 10 additions & 6 deletions twoliter/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@ pub(crate) async fn exec_log(cmd: &mut Command) -> Result<()> {
log::max_level(),
LevelFilter::Off | LevelFilter::Error | LevelFilter::Warn
);
exec(cmd, quiet).await
exec(cmd, quiet).await?;
Ok(())
}

/// Run a `tokio::process::Command` and return a `Result` letting us know whether or not it worked.
/// `quiet` determines whether or not the command output will be piped to `stdout/stderr`. When
/// `quiet=true`, no output will be shown.
pub(crate) async fn exec(cmd: &mut Command, quiet: bool) -> Result<()> {
/// `quiet=true`, no output will be shown and will be returned instead.
pub(crate) async fn exec(cmd: &mut Command, quiet: bool) -> Result<String> {
debug!("Running: {:?}", cmd);
if quiet {
Ok(if quiet {
// For quiet levels of logging we capture stdout and stderr
let output = cmd
.output()
Expand All @@ -30,6 +31,8 @@ pub(crate) async fn exec(cmd: &mut Command, quiet: bool) -> Result<()> {
String::from_utf8_lossy(&output.stdout),
String::from_utf8_lossy(&output.stderr)
);

String::from_utf8(output.stdout).context("Unable to convert command output to `String`")?
} else {
// For less quiet log levels we stream to stdout and stderr.
let status = cmd
Expand All @@ -42,6 +45,7 @@ pub(crate) async fn exec(cmd: &mut Command, quiet: bool) -> Result<()> {
"Command was unsuccessful, exit code {}",
status.code().unwrap_or(1),
);
}
Ok(())

"".to_string()
})
}
3 changes: 2 additions & 1 deletion twoliter/src/docker/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ impl DockerContainer {
let mut args = vec!["cp".to_string()];
args.push(format!("{}:{}", self.name, src.as_ref().display()));
args.push(dest.as_ref().display().to_string());
exec(Command::new("docker").args(args), true).await
exec(Command::new("docker").args(args), true).await?;
Ok(())
}
}

Expand Down
1 change: 1 addition & 0 deletions twoliter/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ mod common;
mod docker;
mod project;
mod tools;
mod variant;

/// Test code that should only be compiled when running tests.
#[cfg(test)]
Expand Down
7 changes: 7 additions & 0 deletions twoliter/src/test/data/variants/test-variant/Cargo.lock

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

10 changes: 10 additions & 0 deletions twoliter/src/test/data/variants/test-variant/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "test-variant"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]

[workspace]
3 changes: 3 additions & 0 deletions twoliter/src/test/data/variants/test-variant/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
println!("Hello, world!");
}
1 change: 1 addition & 0 deletions twoliter/src/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ be compiled for `cfg(test)`, which is accomplished at its declaration in `main.r
!*/
mod cargo_make;
mod variant_version;
use std::path::PathBuf;

/// Return the canonical path to the directory where we store test data.
Expand Down
11 changes: 11 additions & 0 deletions twoliter/src/test/variant_version.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use crate::{test::data_dir, variant::variant_version};

#[tokio::test]
async fn test_variant_version() {
assert_eq!(
variant_version(&data_dir().join("variants"), "test-variant")
.await
.unwrap(),
"0.1.0"
);
}
46 changes: 46 additions & 0 deletions twoliter/src/variant.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use crate::common::exec;
use anyhow::{Context, Result};
use serde_json::Value;
use std::path::Path;
use tokio::process::Command;

pub(crate) async fn variant_version<S>(variants_dir: &Path, variant: S) -> Result<String>
where
S: AsRef<str>,
{
serde_json::from_str::<Value>(
&exec(
Command::new("cargo").args([
"metadata",
"--manifest-path",
&variants_dir
.join(variant.as_ref())
.join("Cargo.toml")
.display()
.to_string(),
"--no-deps",
"--format-version",
"1",
]),
true,
)
.await?,
)?
.as_object()
.context("Unexpected output format from `cargo metadata`")?
.get("packages")
.and_then(Value::as_array)
.context("`packages` is not an array")?
.iter()
.find(|value| {
value
.as_object()
.map(|object| object.get("name") == Some(&Value::String(variant.as_ref().into())))
.unwrap_or_default()
})
.and_then(Value::as_object)
.and_then(|object| object.get("version"))
.and_then(Value::as_str)
.map(str::to_string)
.context("Unable to get variant version from Cargo.toml metadata")
}

0 comments on commit 2495717

Please sign in to comment.