From 32d9cb190070bad1b0b81d69bdb6194fc5cb7814 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Zieli=C5=84ski?= Date: Wed, 19 Jul 2023 12:20:16 +0200 Subject: [PATCH] More validation to generate command (#61) --- .gitignore | 1 + CHANGELOG.md | 5 +++++ Cargo.toml | 2 +- README.md | 3 --- src/actions/generate.rs | 26 ++++++++++++++++++++++++++ src/errors.rs | 8 ++++++++ src/log.rs | 2 +- 7 files changed, 42 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index 93828d8..ff052b3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ /target .idea testproject +Cargo.lock diff --git a/CHANGELOG.md b/CHANGELOG.md index 63b1127..f301df7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,11 @@ Changelog for `cargo-odra`. +## [0.0.9] - 2023-07-19 + +### Added +- `generate` command validates if the module is already added to `lib.rs`. + ## [0.0.8] - 2023-06-26 ### Added diff --git a/Cargo.toml b/Cargo.toml index 1cfd67e..b503d91 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,7 +10,7 @@ description = "A cargo utility that helps to create, manage and test your smart keywords = ["wasm", "webassembly", "blockchain"] categories = ["wasm", "smart contracts"] name = "cargo-odra" -version = "0.0.8" +version = "0.0.9" edition = "2021" [dependencies] diff --git a/README.md b/README.md index e2b3ba5..9c2f3a7 100644 --- a/README.md +++ b/README.md @@ -66,14 +66,11 @@ workspace, put all contracts in the same Odra.toml folder. * [Odra](https://github.com/odradev/odra) * [Cargo Odra](https://github.com/odradev/cargo-odra) -* [Odra Template](https://github.com/odradev/odra-template) * [Odra Casper](https://github.com/odradev/odra-casper) -* [Original Proposal for Odra Framework](https://github.com/odradev/odra-proposal) ## Contact Write **contact@odra.dev** ----
by odra.dev diff --git a/src/actions/generate.rs b/src/actions/generate.rs index dc705da..498326e 100644 --- a/src/actions/generate.rs +++ b/src/actions/generate.rs @@ -62,6 +62,11 @@ impl GenerateAction<'_> { &self.contract_module_ident } + /// Returns the module Ref identifier. + fn module_ref_ident(&self) -> String { + format!("{}Ref", self.contract_module_ident) + } + /// Returns a path to file with contract definition. fn module_file_path(&self) -> PathBuf { self.module_root @@ -96,6 +101,27 @@ impl GenerateAction<'_> { .register_module_snippet(self.contract_name(), self.module_ident()) .unwrap_or_else(|err| err.print_and_die()); + // Read the file. + let lib_rs_path = self.module_root.join("src/lib.rs"); + let lib_rs = command::read_file_content(lib_rs_path) + .unwrap_or_else(|_| Error::LibRsNotFound.print_and_die()); + + // If the file already has module registered, throw an error. + if lib_rs.contains(®ister_module_code) { + Error::ModuleAlreadyInLibRs(String::from(self.contract_name())).print_and_die(); + } + + // Check if he file might have the module registered in another form. + if lib_rs.contains(self.contract_name()) + || (lib_rs.contains(self.module_ident()) && lib_rs.contains(&self.module_ref_ident())) + { + log::warn(format!( + "src/lib.rs probably already has {} enabled. Skipping.", + self.contract_name() + )); + return; + } + // Write to file. command::append_file(self.module_root.join("src/lib.rs"), ®ister_module_code); diff --git a/src/errors.rs b/src/errors.rs index e465e37..1789e76 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -69,6 +69,12 @@ pub enum Error { #[error("Malformed fqn of contract")] MalformedFqn, + + #[error("src/lib.rs not found.")] + LibRsNotFound, + + #[error("Module {0} already in src/lib.rs")] + ModuleAlreadyInLibRs(String), } impl Error { @@ -96,6 +102,8 @@ impl Error { Error::FailedToGenerateProjectFromTemplate(_) => 19, Error::FailedToParseArgument(_) => 20, Error::MalformedFqn => 21, + Error::LibRsNotFound => 22, + Error::ModuleAlreadyInLibRs(_) => 23, } } diff --git a/src/log.rs b/src/log.rs index 44ec18e..67dd890 100644 --- a/src/log.rs +++ b/src/log.rs @@ -6,7 +6,7 @@ pub fn info>(message: T) { } /// Warning message, not used yet - remove underscore when in use. -pub fn _warn>(message: T) { +pub fn warn>(message: T) { prettycli::warn(message.as_ref()); }