Skip to content

Commit

Permalink
Removed unused code
Browse files Browse the repository at this point in the history
Clippy fixes
  • Loading branch information
kubaplas committed Dec 5, 2023
1 parent c9827fc commit a88368d
Show file tree
Hide file tree
Showing 13 changed files with 24 additions and 287 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ license = "MIT"
repository = "https://github.com/odradev/cargo-odra"
description = "A cargo utility that helps to create, manage and test your smart contracts written using Odra framework."
keywords = ["wasm", "webassembly", "blockchain"]
categories = ["wasm", "smart contracts"]
categories = ["wasm", "development-tools::cargo-plugins"]
name = "cargo-odra"
version = "0.2.0"
edition = "2021"
Expand Down
2 changes: 1 addition & 1 deletion justfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
DEVELOPMENT_ODRA_BRANCH := "release/0.5.0"
DEVELOPMENT_ODRA_BRANCH := "release/0.8.0"

default:
just --list
Expand Down
41 changes: 8 additions & 33 deletions src/actions/build.rs
Original file line number Diff line number Diff line change
@@ -1,54 +1,25 @@
//! Module for managing and building backends.
use std::path::Path;

use cargo_toml::{Dependency, DependencyDetail, DepsSet};

use crate::{
cargo_toml::odra_raw_dependency,
command,
consts::ODRA_TEMPLATE_GH_RAW_REPO,
errors::Error,
log,
odra_toml::Contract,
paths::{self, BuilderPaths},
project::Project,
template::TemplateGenerator,
};
use crate::{command, errors::Error, log, odra_toml::Contract, paths, project::Project};

/// BuildAction configuration.
pub struct BuildAction<'a> {
backend: String,
contracts_names: Option<String>,
builder_paths: BuilderPaths,
project: &'a Project,
template_generator: TemplateGenerator,
}

/// BuildAction implementation.
impl<'a> BuildAction<'a> {
/// Crate a new BuildAction for a given backend.
pub fn new(project: &'a Project, backend: String, contracts_names: Option<String>) -> Self {
pub fn new(project: &'a Project, contracts_names: Option<String>) -> Self {
BuildAction {
backend: backend.clone(),
contracts_names,
builder_paths: BuilderPaths::new(backend, project.project_root.clone()),
project,
template_generator: TemplateGenerator::new(
ODRA_TEMPLATE_GH_RAW_REPO.to_string(),
project.project_odra_location(),
),
}
}
}

impl BuildAction<'_> {
/// Returns the name of the backend.
/// It is also the name of the Odra's feature.
pub fn backend_name(&self) -> String {
self.backend.clone()
}

/// Main function that runs the whole workflow for a backend.
pub fn build(&self) {
self.check_target_requirements();
Expand Down Expand Up @@ -99,9 +70,13 @@ impl BuildAction<'_> {
/// Build .wasm files.
fn build_wasm_files(&self) {
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("contract", self.project.project_root());
command::cargo_build_wasm_files(
self.project.project_root(),
&paths::to_snake_titlecase(&contract.name),
);
let source = paths::wasm_path_in_target("build_contract", self.project.project_root());
let target = paths::wasm_path_in_wasm_dir(
&paths::to_snake_titlecase(&contract.name),
self.project.project_root(),
Expand Down
2 changes: 1 addition & 1 deletion src/actions/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ impl TestAction<'_> {

/// Build *.wasm files before testing.
fn build_wasm_files(&self) {
BuildAction::new(self.project, String::from(self.backend_name()), None).build();
BuildAction::new(self.project, None).build();
log::info("Building finished.")
}
}
28 changes: 3 additions & 25 deletions src/actions/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,12 @@
use std::path::PathBuf;

use crate::{cli::UpdateCommand, command, log, paths};
use crate::{cli::UpdateCommand, command, log};

/// Runs `cargo update` on project and backends in .builder* folders.
/// If backend is specified update will be made only in its folder.
pub fn update_action(update_command: UpdateCommand, project_root: PathBuf) {
if let Some(backend) = update_command.backend {
let builder_paths = paths::BuilderPaths::new(backend, project_root);
update_builder(builder_paths.root());
} else {
update_all_builders();
update_project();
}
}

/// Update a builder crate.
fn update_builder(builder: PathBuf) {
log::info(format!(
"Running cargo update for {} builder...",
builder.to_str().unwrap()
));
command::cargo_update(builder);
}

/// Update all builders.
fn update_all_builders() {
for builder_dir in glob::glob(".builder_*").unwrap().flatten() {
update_builder(builder_dir);
}
pub fn update_action(_update_command: UpdateCommand, _project_root: PathBuf) {
update_project();
}

/// Update root project.
Expand Down
81 changes: 2 additions & 79 deletions src/cargo_toml.rs
Original file line number Diff line number Diff line change
@@ -1,86 +1,9 @@
//! Module containing functions used by Builder for managing its Cargo.toml file
use std::path::PathBuf;

use cargo_toml::{Dependency, DepsSet, Edition, FeatureSet, Manifest, Package, Product, Workspace};

use crate::{command, errors::Error, odra_toml::Contract, paths::BuilderPaths};

/// Builds and saves Cargo.toml file for backend.
pub fn builder_cargo_toml(
builder_paths: &BuilderPaths,
builder_deps: DepsSet,
contracts: Vec<Contract>,
) {
let default_bin = Product {
test: false,
doctest: false,
bench: false,
doc: false,
edition: Edition::E2021,
..Default::default()
};

let mut bins = vec![];
let build_name = "contracts_build".to_string();
let path = builder_paths.relative().wasm_build_as_string();

bins.push(Product {
path: Some(path),
name: Some(build_name),
..default_bin.clone()
});
for contract in contracts {
let path = builder_paths
.relative()
.wasm_source_as_string(&contract.name);
bins.push(Product {
path: Some(path),
name: Some(contract.name.clone()),
..default_bin.clone()
});
}

#[allow(deprecated)]
let cargo_toml: Manifest = cargo_toml::Manifest {
package: Some(Package::new("builder".to_string(), "1.0.0".to_string())),
workspace: Some(Workspace {
members: vec![],
package: None,
default_members: vec![],
exclude: vec![],
metadata: None,
resolver: None,
dependencies: Default::default(),
}),
dependencies: builder_deps,
dev_dependencies: Default::default(),
build_dependencies: Default::default(),
target: Default::default(),
features: FeatureSet::new(),
patch: Default::default(),
lib: None,
profile: Default::default(),
badges: Default::default(),
bin: bins,
bench: vec![],
test: vec![],
example: vec![],
replace: Default::default(),
};
use cargo_toml::Manifest;

let toml_content = toml::to_string_pretty(&cargo_toml).unwrap();
command::write_to_file(builder_paths.cargo_toml(), &toml_content);
}

/// Returns Dependency of Odra, taken from project's Cargo.toml.
pub fn odra_raw_dependency(cargo_toml_path: &PathBuf) -> Dependency {
load_cargo_toml(cargo_toml_path)
.dependencies
.get("odra")
.unwrap_or_else(|| Error::OdraNotADependency.print_and_die())
.clone()
}
use crate::errors::Error;

/// Returns Cargo.toml as Manifest struct.
pub fn load_cargo_toml(path: &PathBuf) -> Manifest {
Expand Down
6 changes: 1 addition & 5 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,6 @@ pub struct InitCommand {
#[derive(clap::Args)]
/// `cargo odra build`
pub struct BuildCommand {
/// Name of the backend that will be used for the build process (e.g. casper).
#[clap(value_parser, long, short, value_parser = [consts::ODRA_CASPER_BACKEND])]
pub backend: String,

/// Contracts names separated by a space that matches the names in Odra.toml.
#[clap(value_parser, long, short)]
pub contracts_names: Option<String>,
Expand Down Expand Up @@ -135,7 +131,7 @@ pub fn make_action() {
.unwrap_or_else(|_| Error::CouldNotDetermineCurrentDirectory.print_and_die());
match args.subcommand {
OdraSubcommand::Build(build) => {
Project::detect(current_dir).build(build.backend, build.contracts_names);
Project::detect(current_dir).build(build.contracts_names);
}
OdraSubcommand::Test(test) => {
Project::detect(current_dir).test(test);
Expand Down
17 changes: 5 additions & 12 deletions src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,6 @@ pub fn cp(source: PathBuf, target: PathBuf) {
);
}

/// Creates a directory.
pub fn mkdir(path: PathBuf) {
fs::create_dir_all(path).unwrap();
}

/// Remove a directory.
pub fn rm_dir(path: PathBuf) {
log::info(format!("Removing {}...", path.display()));
Expand All @@ -70,6 +65,11 @@ pub fn rm_dir(path: PathBuf) {
};
}

/// Creates a directory.
pub fn mkdir(path: PathBuf) {
fs::create_dir_all(path).unwrap();
}

/// Runs wasm-strip.
pub fn wasm_strip(contract_name: &str, project_root: PathBuf) {
let command = Command::new("wasm-strip")
Expand Down Expand Up @@ -120,8 +120,6 @@ pub fn cargo_build_wasm_files(current_dir: PathBuf, contract_name: &str) {
"--bin",
"build_contract",
"--release",
// "--target-dir",
// "../target",
],
);
}
Expand All @@ -131,11 +129,6 @@ pub fn cargo_update(current_dir: PathBuf) {
cargo(current_dir, "update", vec![]);
}

/// Runs cargo fmt.
pub fn cargo_fmt(current_dir: PathBuf) {
cargo(current_dir, "fmt", vec![]);
}

/// Runs cargo test.
pub fn cargo_test_mock_vm(current_dir: PathBuf, mut args: Vec<&str>) {
log::info("Running cargo test...");
Expand Down
12 changes: 0 additions & 12 deletions src/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,6 @@ pub const ODRA_GITHUB_API_DATA: &str = "https://api.github.com/repos/odradev/odr
/// Default template name.
pub const ODRA_TEMPLATE_DEFAULT_TEMPLATE: &str = "full";

/// WASM Path
pub const ODRA_WASM_PATH_ENV_KEY: &str = "ODRA_WASM_PATH";

/// WASM Source builder template.
pub const WASM_SINGLE_SOURCE_BUILDER: &str = "contracts_builder/wasm_source_builder";

/// WASM Source builder helper template.
pub const MATCH_CONTRACT_NAME: &str = "contracts_builder/match_contract_name";

/// WASM Source builder helper template.
pub const GEN_CONTRACT_MOD: &str = "contracts_builder/gen_contract_mod";

/// Module template.
pub const MODULE_TEMPLATE: &str = "module";

Expand Down
4 changes: 0 additions & 4 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@ pub enum Error {
#[error("Odra.toml not found at location {0}")]
OdraTomlNotFound(PathBuf),

#[error("Not implemented: {0}")]
NotImplemented(String),

#[error("Failed to fetch template: {0}")]
FailedToFetchTemplate(String),

Expand Down Expand Up @@ -87,7 +84,6 @@ impl Error {
Error::RemoveDirNotPossible(_) => 10,
Error::ModuleNotFound(_) => 11,
Error::OdraTomlNotFound(_) => 12,
Error::NotImplemented(_) => 13,
Error::FailedToFetchTemplate(_) => 14,
Error::FailedToParseTemplate(_) => 15,
Error::CouldNotDetermineCurrentDirectory => 16,
Expand Down
Loading

0 comments on commit a88368d

Please sign in to comment.