Skip to content

Commit

Permalink
feat(cargo-tangle): force initialize git submodules
Browse files Browse the repository at this point in the history
  • Loading branch information
Serial-ATA committed Nov 8, 2024
1 parent 29203b4 commit 47c133d
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 12 deletions.
6 changes: 4 additions & 2 deletions 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ parity-scale-codec = { version = "3.6.12", default-features = false }
scale-info = { version = "2.11.3", default-features = false }

tokio-retry = "0.3.0"
anyhow = "1.0.93"
async-trait = "0.1.82"
auto_impl = "1.2.0"
backon = { version = "1.2.0", default-features = false }
Expand Down
2 changes: 2 additions & 0 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ repository.workspace = true

[dependencies]
# CLI
anyhow.workspace = true
clap = { workspace = true, features = ["derive", "wrap_help"] }
clap-cargo = { workspace = true }
cargo-generate = { workspace = true, default-features = false, features = ["vendored-openssl", "vendored-libgit2"] }
Expand All @@ -31,6 +32,7 @@ alloy-rpc-types = { workspace = true }
alloy-signer-local = { workspace = true }
hex = { workspace = true }
w3f-bls = { workspace = true }
thiserror.workspace = true

[dev-dependencies]
tempfile = "3.10.1"
46 changes: 37 additions & 9 deletions cli/src/create.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
use clap::Args;
use std::path::PathBuf;
use std::process::Command;

#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error("Failed to generate blueprint: {0}")]
GenerationFailed(anyhow::Error),
#[error("Failed to initialize submodules, see .gitmodules to add them manually")]
SubmoduleInit,
#[error("{0}")]
Io(#[from] std::io::Error),
}

#[derive(Args, Debug, Clone, Default)]
#[group(id = "source", required = false, multiple = false)]
Expand Down Expand Up @@ -50,7 +61,7 @@ impl From<Source> for Option<cargo_generate::TemplatePath> {
}
}

pub fn new_blueprint(name: String, source: Option<Source>) {
pub fn new_blueprint(name: String, source: Option<Source>) -> Result<(), Error> {
println!("Generating blueprint with name: {}", name);

let source = source.unwrap_or_default();
Expand Down Expand Up @@ -88,14 +99,31 @@ pub fn new_blueprint(name: String, source: Option<Source>) {
overwrite: false,
skip_submodules: false,
other_args: Default::default(),
});
})
.map_err(Error::GenerationFailed)?;

match path {
Ok(path) => {
println!("Blueprint generated at: {}", path.display());
}
Err(e) => {
eprintln!("Error generating blueprint: {}", e);
}
println!("Blueprint generated at: {}", path.display());

// 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()?;

if !output.status.success() {
eprintln!(
"Failed to add tnt-core submodule: {}",
String::from_utf8_lossy(&output.stderr)
);
}

Ok(())
}
2 changes: 1 addition & 1 deletion cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ async fn main() -> color_eyre::Result<()> {
match cli.command {
Commands::Blueprint { subcommand } => match subcommand {
GadgetCommands::Create { name, source } => {
create::new_blueprint(name, source);
create::new_blueprint(name, source)?;
}
GadgetCommands::Deploy {
http_rpc_url,
Expand Down

0 comments on commit 47c133d

Please sign in to comment.