Skip to content

Commit

Permalink
Added source option to generate command.
Browse files Browse the repository at this point in the history
  • Loading branch information
kubaplas committed May 16, 2024
1 parent b98c06d commit 4ad06bd
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 45 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Changelog for `cargo-odra`.
### Added

- Support for defining template of a contract to grab when using `generate` command.
- Ability to list all available templates using `list-templates` command.

### Fixed

Expand Down
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# cargo-odra

A cargo utility that helps to create, manage and test your smart contracts
written using Odra framework.
written using Odra framework.

## Table of Contents

* [Usage](#usage)
* [Commands](#backends)
* [Links](#links)
Expand Down Expand Up @@ -51,10 +52,11 @@ $ cargo odra test -b casper
* `build` - builds the contracts, generates wasm files,
* `test` - runs tests,
* `generate` - generates sample contract,
* `list-templates` - lists available templates,
* `clean` - removes temporary files (builders and wasm files),
* `completions` - generates autocomplete script for given shell

To see exact syntax of each command, type `cargo odra command --help`.
To see exact syntax of each command, type `cargo odra command_name --help`.

## Workspaces

Expand All @@ -74,6 +76,7 @@ $ cargo odra new --name myproject --template workspace && cd myproject
* [Odra docs](https://odra.dev/docs)

## Contact

Write **[email protected]**

<div align="center">
Expand Down
2 changes: 1 addition & 1 deletion justfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
DEVELOPMENT_ODRA_BRANCH := "release/0.9.0"
DEVELOPMENT_ODRA_BRANCH := "release/1.0.0"
BINARYEN_VERSION := "version_116"
BINARYEN_CHECKSUM := "c55b74f3109cdae97490faf089b0286d3bba926bb6ea5ed00c8c784fc53718fd"

Expand Down
11 changes: 9 additions & 2 deletions src/actions/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{
log,
odra_toml::Contract,
paths::{to_camel_case, to_snake_case},
project::Project,
project::{OdraLocation, Project},
template::TemplateGenerator,
};

Expand All @@ -32,11 +32,18 @@ impl<'a> GenerateAction<'a> {
contract_name: String,
module_name: Option<String>,
template_name: Option<String>,
source: Option<String>,
) -> Self {
if project.is_workspace() && module_name.is_none() {
Error::CrateNotProvided.print_and_die();
}

let odra_location = match source {
None => project.project_odra_location(),

Some(_) => OdraLocation::from_source(source),
};

GenerateAction {
project,
contract_name: contract_name.clone(),
Expand All @@ -46,7 +53,7 @@ impl<'a> GenerateAction<'a> {
template_name,
template_generator: TemplateGenerator::new(
ODRA_TEMPLATE_GH_RAW_REPO.to_string(),
project.project_odra_location(),
odra_location,
),
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/actions/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ impl InitAction {
&odra_location,
&cargo_toml_path,
"#odra_modules_dependency",
"odra-modules",
"modules",
);

rename_file(cargo_toml_path, "Cargo.toml");
Expand Down
4 changes: 2 additions & 2 deletions src/actions/list_templates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ impl ListTemplatesAction {
.iter()
.filter(|template| template.template_type == TemplateType::Contract)
.for_each(|template| {
println!("{:<15}{}", template.name, template.description);
println!(" {:<15}{}", template.name, template.description);
});
println!("\nAvailable project templates:");
templates
.iter()
.filter(|template| template.template_type == TemplateType::Project)
.for_each(|template| {
println!("{:<15}{}", template.name, template.description);
println!(" {:<15}{}", template.name, template.description);
});
}
}
5 changes: 5 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ pub struct GenerateCommand {
/// To see all available templates, run `cargo odra list-templates`.
#[clap(value_parser, long, short, default_value = consts::MODULE_TEMPLATE)]
pub template: Option<String>,
/// Odra source to use. By default, it uses version from Cargo.toml,
/// but can be overriden if needed.
#[clap(value_parser, long, short)]
pub source: Option<String>,
}

#[derive(clap::Args, Debug)]
Expand Down Expand Up @@ -175,6 +179,7 @@ pub fn make_action() {
generate.contract_name,
generate.module,
generate.template,
generate.source,
)
.generate_contract();
}
Expand Down
2 changes: 1 addition & 1 deletion src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ impl Project {
}

/// Name of the crate.
/// If there is no subcrate, the project name is returned.
/// If there is no sub-crate, the project name is returned.
pub fn crate_name(&self, module_name: Option<String>) -> String {
match module_name {
None => self.project_crate_name(),
Expand Down
52 changes: 16 additions & 36 deletions src/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,31 +57,15 @@ impl TemplateGenerator {
OdraLocation::Remote(_, branch) => {
let branch = branch.unwrap_or_else(|| "releases/latest".to_string());
let template_path = self.template_path(TEMPLATES_JSON_PATH, branch);
let templates_json = get(&template_path)
.call()
.unwrap_or_else(|_| {
Error::FailedToFetchTemplatesFile(template_path.clone()).print_and_die()
})
.into_string()
.unwrap_or_else(|_| {
Error::FailedToFetchTemplatesFile(template_path.clone()).print_and_die()
});
let templates_json = Self::download_template(&template_path);
serde_json::from_str(&templates_json).unwrap_or_else(|_| {
Error::FailedToParseTemplatesFile(template_path).print_and_die()
})
}
OdraLocation::CratesIO(version) => {
let branch = format!("release/{}", version);
let template_path = self.template_path(TEMPLATES_JSON_PATH, branch);
let templates_json = get(&template_path)
.call()
.unwrap_or_else(|_| {
Error::FailedToFetchTemplatesFile(template_path.clone()).print_and_die()
})
.into_string()
.unwrap_or_else(|_| {
Error::FailedToFetchTemplatesFile(template_path.clone()).print_and_die()
});
let templates_json = Self::download_template(&template_path);
serde_json::from_str(&templates_json).unwrap_or_else(|_| {
Error::FailedToParseTemplatesFile(template_path).print_and_die()
})
Expand Down Expand Up @@ -113,32 +97,28 @@ impl TemplateGenerator {
OdraLocation::Remote(_, branch) => {
let branch = branch.unwrap_or_else(|| "releases/latest".to_string());
let template_path = self.template_path(&template.path, branch);
get(&template_path)
.call()
.unwrap_or_else(|_| {
Error::FailedToFetchTemplate(template_path.clone()).print_and_die()
})
.into_string()
.unwrap_or_else(|_| {
Error::FailedToParseTemplate(template_path.clone()).print_and_die()
})
Self::download_template(&template_path)
}
OdraLocation::CratesIO(version) => {
let branch = format!("release/{}", version);
let template_path = self.template_path(&template.path, branch);
get(&template_path)
.call()
.unwrap_or_else(|_| {
Error::FailedToFetchTemplate(template_path.clone()).print_and_die()
})
.into_string()
.unwrap_or_else(|_| {
Error::FailedToParseTemplate(template_path.clone()).print_and_die()
})
Self::download_template(&template_path)
}
}
}

fn download_template(template_path: &String) -> String {
get(&template_path.clone())
.call()
.unwrap_or_else(|_| {
Error::FailedToFetchTemplate(template_path.to_string()).print_and_die()
})
.into_string()
.unwrap_or_else(|_| {
Error::FailedToParseTemplate(template_path.to_string()).print_and_die()
})
}

/// Returns content of the new module file.
pub fn module_template(
&self,
Expand Down

0 comments on commit 4ad06bd

Please sign in to comment.