From 4fcef45dc09429dd8280b0923b4b6978d550d7b1 Mon Sep 17 00:00:00 2001 From: John Anthos Date: Fri, 10 Nov 2023 10:03:45 +0000 Subject: [PATCH 01/16] Allow force init --- cli/src/lib.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/cli/src/lib.rs b/cli/src/lib.rs index ac40d78aa6..3758c10544 100644 --- a/cli/src/lib.rs +++ b/cli/src/lib.rs @@ -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(short, long, action)] + force: bool, }, /// Builds the workspace. #[clap(name = "build", alias = "b")] @@ -592,6 +595,7 @@ fn process_command(opts: Opts) -> Result<()> { no_git, jest, template, + force, } => init( &opts.cfg_override, name, @@ -600,6 +604,7 @@ fn process_command(opts: Opts) -> Result<()> { no_git, jest, template, + force, ), Command::New { solidity, @@ -753,8 +758,9 @@ fn init( no_git: bool, jest: bool, template: ProgramTemplate, + force: bool, ) -> Result<()> { - if Config::discover(cfg_override)?.is_some() { + if Config::discover(cfg_override)?.is_some() && !force { return Err(anyhow!("Workspace already initialized")); } @@ -4416,6 +4422,7 @@ mod tests { false, false, ProgramTemplate::default(), + false, ) .unwrap(); } @@ -4434,6 +4441,7 @@ mod tests { false, false, ProgramTemplate::default(), + false, ) .unwrap(); } @@ -4452,6 +4460,7 @@ mod tests { false, false, ProgramTemplate::default(), + false, ) .unwrap(); } From abaab8a89a0ab08708b130b08005bae7308a214c Mon Sep 17 00:00:00 2001 From: John Anthos Date: Fri, 10 Nov 2023 10:19:15 +0000 Subject: [PATCH 02/16] Update CHANGELOG --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3d093dbd91..95be53436d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 ([#2697](https://github.com/coral-xyz/anchor/pull/2697)). ### Breaking From f0d6b47f9a4a250b63adee2265746f419ebd916e Mon Sep 17 00:00:00 2001 From: John Anthos Date: Fri, 10 Nov 2023 10:22:11 +0000 Subject: [PATCH 03/16] Remove force short to avoid confusion with file --- cli/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cli/src/lib.rs b/cli/src/lib.rs index 3758c10544..6c4807ecd6 100644 --- a/cli/src/lib.rs +++ b/cli/src/lib.rs @@ -89,7 +89,7 @@ pub enum Command { #[clap(value_enum, short, long, default_value = "single")] template: ProgramTemplate, /// Initialize even if there are files - #[clap(short, long, action)] + #[clap(long, action)] force: bool, }, /// Builds the workspace. From e42312a692a2cfa40390e30f20a8fbe7527beddd Mon Sep 17 00:00:00 2001 From: John Anthos <97734034+johnanthos@users.noreply.github.com> Date: Fri, 10 Nov 2023 10:53:36 +0000 Subject: [PATCH 04/16] Update PR number Co-authored-by: acheron <98934430+acheroncrypto@users.noreply.github.com> --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 95be53436d..868a949032 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,7 +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 ([#2697](https://github.com/coral-xyz/anchor/pull/2697)). +- cli: Allow force init ([#2698](https://github.com/coral-xyz/anchor/pull/2698)). ### Breaking From 049c496a9b93db2782b34fa367bf08c77914d8ae Mon Sep 17 00:00:00 2001 From: John Anthos <97734034+johnanthos@users.noreply.github.com> Date: Fri, 10 Nov 2023 10:54:30 +0000 Subject: [PATCH 05/16] Change force execution order Co-authored-by: acheron <98934430+acheroncrypto@users.noreply.github.com> --- cli/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cli/src/lib.rs b/cli/src/lib.rs index 6c4807ecd6..b231256296 100644 --- a/cli/src/lib.rs +++ b/cli/src/lib.rs @@ -760,7 +760,7 @@ fn init( template: ProgramTemplate, force: bool, ) -> Result<()> { - if Config::discover(cfg_override)?.is_some() && !force { + if !force && Config::discover(cfg_override)?.is_some() { return Err(anyhow!("Workspace already initialized")); } From 63e3948ff40e526495b107f6ee9b1f78b53e05e2 Mon Sep 17 00:00:00 2001 From: John Anthos Date: Fri, 10 Nov 2023 11:07:58 +0000 Subject: [PATCH 06/16] Add force to new cli command --- cli/src/lib.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/cli/src/lib.rs b/cli/src/lib.rs index b231256296..add4ecc428 100644 --- a/cli/src/lib.rs +++ b/cli/src/lib.rs @@ -228,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 { @@ -610,7 +613,8 @@ fn process_command(opts: Opts) -> Result<()> { solidity, name, template, - } => new(&opts.cfg_override, solidity, name, template), + force, + } => new(&opts.cfg_override, solidity, name, template, force), Command::Build { idl, idl_ts, @@ -933,6 +937,7 @@ fn new( solidity: bool, name: String, template: ProgramTemplate, + force: bool, ) -> Result<()> { with_workspace(cfg_override, |cfg| { match cfg.path().parent() { @@ -944,7 +949,7 @@ 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) { return Err(anyhow!("Program already exists")); } From d35fdb4a58ef1059ef268f8e9d8f7846db661ee4 Mon Sep 17 00:00:00 2001 From: John Anthos Date: Fri, 10 Nov 2023 11:08:28 +0000 Subject: [PATCH 07/16] Update CHANGELOG with force command usage --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 868a949032..ad4905898d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,7 +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 ([#2698](https://github.com/coral-xyz/anchor/pull/2698)). +- cli: Allow force `init` and `new` ([#2698](https://github.com/coral-xyz/anchor/pull/2698)). ### Breaking From ba6b7c90e46f868c1d6d2b5a150d3d0294e11a8c Mon Sep 17 00:00:00 2001 From: John Anthos Date: Fri, 10 Nov 2023 11:53:01 +0000 Subject: [PATCH 08/16] Do not create folder if already exists on force init --- cli/src/lib.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/cli/src/lib.rs b/cli/src/lib.rs index add4ecc428..57dabb4b08 100644 --- a/cli/src/lib.rs +++ b/cli/src/lib.rs @@ -788,7 +788,9 @@ fn init( )); } - fs::create_dir(&project_name)?; + if !force { + fs::create_dir(&project_name)?; + } std::env::set_current_dir(&project_name)?; fs::create_dir("app")?; From ff09ddee97864cad8e2a915824e5e635a7690661 Mon Sep 17 00:00:00 2001 From: John Anthos Date: Fri, 10 Nov 2023 15:29:23 +0000 Subject: [PATCH 09/16] Delete all files within the program folder when using new force --- cli/src/lib.rs | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/cli/src/lib.rs b/cli/src/lib.rs index 57dabb4b08..79f868ddce 100644 --- a/cli/src/lib.rs +++ b/cli/src/lib.rs @@ -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}; @@ -788,11 +788,9 @@ fn init( )); } - if !force { - fs::create_dir(&project_name)?; - } + fs::create_dir_all(&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 { @@ -845,9 +843,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 @@ -953,6 +951,13 @@ fn new( let programs = cfg.programs.entry(cluster).or_default(); if !force && programs.contains_key(&name) { 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 { From 692a5933763d91bb57cb8c3daa5eae40803d4b6d Mon Sep 17 00:00:00 2001 From: John Anthos Date: Fri, 10 Nov 2023 15:53:25 +0000 Subject: [PATCH 10/16] Rewrite files on init force --- cli/src/lib.rs | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/cli/src/lib.rs b/cli/src/lib.rs index 79f868ddce..ada133ced6 100644 --- a/cli/src/lib.rs +++ b/cli/src/lib.rs @@ -788,9 +788,17 @@ fn init( )); } - fs::create_dir_all(&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_all("app")?; + if force { + fs::create_dir_all("app")?; + } else { + fs::create_dir("app")?; + } let mut cfg = Config::default(); if jest { @@ -836,6 +844,14 @@ 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))?; + } + } + if solidity { solidity_template::create_program(&project_name)?; } else { @@ -843,9 +859,17 @@ fn init( } // Build the test suite. - fs::create_dir_all("tests")?; + if force { + fs::create_dir_all("tests")?; + } else { + fs::create_dir("tests")?; + } // Build the migrations directory. - fs::create_dir_all("migrations")?; + if force { + fs::create_dir_all("migrations")?; + } else { + fs::create_dir("migrations")?; + } if javascript { // Build javascript config From db3be85527591f709ef1aca4272e64547a4c561d Mon Sep 17 00:00:00 2001 From: acheron Date: Sat, 11 Nov 2023 08:11:49 +0100 Subject: [PATCH 11/16] Fix CI rustfmt --- cli/src/lib.rs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/cli/src/lib.rs b/cli/src/lib.rs index ada133ced6..07b3902a20 100644 --- a/cli/src/lib.rs +++ b/cli/src/lib.rs @@ -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, remove_dir_all}; +use std::fs::{self, remove_dir_all, File}; use std::io::prelude::*; use std::path::{Path, PathBuf}; use std::process::{Child, Stdio}; @@ -846,9 +846,17 @@ fn init( // Build the program. if force { if solidity { - remove_dir_all(std::env::current_dir()?.join("solidity").join(&project_name))?; + 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))?; + remove_dir_all( + std::env::current_dir()? + .join("programs") + .join(&project_name), + )?; } } From e9bfc6a48a17eec65b31b5ca6a958f87a68871f0 Mon Sep 17 00:00:00 2001 From: acheron Date: Sat, 11 Nov 2023 08:13:34 +0100 Subject: [PATCH 12/16] Fix mismatched comments --- cli/src/lib.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cli/src/lib.rs b/cli/src/lib.rs index 07b3902a20..7fa72e1c16 100644 --- a/cli/src/lib.rs +++ b/cli/src/lib.rs @@ -843,7 +843,7 @@ fn init( // Initialize .prettierignore file fs::write(".prettierignore", rust_template::prettier_ignore())?; - // Build the program. + // Remove the default program if `--force` is passed if force { if solidity { remove_dir_all( @@ -860,6 +860,7 @@ fn init( } } + // Build the program. if solidity { solidity_template::create_program(&project_name)?; } else { From ce5703ec6066bbf9193eb8a3a5acaa24f7f6c2d5 Mon Sep 17 00:00:00 2001 From: acheron Date: Sat, 11 Nov 2023 08:15:52 +0100 Subject: [PATCH 13/16] Refactor `solidity` checks for remove dir --- cli/src/lib.rs | 30 +++++++++++------------------- 1 file changed, 11 insertions(+), 19 deletions(-) diff --git a/cli/src/lib.rs b/cli/src/lib.rs index 7fa72e1c16..5b6fa03117 100644 --- a/cli/src/lib.rs +++ b/cli/src/lib.rs @@ -42,7 +42,7 @@ use std::collections::BTreeMap; use std::collections::HashMap; use std::collections::HashSet; use std::ffi::OsString; -use std::fs::{self, remove_dir_all, File}; +use std::fs::{self, File}; use std::io::prelude::*; use std::path::{Path, PathBuf}; use std::process::{Child, Stdio}; @@ -845,19 +845,11 @@ fn init( // Remove the default program if `--force` is passed 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), - )?; - } + fs::remove_dir_all( + std::env::current_dir()? + .join(if solidity { "solidity" } else { "programs" }) + .join(&project_name), + )?; } // Build the program. @@ -986,11 +978,11 @@ fn new( 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))?; - } + fs::remove_dir_all( + std::env::current_dir()? + .join(if solidity { "solidity" } else { "programs" }) + .join(&name), + )?; } if solidity { From 0a8c2a74054ae47adeba6bbbb3bd663f2541de26 Mon Sep 17 00:00:00 2001 From: acheron Date: Sat, 11 Nov 2023 08:19:46 +0100 Subject: [PATCH 14/16] Remove `else if` check in `new` --- cli/src/lib.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/cli/src/lib.rs b/cli/src/lib.rs index 5b6fa03117..2b7c98507c 100644 --- a/cli/src/lib.rs +++ b/cli/src/lib.rs @@ -974,9 +974,11 @@ fn new( let cluster = cfg.provider.cluster.clone(); let programs = cfg.programs.entry(cluster).or_default(); - if !force && programs.contains_key(&name) { - return Err(anyhow!("Program already exists")); - } else if force && programs.contains_key(&name) { + if programs.contains_key(&name) { + if !force { + return Err(anyhow!("Program already exists")); + } + // Delete all files within the program folder fs::remove_dir_all( std::env::current_dir()? From 62bee5723ca017f0c791858c3ec352a915ef7617 Mon Sep 17 00:00:00 2001 From: acheron Date: Sat, 11 Nov 2023 08:29:54 +0100 Subject: [PATCH 15/16] Fix CI `clippy::too-many-arguments` --- cli/src/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/cli/src/lib.rs b/cli/src/lib.rs index 2b7c98507c..92bb872124 100644 --- a/cli/src/lib.rs +++ b/cli/src/lib.rs @@ -754,6 +754,7 @@ fn process_command(opts: Opts) -> Result<()> { } } +#[allow(clippy::too_many_arguments)] fn init( cfg_override: &ConfigOverride, name: String, From 488c012e9fe1f57fa064c6a7826844ff606e017c Mon Sep 17 00:00:00 2001 From: John Anthos Date: Sat, 11 Nov 2023 08:54:17 +0000 Subject: [PATCH 16/16] Remove unnecessary force checks on internal directories --- cli/src/lib.rs | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-) diff --git a/cli/src/lib.rs b/cli/src/lib.rs index 92bb872124..5b17ab2be8 100644 --- a/cli/src/lib.rs +++ b/cli/src/lib.rs @@ -795,11 +795,7 @@ fn init( fs::create_dir(&project_name)?; } std::env::set_current_dir(&project_name)?; - if force { - fs::create_dir_all("app")?; - } else { - fs::create_dir("app")?; - } + fs::create_dir_all("app")?; let mut cfg = Config::default(); if jest { @@ -861,17 +857,9 @@ fn init( } // Build the test suite. - if force { - fs::create_dir_all("tests")?; - } else { - fs::create_dir("tests")?; - } + fs::create_dir_all("tests")?; // Build the migrations directory. - if force { - fs::create_dir_all("migrations")?; - } else { - fs::create_dir("migrations")?; - } + fs::create_dir_all("migrations")?; if javascript { // Build javascript config