Skip to content

Commit

Permalink
fix(CLI): use Loam SDK deps with loam init (#120)
Browse files Browse the repository at this point in the history
The frontend needs `loam-sdk` and related deps, not `soroban-sdk`.
  • Loading branch information
elizabethengelman authored Jul 18, 2024
1 parent ee52a8b commit b92f855
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
8 changes: 5 additions & 3 deletions 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 @@ -61,6 +61,8 @@ soroban-cli = "21.0.0"
notify = "5.0"
stellar-xdr = "21.0.0"
rust-embed = { version = "8.2.0", features = ["debug-embed"] }
regex = "1.10.5"
toml_edit = "0.22.16"

[dev-dependencies]
assert_cmd = "2.0.4"
Expand Down
34 changes: 33 additions & 1 deletion crates/loam-cli/src/commands/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ use clap::Parser;
use rust_embed::{EmbeddedFile, RustEmbed};
use soroban_cli::commands::contract::init as soroban_init;
use std::{
fs::{self, create_dir_all, metadata, remove_dir_all, write, Metadata},
fs::{self, create_dir_all, metadata, read_to_string, remove_dir_all, write, Metadata},
io,
path::{Path, PathBuf},
};
use toml_edit::{DocumentMut, TomlError};

const FRONTEND_TEMPLATE: &str = "https://github.com/loambuild/frontend";

Expand All @@ -32,6 +33,8 @@ pub enum Error {
SorobanInitError(#[from] soroban_init::Error),
#[error("Failed to convert bytes to string: {0}")]
ConverBytesToStringErr(#[from] std::str::Utf8Error),
#[error("Failed to parse toml file: {0}")]
TomlParseError(#[from] TomlError),
}

impl Cmd {
Expand Down Expand Up @@ -64,10 +67,39 @@ impl Cmd {
copy_example_contracts(&self.project_path)?;
rename_cargo_toml_remove(&self.project_path, "core")?;
rename_cargo_toml_remove(&self.project_path, "status_message")?;
update_workspace_cargo_toml(&self.project_path.join("Cargo.toml"))?;
Ok(())
}
}

// update a soroban project to a loam project
fn update_workspace_cargo_toml(cargo_path: &Path) -> Result<(), Error> {
let cargo_toml_str = read_to_string(cargo_path).map_err(|e| {
eprintln!("Error reading Cargo.toml file in: {cargo_path:?}");
e
})?;

let cargo_toml_str = regex::Regex::new(r#"soroban-sdk = "[^\"]+""#)
.unwrap()
.replace_all(
cargo_toml_str.as_str(),
r#"loam-sdk = "0.6.12"
loam-subcontract-core = "0.7.5""#,
);

let doc = cargo_toml_str.parse::<DocumentMut>().map_err(|e| {
eprintln!("Error parsing Cargo.toml file in: {cargo_path:?}");
e
})?;

write(cargo_path, doc.to_string()).map_err(|e| {
eprintln!("Error writing to Cargo.toml file in: {cargo_path:?}");
e
})?;

Ok(())
}

fn copy_example_contracts(to: &Path) -> Result<(), Error> {
for item in ExampleCore::iter() {
copy_file(
Expand Down

0 comments on commit b92f855

Please sign in to comment.