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 10 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
61 changes: 53 additions & 8 deletions cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ use std::collections::BTreeMap;
use std::collections::HashMap;
use std::collections::HashSet;
use std::ffi::OsString;
use std::fs::{self, File};
use std::fs::{self, File, remove_dir_all};
use std::io::prelude::*;
use std::path::{Path, PathBuf};
use std::process::{Child, Stdio};
Expand Down 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 @@ -753,8 +762,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 +788,17 @@ 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")?;
if force {
fs::create_dir_all("app")?;
} else {
fs::create_dir("app")?;
}

let mut cfg = Config::default();
if jest {
Expand Down Expand Up @@ -826,16 +844,32 @@ fn init(
fs::write(".prettierignore", rust_template::prettier_ignore())?;

// Build the program.
if force {
if solidity {
remove_dir_all(std::env::current_dir()?.join("solidity").join(&project_name))?;
} else {
remove_dir_all(std::env::current_dir()?.join("programs").join(&project_name))?;
}
}

acheroncrypto marked this conversation as resolved.
Show resolved Hide resolved
if solidity {
solidity_template::create_program(&project_name)?;
} else {
rust_template::create_program(&project_name, template)?;
}

// Build the test suite.
fs::create_dir("tests")?;
if force {
fs::create_dir_all("tests")?;
} else {
fs::create_dir("tests")?;
}
acheroncrypto marked this conversation as resolved.
Show resolved Hide resolved
// Build the migrations directory.
fs::create_dir("migrations")?;
if force {
fs::create_dir_all("migrations")?;
} else {
fs::create_dir("migrations")?;
}

if javascript {
// Build javascript config
Expand Down Expand Up @@ -927,6 +961,7 @@ fn new(
solidity: bool,
name: String,
template: ProgramTemplate,
force: bool,
) -> Result<()> {
with_workspace(cfg_override, |cfg| {
match cfg.path().parent() {
Expand All @@ -938,8 +973,15 @@ fn new(

let cluster = cfg.provider.cluster.clone();
let programs = cfg.programs.entry(cluster).or_default();
if programs.contains_key(&name) {
if !force && programs.contains_key(&name) {
acheroncrypto marked this conversation as resolved.
Show resolved Hide resolved
return Err(anyhow!("Program already exists"));
} else if force && programs.contains_key(&name) {
// Delete all files within the program folder
if solidity {
remove_dir_all(std::env::current_dir()?.join("solidity").join(&name))?;
} else {
remove_dir_all(std::env::current_dir()?.join("programs").join(&name))?;
}
}

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