Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove wasm renaming in template #1276

Merged
merged 9 commits into from
Dec 18, 2023
1 change: 0 additions & 1 deletion contracts/examples/adder/mxsc-template.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ files_include = [
"scenarios",
"src",
"tests",
"wasm/Cargo.toml",
"Cargo.toml",
"README.md",
"multiversx.json",
Expand Down
1 change: 0 additions & 1 deletion contracts/examples/crypto-zombies/mxsc-template.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ files_include = [
"scenarios",
"src",
"tests",
"wasm/Cargo.toml",
"Cargo.toml",
"multiversx.json",
]
1 change: 0 additions & 1 deletion contracts/examples/empty/mxsc-template.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ files_include = [
"scenarios",
"src",
"tests",
"wasm/Cargo.toml",
"Cargo.toml",
"multiversx.json",
]
1 change: 0 additions & 1 deletion contracts/examples/ping-pong-egld/mxsc-template.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ files_include = [
"scenarios",
"src",
"tests",
"wasm/Cargo.toml",
"Cargo.toml",
"README.md",
"multiversx.json",
Expand Down
17 changes: 9 additions & 8 deletions framework/meta/src/cmd/standalone/template/contract_creator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ use super::{
/// Creates a new contract on disk, from a template, given a name.
pub fn create_contract(args: &TemplateArgs) {
let version = get_repo_version(&args.tag);
let version_tag: String = version.get_tag();
let repo_temp_download = RepoSource::download_from_github(version, std::env::temp_dir());
let target = target_from_args(args);

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

creator.create_contract();
creator.create_contract(version_tag);
}

fn target_from_args(args: &TemplateArgs) -> ContractCreatorTarget {
Expand Down Expand Up @@ -71,19 +72,19 @@ impl<'a> ContractCreator<'a> {
}
}

pub fn create_contract(&self) {
self.copy_template();
self.update_dependencies();
pub fn create_contract(&self, args_tag: String) {
self.copy_template(&args_tag);
self.update_dependencies(&args_tag);
self.rename_template();
}

pub fn copy_template(&self) {
pub fn copy_template(&self, args_tag: &str) {
self.template_source
.copy_template(self.target.contract_dir());
.copy_template(self.target.contract_dir(), args_tag);
}

pub fn update_dependencies(&self) {
self.adjuster.update_dependencies();
pub fn update_dependencies(&self, args_tag: &str) {
self.adjuster.update_dependencies(args_tag);
}

pub fn rename_template(&self) {
Expand Down
17 changes: 15 additions & 2 deletions framework/meta/src/cmd/standalone/template/copy_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,28 @@ use std::{
path::{Path, PathBuf},
};

use crate::version_history::validate_template_with_autogenerated_json;

/// Will copy an entire folder according to a whitelist of allowed paths.
///
/// The whitelist is expected to contain paths relative from the source path.
///
/// If a folder is whitelisted, then everything in the folder is considered whitelisted too.
///
/// The function creates all necessary folder structure in the target, no additional preparation required.
pub fn whitelisted_deep_copy(source_root: &Path, target_root: &Path, whitelist: &[String]) {
perform_file_copy(source_root, &PathBuf::new(), target_root, whitelist);
pub fn whitelisted_deep_copy(
source_root: &Path,
target_root: &Path,
whitelist: &[String],
args_tag: &str,
) {
if validate_template_with_autogenerated_json(args_tag) {
perform_file_copy(source_root, &PathBuf::new(), target_root, whitelist);
} else {
let mut tmp_whitelist = whitelist.to_vec();
tmp_whitelist.push("multiversx.json".into());
perform_file_copy(source_root, &PathBuf::new(), target_root, &tmp_whitelist);
}
}

fn is_whitelisted(path: &Path, whitelist: &[String]) -> bool {
Expand Down
9 changes: 9 additions & 0 deletions framework/meta/src/cmd/standalone/template/repo_version.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use crate::version_history::LAST_TEMPLATE_VERSION;

pub enum RepoVersion {
Master,
Tag(String),
Expand All @@ -23,4 +25,11 @@ impl RepoVersion {
},
}
}

pub fn get_tag(&self) -> String {
match self {
RepoVersion::Master => LAST_TEMPLATE_VERSION.to_string(),
RepoVersion::Tag(tag) => tag.to_string(),
}
}
}
11 changes: 8 additions & 3 deletions framework/meta/src/cmd/standalone/template/template_adjuster.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use super::{template_metadata::TemplateMetadata, ContractCreatorTarget};
use crate::{
cmd::standalone::upgrade::upgrade_common::{rename_files, replace_in_files},
version_history::validate_template_with_autogenerated_wasm,
CargoTomlContents,
};
use convert_case::{Case, Casing};
Expand All @@ -18,10 +19,10 @@ pub struct TemplateAdjuster {
pub keep_paths: bool,
}
impl TemplateAdjuster {
pub fn update_dependencies(&self) {
pub fn update_dependencies(&self, args_tag: &str) {
self.update_dependencies_root();
self.update_dependencies_wasm();
andrei-marinica marked this conversation as resolved.
Show resolved Hide resolved
self.update_dependencies_meta();
self.update_dependencies_wasm(args_tag);
}

fn update_dependencies_root(&self) {
Expand All @@ -48,7 +49,11 @@ impl TemplateAdjuster {
toml.save_to_file(&cargo_toml_path);
}

fn update_dependencies_wasm(&self) {
fn update_dependencies_wasm(&self, args_tag: &str) {
if validate_template_with_autogenerated_wasm(args_tag) {
return;
}

let cargo_toml_path = self.target.contract_dir().join(WASM_CARGO_TOML);
let mut toml = CargoTomlContents::load_from_file(&cargo_toml_path);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ pub struct TemplateSource<'a> {
}

impl<'a> TemplateSource<'a> {
pub fn copy_template(&self, target_path: impl AsRef<Path>) {
pub fn copy_template(&self, target_path: impl AsRef<Path>, args_tag: &str) {
whitelisted_deep_copy(
&self.source_path,
target_path.as_ref(),
&self.metadata.files_include,
args_tag,
);
}
}
Expand Down
39 changes: 37 additions & 2 deletions framework/meta/src/version_history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,27 @@ pub fn template_versions() -> &'static [&'static str] {
}

pub fn validate_template_tag(tag: &str) -> bool {
let versions = template_versions();
versions.iter().any(|&tt| tt == tag)
template_versions().iter().any(|&tt| tt == tag)
}

pub fn template_versions_with_autogenerated_wasm() -> &'static [&'static str] {
&VERSIONS[40..]
}

pub fn validate_template_with_autogenerated_wasm(tag: &str) -> bool {
template_versions_with_autogenerated_wasm()
.iter()
.any(|&tt| tt == tag)
}

pub fn template_versions_with_autogenerated_json() -> &'static [&'static str] {
&VERSIONS[39..]
}

pub fn validate_template_with_autogenerated_json(tag: &str) -> bool {
template_versions_with_autogenerated_json()
.iter()
.any(|&tt| tt == tag)
}

pub struct VersionIterator {
Expand Down Expand Up @@ -115,4 +134,20 @@ pub mod tests {
assert!(validate_template_tag("0.43.0"));
assert!(!validate_template_tag("0.42.0"));
}

#[test]
fn template_versions_with_autogenerated_wasm_test() {
assert_eq!(template_versions_with_autogenerated_wasm()[0], "0.45.0");

assert!(validate_template_with_autogenerated_wasm("0.45.0"));
assert!(!validate_template_with_autogenerated_wasm("0.44.0"));
}

#[test]
fn template_versions_with_autogenerated_json_test() {
assert_eq!(template_versions_with_autogenerated_json()[0], "0.44.0");

assert!(validate_template_with_autogenerated_json("0.44.0"));
assert!(!validate_template_with_autogenerated_json("0.43.0"));
}
}
6 changes: 3 additions & 3 deletions framework/meta/tests/template_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use multiversx_sc_meta::{
template_names_from_repo, ContractCreator, ContractCreatorTarget, RepoSource, RepoVersion,
},
find_workspace::find_current_workspace,
version_history,
version_history::{self, LAST_TEMPLATE_VERSION},
};

const TEMPLATE_TEMP_DIR_NAME: &str = "template-test";
Expand Down Expand Up @@ -72,7 +72,7 @@ fn template_test_current(template_name: &str, sub_path: &str, new_name: &str) {
target.clone(),
true,
)
.create_contract();
.create_contract(LAST_TEMPLATE_VERSION.to_string());

if BUILD_CONTRACTS {
build_contract(&target);
Expand Down Expand Up @@ -127,7 +127,7 @@ fn template_test_released(template_name: &str, new_name: &str) {
target.clone(),
false,
)
.create_contract();
.create_contract(LAST_TEMPLATE_VERSION.to_string());

if BUILD_CONTRACTS {
build_contract(&target);
Expand Down