Skip to content

Commit

Permalink
feat: feature support in workspace (#3)
Browse files Browse the repository at this point in the history
* feat: feature support in workspace under `metadata = {  build_variants = [VARIANT] }`

* fix: building script
  • Loading branch information
j0nl1 authored Apr 23, 2024
1 parent 8d4d7f5 commit 898269b
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 11 deletions.
20 changes: 10 additions & 10 deletions bob_the_builder/Cargo.lock

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

12 changes: 12 additions & 0 deletions bob_the_builder/src/cargo_toml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use serde::Deserialize;

#[derive(Deserialize, Debug)]
pub struct CargoToml {
pub package: Option<Package>,
pub workspace: Option<Workspace>,
}

Expand All @@ -10,6 +11,17 @@ pub struct Workspace {
pub members: Option<Vec<String>>,
}

#[derive(Deserialize, Debug)]
pub struct Package {
pub name: String,
pub metadata: Option<Metadata>,
}

#[derive(Default, Deserialize, Debug)]
pub struct Metadata {
pub build_variants: Option<Vec<String>>,
}

#[derive(Debug, PartialEq)]
pub enum IsWorkspace {
Yes {
Expand Down
45 changes: 44 additions & 1 deletion bob_the_builder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::{
process::Command,
};

use cargo_toml::{is_workspace, IsWorkspace};
use cargo_toml::{is_workspace, CargoToml, IsWorkspace};

const CARGO_PATH: &str = "cargo";
const PACKAGE_PREFIX: &str = "contracts/";
Expand Down Expand Up @@ -61,8 +61,51 @@ pub fn build_workspace(workspace_members: &[String]) {

println!("Contracts to be built: {:?}", contract_packages);

let build_dir = PathBuf::from("/target/wasm32-unknown-unknown/release");

for contract in contract_packages {
println!("Building {:?} ...", contract);
let cargo_file = fs::read_to_string(contract.join("Cargo.toml")).unwrap();

let CargoToml { package, .. } = toml::from_str(&cargo_file).unwrap();

let package = package.expect("package is required in Cargo.toml");
let name = package.name.replace("-", "_");
let variants = package
.metadata
.unwrap_or_default()
.build_variants
.unwrap_or_default();

if variants.len() > 0 {
for variant in variants {
let mut child = Command::new(CARGO_PATH)
.args(&[
"build",
"--target-dir=/target",
"--release",
"--features",
variant.as_str(),
"--lib",
"--target=wasm32-unknown-unknown",
"--locked",
])
.env("RUSTFLAGS", "-C link-arg=-s")
.current_dir(canonicalize(contract).unwrap())
.spawn()
.unwrap();
let error_code = child.wait().unwrap();
assert!(error_code.success());

println!("Built variant: {}", variant);

fs::rename(
build_dir.join(format!("{name}.wasm")),
build_dir.join(format!("{name}_{variant}.wasm")),
)
.unwrap();
}
}

let mut child = Command::new(CARGO_PATH)
.args(&[
Expand Down

0 comments on commit 898269b

Please sign in to comment.