From 26cff0d6037d320834f24bfbcdd1d452b5acb072 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kuba=20P=C5=82askonka?= Date: Mon, 8 Jan 2024 16:22:34 +0100 Subject: [PATCH] Fix for working with workspaces --- .github/workflows/ci-cargo-odra.yml | 4 +++- justfile | 10 ++++++++++ src/actions/build.rs | 25 ++++++++++++++++++++++--- src/actions/clean.rs | 8 +------- src/command.rs | 5 +++-- src/odra_toml.rs | 17 +++++++++++++++-- src/project.rs | 3 +++ 7 files changed, 57 insertions(+), 15 deletions(-) diff --git a/.github/workflows/ci-cargo-odra.yml b/.github/workflows/ci-cargo-odra.yml index 63b18de..6c8fee9 100644 --- a/.github/workflows/ci-cargo-odra.yml +++ b/.github/workflows/ci-cargo-odra.yml @@ -32,5 +32,7 @@ jobs: - run: just prepare - run: just check-lint - run: just install - - run: just test-project-generation-on-stable-odra - run: just test-project-generation-on-future-odra + - run: just test-workspace-generation-on-future-odra +# - run: just test-project-generation-on-stable-odra +# - run: just test-workspace-generation-on-stable-odra diff --git a/justfile b/justfile index 7b4073e..666d75f 100644 --- a/justfile +++ b/justfile @@ -20,6 +20,16 @@ test-project-generation-on-future-odra: cargo odra new --name testproject --source {{DEVELOPMENT_ODRA_BRANCH}} just test-testproject +test-workspace-generation-on-stable-odra: + rm -rf testworkspace + cargo odra new --name testproject --template workspace + just test-testproject + +test-workspace-generation-on-future-odra: + rm -rf testworkspace + cargo odra new --name testproject --template workspace --source {{DEVELOPMENT_ODRA_BRANCH}} + just test-testproject + test-testproject: cd testproject && cargo odra generate -c plascoin cd testproject && cargo odra test diff --git a/src/actions/build.rs b/src/actions/build.rs index f0e29f9..8b4f3b4 100644 --- a/src/actions/build.rs +++ b/src/actions/build.rs @@ -72,11 +72,29 @@ impl BuildAction<'_> { log::info("Generating wasm files..."); command::mkdir(paths::wasm_dir(self.project.project_root())); for contract in self.contracts() { - command::cargo_build_wasm_files(self.project.project_root(), &contract.name); - let source = paths::wasm_path_in_target("build_contract", self.project.project_root()); + let build_contract = format!("{}_build_contract", &contract.module_name()); + command::cargo_build_wasm_files( + self.project.project_root(), + &contract.name, + &contract.module_name(), + ); + let source = paths::wasm_path_in_target(&build_contract, self.project.project_root()); let target = paths::wasm_path_in_wasm_dir(&contract.name, self.project.project_root()); log::info(format!("Saving {}", target.display())); - command::cp(source, target); + command::cp(source.clone(), target); + // if a contract is in a module, copy the file also to the module wasm folder + if self.project.odra_toml().has_module(&contract.module_name()) { + let module_wasm_dir = self + .project + .project_root() + .join(contract.module_name()) + .join("wasm"); + command::mkdir(module_wasm_dir.clone()); + let mut module_wasm_path = module_wasm_dir.clone().join(&contract.name); + module_wasm_path.set_extension("wasm"); + log::info(format!("Copying to {}", module_wasm_path.display())); + command::cp(source, module_wasm_path); + } } } @@ -84,6 +102,7 @@ impl BuildAction<'_> { fn optimize_wasm_files(&self) { log::info("Optimizing wasm files..."); for contract in self.contracts() { + // TODO: Optimize wasm files in modules command::wasm_strip(&contract.name, self.project.project_root()); } } diff --git a/src/actions/clean.rs b/src/actions/clean.rs index 3b617c3..f31db7e 100644 --- a/src/actions/clean.rs +++ b/src/actions/clean.rs @@ -6,6 +6,7 @@ use crate::command; /// Removes wasm folder, .builder* folders and runs `cargo clean`. pub fn clean_action(project_root: PathBuf) { + // TODO: Clean wasm folders of modules for folder in glob::glob(project_root.join("wasm/*").as_os_str().to_str().unwrap()) .unwrap() .flatten() @@ -13,12 +14,5 @@ pub fn clean_action(project_root: PathBuf) { command::rm_dir(folder); } - for folder in glob::glob(project_root.join(".builder*").as_os_str().to_str().unwrap()) - .unwrap() - .flatten() - { - command::rm_dir(folder); - } - command::cargo_clean(project_root); } diff --git a/src/command.rs b/src/command.rs index a53280f..0e8975b 100644 --- a/src/command.rs +++ b/src/command.rs @@ -109,8 +109,9 @@ fn cargo(current_dir: PathBuf, command: &str, tail_args: Vec<&str>) { } /// Build wasm files. -pub fn cargo_build_wasm_files(current_dir: PathBuf, contract_name: &str) { +pub fn cargo_build_wasm_files(current_dir: PathBuf, contract_name: &str, module_name: &str) { env::set_var(ODRA_MODULE_ENV_KEY, contract_name); + let build_contract = format!("{}_build_contract", module_name); cargo( current_dir, "build", @@ -118,7 +119,7 @@ pub fn cargo_build_wasm_files(current_dir: PathBuf, contract_name: &str) { "--target", "wasm32-unknown-unknown", "--bin", - "build_contract", + &build_contract, "--release", ], ); diff --git a/src/odra_toml.rs b/src/odra_toml.rs index 090ba45..5746aee 100644 --- a/src/odra_toml.rs +++ b/src/odra_toml.rs @@ -4,17 +4,30 @@ use std::path::{Path, PathBuf}; use serde_derive::{Deserialize, Serialize}; -use crate::{command, errors::Error}; +use crate::{ + command, + errors::{Error, Error::MalformedFqn}, +}; /// Struct describing contract. #[derive(Deserialize, Serialize, Debug, Clone)] pub struct Contract { /// Name of the contract pub name: String, - /// Fully Qualified Name of the contract struct pub fqn: String, } +impl Contract { + /// Extracts first part from fqn + pub fn module_name(&self) -> String { + self.fqn + .split_terminator("::") + .next() + .unwrap_or_else(|| MalformedFqn.print_and_die()) + .to_string() + } +} + /// Odra configuration. #[derive(Deserialize, Serialize, Debug, Clone)] pub struct OdraToml { diff --git a/src/project.rs b/src/project.rs index a5ab96c..c6c788f 100644 --- a/src/project.rs +++ b/src/project.rs @@ -15,6 +15,7 @@ use crate::{ command::replace_in_file, consts::{ODRA_GITHUB_API_DATA, ODRA_TEMPLATE_GH_REPO}, errors::Error, + log, odra_toml::OdraToml, paths, }; @@ -47,6 +48,7 @@ impl Project { if init_action.init { Self::assert_dir_is_empty(init_action.current_dir.clone()); } + log::info("Generating a new project..."); let odra_location = Self::odra_location(init_action.source); @@ -153,6 +155,7 @@ impl Project { ) .as_str(), ); + log::info("Done!"); } /// Detects an existing project.