Skip to content

Commit

Permalink
[aptos-cli] Add Move compliation & remove old move cli
Browse files Browse the repository at this point in the history
  • Loading branch information
gregnazario authored and aptos-bot committed Apr 18, 2022
1 parent 9ebc914 commit dcf805c
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 58 deletions.
4 changes: 4 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion crates/aptos/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ serde = "1.0.124"
serde_json = "1.0.64"
serde_yaml = "0.8.17"
thiserror = "1.0.24"
tempfile = "3.2.0"
tokio = { version = "1.8.1", features = ["full"] }
tokio-util = { version = "0.6.4", features = ["compat"] }

Expand All @@ -36,8 +37,11 @@ aptos-rest-client = { path = "../../crates/aptos-rest-client"}
aptos-workspace-hack = { version = "0.1", path = "../aptos-workspace-hack" }
aptos-vm = { path = "../../aptos-move/aptos-vm" }
bcs = "0.1.2"
move-cli = { git = "https://github.com/diem/move", rev = "3fe033b112eae7df2d15ab3467624165ae510caa" }
short-hex-str = { path = "../short-hex-str" }
cached-framework-packages = { path = "../../aptos-move/framework/cached-packages" }
move-binary-format = { git = "https://github.com/diem/move", rev = "3fe033b112eae7df2d15ab3467624165ae510caa" }
move-cli = { git = "https://github.com/diem/move", rev = "3fe033b112eae7df2d15ab3467624165ae510caa" }
move-core-types = { git = "https://github.com/diem/move", rev = "3fe033b112eae7df2d15ab3467624165ae510caa", features=["address32"] }
move-package = { git = "https://github.com/diem/move", rev = "3fe033b112eae7df2d15ab3467624165ae510caa" }
move-unit-test = { git = "https://github.com/diem/move", rev = "3fe033b112eae7df2d15ab3467624165ae510caa" }
move-vm-types = { git = "https://github.com/diem/move", rev = "3fe033b112eae7df2d15ab3467624165ae510caa" }
13 changes: 13 additions & 0 deletions crates/aptos/src/common/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ pub enum Error {
UnexpectedError(String),
#[error("Aborted command")]
AbortedError,
#[error("Move compiliation failed: {0}")]
MoveCompiliationError(String),
}

#[derive(Debug, Default, Serialize, Deserialize)]
Expand Down Expand Up @@ -344,3 +346,14 @@ pub struct NodeOptions {
)]
pub url: reqwest::Url,
}

/// Options for a move package dir
#[derive(Debug, Parser)]
pub struct MovePackageDir {
/// Path to a move package (the folder with a Move.toml file)
#[clap(long, parse(from_os_str))]
pub package_dir: PathBuf,
/// Path to save the compiled move package
#[clap(long, parse(from_os_str))]
pub output_dir: Option<PathBuf>,
}
32 changes: 0 additions & 32 deletions crates/aptos/src/move_tool/chain.rs

This file was deleted.

95 changes: 70 additions & 25 deletions crates/aptos/src/move_tool/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,50 +6,95 @@
//! TODO: Examples
//!
use crate::CliResult;
use crate::{
common::{types::MovePackageDir, utils::to_common_result},
CliResult, Error,
};
use aptos_vm::natives::aptos_natives;
use clap::{Parser, Subcommand};
use move_core_types::errmap::ErrorMapping;
use move_vm_types::gas_schedule::INITIAL_COST_SCHEDULE;

pub mod chain;
use move_cli::package::cli::{run_move_unit_tests, UnitTestResult};
use move_package::{compilation::compiled_package::CompiledPackage, BuildConfig};
use move_unit_test::UnitTestingConfig;
use std::path::Path;

/// CLI tool for performing Move tasks
///
#[derive(Subcommand)]
pub enum MoveTool {
Command(MoveCli),
Compile(CompilePackage),
Test(TestPackage),
}

impl MoveTool {
pub async fn execute(self) -> CliResult {
match self {
// TODO: Rethink using the Move CLI and think about how we can make the experience better
MoveTool::Command(tool) => tool.execute(),
MoveTool::Compile(tool) => to_common_result(tool.execute().await),
MoveTool::Test(tool) => to_common_result(tool.execute().await),
}
}
}

/// Compiles a package and returns the [`ModuleId`]s
#[derive(Parser)]
pub struct CompilePackage {
#[clap(flatten)]
move_options: MovePackageDir,
}

impl CompilePackage {
pub async fn execute(&self) -> Result<Vec<String>, Error> {
let build_config = BuildConfig {
generate_docs: true,
install_dir: self.move_options.output_dir.clone(),
..Default::default()
};
let compiled_package = compile_move(build_config, self.move_options.package_dir.as_path())?;
// TODO: This can be serialized once move is updated
let mut ids = Vec::new();
compiled_package
.compiled_modules()
.iter_modules()
.iter()
.for_each(|module| ids.push(module.self_id().to_string()));
Ok(ids)
}
}

/// Run Move unit tests against a package path
#[derive(Parser)]
pub struct MoveCli {
pub struct TestPackage {
#[clap(flatten)]
move_args: move_cli::Move,
#[clap(subcommand)]
command: move_cli::Command,
move_options: MovePackageDir,
}

impl MoveCli {
fn execute(self) -> CliResult {
let error_descriptions: ErrorMapping =
bcs::from_bytes(cached_framework_packages::error_map())
.map_err(|err| err.to_string())?;
move_cli::run_cli(
aptos_vm::natives::aptos_natives(),
&INITIAL_COST_SCHEDULE,
&error_descriptions,
&self.move_args,
&self.command,
impl TestPackage {
pub async fn execute(&self) -> Result<&'static str, Error> {
let config = BuildConfig {
test_mode: true,
install_dir: self.move_options.output_dir.clone(),
..Default::default()
};
let result = run_move_unit_tests(
self.move_options.package_dir.as_path(),
config,
UnitTestingConfig::default_with_bound(Some(100_000)),
aptos_natives(),
false,
)
.map(|_| "".to_string())
.map_err(|err| err.to_string())
.map_err(|err| Error::UnexpectedError(err.to_string()))?;

// TODO: commit back up to the move repo
match result {
UnitTestResult::Success => Ok("Success"),
UnitTestResult::Failure => Ok("Failure"),
}
}
}

/// Compiles a Move package dir, and returns the compiled modules.
fn compile_move(build_config: BuildConfig, package_dir: &Path) -> Result<CompiledPackage, Error> {
// TODO: Add caching
build_config
.compile_package(package_dir, &mut Vec::new())
.map_err(|err| Error::MoveCompiliationError(err.to_string()))
}

0 comments on commit dcf805c

Please sign in to comment.