Skip to content

Commit

Permalink
sc-meta upgrade support for more releases, refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
andrei-marinica committed Jan 23, 2023
1 parent 9028a27 commit a777bcb
Show file tree
Hide file tree
Showing 11 changed files with 207 additions and 53 deletions.
17 changes: 15 additions & 2 deletions framework/meta/src/folder_structure/relevant_directory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub enum DirectoryType {
pub struct RelevantDirectory {
pub path: PathBuf,
pub version: VersionReq,
pub upgrade_in_progress: Option<(&'static str, &'static str)>,
pub dir_type: DirectoryType,
}

Expand Down Expand Up @@ -86,11 +87,22 @@ impl RelevantDirectories {
.filter(move |dir| dir.version.semver == version)
}

/// Operates no changes on the disk. Only changes this structure in memory.
pub fn update_versions_in_memory(&mut self, from_version: &str, to_version: &str) {
/// Marks all appropriate directories as ready for upgrade.
pub fn start_upgrade(&mut self, from_version: &'static str, to_version: &'static str) {
for dir in self.0.iter_mut() {
if dir.version.semver == from_version {
dir.upgrade_in_progress = Some((from_version, to_version));
}
}
}

/// Updates the versions of all directories being upgraded (in memory)
/// and resets upgrade status.
pub fn finish_upgrade(&mut self) {
for dir in self.0.iter_mut() {
if let Some((_, to_version)) = &dir.upgrade_in_progress {
dir.version.semver = to_version.to_string();
dir.upgrade_in_progress = None;
}
}
}
Expand Down Expand Up @@ -118,6 +130,7 @@ fn populate_directories(path: &Path, ignore: &[String], result: &mut Vec<Relevan
result.push(RelevantDirectory {
path: path.to_owned(),
version,
upgrade_in_progress: None,
dir_type,
});
}
Expand Down
2 changes: 1 addition & 1 deletion framework/meta/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ mod meta_abi;
mod meta_all;
mod meta_cli;
mod meta_config;
mod meta_info;
mod meta_validate_abi;
mod meta_wasm_tools;
pub mod output_contract;
mod sc_upgrade;
mod meta_info;

pub use cargo_toml_contents::CargoTomlContents;
pub use meta_cli::{cli_main, cli_main_standalone, multi_contract_config};
6 changes: 3 additions & 3 deletions framework/meta/src/meta_info.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::folder_structure::dir_pretty_print;
use crate::sc_upgrade::DEFAULT_LAST_VERSION;
use crate::{
cli_args::InfoArgs, folder_structure::RelevantDirectories, sc_upgrade::print_tree_dir_metadata,
cli_args::InfoArgs,
folder_structure::{dir_pretty_print, RelevantDirectories},
sc_upgrade::{print_tree_dir_metadata, DEFAULT_LAST_VERSION},
};

pub fn call_info(args: &InfoArgs) {
Expand Down
2 changes: 2 additions & 0 deletions framework/meta/src/sc_upgrade/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
mod upgrade_0_31;
mod upgrade_0_32;
mod upgrade_0_39;
mod upgrade_common;
mod upgrade_print;
Expand Down
20 changes: 20 additions & 0 deletions framework/meta/src/sc_upgrade/upgrade_0_31.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use super::upgrade_common::{replace_in_files, version_bump_in_cargo_toml};
use crate::folder_structure::RelevantDirectory;
use ruplacer::Query;
use std::path::Path;

/// Migrate `0.30` to `0.31.0`, including the version bump.
pub fn upgrade_to_31_0(dir: &RelevantDirectory) {
v_0_31_replace_in_files(dir.path.as_ref());

let (from_version, to_version) = dir.upgrade_in_progress.unwrap();
version_bump_in_cargo_toml(&dir.path, from_version, to_version);
}

fn v_0_31_replace_in_files(sc_crate_path: &Path) {
replace_in_files(
sc_crate_path,
"*rs",
&[Query::substring("#[var_args]", "")][..],
);
}
23 changes: 23 additions & 0 deletions framework/meta/src/sc_upgrade/upgrade_0_32.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use super::upgrade_common::{replace_in_files, version_bump_in_cargo_toml};
use crate::folder_structure::RelevantDirectory;
use ruplacer::Query;
use std::path::Path;

/// Migrate `0.30` to `0.31.0`, including the version bump.
pub fn upgrade_to_32_0(dir: &RelevantDirectory) {
v_0_32_replace_in_files(dir.path.as_ref());

let (from_version, to_version) = dir.upgrade_in_progress.unwrap();
version_bump_in_cargo_toml(&dir.path, from_version, to_version);
}

fn v_0_32_replace_in_files(sc_crate_path: &Path) {
replace_in_files(
sc_crate_path,
"*rs",
&[Query::substring(
"TokenIdentifier::egld()",
"EgldOrEsdtTokenIdentifier::egld()",
)][..],
);
}
32 changes: 9 additions & 23 deletions framework/meta/src/sc_upgrade/upgrade_0_39.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use crate::{
folder_structure::{DirectoryType, RelevantDirectory},
CargoTomlContents,
};
use colored::Colorize;
use ruplacer::Query;
use toml::{value::Table, Value};

Expand All @@ -28,11 +27,13 @@ pub fn upgrade_to_39_0(dir: &RelevantDirectory) {
}
v_0_39_replace_in_files(&dir.path);
rename_files(dir.path.as_ref(), SCENARIO_FILE_PATTERNS);
version_bump_in_cargo_toml(&dir.path, "0.38.0", "0.39.0");

let (from_version, to_version) = dir.upgrade_in_progress.unwrap();
version_bump_in_cargo_toml(&dir.path, from_version, to_version);
}

/// Post-processing: re-generate the wasm crates.
pub fn postprocessing_after_39_1(dir: &RelevantDirectory) {
pub fn postprocessing_after_39_0(dir: &RelevantDirectory) {
if dir.dir_type != DirectoryType::Contract {
return;
}
Expand All @@ -50,25 +51,13 @@ fn v_0_39_prepare_meta(sc_crate_path: &Path) {
let mut meta_cargo_toml = CargoTomlContents::load_from_file(&cargo_toml_path);
let deps = meta_cargo_toml.dependencies_mut();

println!(
"{}/dependencies/{}",
cargo_toml_path.as_path().display(),
"elrond-wasm".red().strikethrough(),
);
print_cargo_dep_remove(cargo_toml_path.as_path(), "elrond-wasm");
deps.remove("elrond-wasm");

println!(
"{}/dependencies/{}",
cargo_toml_path.as_path().display(),
"elrond-wasm-debug".red().strikethrough(),
);
print_cargo_dep_remove(cargo_toml_path.as_path(), "elrond-wasm-debug");
deps.remove("elrond-wasm-debug");

println!(
"{}/dependencies/{}",
cargo_toml_path.as_path().display(),
"multiversx-sc-meta".green(),
);
print_cargo_dep_add(cargo_toml_path.as_path(), "multiversx-sc-meta");
let mut meta_dep = Table::new();
meta_dep.insert("version".to_string(), Value::String("0.39.0".to_string()));
deps.insert("multiversx-sc-meta".to_string(), Value::Table(meta_dep));
Expand All @@ -86,11 +75,7 @@ fn v_0_39_prepare_wasm(sc_crate_path: &Path) {
let mut meta_cargo_toml = CargoTomlContents::load_from_file(&cargo_toml_path);
let deps = meta_cargo_toml.dependencies_mut();

println!(
"{}/dependencies/{}",
cargo_toml_path.as_path().display(),
"elrond-wasm-output".red().strikethrough(),
);
print_cargo_dep_remove(cargo_toml_path.as_path(), "elrond-wasm-output");
deps.remove("elrond-wasm-output");

meta_cargo_toml.save_to_file(&cargo_toml_path);
Expand Down Expand Up @@ -132,6 +117,7 @@ fn v_0_39_replace_in_files(sc_crate_path: &Path) {
Query::substring("BlockchainMock", "ScenarioWorld"),
Query::substring("testing_framework", "whitebox"),
Query::substring("tx_mock", "whitebox"),
Query::substring("register_contract_builder", "register_contract"),
][..],
);
}
23 changes: 22 additions & 1 deletion framework/meta/src/sc_upgrade/upgrade_common.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
use ruplacer::{Console, DirectoryPatcher, Query, Settings};
use std::{fs, path::Path};
use std::{
fs,
path::Path,
process::{self, Command},
};
use toml::Value;

use crate::{
Expand Down Expand Up @@ -191,3 +195,20 @@ pub fn re_generate_wasm_crate(dir: &RelevantDirectory) {
&["abi".to_string(), "--no-abi-git-version".to_string()],
);
}

pub fn cargo_check(dir: &RelevantDirectory) {
print_cargo_check(dir);

let result = Command::new("cargo")
.current_dir(&dir.path)
.args(["check", "--tests"])
.spawn()
.expect("failed to spawn cargo check process")
.wait()
.expect("cargo check was not running");

if !result.success() {
print_cargo_check_fail();
process::exit(1);
}
}
68 changes: 59 additions & 9 deletions framework/meta/src/sc_upgrade/upgrade_print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,30 @@ use crate::folder_structure::{
use colored::Colorize;
use std::path::Path;

pub fn print_upgrading(dir: &RelevantDirectory, from_version: &str, to_version: &str) {
println!(
"\n{}",
format!(
"Upgrading from {from_version} to {to_version} in {}\n",
dir.path.display(),
)
.purple()
);
pub fn print_upgrading(dir: &RelevantDirectory) {
if let Some((from_version, to_version)) = dir.upgrade_in_progress {
println!(
"\n{}",
format!(
"Upgrading from {from_version} to {to_version} in {}\n",
dir.path.display(),
)
.purple()
);
}
}

pub fn print_post_processing(dir: &RelevantDirectory) {
if let Some((from_version, to_version)) = dir.upgrade_in_progress {
println!(
"\n{}",
format!(
"Post-processing after upgrade from {from_version} to {to_version} in {}\n",
dir.path.display(),
)
.purple()
);
}
}

pub fn print_upgrading_all(from_version: &str, to_version: &str) {
Expand Down Expand Up @@ -69,3 +84,38 @@ pub fn print_tree_dir_metadata(dir: &RelevantDirectory, last_version: &str) {
print!(" {}", version_string.red());
};
}

pub fn print_cargo_dep_remove(path: &Path, dep_name: &str) {
println!(
"{}/dependencies/{}",
path.display(),
dep_name.red().strikethrough(),
);
}

pub fn print_cargo_dep_add(path: &Path, dep_name: &str) {
println!(
"{}/dependencies/{}",
path.display(),
dep_name.red().strikethrough(),
);
}

pub fn print_cargo_check(dir: &RelevantDirectory) {
println!(
"\n{}",
format!(
"Running cargo check after upgrading to version {} in {}\n",
dir.version.semver,
dir.path.display(),
)
.purple()
);
}

pub fn print_cargo_check_fail() {
let message =
"Automatic upgrade failed to fix all issues. Fix them manually, make `cargo check` pass, then continue automatic upgrade!"
.red();
println!("\n{message}");
}
50 changes: 36 additions & 14 deletions framework/meta/src/sc_upgrade/upgrade_selector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ use crate::{
cli_args::UpgradeArgs,
folder_structure::{dir_pretty_print, RelevantDirectories, RelevantDirectory},
sc_upgrade::{
upgrade_0_39::{postprocessing_after_39_1, upgrade_to_39_0},
upgrade_0_39::{postprocessing_after_39_0, upgrade_to_39_0},
upgrade_common::version_bump_in_cargo_toml,
upgrade_print::*,
upgrade_versions::{versions_iter, DEFAULT_LAST_VERSION, VERSIONS},
},
};

use super::{
upgrade_0_31::upgrade_to_31_0, upgrade_0_32::upgrade_to_32_0, upgrade_common::cargo_check,
};

pub fn upgrade_sc(args: &UpgradeArgs) {
let path = if let Some(some_path) = &args.path {
some_path.as_str()
Expand Down Expand Up @@ -42,37 +46,55 @@ pub fn upgrade_sc(args: &UpgradeArgs) {
}

print_upgrading_all(from_version, to_version);
dirs.start_upgrade(from_version, to_version);
for dir in dirs.iter_version(from_version) {
print_upgrading(dir, from_version, to_version);
upgrade_function_selector(dir, from_version, to_version);
upgrade_function_selector(dir);
}

for dir in dirs.iter_version(from_version) {
upgrade_post_processing(dir);
}

// change the version in memory for the next iteration (dirs is not reloaded from disk)
dirs.update_versions_in_memory(from_version, to_version);
// // change the version in memory for the next iteration (dirs is not reloaded from disk)
// dirs.update_versions_in_memory(from_version, to_version);
dirs.finish_upgrade();
}
}

fn upgrade_function_selector(dir: &RelevantDirectory, from_version: &str, to_version: &str) {
#[allow(clippy::single_match)]
match dir.version.semver.as_str() {
"0.38.0" => {
fn upgrade_function_selector(dir: &RelevantDirectory) {
if dir.upgrade_in_progress.is_some() {
print_upgrading(dir);
}

match dir.upgrade_in_progress {
Some((_, "0.31.0")) => {
upgrade_to_31_0(dir);
},
Some((_, "0.32.0")) => {
upgrade_to_32_0(dir);
},
Some((_, "0.38.0")) => {
upgrade_to_39_0(dir);
},
_ => {
Some((from_version, to_version)) => {
version_bump_in_cargo_toml(&dir.path, from_version, to_version);
},
None => {},
}
}

fn upgrade_post_processing(dir: &RelevantDirectory) {
#[allow(clippy::single_match)]
match dir.version.semver.as_str() {
"0.39.0" => {
postprocessing_after_39_1(dir);
match dir.upgrade_in_progress {
Some((_, "0.28.0")) | Some((_, "0.29.0")) | Some((_, "0.30.0")) | Some((_, "0.31.0"))
| Some((_, "0.32.0")) | Some((_, "0.33.0")) | Some((_, "0.34.0")) | Some((_, "0.35.0"))
| Some((_, "0.36.0")) | Some((_, "0.37.0")) => {
print_post_processing(dir);
cargo_check(dir);
},
Some((_, "0.39.0")) => {
print_post_processing(dir);
postprocessing_after_39_0(dir);
cargo_check(dir);
},
_ => {},
}
Expand Down
Loading

0 comments on commit a777bcb

Please sign in to comment.