Skip to content

Commit

Permalink
Update wasm crate manually for version < 0.45.1.
Browse files Browse the repository at this point in the history
  • Loading branch information
BiancaIalangi committed Dec 13, 2023
1 parent eddca35 commit 2ff60c4
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 9 deletions.
11 changes: 6 additions & 5 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,9 +72,9 @@ impl<'a> ContractCreator<'a> {
}
}

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

Expand All @@ -82,8 +83,8 @@ impl<'a> ContractCreator<'a> {
.copy_template(self.target.contract_dir());
}

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

pub fn rename_template(&self) {
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(),
}
}
}
30 changes: 29 additions & 1 deletion framework/meta/src/cmd/standalone/template/template_adjuster.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,25 @@ use crate::{
};
use convert_case::{Case, Casing};
use ruplacer::Query;
use rustc_version::Version;
use toml::value::Table;

const TEST_DIRECTORY: &str = "./tests";
const ROOT_CARGO_TOML: &str = "./Cargo.toml";
const META_CARGO_TOML: &str = "./meta/Cargo.toml";
const WASM_CARGO_TOML: &str = "./wasm/Cargo.toml";
const TARGET_VERSION_0_45_1: Version = Version::new(0, 45, 1);

pub struct TemplateAdjuster {
pub metadata: TemplateMetadata,
pub target: ContractCreatorTarget,
pub keep_paths: bool,
}
impl TemplateAdjuster {
pub fn update_dependencies(&self) {
pub fn update_dependencies(&self, args_tag: String) {
self.update_dependencies_root();
self.update_dependencies_meta();
self.update_dependencies_wasm(args_tag);
}

fn update_dependencies_root(&self) {
Expand All @@ -46,6 +50,23 @@ impl TemplateAdjuster {
toml.save_to_file(&cargo_toml_path);
}

fn update_dependencies_wasm(&self, args_tag: String) {
if is_version_at_least_0_45_1(args_tag) {
println!(">>>>>>>>>>>>YES");
return;
}
println!(">>>>>>>>>>>>YESSSSSSS");

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

if !self.keep_paths {
remove_paths_from_deps(&mut toml, &[&self.metadata.name]);
}

toml.save_to_file(&cargo_toml_path);
}

pub fn rename_template_to(&self) {
self.rename_trait_to();
self.rename_in_cargo_toml_root();
Expand Down Expand Up @@ -177,6 +198,13 @@ impl TemplateAdjuster {
}
}

fn is_version_at_least_0_45_1(args_tag: String) -> bool {
match Version::parse(&args_tag) {
Ok(version) => version >= TARGET_VERSION_0_45_1,
Err(_error) => false,
}
}

fn wasm_file_name(name: &str) -> String {
format!("{name}.wasm",)
}
Expand Down
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

0 comments on commit 2ff60c4

Please sign in to comment.