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

Hyphens support #83

Merged
merged 2 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
1,287 changes: 687 additions & 600 deletions Cargo.lock

Large diffs are not rendered by default.

9 changes: 6 additions & 3 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ install:

prepare:
rustup target add wasm32-unknown-unknown
rustup toolchain install nightly
rustup component add --toolchain nightly-x86_64-unknown-linux-gnu clippy
rustup component add --toolchain nightly-x86_64-unknown-linux-gnu rustfmt
sudo apt install wabt
wget https://github.com/WebAssembly/binaryen/releases/download/{{BINARYEN_VERSION}}/binaryen-{{BINARYEN_VERSION}}-x86_64-linux.tar.gz || { echo "Download failed"; exit 1; }
sha256sum binaryen-{{BINARYEN_VERSION}}-x86_64-linux.tar.gz | grep {{BINARYEN_CHECKSUM}} || { echo "Checksum verification failed"; exit 1; }
Expand Down Expand Up @@ -51,13 +54,13 @@ test-workspace-project:
cd testproject && cargo odra clean

clippy:
cargo clippy --all-targets -- -D warnings
cargo +nightly clippy --all-targets -- -D warnings

check-lint: clippy
cargo fmt -- --check
cargo +nightly fmt -- --check

lint: clippy
cargo fmt
cargo +nightly fmt

clean:
cargo clean
2 changes: 1 addition & 1 deletion rust-toolchain
Original file line number Diff line number Diff line change
@@ -1 +1 @@
stable
stable
14 changes: 10 additions & 4 deletions src/actions/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,15 @@ impl BuildAction<'_> {
});

for contract in contracts {
let build_contract = format!("{}_build_contract", &contract.crate_name(self.project));
let module_name = match self.project.is_workspace() {
true => contract.module_name(),
false => contract.crate_name(self.project),
};
let build_contract = format!("{}_build_contract", &module_name);
command::cargo_build_wasm_files(
self.project.project_root(),
&contract.struct_name(),
&contract.crate_name(self.project),
&module_name,
);
let source = paths::wasm_path_in_target(&build_contract, self.project.project_root());
let target =
Expand All @@ -56,7 +60,7 @@ impl BuildAction<'_> {
let module_wasm_dir = self
.project
.project_root()
.join(contract.module_name())
.join(contract.module_crate_name(self.project))
.join("wasm");
command::mkdir(module_wasm_dir.clone());
let mut module_wasm_path = module_wasm_dir.clone().join(&contract.struct_name());
Expand All @@ -80,7 +84,9 @@ impl BuildAction<'_> {
if self.project.is_workspace() {
command::process_wasm(
&contract.struct_name(),
self.project.project_root().join(contract.module_name()),
self.project
.project_root()
.join(contract.module_crate_name(self.project)),
);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/actions/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl<'a> GenerateAction<'a> {
/// Crate a new GenerateAction for a given contract.
pub fn new(project: &'a Project, contract_name: String, module_name: Option<String>) -> Self {
if project.is_workspace() && module_name.is_none() {
Error::ModuleNotProvided.print_and_die();
Error::CrateNotProvided.print_and_die();
}

GenerateAction {
Expand Down
6 changes: 1 addition & 5 deletions src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,7 @@ pub fn write_to_file(path: PathBuf, content: &str) {

/// Appends a content to a file at the given path.
pub fn append_file(path: PathBuf, content: &str) {
let mut file = OpenOptions::new()
.write(true)
.append(true)
.open(path)
.unwrap();
let mut file = OpenOptions::new().append(true).open(path).unwrap();

file.write_all(content.as_bytes()).unwrap();
}
Expand Down
10 changes: 7 additions & 3 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,11 @@ pub enum Error {
#[error("Module {0} already in src/lib.rs")]
ModuleAlreadyInLibRs(String),

#[error("Project is a workspace, module name is required")]
ModuleNotProvided,
#[error("Project is a workspace, crate name is required")]
CrateNotProvided,

#[error("Crate for contract {0} not found in workspace members")]
CrateOfContractNotFound(String),
}

impl Error {
Expand Down Expand Up @@ -107,7 +110,8 @@ impl Error {
Error::LibRsNotFound => 22,
Error::ModuleAlreadyInLibRs(_) => 23,
Error::WasmoptDidNotFinish => 24,
Error::ModuleNotProvided => 25,
Error::CrateNotProvided => 25,
Error::CrateOfContractNotFound(_) => 26,
}
}

Expand Down
26 changes: 21 additions & 5 deletions src/odra_toml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,34 @@ pub struct Contract {
}

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())
.replace("-", "_")
.to_string()
}

pub fn module_crate_name(&self, project: &Project) -> String {
if project.is_workspace() {
project
.members
.iter()
.find(|m| m.name.replace("-", "_") == self.module_name())
.unwrap_or_else(|| {
Error::CrateOfContractNotFound(self.module_name()).print_and_die()
})
.name
.clone()
} else {
project.project_crate_name()
}
}

pub fn crate_name(&self, project: &Project) -> String {
if project.is_workspace() {
self.module_name()
self.module_crate_name(project)
} else {
project.project_crate_name()
}
Expand Down Expand Up @@ -78,14 +94,14 @@ impl OdraToml {
.any(|c| c.struct_name() == contract_name)
}

/// Check if any contract in Odra.toml is a part of a module with given name
pub fn has_module(&self, module_name: &str) -> bool {
/// Check if any contract in Odra.toml is a part of a crate with given name
pub fn crate_has_contracts(&self, crate_name: &str) -> bool {
self.contracts.iter().any(|c| {
c.fqn
.split_terminator("::")
.next()
.unwrap_or_else(|| Error::MalformedFqn.print_and_die())
== module_name
== crate_name.replace("-", "_")
})
}
}
1 change: 0 additions & 1 deletion src/paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ pub fn wasm_path_in_target(contract_name: &str, project_root: PathBuf) -> PathBu

fn get_build_target_dir() -> PathBuf {
let args: Vec<String> = "config get build.target-dir -Z unstable-options"
.to_string()
.split(' ')
.map(|s| s.to_string())
.collect();
Expand Down
7 changes: 2 additions & 5 deletions src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,9 @@ impl Project {
.iter()
.map(|member| {
let root = cargo_toml_path.parent().unwrap().join(member.clone().1);
let cargo_toml = root.join("Cargo.toml");
Member {
name: member.clone().0,
root,
cargo_toml,
}
})
.collect()
Expand All @@ -156,13 +154,14 @@ impl Project {
}
}

/// Detects members of workspace which have Odra contracts.
fn detect_members(cargo_toml_path: &PathBuf, odra_toml_path: &Path) -> Vec<(String, String)> {
let odra_toml = OdraToml::load(odra_toml_path);
match load_cargo_toml(cargo_toml_path).workspace {
Some(workspace) => workspace
.members
.iter()
.filter(|member| odra_toml.has_module(member))
.filter(|crate_name| odra_toml.crate_has_contracts(crate_name))
.map(|member| (member.clone(), member.clone()))
.collect(),
None => vec![],
Expand Down Expand Up @@ -229,6 +228,4 @@ pub struct Member {
pub name: String,
/// Root directory of the member.
pub root: PathBuf,
/// Path to the Cargo.toml file.
pub cargo_toml: PathBuf,
}
Loading