Skip to content

Commit 3952839

Browse files
committed
fix: handle missing OZ contract
1 parent fd7d0e2 commit 3952839

1 file changed

Lines changed: 39 additions & 6 deletions

File tree

  • crates/stellar-scaffold-cli/src/commands

crates/stellar-scaffold-cli/src/commands/init.rs

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,19 +86,28 @@ impl Cmd {
8686
}
8787

8888
// Update the project with the latest OpenZeppelin examples
89-
self.update_oz_example(&absolute_project_path, "nft-enumerable", global_args)
90-
.await?;
89+
let example_contracts = ["nft-enumerable", "fungible-allowlist-example"];
90+
91+
for contract in example_contracts {
92+
self.update_oz_example(&absolute_project_path, contract, global_args)
93+
.await?;
94+
}
9195

9296
printer.checkln(format!("Project successfully created at {project_str}"));
9397
Ok(())
9498
}
9599

100+
/// Updates the project with an OpenZeppelin example contract
101+
///
102+
/// This method attempts to generate a contract from OpenZeppelin
103+
/// and prints a warning if it can't be found or generated.
96104
async fn update_oz_example(
97105
&self,
98106
absolute_project_path: &PathBuf,
99-
contract_path: &'static str,
107+
contract_path: &str,
100108
global_args: &global::Args,
101109
) -> Result<(), Error> {
110+
let printer = Print::new(global_args.quiet);
102111
let original_dir = env::current_dir()?;
103112
env::set_current_dir(absolute_project_path)?;
104113

@@ -112,7 +121,7 @@ impl Cmd {
112121
let mut quiet_global_args = global_args.clone();
113122
quiet_global_args.quiet = true;
114123

115-
generate::contract::Cmd {
124+
let result = generate::contract::Cmd {
116125
from: Some(contract_path.to_owned()),
117126
ls: false,
118127
from_wizard: false,
@@ -124,8 +133,32 @@ impl Cmd {
124133
),
125134
}
126135
.run(&quiet_global_args)
127-
.await?;
128-
env::set_current_dir(original_dir)?;
136+
.await;
137+
138+
// Restore directory before handling result
139+
let _ = env::set_current_dir(original_dir);
140+
141+
match result {
142+
Ok(()) => {
143+
printer.infoln(format!(
144+
"Successfully added OpenZeppelin example contract: {}",
145+
contract_path
146+
));
147+
}
148+
Err(generate::contract::Error::ExampleNotFound(_)) => {
149+
printer.infoln(format!(
150+
"Skipping OpenZeppelin example contract: {} (not found)",
151+
contract_path
152+
));
153+
}
154+
Err(e) => {
155+
printer.warnln(format!(
156+
"Failed to generate example contract: {}\n{}",
157+
contract_path, e
158+
));
159+
}
160+
}
161+
129162
Ok(())
130163
}
131164
}

0 commit comments

Comments
 (0)