Skip to content

Commit

Permalink
CI fix?
Browse files Browse the repository at this point in the history
  • Loading branch information
BlaineHeffron committed Jul 2, 2024
1 parent a1a4593 commit 1df943d
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 15 deletions.
26 changes: 25 additions & 1 deletion Cargo.lock

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

2 changes: 2 additions & 0 deletions crates/loam-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ hex = "0.4.3"
shlex = "1.1.0"
symlink = "0.1.0"
toml = { version = "0.8.12", features = ["parse"] }
rand = "0.8.5"
wasm-gen = { version = "0.1.4" }
soroban-cli = "21.0.0"
stellar-xdr = "21.0.0"

Expand Down
13 changes: 9 additions & 4 deletions crates/loam-cli/src/commands/build/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,11 @@ pub struct Cmd {
/// Print commands to build without executing them
#[arg(long, conflicts_with = "out_dir", help_heading = "Other")]
pub print_commands_only: bool,
/// Build client code in addition to building the contract
#[arg(long)]
pub build_clients: bool,
#[command(flatten)]
pub build_clients: clients::Args,
pub build_clients_args: clients::Args,
}

#[derive(thiserror::Error, Debug)]
Expand Down Expand Up @@ -177,9 +180,11 @@ impl Cmd {
}
}

self.build_clients
.run(&metadata.workspace_root.into_std_path_buf())
.await?;
if self.build_clients {
self.build_clients_args
.run(&metadata.workspace_root.into_std_path_buf())
.await?;
}

Ok(())
}
Expand Down
6 changes: 3 additions & 3 deletions crates/loam-cli/tests/it/build_clients/contracts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ hello_world.workspace = true
"#,
);
let output = env
.loam_env("build", "development")
.loam_env("development")
.output()
.expect("Failed to execute command");

Expand All @@ -108,7 +108,7 @@ hello_world.workspace = true
.contains("🍽️ importing \"hello_world\" contract"));

let output2 = env
.loam_env("build", "development")
.loam_env("development")
.output()
.expect("Failed to execute command");

Expand All @@ -123,7 +123,7 @@ hello_world.workspace = true
env.replace_file(file, file_replaced);

let output3 = env
.loam_env("build", "development")
.loam_env("development")
.output()
.expect("Failed to execute command");

Expand Down
91 changes: 84 additions & 7 deletions crates/loam-cli/tests/it/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ use assert_cmd::{assert::Assert, Command};
use assert_fs::TempDir;
use fs_extra::dir::{copy, CopyOptions};
use std::path::PathBuf;
use std::fs;
use rand::{thread_rng, Rng};
use toml::Value;
use std::error::Error;

pub struct TestEnv {
pub temp_dir: TempDir,
Expand Down Expand Up @@ -46,21 +50,94 @@ impl TestEnv {
f(&test_env);
}

pub fn loam(&self, cmd: &str) -> Command {
let mut loam = Command::cargo_bin("loam").unwrap();
loam.current_dir(&self.cwd);
loam.arg(cmd);
loam
pub fn modify_wasm(&self, contract_name: &str) -> Result<(), Box<dyn Error>> {
// Read Cargo.toml to get the actual name
let cargo_toml_path = self.cwd.join("contracts").join(contract_name).join("Cargo.toml");
println!("cargo toml path is {:?}", cargo_toml_path);
let cargo_toml_content = fs::read_to_string(cargo_toml_path)?;
let cargo_toml: Value = toml::from_str(&cargo_toml_content)
.map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e))?;

let package_name = cargo_toml["package"]["name"]
.as_str()
.ok_or_else(|| std::io::Error::new(std::io::ErrorKind::InvalidData, "Invalid Cargo.toml"))?;

println!("packange name is is {:?}", package_name);

// Convert package name to proper filename format
let filename = package_name.replace('-', "_");

// Construct the path to the .wasm file
let wasm_path = self.cwd
.join("target")
.join("wasm32-unknown-unknown")
.join("release")
.join(format!("{}.wasm", filename));

println!("wasm path is {:?}", wasm_path);
// Read the original wasm
let mut wasm_bytes = fs::read(&wasm_path)?;

// Generate random bytes
let mut rng = thread_rng();
let random_bytes: Vec<u8> = (0..10).map(|_| rng.gen()).collect();

// Write the custom section
wasm_gen::write_custom_section(&mut wasm_bytes, "random_data", &random_bytes);

// Write the modified wasm
fs::write(&wasm_path, wasm_bytes)?;

Ok(())
}

pub fn loam_env(&self, cmd: &str, env: &str) -> Command {
pub fn loam_build(&self, env: &str) -> Command {
// Run initial build
let mut initial_build = Command::cargo_bin("loam").unwrap();
initial_build.current_dir(&self.cwd);
initial_build.arg("build");
initial_build.arg(env);
initial_build.output().expect("Failed to execute initial build");

// Modify WASM files
let contracts_dir = self.cwd.join("contracts");
if let Ok(entries) = fs::read_dir(contracts_dir) {
for entry in entries.flatten() {
if let Ok(file_type) = entry.file_type() {
if file_type.is_dir() {
if let Some(contract_name) = entry.file_name().to_str() {
self.modify_wasm(contract_name).expect("Failed to modify WASM");
}
}
}
}
}

// Run final build with --build-clients
let mut loam = Command::cargo_bin("loam").unwrap();
loam.current_dir(&self.cwd);
loam.arg(cmd);
loam.arg("build");
loam.arg(env);
loam.arg("--build-clients");
loam
}

pub fn loam(&self, cmd: &str) -> Command {
if cmd == "build" {
self.loam_build("production")
}
else{
let mut loam = Command::cargo_bin("loam").unwrap();
loam.current_dir(&self.cwd);
loam.arg(cmd);
loam
}
}

pub fn loam_env(&self, env: &str) -> Command {
self.loam_build(env)
}

pub fn soroban(&self, cmd: &str) -> Command {
let mut soroban = Command::new("soroban");
soroban.env(
Expand Down

0 comments on commit 1df943d

Please sign in to comment.