Skip to content

Commit

Permalink
Merge pull request #1266 from multiversx/generate-full-cargo
Browse files Browse the repository at this point in the history
Generate full cargo
  • Loading branch information
andrei-marinica authored Nov 16, 2023
2 parents 88843dd + b2ccfa8 commit 6b125fc
Show file tree
Hide file tree
Showing 25 changed files with 411 additions and 68 deletions.
1 change: 0 additions & 1 deletion contracts/benchmarks/large-storage/wasm/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
[package]
name = "large-storage-wasm"
version = "0.0.0"
authors = ["Andrei Marinica <[email protected]>"]
edition = "2021"
publish = false

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
[package]
name = "linked-list-repeat-wasm"
version = "0.0.0"
authors = ["Claudiu-Marcel Bruda <[email protected]>"]
edition = "2021"
publish = false

Expand Down
1 change: 0 additions & 1 deletion contracts/benchmarks/mappers/map-repeat/wasm/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
[package]
name = "map-repeat-wasm"
version = "0.0.0"
authors = ["Claudiu-Marcel Bruda <[email protected]>"]
edition = "2021"
publish = false

Expand Down
1 change: 0 additions & 1 deletion contracts/benchmarks/mappers/queue-repeat/wasm/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
[package]
name = "queue-repeat-wasm"
version = "0.0.0"
authors = ["Claudiu-Marcel Bruda <[email protected]>"]
edition = "2021"
publish = false

Expand Down
1 change: 0 additions & 1 deletion contracts/benchmarks/mappers/set-repeat/wasm/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
[package]
name = "set-repeat-wasm"
version = "0.0.0"
authors = ["Claudiu-Marcel Bruda <[email protected]>"]
edition = "2021"
publish = false

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
[package]
name = "single-value-repeat-wasm"
version = "0.0.0"
authors = ["Claudiu-Marcel Bruda <[email protected]>"]
edition = "2021"
publish = false

Expand Down
1 change: 0 additions & 1 deletion contracts/benchmarks/mappers/vec-repeat/wasm/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
[package]
name = "vec-repeat-wasm"
version = "0.0.0"
authors = ["Claudiu-Marcel Bruda <[email protected]>"]
edition = "2021"
publish = false

Expand Down
1 change: 0 additions & 1 deletion contracts/benchmarks/send-tx-repeat/wasm/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
[package]
name = "send-tx-repeat-wasm"
version = "0.0.0"
authors = ["Dorin Iancu <[email protected]>"]
edition = "2021"
publish = false

Expand Down
1 change: 0 additions & 1 deletion contracts/benchmarks/str-repeat/wasm/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
[package]
name = "str-repeat-wasm"
version = "0.0.0"
authors = ["Andrei Marinica <[email protected]>"]
edition = "2021"
publish = false

Expand Down
1 change: 0 additions & 1 deletion contracts/core/price-aggregator/wasm/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
[package]
name = "multiversx-price-aggregator-sc-wasm"
version = "0.0.0"
authors = [ "Claudiu-Marcel Bruda <[email protected]>",]
edition = "2021"
publish = false

Expand Down
11 changes: 4 additions & 7 deletions contracts/core/wegld-swap/wasm/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
[package]
name = "multiversx-wegld-swap-sc-wasm"
version = "0.0.0"
authors = [ "Dorin Iancu <[email protected]>",]
edition = "2021"
publish = false

[lib]
crate-type = [ "cdylib",]

[workspace]
members = [ ".",]

[dev-dependencies]
crate-type = ["cdylib"]

[profile.release]
codegen-units = 1
Expand All @@ -26,3 +20,6 @@ path = ".."
[dependencies.multiversx-sc-wasm-adapter]
version = "0.44.0"
path = "../../../../framework/wasm-adapter"

[workspace]
members = ["."]
7 changes: 6 additions & 1 deletion contracts/examples/multisig/wasm-multisig-full/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
# Code generated by the multiversx-sc multi-contract system. DO NOT EDIT.

# ##########################################
# ############## AUTO-GENERATED #############
# ##########################################

[package]
name = "multisig-full-wasm"
version = "0.0.0"
authors = ["Andrei Marinica <[email protected]>"]
edition = "2021"
publish = false

Expand Down
7 changes: 6 additions & 1 deletion contracts/examples/multisig/wasm-multisig-view/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
# Code generated by the multiversx-sc multi-contract system. DO NOT EDIT.

# ##########################################
# ############## AUTO-GENERATED #############
# ##########################################

[package]
name = "multisig-view-wasm"
version = "0.0.0"
authors = ["Andrei Marinica <[email protected]>"]
edition = "2021"
publish = false

Expand Down
7 changes: 6 additions & 1 deletion contracts/examples/multisig/wasm/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
# Code generated by the multiversx-sc multi-contract system. DO NOT EDIT.

# ##########################################
# ############## AUTO-GENERATED #############
# ##########################################

[package]
name = "multisig-wasm"
version = "0.0.0"
authors = ["Andrei Marinica <[email protected]>"]
edition = "2021"
publish = false

Expand Down
171 changes: 161 additions & 10 deletions framework/meta/src/cargo_toml_contents.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,18 @@ use std::{

use toml::{value::Table, Value};

use crate::cmd::contract::sc_config::ContractVariantProfile;

pub const CARGO_TOML_DEPENDENCIES: &str = "dependencies";
pub const CARGO_TOML_DEV_DEPENDENCIES: &str = "dev-dependencies";
const AUTO_GENERATED: &str =
"# Code generated by the multiversx-sc multi-contract system. DO NOT EDIT.
# ##########################################
# ############## AUTO-GENERATED #############
# ##########################################
";

/// Contains an in-memory representation of a Cargo.toml file.
///
Expand All @@ -19,6 +29,7 @@ pub const CARGO_TOML_DEV_DEPENDENCIES: &str = "dev-dependencies";
pub struct CargoTomlContents {
pub path: PathBuf,
pub toml_value: toml::Value,
pub prepend_auto_generated_comment: bool,
}

impl CargoTomlContents {
Expand All @@ -29,16 +40,29 @@ impl CargoTomlContents {
String::from_utf8(cargo_toml_content).expect("error decoding Cargo.toml utf-8");
let toml_value = cargo_toml_content_str
.parse::<toml::Value>()
.expect("failed to parse Cargo.toml toml format");
.unwrap_or_else(|e| {
panic!(
"failed to parse Cargo.toml toml format, path:{}, error: {e}",
path_ref.display()
)
});
CargoTomlContents {
path: path_ref.to_owned(),
toml_value,
prepend_auto_generated_comment: false,
}
}

pub fn new() -> Self {
CargoTomlContents {
path: PathBuf::new(),
toml_value: toml::Value::Table(Table::new()),
prepend_auto_generated_comment: false,
}
}

pub fn save_to_file<P: AsRef<Path>>(&self, path: P) {
let cargo_toml_content_str =
toml::to_string_pretty(&self.toml_value).expect("failed to format Cargo.toml contents");
let cargo_toml_content_str = &self.to_string_pretty();
let mut file = std::fs::File::create(path).expect("failed to create Cargo.toml file");
file.write_all(cargo_toml_content_str.as_bytes())
.expect("failed to write Cargo.toml contents to file");
Expand All @@ -55,6 +79,40 @@ impl CargoTomlContents {
.to_string()
}

pub fn package_edition(&self) -> String {
self.toml_value
.get("package")
.expect("missing package in Cargo.toml")
.get("edition")
.expect("missing package name in Cargo.toml")
.as_str()
.expect("package name not a string value")
.to_string()
}

pub fn dependency_version(&self, crate_name: &str) -> String {
let toml_value = self.dependency(crate_name).unwrap().to_owned();

if let Value::Table(table) = toml_value {
if let Some(version) = table.get("version") {
return remove_quotes(version);
}
}
panic!("could not find multiversx-sc dependency version in cargo toml")
}

pub fn dependency_path(&self, crate_name: &str) -> Option<String> {
let toml_value = self.dependency(crate_name).unwrap().to_owned();

if let Value::Table(table) = toml_value {
if let Some(path) = table.get("path") {
return Option::Some(remove_quotes(path));
}
}

Option::None
}

/// Assumes that a package section already exists.
pub fn change_package_name(&mut self, new_package_name: String) {
let package = self
Expand Down Expand Up @@ -109,17 +167,76 @@ impl CargoTomlContents {
.expect("malformed crate Cargo.toml")
}

pub fn insert_default_workspace(&mut self) {
let array = vec![
toml::Value::String(".".to_string()),
toml::Value::String("meta".to_string()),
];
pub fn add_crate_type(&mut self, crate_type: &str) {
let mut value = toml::map::Map::new();
let array = vec![toml::Value::String(crate_type.to_string())];
let members = toml::Value::Array(array);
let mut workspace = toml::Value::Table(Table::new());
value.insert("crate-type".to_string(), members);

self.toml_value
.as_table_mut()
.expect("malformed package in Cargo.toml")
.insert("lib".to_string(), toml::Value::Table(value));
}

pub fn add_package_info(
&mut self,
name: &String,
version: String,
current_edition: String,
publish: bool,
) {
let mut value = toml::map::Map::new();
value.insert("name".to_string(), Value::String(name.to_string()));
value.insert("version".to_string(), Value::String(version));
value.insert("edition".to_string(), Value::String(current_edition));
value.insert("publish".to_string(), Value::Boolean(publish));

self.toml_value
.as_table_mut()
.expect("malformed package in Cargo.toml / add_package")
.insert("package".to_string(), toml::Value::Table(value));
}

pub fn add_contract_variant_profile(&mut self, contract_profile: &ContractVariantProfile) {
let mut profile_props = toml::map::Map::new();
profile_props.insert(
"codegen-units".to_string(),
Value::Integer(contract_profile.codegen_units.into()),
);
profile_props.insert(
"opt-level".to_string(),
Value::String(contract_profile.opt_level.to_owned()),
);
profile_props.insert("lto".to_string(), Value::Boolean(contract_profile.lto));
profile_props.insert("debug".to_string(), Value::Boolean(contract_profile.debug));
profile_props.insert(
"panic".to_string(),
Value::String(contract_profile.panic.to_owned()),
);

let mut toml_table = toml::map::Map::new();
toml_table.insert("release".to_string(), toml::Value::Table(profile_props));

self.toml_value
.as_table_mut()
.expect("malformed package in Cargo.toml")
.insert("profile".to_string(), toml::Value::Table(toml_table));
}

pub fn add_workspace(&mut self, members: &[&str]) {
let array: Vec<toml::Value> = members
.iter()
.map(|s| toml::Value::String(s.to_string()))
.collect();
let members_toml = toml::Value::Array(array);

let mut workspace = toml::Value::Table(toml::map::Map::new());
workspace
.as_table_mut()
.expect("malformed package in Cargo.toml")
.insert("members".to_string(), members);
.insert("members".to_string(), members_toml);

self.toml_value
.as_table_mut()
.expect("malformed package in Cargo.toml")
Expand Down Expand Up @@ -156,6 +273,16 @@ impl CargoTomlContents {
}
}
}

pub fn to_string_pretty(&self) -> String {
let toml_string =
toml::to_string_pretty(&self.toml_value).expect("failed to format Cargo.toml contents");
if self.prepend_auto_generated_comment {
return format!("{}{}", AUTO_GENERATED, toml_string);
}

toml_string
}
}

/// Checks that path == ".." in a depdency.
Expand All @@ -168,3 +295,27 @@ fn is_dep_path_above(dep: &Value) -> bool {

false
}

pub fn change_from_base_to_adapter_path(base_path: &str) -> String {
format!(
"../{}",
base_path.to_string().replace("base", "wasm-adapter")
)
}

fn remove_quotes(var: &Value) -> String {
var.to_string().replace('\"', "")
}

mod tests {

#[test]
fn test_change_from_base_to_adapter_path() {
let base_path = "../../../framework/base";
let adapter_path = "../../../../framework/wasm-adapter".to_string();
assert_eq!(
super::change_from_base_to_adapter_path(base_path),
adapter_path
);
}
}
8 changes: 4 additions & 4 deletions framework/meta/src/cmd/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ mod generate_snippets;
mod meta_abi;
mod meta_config;
pub mod sc_config;
pub mod wasm_cargo_toml_data;
pub mod wasm_cargo_toml_generate;

use std::path::Path;

Expand Down Expand Up @@ -48,10 +50,8 @@ where
{
let original_contract_abi = <AbiObj as ContractAbiProvider>::abi();

let sc_config = ScConfig::load_from_crate_or_default(
contract_crate_path,
&original_contract_abi,
);
let sc_config =
ScConfig::load_from_crate_or_default(contract_crate_path, &original_contract_abi);
sc_config.validate_contract_variants();
sc_config
}
Loading

0 comments on commit 6b125fc

Please sign in to comment.