Skip to content

Commit

Permalink
Removed name from Odra.toml
Browse files Browse the repository at this point in the history
  • Loading branch information
kubaplas committed Jan 9, 2024
1 parent e05e9f1 commit 58e7f54
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 15 deletions.
16 changes: 9 additions & 7 deletions src/actions/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl BuildAction<'_> {
false => odra_toml
.contracts
.into_iter()
.filter(|c| names.contains(&c.name))
.filter(|c| names.contains(&c.struct_name()))
.collect(),
}
}
Expand All @@ -60,7 +60,7 @@ impl BuildAction<'_> {
.odra_toml()
.contracts
.iter()
.any(|c| c.name == *contract_name)
.any(|c| c.struct_name() == *contract_name)
{
Error::ContractNotFound(contract_name.clone()).print_and_die();
}
Expand All @@ -71,15 +71,17 @@ impl BuildAction<'_> {
fn build_wasm_files(&self) {
log::info("Generating wasm files...");
command::mkdir(paths::wasm_dir(self.project.project_root()));

for contract in self.contracts() {
let build_contract = format!("{}_build_contract", &contract.module_name());
command::cargo_build_wasm_files(
self.project.project_root(),
&contract.name,
&contract.struct_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());
let target =
paths::wasm_path_in_wasm_dir(&contract.struct_name(), self.project.project_root());
log::info(format!("Saving {}", target.display()));
command::cp(source.clone(), target);
// if a contract is in a module, copy the file also to the module wasm folder
Expand All @@ -92,7 +94,7 @@ impl BuildAction<'_> {
.join(contract.module_name())
.join("wasm");
command::mkdir(module_wasm_dir.clone());
let mut module_wasm_path = module_wasm_dir.clone().join(&contract.name);
let mut module_wasm_path = module_wasm_dir.clone().join(&contract.struct_name());
module_wasm_path.set_extension("wasm");
log::info(format!("Copying to {}", module_wasm_path.display()));
command::cp(source, module_wasm_path);
Expand All @@ -104,10 +106,10 @@ impl BuildAction<'_> {
fn optimize_wasm_files(&self) {
log::info("Optimizing wasm files...");
for contract in self.contracts() {
command::wasm_strip(&contract.name, self.project.project_root());
command::wasm_strip(&contract.struct_name(), self.project.project_root());
if contract.module_name() != self.project.name {
command::wasm_strip(
&contract.name,
&contract.struct_name(),
self.project.project_root().join(contract.module_name()),
);
}
Expand Down
9 changes: 4 additions & 5 deletions src/actions/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,21 +137,20 @@ impl GenerateAction<'_> {
/// Add contract definition to Odra.toml.
fn update_odra_toml(&self) {
let mut odra_toml = self.project.odra_toml();
let contract_name = self.contract_name();
let contract_name = self.module_ident();

// Check if Odra.toml has already a contract.
let exists = odra_toml.has_contract(contract_name);
let exists = odra_toml.has_contract(contract_name.as_str());
if exists {
Error::ContractAlreadyInOdraToml(String::from(contract_name)).print_and_die();
Error::ContractAlreadyInOdraToml(contract_name).print_and_die();
}

// Add contract to Odra.toml.
odra_toml.contracts.push(Contract {
name: self.contract_name().to_string(),
fqn: format!(
"{}::{}::{}",
self.module_name,
self.contract_name(),
self.module_ident(),
self.module_ident()
),
});
Expand Down
15 changes: 12 additions & 3 deletions src/odra_toml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
use std::path::{Path, PathBuf};

use convert_case::{Boundary, Case, Casing};
use serde_derive::{Deserialize, Serialize};

use crate::{
Expand All @@ -12,8 +13,6 @@ use crate::{
/// Struct describing contract.
#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct Contract {
/// Name of the contract
pub name: String,
pub fqn: String,
}

Expand All @@ -26,6 +25,14 @@ impl Contract {
.unwrap_or_else(|| MalformedFqn.print_and_die())
.to_string()
}

pub fn struct_name(&self) -> String {
self.fqn
.split_terminator("::")
.last()
.unwrap_or_else(|| MalformedFqn.print_and_die())
.to_string()
}
}

/// Odra configuration.
Expand Down Expand Up @@ -58,7 +65,9 @@ impl OdraToml {

/// Check if the contract is defined in Odra.toml file.
pub fn has_contract(&self, contract_name: &str) -> bool {
self.contracts.iter().any(|c| c.name == contract_name)
self.contracts
.iter()
.any(|c| c.struct_name() == contract_name)
}

/// Check if any contract in Odra.toml is a part of a module with given name
Expand Down

0 comments on commit 58e7f54

Please sign in to comment.