Skip to content

Commit

Permalink
Merge pull request #1198 from multiversx/template-tag
Browse files Browse the repository at this point in the history
Template tags, refactor, improved tests
  • Loading branch information
andrei-marinica authored Aug 18, 2023
2 parents 9e542e4 + e030f91 commit cb907d2
Show file tree
Hide file tree
Showing 23 changed files with 468 additions and 223 deletions.
26 changes: 26 additions & 0 deletions .github/workflows/template-test-current.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: CI

on:
push:
branches:
- master
pull_request:

permissions:
checks: write
pull-requests: write

jobs:
template_test:
name: Template tool test - current (unreleased) templates
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
default: true
toolchain: nightly-2023-08-08
- name: Run template tool test
run: |
cd framework/meta
cargo test --features template-test-current
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ permissions:

jobs:
template_test:
name: Template tool test
name: Template tool test - released templates
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
Expand All @@ -23,4 +23,4 @@ jobs:
- name: Run template tool test
run: |
cd framework/meta
cargo test --features template-test
cargo test --features template-test-released
3 changes: 2 additions & 1 deletion framework/meta/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ name = "sc-meta"
path = "src/main.rs"

[features]
template-test = []
template-test-current = []
template-test-released = []

[dependencies]
tokio = { version = "1.24", features = ["full"] }
Expand Down
8 changes: 8 additions & 0 deletions framework/meta/src/cargo_toml_contents.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ impl CargoTomlContents {
}
}

pub fn has_dependencies(&self) -> bool {
self.toml_value.get(CARGO_TOML_DEPENDENCIES).is_some()
}

pub fn dependencies_mut(&mut self) -> &mut Table {
self.toml_value
.get_mut(CARGO_TOML_DEPENDENCIES)
Expand All @@ -92,6 +96,10 @@ impl CargoTomlContents {
.expect("malformed crate Cargo.toml")
}

pub fn has_dev_dependencies(&self) -> bool {
self.toml_value.get(CARGO_TOML_DEV_DEPENDENCIES).is_some()
}

pub fn dev_dependencies_mut(&mut self) -> &mut Table {
self.toml_value
.get_mut(CARGO_TOML_DEV_DEPENDENCIES)
Expand Down
9 changes: 6 additions & 3 deletions framework/meta/src/cli_args/cli_args_build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ impl CliArgsToRaw for BuildArgs {
raw.push("--no-imports".to_string());
}
if let Some(target_dir_wasm) = &self.target_dir_wasm {
raw.push("--target-dir-wasm".to_string());
// not using --target-dir-wasm, for backward compatibility
raw.push("--target-dir".to_string());
raw.push(target_dir_wasm.clone());
}
if self.twiggy_top {
Expand Down Expand Up @@ -214,7 +215,8 @@ impl CliArgsToRaw for BuildDbgArgs {
fn to_raw(&self) -> Vec<String> {
let mut raw = Vec::new();
if let Some(target_dir_wasm) = &self.target_dir_wasm {
raw.push("--target-dir-wasm".to_string());
// not using --target-dir-wasm, for backward compatibility
raw.push("--target-dir".to_string());
raw.push(target_dir_wasm.clone());
}
if self.twiggy_top {
Expand Down Expand Up @@ -259,7 +261,8 @@ impl CliArgsToRaw for TwiggyArgs {
fn to_raw(&self) -> Vec<String> {
let mut raw = Vec::new();
if let Some(target_dir) = &self.target_dir_wasm {
raw.push("--target-dir-wasm".to_string());
// not using --target-dir-wasm, for backward compatibility
raw.push("--target-dir".to_string());
raw.push(target_dir.clone());
}
raw
Expand Down
29 changes: 23 additions & 6 deletions framework/meta/src/cli_args/cli_args_standalone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ pub enum StandaloneCliAction {
name = "templates",
about = "Creates a contract by a pre-existing template"
)]
TemplateList,
TemplateList(TemplateListArgs),
#[command(
name = "test-gen",
about = "Generates Rust integration tests based on scenarios provided in the scenarios folder of each contract."
Expand Down Expand Up @@ -184,13 +184,23 @@ pub struct LocalDepsArgs {

#[derive(Default, Clone, PartialEq, Eq, Debug, Args)]
pub struct TemplateArgs {
/// Provide the template you want to clone
#[arg(long = "name", verbatim_doc_comment)]
pub name: PathBuf,
/// The new name the contract is to receive.
/// If missing, the template name will be considered.
#[arg(long, verbatim_doc_comment)]
pub name: Option<String>,

/// Provide the he template you want to clone
#[arg(long = "template", verbatim_doc_comment)]
/// The contract template to clone.
#[arg(long, verbatim_doc_comment)]
pub template: String,

/// The framework version on which the contracts should be created.
#[arg(long, verbatim_doc_comment)]
pub tag: Option<String>,

/// Target directory where to create the new contract directory.
/// Will be current directory if not specified.
#[arg(long, verbatim_doc_comment)]
pub path: Option<PathBuf>,
}

impl CliArgsToRaw for TemplateArgs {
Expand All @@ -199,6 +209,13 @@ impl CliArgsToRaw for TemplateArgs {
}
}

#[derive(Default, Clone, PartialEq, Eq, Debug, Args)]
pub struct TemplateListArgs {
/// The framework version referred to.
#[arg(long = "tag", verbatim_doc_comment)]
pub tag: Option<String>,
}

#[derive(Default, Clone, PartialEq, Eq, Debug, Args)]
pub struct TestGenArgs {
/// Target directory where to generate contract integration tests.
Expand Down
8 changes: 4 additions & 4 deletions framework/meta/src/cmd/standalone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub(crate) mod upgrade;

use crate::{
cli_args::{StandaloneCliAction, StandaloneCliArgs},
template::{print_template_names, template_download},
template::{create_contract, print_template_names},
};
use all::call_all_meta;
use clap::Parser;
Expand All @@ -28,10 +28,10 @@ pub async fn cli_main_standalone() {
local_deps(args);
},
Some(StandaloneCliAction::Template(args)) => {
template_download(args).await;
create_contract(args).await;
},
Some(StandaloneCliAction::TemplateList) => {
print_template_names().await;
Some(StandaloneCliAction::TemplateList(args)) => {
print_template_names(args).await;
},
Some(StandaloneCliAction::TestGen(args)) => {
test_gen_tool(args);
Expand Down
5 changes: 3 additions & 2 deletions framework/meta/src/cmd/standalone/info.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use super::upgrade::{print_tree_dir_metadata, DEFAULT_LAST_VERSION};
use super::upgrade::print_tree_dir_metadata;
use crate::{
cli_args::InfoArgs,
folder_structure::{dir_pretty_print, RelevantDirectories},
version_history::LAST_UPGRADE_VERSION,
};

pub fn call_info(args: &InfoArgs) {
Expand All @@ -13,6 +14,6 @@ pub fn call_info(args: &InfoArgs) {

let dirs = RelevantDirectories::find_all(path, args.ignore.as_slice());
dir_pretty_print(dirs.iter(), "", &|dir| {
print_tree_dir_metadata(dir, DEFAULT_LAST_VERSION)
print_tree_dir_metadata(dir, LAST_UPGRADE_VERSION)
});
}
2 changes: 0 additions & 2 deletions framework/meta/src/cmd/standalone/upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ mod upgrade_0_39;
pub(crate) mod upgrade_common;
mod upgrade_print;
mod upgrade_selector;
mod upgrade_versions;

pub use upgrade_print::print_tree_dir_metadata;
pub use upgrade_selector::upgrade_sc;
pub use upgrade_versions::DEFAULT_LAST_VERSION;
4 changes: 2 additions & 2 deletions framework/meta/src/cmd/standalone/upgrade/upgrade_selector.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::{
cli_args::UpgradeArgs,
folder_structure::{dir_pretty_print, RelevantDirectories, RelevantDirectory},
version_history::{versions_iter, LAST_UPGRADE_VERSION, VERSIONS},
};

use super::{
Expand All @@ -9,7 +10,6 @@ use super::{
upgrade_0_39::{postprocessing_after_39_0, upgrade_to_39_0},
upgrade_common::{cargo_check, version_bump_in_cargo_toml},
upgrade_print::*,
upgrade_versions::{versions_iter, DEFAULT_LAST_VERSION, VERSIONS},
};

pub fn upgrade_sc(args: &UpgradeArgs) {
Expand All @@ -22,7 +22,7 @@ pub fn upgrade_sc(args: &UpgradeArgs) {
let last_version = args
.override_target_version
.clone()
.unwrap_or_else(|| DEFAULT_LAST_VERSION.to_string());
.unwrap_or_else(|| LAST_UPGRADE_VERSION.to_string());

assert!(
VERSIONS.contains(&last_version.as_str()),
Expand Down
1 change: 1 addition & 0 deletions framework/meta/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ mod mxsc_file_json;
mod print_util;
pub mod template;
mod tools;
pub mod version_history;

#[macro_use]
extern crate lazy_static;
Expand Down
12 changes: 9 additions & 3 deletions framework/meta/src/template.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
mod contract_creator;
mod contract_creator_target;
mod repo_source;
mod repo_temp_download;
mod repo_version;
mod template_adjuster;
mod template_download;
mod template_list;
mod template_metadata;
mod template_source;

pub use repo_temp_download::{RepoSource, RepoTempDownload};
pub use contract_creator::{create_contract, ContractCreator};
pub use contract_creator_target::ContractCreatorTarget;
pub use repo_source::RepoSource;
pub use repo_temp_download::RepoTempDownload;
pub use repo_version::RepoVersion;
pub use template_adjuster::TemplateAdjuster;
pub use template_download::{template_download, TemplateDownloader};
pub use template_list::{print_template_names, template_names_from_repo};
92 changes: 92 additions & 0 deletions framework/meta/src/template/contract_creator.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
use crate::{
cli_args::TemplateArgs,
version_history::{validate_template_tag, LAST_TEMPLATE_VERSION},
};

use super::{
template_source::{template_sources, TemplateSource},
ContractCreatorTarget, RepoSource, RepoVersion, TemplateAdjuster,
};

/// Creates a new contract on disk, from a template, given a name.
pub async fn create_contract(args: &TemplateArgs) {
let version = get_repo_version(&args.tag);
let repo_temp_download = RepoSource::download_from_github(version, std::env::temp_dir()).await;
let target = target_from_args(args);

let creator = ContractCreator::new(&repo_temp_download, args.template.clone(), target, false);

creator.create_contract();
}

fn target_from_args(args: &TemplateArgs) -> ContractCreatorTarget {
let new_name = args.name.clone().unwrap_or_else(|| args.template.clone());
let target_path = args.path.clone().unwrap_or_default();
ContractCreatorTarget {
target_path,
new_name,
}
}

pub(crate) fn get_repo_version(args_tag: &Option<String>) -> RepoVersion {
if let Some(tag) = args_tag {
assert!(validate_template_tag(tag), "invalid template tag");
RepoVersion::Tag(tag.clone())
} else {
RepoVersion::Tag(LAST_TEMPLATE_VERSION.to_string())
}
}

/// Coordinates the creation of a new contract from a template.
pub struct ContractCreator<'a> {
pub repo_source: &'a RepoSource,
pub template_source: TemplateSource<'a>,
pub target: ContractCreatorTarget,
pub adjuster: TemplateAdjuster,
}

impl<'a> ContractCreator<'a> {
pub fn new(
repo_source: &'a RepoSource,
template_name: String,
target: ContractCreatorTarget,
keep_paths: bool,
) -> Self {
let template_sources = template_sources(repo_source);
let template_source = template_sources
.into_iter()
.find(|source| source.metadata.name == template_name)
.unwrap_or_else(|| panic!("Unknown template {template_name}"));

let metadata = template_source.metadata.clone();
ContractCreator {
repo_source,
template_source,
target: target.clone(),
adjuster: TemplateAdjuster {
metadata,
target,
keep_paths,
},
}
}

pub fn create_contract(&self) {
self.copy_template();
self.update_dependencies();
self.rename_template();
}

pub fn copy_template(&self) {
self.template_source
.copy_template(self.target.contract_dir());
}

pub fn update_dependencies(&self) {
self.adjuster.update_dependencies();
}

pub fn rename_template(&self) {
self.adjuster.rename_template_to();
}
}
13 changes: 13 additions & 0 deletions framework/meta/src/template/contract_creator_target.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use std::path::PathBuf;

#[derive(Clone)]
pub struct ContractCreatorTarget {
pub target_path: PathBuf,
pub new_name: String,
}

impl ContractCreatorTarget {
pub fn contract_dir(&self) -> PathBuf {
self.target_path.join(&self.new_name)
}
}
31 changes: 31 additions & 0 deletions framework/meta/src/template/repo_source.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use std::{
fs,
path::{Path, PathBuf},
};

use super::{RepoTempDownload, RepoVersion};

pub enum RepoSource {
Downloaded(RepoTempDownload),
LocalPath(PathBuf),
}

impl RepoSource {
pub async fn download_from_github(version: RepoVersion, temp_dir_path: PathBuf) -> Self {
fs::create_dir_all(&temp_dir_path).unwrap();
RepoSource::Downloaded(RepoTempDownload::download_from_github(version, temp_dir_path).await)
}

pub fn from_local_path(repo_local_path: impl AsRef<Path>) -> Self {
RepoSource::LocalPath(repo_local_path.as_ref().to_path_buf())
}

pub fn repo_path(&self) -> PathBuf {
match self {
RepoSource::Downloaded(repo_temp_download) => {
repo_temp_download.repository_temp_dir_path()
},
RepoSource::LocalPath(local_path) => local_path.clone(),
}
}
}
Loading

0 comments on commit cb907d2

Please sign in to comment.