Skip to content

Commit

Permalink
Added support for providing a template name when generating
Browse files Browse the repository at this point in the history
a contract.
  • Loading branch information
kubaplas committed May 14, 2024
1 parent 6b4a67c commit efce736
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 8 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@

Changelog for `cargo-odra`.

## [0.1.2] - 2024-XX-XX

### Added

- Support for defining template of a contract to grab when using `generate` command.

### Fixed

- Fixed error that caused contracts to fail to be built when coming from crates with
hyphens in their name.

## [0.1.1] - 2024-02-28

### Added
Expand Down
15 changes: 11 additions & 4 deletions src/actions/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,19 @@ pub struct GenerateAction<'a> {
contract_module_ident: String,
module_root: PathBuf,
module_name: Option<String>,
template_name: Option<String>,
template_generator: TemplateGenerator,
}

/// GenerateAction implementation.
impl<'a> GenerateAction<'a> {
/// Crate a new GenerateAction for a given contract.
pub fn new(project: &'a Project, contract_name: String, module_name: Option<String>) -> Self {
pub fn new(
project: &'a Project,
contract_name: String,
module_name: Option<String>,
template_name: Option<String>,
) -> Self {
if project.is_workspace() && module_name.is_none() {
Error::CrateNotProvided.print_and_die();
}
Expand All @@ -37,6 +43,7 @@ impl<'a> GenerateAction<'a> {
contract_module_ident: to_snake_case(contract_name),
module_root: project.module_root(module_name.clone()),
module_name,
template_name,
template_generator: TemplateGenerator::new(
ODRA_TEMPLATE_GH_RAW_REPO.to_string(),
project.project_odra_location(),
Expand All @@ -49,7 +56,7 @@ impl GenerateAction<'_> {
/// Main function that runs the generation action.
pub fn generate_contract(&self) {
log::info(format!("Adding new contract: {} ...", self.contract_name()));
self.add_contract_file_to_src();
self.add_contract_file_to_src(self.template_name.clone());
self.update_lib_rs();
self.update_odra_toml();
}
Expand Down Expand Up @@ -84,11 +91,11 @@ impl GenerateAction<'_> {
}

/// Crates a new module file in src directory.
fn add_contract_file_to_src(&self) {
fn add_contract_file_to_src(&self, template_name: Option<String>) {
// Rename module name.
let contract_body = self
.template_generator
.module_template(&self.contract_struct_name())
.module_template(&self.contract_struct_name(), template_name)
.unwrap_or_else(|err| err.print_and_die());

// Make sure the file do not exist.
Expand Down
12 changes: 10 additions & 2 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ pub struct GenerateCommand {
/// Name of the module in which the contract will be created.
#[clap(value_parser, long, short)]
pub module: Option<String>,
/// Name of the template to use.
#[clap(value_parser, long, short)]
pub template: Option<String>,
}

#[derive(clap::Args, Debug)]
Expand Down Expand Up @@ -157,8 +160,13 @@ pub fn make_action() {
}
OdraSubcommand::Generate(generate) => {
let project = Project::detect(current_dir);
GenerateAction::new(&project, generate.contract_name, generate.module)
.generate_contract();
GenerateAction::new(
&project,
generate.contract_name,
generate.module,
generate.template,
)
.generate_contract();
}
OdraSubcommand::New(init) => {
InitAction::generate_project(init, current_dir, false);
Expand Down
9 changes: 7 additions & 2 deletions src/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,14 @@ impl TemplateGenerator {
}

/// Returns content of the new module file.
pub fn module_template(&self, module_name: &str) -> Result<String, Error> {
pub fn module_template(
&self,
module_name: &str,
template_name: Option<String>,
) -> Result<String, Error> {
let template_name = template_name.unwrap_or_else(|| MODULE_TEMPLATE.to_string());
Ok(self
.fetch_template(MODULE_TEMPLATE)?
.fetch_template(&template_name)?
.replace("#module_name", module_name))
}

Expand Down

0 comments on commit efce736

Please sign in to comment.