Procházet zdrojové kódy

cli: Update programs in `Anchor.toml` when using `anchor new` (#2516)

CanardMandarin před 2 roky
rodič
revize
1902b8e586
2 změnil soubory, kde provedl 25 přidání a 4 odebrání
  1. 1 0
      CHANGELOG.md
  2. 24 4
      cli/src/lib.rs

+ 1 - 0
CHANGELOG.md

@@ -28,6 +28,7 @@ The minor version will be incremented upon a breaking change and the patch versi
 - lang: Fix inability to use identifiers `program_id`, `accounts`, `ix_data`, `remaining_accounts` in instruction arguments ([#2464](https://github.com/coral-xyz/anchor/pull/2464))
 - cli: Fix incorrect `metadata.address` generation in IDL after deploying with a custom keypair ([#2485](https://github.com/coral-xyz/anchor/pull/2485))
 - cli: IDL commands no longer hang when the payer doesn't have funds to pay for the transaction fee ([#2492](https://github.com/coral-xyz/anchor/pull/2492))
+- cli: Fix `anchor new` not updating `Anchor.toml` ([#2516](https://github.com/coral-xyz/anchor/pull/2516)).
 
 ### Breaking
 

+ 24 - 4
cli/src/lib.rs

@@ -776,11 +776,31 @@ fn new(cfg_override: &ConfigOverride, solidity: bool, name: String) -> Result<()
             }
             Some(parent) => {
                 std::env::set_current_dir(parent)?;
-                if solidity {
-                    new_solidity_program(&name)?;
-                } else {
-                    new_rust_program(&name)?;
+
+                let cluster = cfg.provider.cluster.clone();
+                let programs = cfg.programs.entry(cluster).or_insert(BTreeMap::new());
+                if programs.contains_key(&name) {
+                    return Err(anyhow!("Program already exists"));
                 }
+
+                programs.insert(
+                    name.clone(),
+                    ProgramDeployment {
+                        address: if solidity {
+                            new_solidity_program(&name)?;
+                            solidity_template::default_program_id()
+                        } else {
+                            new_rust_program(&name)?;
+                            rust_template::get_or_create_program_id(&name)
+                        },
+                        path: None,
+                        idl: None,
+                    },
+                );
+
+                let toml = cfg.to_string();
+                fs::write("Anchor.toml", toml)?;
+
                 println!("Created new program.");
             }
         };