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

add author flag to sc-meta template + default #1725

Merged
merged 6 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions framework/meta-lib/src/cargo_toml_contents.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ use crate::contract::sc_config::ContractVariantProfile;

pub const CARGO_TOML_DEPENDENCIES: &str = "dependencies";
pub const CARGO_TOML_DEV_DEPENDENCIES: &str = "dev-dependencies";
pub const PACKAGE: &str = "package";
pub const AUTHORS: &str = "authors";
const DEFAULT_AUTHOR: &str = "you";
const AUTO_GENERATED: &str = "# Code generated by the multiversx-sc build system. DO NOT EDIT.

# ##########################################
Expand Down Expand Up @@ -69,7 +72,7 @@ impl CargoTomlContents {

pub fn package_name(&self) -> String {
self.toml_value
.get("package")
.get(PACKAGE)
.expect("missing package in Cargo.toml")
.get("name")
.expect("missing package name in Cargo.toml")
Expand All @@ -80,7 +83,7 @@ impl CargoTomlContents {

pub fn package_edition(&self) -> String {
self.toml_value
.get("package")
.get(PACKAGE)
.expect("missing package in Cargo.toml")
.get("edition")
.expect("missing package name in Cargo.toml")
Expand Down Expand Up @@ -158,6 +161,29 @@ impl CargoTomlContents {
self.toml_value.get(CARGO_TOML_DEV_DEPENDENCIES).is_some()
}

pub fn change_author(&mut self, authors: PathBuf) -> bool {
andrei-marinica marked this conversation as resolved.
Show resolved Hide resolved
let package = self
.toml_value
.get_mut(PACKAGE)
.unwrap_or_else(|| panic!("no dependencies found in crate {}", self.path.display()))
.as_table_mut()
.expect("missing package in Cargo.toml");

package.remove(AUTHORS);

let mut author_as_str = authors
.into_os_string()
.into_string()
.unwrap_or(DEFAULT_AUTHOR.to_string());
if author_as_str.is_empty() {
author_as_str = DEFAULT_AUTHOR.to_string();
}
let new_authors = vec![toml::Value::String(author_as_str)];
package.insert(AUTHORS.to_owned(), toml::Value::Array(new_authors));

true
}

pub fn dev_dependencies_mut(&mut self) -> &mut Table {
self.toml_value
.get_mut(CARGO_TOML_DEV_DEPENDENCIES)
Expand Down
5 changes: 5 additions & 0 deletions framework/meta/src/cli/cli_args_standalone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,11 @@ pub struct TemplateArgs {
/// Will be current directory if not specified.
#[arg(long, verbatim_doc_comment)]
pub path: Option<PathBuf>,

/// The author of the contract is to receive.
/// If missing, the template author will be considered.
andrei-marinica marked this conversation as resolved.
Show resolved Hide resolved
#[arg(long, verbatim_doc_comment)]
pub author: Option<PathBuf>,
}

impl CliArgsToRaw for TemplateArgs {
Expand Down
12 changes: 7 additions & 5 deletions framework/meta/src/cmd/template/contract_creator.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::path::PathBuf;

use convert_case::{Case, Casing};

use crate::{
Expand All @@ -20,7 +22,7 @@ pub fn create_contract(args: &TemplateArgs) {

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

creator.create_contract(version_tag);
creator.create_contract(version_tag, args.author.clone().unwrap_or_default());
}

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

pub fn create_contract(&self, args_tag: FrameworkVersion) {
pub fn create_contract(&self, args_tag: FrameworkVersion, new_authors: PathBuf) {
self.copy_template(args_tag.clone());
self.update_dependencies(args_tag);
self.update_dependencies(args_tag, new_authors);
self.rename_template();
}

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

pub fn update_dependencies(&self, args_tag: FrameworkVersion) {
self.adjuster.update_dependencies(args_tag);
pub fn update_dependencies(&self, args_tag: FrameworkVersion, new_authors: PathBuf) {
self.adjuster.update_dependencies(args_tag, new_authors);
}

pub fn rename_template(&self) {
Expand Down
22 changes: 13 additions & 9 deletions framework/meta/src/cmd/template/template_adjuster.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::path::PathBuf;

use super::{template_metadata::TemplateMetadata, ContractCreatorTarget};
use crate::{
cmd::upgrade::upgrade_common::{rename_files, replace_in_files},
Expand All @@ -22,14 +24,14 @@ pub struct TemplateAdjuster {
pub keep_paths: bool,
}
impl TemplateAdjuster {
pub fn update_dependencies(&self, args_tag: FrameworkVersion) {
self.update_dependencies_root();
self.update_dependencies_meta();
self.update_dependencies_wasm(args_tag);
self.update_dependencies_interact();
pub fn update_dependencies(&self, args_tag: FrameworkVersion, new_authors: PathBuf) {
andrei-marinica marked this conversation as resolved.
Show resolved Hide resolved
self.update_cargo_toml_root(new_authors.clone());
self.update_cargo_toml_meta();
self.update_cargo_toml_wasm(args_tag);
self.update_cargo_toml_interact(new_authors);
}

fn update_dependencies_root(&self) {
fn update_cargo_toml_root(&self, new_authors: PathBuf) {
let cargo_toml_path = self.target.contract_dir().join(ROOT_CARGO_TOML);
let mut toml = CargoTomlContents::load_from_file(&cargo_toml_path);

Expand All @@ -43,10 +45,11 @@ impl TemplateAdjuster {
toml.add_workspace(&[".", "meta"]);
}

toml.change_author(new_authors);
toml.save_to_file(&cargo_toml_path);
}

fn update_dependencies_meta(&self) {
fn update_cargo_toml_meta(&self) {
let cargo_toml_path = self.target.contract_dir().join(META_CARGO_TOML);
let mut toml = CargoTomlContents::load_from_file(&cargo_toml_path);

Expand All @@ -57,7 +60,7 @@ impl TemplateAdjuster {
toml.save_to_file(&cargo_toml_path);
}

fn update_dependencies_wasm(&self, args_tag: FrameworkVersion) {
fn update_cargo_toml_wasm(&self, args_tag: FrameworkVersion) {
if is_template_with_autogenerated_wasm(args_tag) {
return;
}
Expand All @@ -72,7 +75,7 @@ impl TemplateAdjuster {
toml.save_to_file(&cargo_toml_path);
}

fn update_dependencies_interact(&self) {
fn update_cargo_toml_interact(&self, new_authors: PathBuf) {
if !self.metadata.has_interactor {
return;
}
Expand All @@ -84,6 +87,7 @@ impl TemplateAdjuster {
remove_paths_from_deps(&mut toml, &[&self.metadata.name]);
}

toml.change_author(new_authors);
toml.save_to_file(&cargo_toml_path);
}

Expand Down
47 changes: 34 additions & 13 deletions framework/meta/tests/template_test.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{fs, process::Command};
use std::{fs, path::PathBuf, process::Command};

use convert_case::{Case, Casing};
use multiversx_sc_meta::{
Expand Down Expand Up @@ -32,27 +32,32 @@ fn test_template_list() {
#[test]
#[cfg_attr(not(feature = "template-test-current"), ignore)]
fn template_current_adder() {
template_test_current("adder", "examples", "new-adder");
template_test_current(
"adder",
"examples",
"new-adder",
"Alin Cruceat <[email protected]>",
);

cargo_check_interactor("examples", "new-adder");
}

#[test]
#[cfg_attr(not(feature = "template-test-current"), ignore)]
fn template_current_crypto_zombies() {
template_test_current("crypto-zombies", "examples", "new-crypto-zombies");
template_test_current("crypto-zombies", "examples", "new-crypto-zombies", "");
}

#[test]
#[cfg_attr(not(feature = "template-test-current"), ignore)]
fn template_current_empty() {
template_test_current("empty", "examples", "new-empty");
template_test_current("empty", "examples", "new-empty", "");
}

#[test]
#[cfg_attr(not(feature = "template-test-current"), ignore)]
fn template_current_ping_pong_egld() {
template_test_current("ping-pong-egld", "examples", "new-ping-pong-egld");
template_test_current("ping-pong-egld", "examples", "new-ping-pong-egld", "");
}

#[test]
Expand All @@ -63,13 +68,13 @@ fn test_correct_naming() {
"my-new-42-correct-empty"
);

template_test_current("empty", "examples", "my1New2_3-correct_Empty");
template_test_current("empty", "examples", "my1New2_3-correct_Empty", "");
}

/// Recreates the folder structure in `contracts`, on the same level.
/// This way, the relative paths are still valid in this case,
/// and we can test the templates with the framework version of the current branch.
fn template_test_current(template_name: &str, sub_path: &str, new_name: &str) {
fn template_test_current(template_name: &str, sub_path: &str, new_name: &str, new_author: &str) {
let workspace_path = find_current_workspace().unwrap();
let target = ContractCreatorTarget {
target_path: workspace_path.join(TEMPLATE_TEMP_DIR_NAME).join(sub_path),
Expand All @@ -80,13 +85,19 @@ fn template_test_current(template_name: &str, sub_path: &str, new_name: &str) {

prepare_target_dir(&target);

let author = if new_author.is_empty() {
PathBuf::new()
} else {
PathBuf::from(new_author)
};

ContractCreator::new(
&repo_source,
template_name.to_string(),
target.clone(),
true,
)
.create_contract(LAST_TEMPLATE_VERSION);
.create_contract(LAST_TEMPLATE_VERSION, author);

if BUILD_CONTRACTS {
build_contract(&target);
Expand All @@ -97,29 +108,33 @@ fn template_test_current(template_name: &str, sub_path: &str, new_name: &str) {
#[test]
#[cfg_attr(not(feature = "template-test-released"), ignore)]
fn template_released_adder() {
template_test_released("adder", "released-adder");
template_test_released(
"adder",
"released-adder",
"Alin Cruceat <[email protected]>",
);

cargo_check_interactor("", "released-adder");
}

#[test]
#[cfg_attr(not(feature = "template-test-released"), ignore)]
fn template_released_crypto_zombies() {
template_test_released("crypto-zombies", "released-crypto-zombies");
template_test_released("crypto-zombies", "released-crypto-zombies", "");
}

#[test]
#[cfg_attr(not(feature = "template-test-released"), ignore)]
fn template_released_empty() {
template_test_released("empty", "released-empty");
template_test_released("empty", "released-empty", "");
}

/// These tests fully replicate the templating process. They
/// - download the last released version of the repo,
/// - create proper contracts,
/// - build the newly created contracts (to wasm)
/// - run all tests (including Go scenarios) on them.
fn template_test_released(template_name: &str, new_name: &str) {
fn template_test_released(template_name: &str, new_name: &str, new_author: &str) {
let workspace_path = find_current_workspace().unwrap();
let target = ContractCreatorTarget {
target_path: workspace_path.join(TEMPLATE_TEMP_DIR_NAME),
Expand All @@ -137,13 +152,19 @@ fn template_test_released(template_name: &str, new_name: &str) {

prepare_target_dir(&target);

let author = if new_author.is_empty() {
PathBuf::new()
} else {
PathBuf::from(new_author)
};

ContractCreator::new(
&repo_source,
template_name.to_string(),
target.clone(),
false,
)
.create_contract(LAST_TEMPLATE_VERSION);
.create_contract(LAST_TEMPLATE_VERSION, author);

if BUILD_CONTRACTS {
build_contract(&target);
Expand Down
Loading