Skip to content

Commit 05d4a02

Browse files
feat(forge): add --empty to forge init (#11554)
* add example control * fix CI * 'no_example' -> 'empty' --------- Co-authored-by: 0xrusowsky <[email protected]>
1 parent 3396cc7 commit 05d4a02

File tree

2 files changed

+69
-46
lines changed

2 files changed

+69
-46
lines changed

crates/forge/src/cmd/clone.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -161,17 +161,11 @@ impl CloneArgs {
161161
/// * `enable_git` - whether to enable git for the project.
162162
/// * `quiet` - whether to print messages.
163163
pub(crate) fn init_an_empty_project(root: &Path, install: DependencyInstallOpts) -> Result<()> {
164-
// let's try to init the project with default init args
165-
let init_args = InitArgs { root: root.to_path_buf(), install, ..Default::default() };
164+
// Initialize the project with empty set to true to avoid creating example contracts
165+
let init_args =
166+
InitArgs { root: root.to_path_buf(), install, empty: true, ..Default::default() };
166167
init_args.run().map_err(|e| eyre::eyre!("Project init error: {:?}", e))?;
167168

168-
// remove the unnecessary example contracts
169-
// XXX (ZZ): this is a temporary solution until we have a proper way to remove contracts,
170-
// e.g., add a field in the InitArgs to control the example contract generation
171-
fs::remove_file(root.join("src/Counter.sol"))?;
172-
fs::remove_file(root.join("test/Counter.t.sol"))?;
173-
fs::remove_file(root.join("script/Counter.s.sol"))?;
174-
175169
Ok(())
176170
}
177171

crates/forge/src/cmd/init.rs

Lines changed: 66 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,28 @@ pub struct InitArgs {
4646
#[arg(long, conflicts_with = "template")]
4747
pub use_parent_git: bool,
4848

49+
/// Do not create example contracts (Counter.sol, Counter.t.sol, Counter.s.sol).
50+
#[arg(long, conflicts_with = "template")]
51+
pub empty: bool,
52+
4953
#[command(flatten)]
5054
pub install: DependencyInstallOpts,
5155
}
5256

5357
impl InitArgs {
5458
pub fn run(self) -> Result<()> {
55-
let Self { root, template, branch, install, offline, force, vscode, use_parent_git, vyper } =
56-
self;
59+
let Self {
60+
root,
61+
template,
62+
branch,
63+
install,
64+
offline,
65+
force,
66+
vscode,
67+
use_parent_git,
68+
vyper,
69+
empty,
70+
} = self;
5771
let DependencyInstallOpts { shallow, no_git, commit } = install;
5872

5973
// create the root dir if it does not exist
@@ -128,41 +142,56 @@ impl InitArgs {
128142
let script = root.join("script");
129143
fs::create_dir_all(&script)?;
130144

131-
if vyper {
132-
// write the contract file
133-
let contract_path = src.join("Counter.vy");
134-
fs::write(contract_path, include_str!("../../assets/vyper/CounterTemplate.vy"))?;
135-
let interface_path = src.join("ICounter.sol");
136-
fs::write(interface_path, include_str!("../../assets/vyper/ICounterTemplate.sol"))?;
137-
138-
// write the tests
139-
let contract_path = test.join("Counter.t.sol");
140-
fs::write(contract_path, include_str!("../../assets/vyper/CounterTemplate.t.sol"))?;
141-
142-
// write the script
143-
let contract_path = script.join("Counter.s.sol");
144-
fs::write(contract_path, include_str!("../../assets/vyper/CounterTemplate.s.sol"))?;
145-
} else {
146-
// write the contract file
147-
let contract_path = src.join("Counter.sol");
148-
fs::write(
149-
contract_path,
150-
include_str!("../../assets/solidity/CounterTemplate.sol"),
151-
)?;
152-
153-
// write the tests
154-
let contract_path = test.join("Counter.t.sol");
155-
fs::write(
156-
contract_path,
157-
include_str!("../../assets/solidity/CounterTemplate.t.sol"),
158-
)?;
159-
160-
// write the script
161-
let contract_path = script.join("Counter.s.sol");
162-
fs::write(
163-
contract_path,
164-
include_str!("../../assets/solidity/CounterTemplate.s.sol"),
165-
)?;
145+
// Only create example contracts if not disabled
146+
if !empty {
147+
if vyper {
148+
// write the contract file
149+
let contract_path = src.join("Counter.vy");
150+
fs::write(
151+
contract_path,
152+
include_str!("../../assets/vyper/CounterTemplate.vy"),
153+
)?;
154+
let interface_path = src.join("ICounter.sol");
155+
fs::write(
156+
interface_path,
157+
include_str!("../../assets/vyper/ICounterTemplate.sol"),
158+
)?;
159+
160+
// write the tests
161+
let contract_path = test.join("Counter.t.sol");
162+
fs::write(
163+
contract_path,
164+
include_str!("../../assets/vyper/CounterTemplate.t.sol"),
165+
)?;
166+
167+
// write the script
168+
let contract_path = script.join("Counter.s.sol");
169+
fs::write(
170+
contract_path,
171+
include_str!("../../assets/vyper/CounterTemplate.s.sol"),
172+
)?;
173+
} else {
174+
// write the contract file
175+
let contract_path = src.join("Counter.sol");
176+
fs::write(
177+
contract_path,
178+
include_str!("../../assets/solidity/CounterTemplate.sol"),
179+
)?;
180+
181+
// write the tests
182+
let contract_path = test.join("Counter.t.sol");
183+
fs::write(
184+
contract_path,
185+
include_str!("../../assets/solidity/CounterTemplate.t.sol"),
186+
)?;
187+
188+
// write the script
189+
let contract_path = script.join("Counter.s.sol");
190+
fs::write(
191+
contract_path,
192+
include_str!("../../assets/solidity/CounterTemplate.s.sol"),
193+
)?;
194+
}
166195
}
167196

168197
// Write the default README file

0 commit comments

Comments
 (0)