Skip to content

Commit

Permalink
Fix for working with workspaces
Browse files Browse the repository at this point in the history
  • Loading branch information
kubaplas committed Jan 8, 2024
1 parent 97d971d commit 5104ef3
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 15 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/ci-cargo-odra.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 10 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 24 additions & 3 deletions src/actions/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,39 @@ 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())
&& contract.module_name() != self.project.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);
}
}
}

/// Run wasm-strip on *.wasm files in wasm directory.
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());
}
}
Expand Down
8 changes: 1 addition & 7 deletions src/actions/clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,13 @@ 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()
{
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);
}
5 changes: 3 additions & 2 deletions src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,16 +109,17 @@ 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",
vec![
"--target",
"wasm32-unknown-unknown",
"--bin",
"build_contract",
&build_contract,
"--release",
],
);
Expand Down
17 changes: 15 additions & 2 deletions src/odra_toml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 3 additions & 0 deletions src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -153,6 +155,7 @@ impl Project {
)
.as_str(),
);
log::info("Done!");
}

/// Detects an existing project.
Expand Down

0 comments on commit 5104ef3

Please sign in to comment.