Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cli: Add ability to force init and new commands via cli #2698

Merged
merged 16 commits into from
Nov 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ The minor version will be incremented upon a breaking change and the patch versi
- syn: Add missing `new_from_array` method to `Hash` ([#2682](https://github.com/coral-xyz/anchor/pull/2682)).
- cli: Switch to Cargo feature resolver(`resolver = "2"`) ([#2676](https://github.com/coral-xyz/anchor/pull/2676)).
- cli: Fix using user specific path for `provider.wallet` in `Anchor.toml` ([#2696](https://github.com/coral-xyz/anchor/pull/2696)).
- cli: Allow force `init` and `new` ([#2698](https://github.com/coral-xyz/anchor/pull/2698)).

### Breaking

Expand Down
51 changes: 44 additions & 7 deletions cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ pub enum Command {
/// Rust program template to use
#[clap(value_enum, short, long, default_value = "single")]
template: ProgramTemplate,
/// Initialize even if there are files
#[clap(long, action)]
force: bool,
},
/// Builds the workspace.
#[clap(name = "build", alias = "b")]
Expand Down Expand Up @@ -225,6 +228,9 @@ pub enum Command {
/// Rust program template to use
#[clap(value_enum, short, long, default_value = "single")]
template: ProgramTemplate,
/// Create new program even if there is already one
#[clap(long, action)]
force: bool,
},
/// Commands for interacting with interface definitions.
Idl {
Expand Down Expand Up @@ -592,6 +598,7 @@ fn process_command(opts: Opts) -> Result<()> {
no_git,
jest,
template,
force,
} => init(
&opts.cfg_override,
name,
Expand All @@ -600,12 +607,14 @@ fn process_command(opts: Opts) -> Result<()> {
no_git,
jest,
template,
force,
),
Command::New {
solidity,
name,
template,
} => new(&opts.cfg_override, solidity, name, template),
force,
} => new(&opts.cfg_override, solidity, name, template, force),
Command::Build {
idl,
idl_ts,
Expand Down Expand Up @@ -745,6 +754,7 @@ fn process_command(opts: Opts) -> Result<()> {
}
}

#[allow(clippy::too_many_arguments)]
fn init(
cfg_override: &ConfigOverride,
name: String,
Expand All @@ -753,8 +763,9 @@ fn init(
no_git: bool,
jest: bool,
template: ProgramTemplate,
force: bool,
) -> Result<()> {
if Config::discover(cfg_override)?.is_some() {
if !force && Config::discover(cfg_override)?.is_some() {
return Err(anyhow!("Workspace already initialized"));
}

Expand All @@ -778,9 +789,13 @@ fn init(
));
}

fs::create_dir(&project_name)?;
if force {
fs::create_dir_all(&project_name)?;
} else {
fs::create_dir(&project_name)?;
}
std::env::set_current_dir(&project_name)?;
fs::create_dir("app")?;
fs::create_dir_all("app")?;

let mut cfg = Config::default();
if jest {
Expand Down Expand Up @@ -825,6 +840,15 @@ fn init(
// Initialize .prettierignore file
fs::write(".prettierignore", rust_template::prettier_ignore())?;

// Remove the default program if `--force` is passed
if force {
fs::remove_dir_all(
std::env::current_dir()?
.join(if solidity { "solidity" } else { "programs" })
.join(&project_name),
)?;
}

// Build the program.
if solidity {
solidity_template::create_program(&project_name)?;
Expand All @@ -833,9 +857,9 @@ fn init(
}

// Build the test suite.
fs::create_dir("tests")?;
fs::create_dir_all("tests")?;
// Build the migrations directory.
fs::create_dir("migrations")?;
fs::create_dir_all("migrations")?;

if javascript {
// Build javascript config
Expand Down Expand Up @@ -927,6 +951,7 @@ fn new(
solidity: bool,
name: String,
template: ProgramTemplate,
force: bool,
) -> Result<()> {
with_workspace(cfg_override, |cfg| {
match cfg.path().parent() {
Expand All @@ -939,7 +964,16 @@ fn new(
let cluster = cfg.provider.cluster.clone();
let programs = cfg.programs.entry(cluster).or_default();
if programs.contains_key(&name) {
return Err(anyhow!("Program already exists"));
if !force {
return Err(anyhow!("Program already exists"));
}

// Delete all files within the program folder
fs::remove_dir_all(
std::env::current_dir()?
.join(if solidity { "solidity" } else { "programs" })
.join(&name),
)?;
}

if solidity {
Expand Down Expand Up @@ -4416,6 +4450,7 @@ mod tests {
false,
false,
ProgramTemplate::default(),
false,
)
.unwrap();
}
Expand All @@ -4434,6 +4469,7 @@ mod tests {
false,
false,
ProgramTemplate::default(),
false,
)
.unwrap();
}
Expand All @@ -4452,6 +4488,7 @@ mod tests {
false,
false,
ProgramTemplate::default(),
false,
)
.unwrap();
}
Expand Down
Loading