Skip to content

Commit

Permalink
feat(cargo-tangle)!: use Soldeer for dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
Serial-ATA committed Nov 19, 2024
1 parent da774fa commit aecae68
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 23 deletions.
35 changes: 16 additions & 19 deletions cli/src/create.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::foundry::FoundryToolchain;
use clap::Args;
use gadget_sdk::tracing;
use std::path::PathBuf;
use std::process::Command;

#[derive(thiserror::Error, Debug)]
pub enum Error {
Expand Down Expand Up @@ -103,26 +104,22 @@ pub fn new_blueprint(name: String, source: Option<Source>) -> Result<(), Error>
.map_err(Error::GenerationFailed)?;

println!("Blueprint generated at: {}", path.display());
let contracts = path.join("contracts");
if !contracts.exists() {
return Ok(());
}

// TODO: Hack, we have to initialize submodules ourselves, cargo-generate just copies
// them as normal directories: https://github.com/cargo-generate/cargo-generate/issues/1317
std::env::set_current_dir(path)?;
std::fs::remove_dir_all("./contracts/lib/tnt-core")?;

let output = Command::new("git")
.args([
"submodule",
"add",
"https://github.com/tangle-network/tnt-core",
"contracts/lib/tnt-core",
])
.output()?;
let foundry = FoundryToolchain::new();
if !foundry.forge.is_installed() {
tracing::warn!("Forge not installed, skipping dependencies");
tracing::warn!("NOTE: See <https://getfoundry.sh>");
tracing::warn!("NOTE: After installing Forge, you can run `forge soldeer update -d` to install dependencies");
return Ok(());
}

if !output.status.success() {
eprintln!(
"Failed to add tnt-core submodule: {}",
String::from_utf8_lossy(&output.stderr)
);
std::env::set_current_dir(contracts)?;
if let Err(e) = foundry.forge.install_dependencies() {
tracing::error!("{e}");
}

Ok(())
Expand Down
2 changes: 1 addition & 1 deletion cli/src/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ fn build_contracts_if_needed(
let needs_build = abs_pathes_to_check.iter().any(|path| !path.exists());

if needs_build {
let mut foundry = crate::foundry::FoundryToolchain::new();
let foundry = crate::foundry::FoundryToolchain::new();
foundry.check_installed_or_exit();
foundry.forge.build()?;
}
Expand Down
20 changes: 17 additions & 3 deletions cli/src/foundry/forge.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use super::CommandInstalled;
use color_eyre::{eyre::eyre, Result};
use gadget_sdk::tracing;
use std::process::Command;

use super::CommandInstalled;

pub struct Forge {
cmd: Command,
}
Expand Down Expand Up @@ -32,7 +32,7 @@ impl Forge {
}

/// Builds the contracts.
pub fn build(&mut self) -> Result<()> {
pub fn build(mut self) -> Result<()> {
eprintln!("Building contracts...");
let status = self
.cmd
Expand All @@ -46,4 +46,18 @@ impl Forge {
}
Ok(())
}

pub fn install_dependencies(mut self) -> Result<()> {
tracing::info!("Installing dependencies...");
let output = self
.cmd
.args(["soldeer", "update", "-d"])
.stderr(std::process::Stdio::inherit())
.stdout(std::process::Stdio::inherit())
.output()?;
if !output.status.success() {
return Err(eyre!("Failed to install dependencies"));
}
Ok(())
}
}

0 comments on commit aecae68

Please sign in to comment.