Skip to content

Commit

Permalink
Merge branch 'master' into hansl/canister-names
Browse files Browse the repository at this point in the history
  • Loading branch information
hansl committed Nov 9, 2019
2 parents 62000ff + e0b81a5 commit f32cc03
Show file tree
Hide file tree
Showing 15 changed files with 150 additions and 114 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion dfx/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "dfx"
version = "0.4.0"
version = "0.4.1"
authors = ["DFINITY Team"]
edition = "2018"
build = "assets/build.rs"
Expand Down
9 changes: 4 additions & 5 deletions dfx/src/commands/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,6 @@ mod tests {

use std::env::temp_dir;
use std::fs;
use std::io;
use std::io::{Read, Write};
use std::path::PathBuf;
use std::process;
Expand All @@ -194,14 +193,14 @@ mod tests {
}

impl<'a> BinaryResolverEnv for TestEnv<'a> {
fn get_binary_command_path(&self, binary_name: &str) -> io::Result<PathBuf> {
fn get_binary_command_path(&self, binary_name: &str) -> DfxResult<PathBuf> {
// We need to implement this function as it's used to set the "MOC_RTS"
// environment variable and pass the stdlib package. For the
// purposes of this test we just return the name of the binary
// that was requested.
Ok(PathBuf::from(binary_name))
}
fn get_binary_command(&self, binary_name: &str) -> io::Result<process::Command> {
fn get_binary_command(&self, binary_name: &str) -> DfxResult<process::Command> {
let stdout = self.out_file.try_clone()?;
let stderr = self.out_file.try_clone()?;

Expand Down Expand Up @@ -257,10 +256,10 @@ mod tests {
struct TestEnv {}

impl BinaryResolverEnv for TestEnv {
fn get_binary_command_path(&self, _binary_name: &str) -> io::Result<PathBuf> {
fn get_binary_command_path(&self, _binary_name: &str) -> DfxResult<PathBuf> {
panic!("get_binary_command_path should not be called.")
}
fn get_binary_command(&self, _binary_name: &str) -> io::Result<process::Command> {
fn get_binary_command(&self, _binary_name: &str) -> DfxResult<process::Command> {
panic!("get_binary_command should not be called.")
}
}
Expand Down
49 changes: 16 additions & 33 deletions dfx/src/config/cache.rs
Original file line number Diff line number Diff line change
@@ -1,55 +1,41 @@
use crate::config::dfx_version;
use crate::lib::error::DfxError::CacheError;
use crate::lib::error::{CacheErrorKind, DfxResult};
use crate::util;
use indicatif::{ProgressBar, ProgressDrawTarget};
use std::io::{Error, ErrorKind, Result};
use std::os::unix::fs::PermissionsExt;
use std::path::PathBuf;

pub fn get_bin_cache_root() -> Result<PathBuf> {
let home = match std::env::var("HOME") {
Ok(h) => Ok(h),
Err(_) => Err(Error::new(
ErrorKind::Other,
"Could not find the HOME directory.",
)),
}?;
pub fn get_bin_cache_root() -> DfxResult<PathBuf> {
let home = std::env::var("HOME")
.map_err(|_| CacheError(CacheErrorKind::CannotFindUserHomeDirectory()))?;

let p = PathBuf::from(home)
.join(".cache")
.join("dfinity")
.join("versions");

if !p.exists() {
std::fs::create_dir_all(&p)?;
if let Err(e) = std::fs::create_dir_all(&p) {
return Err(CacheError(CacheErrorKind::CannotCreateCacheDirectory(p, e)));
}
} else if !p.is_dir() {
return Err(Error::new(
ErrorKind::Other,
"Cache root is not a directory.",
));
return Err(CacheError(CacheErrorKind::CacheShouldBeADirectory(p)));
}

Ok(p)
}

pub fn get_bin_cache(v: &str) -> Result<PathBuf> {
pub fn get_bin_cache(v: &str) -> DfxResult<PathBuf> {
let root = get_bin_cache_root()?;
Ok(root.join(v))
}

pub fn is_version_installed(v: &str) -> Result<bool> {
match get_bin_cache(v) {
Ok(v) => Ok(v.is_dir()),
Err(err) => {
if err.kind() == ErrorKind::Other {
Ok(false)
} else {
Err(err)
}
}
}
pub fn is_version_installed(v: &str) -> DfxResult<bool> {
get_bin_cache(v).and_then(|c| Ok(c.is_dir()))
}

pub fn install_version(v: &str) -> Result<PathBuf> {
pub fn install_version(v: &str) -> DfxResult<PathBuf> {
let p = get_bin_cache(v)?;
if is_version_installed(v).unwrap_or(false) {
return Ok(p);
Expand Down Expand Up @@ -88,20 +74,17 @@ pub fn install_version(v: &str) -> Result<PathBuf> {

Ok(p)
} else {
Err(Error::new(
ErrorKind::Other,
format!("Unknown version: {}", v),
))
Err(CacheError(CacheErrorKind::UnknownDfxVersion(v.to_owned())))
}
}

pub fn get_binary_path_from_version(version: &str, binary_name: &str) -> Result<PathBuf> {
pub fn get_binary_path_from_version(version: &str, binary_name: &str) -> DfxResult<PathBuf> {
install_version(version)?;

Ok(get_bin_cache(version)?.join(binary_name))
}

pub fn binary_command_from_version(version: &str, name: &str) -> Result<std::process::Command> {
pub fn binary_command_from_version(version: &str, name: &str) -> DfxResult<std::process::Command> {
let path = get_binary_path_from_version(version, name)?;
let cmd = std::process::Command::new(path);

Expand Down
25 changes: 12 additions & 13 deletions dfx/src/lib/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use crate::config::{cache, dfx_version};
use crate::lib::api_client::{Client, ClientConfig};
use crate::lib::error::DfxResult;
use std::cell::RefCell;
use std::io;
use std::path::PathBuf;

/// An environment that contains the platform and general environment.
Expand All @@ -13,14 +12,14 @@ pub trait PlatformEnv {

/// An environment that manages the global binary cache.
pub trait BinaryCacheEnv {
fn is_installed(&self) -> io::Result<bool>;
fn install(&self) -> io::Result<()>;
fn is_installed(&self) -> DfxResult<bool>;
fn install(&self) -> DfxResult<()>;
}

/// An environment that can resolve binaries from the user-level cache.
pub trait BinaryResolverEnv {
fn get_binary_command_path(&self, binary_name: &str) -> io::Result<PathBuf>;
fn get_binary_command(&self, binary_name: &str) -> io::Result<std::process::Command>;
fn get_binary_command_path(&self, binary_name: &str) -> DfxResult<PathBuf>;
fn get_binary_command(&self, binary_name: &str) -> DfxResult<std::process::Command>;
}

/// An environment that can get the project configuration.
Expand Down Expand Up @@ -54,19 +53,19 @@ impl PlatformEnv for InProjectEnvironment {
}

impl BinaryCacheEnv for InProjectEnvironment {
fn is_installed(&self) -> io::Result<bool> {
fn is_installed(&self) -> DfxResult<bool> {
cache::is_version_installed(self.version.as_str())
}
fn install(&self) -> io::Result<()> {
fn install(&self) -> DfxResult<()> {
cache::install_version(self.version.as_str()).map(|_| ())
}
}

impl BinaryResolverEnv for InProjectEnvironment {
fn get_binary_command_path(&self, binary_name: &str) -> io::Result<PathBuf> {
fn get_binary_command_path(&self, binary_name: &str) -> DfxResult<PathBuf> {
cache::get_binary_path_from_version(self.version.as_str(), binary_name)
}
fn get_binary_command(&self, binary_name: &str) -> io::Result<std::process::Command> {
fn get_binary_command(&self, binary_name: &str) -> DfxResult<std::process::Command> {
cache::binary_command_from_version(self.version.as_str(), binary_name)
}
}
Expand Down Expand Up @@ -134,19 +133,19 @@ impl PlatformEnv for GlobalEnvironment {
}

impl BinaryCacheEnv for GlobalEnvironment {
fn is_installed(&self) -> io::Result<bool> {
fn is_installed(&self) -> DfxResult<bool> {
cache::is_version_installed(self.version.as_str())
}
fn install(&self) -> io::Result<()> {
fn install(&self) -> DfxResult<()> {
cache::install_version(self.version.as_str()).map(|_| ())
}
}

impl BinaryResolverEnv for GlobalEnvironment {
fn get_binary_command_path(&self, binary_name: &str) -> std::io::Result<PathBuf> {
fn get_binary_command_path(&self, binary_name: &str) -> DfxResult<PathBuf> {
cache::get_binary_path_from_version(self.version.as_str(), binary_name)
}
fn get_binary_command(&self, binary_name: &str) -> std::io::Result<std::process::Command> {
fn get_binary_command(&self, binary_name: &str) -> DfxResult<std::process::Command> {
cache::binary_command_from_version(self.version.as_str(), binary_name)
}
}
Expand Down
44 changes: 44 additions & 0 deletions dfx/src/lib/error/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use std::fmt;

/// An error happened during build.
#[derive(Debug)]
pub enum BuildErrorKind {
/// Invalid extension.
InvalidExtension(String),

/// A compiler error happened.
MotokoCompilerError(String),

/// An error happened during the generation of the Idl.
IdlGenerationError(String),

/// An error happened while generating the user library.
UserLibGenerationError(String),

/// An error happened while compiling WAT to WASM.
WatCompileError(wabt::Error),
}

impl fmt::Display for BuildErrorKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
use BuildErrorKind::*;

match self {
InvalidExtension(ext) => f.write_fmt(format_args!("Invalid extension: {}", ext)),
MotokoCompilerError(stdout) => {
f.write_fmt(format_args!("Motoko returned an error:\n{}", stdout))
}
IdlGenerationError(stdout) => f.write_fmt(format_args!(
"IDL generation returned an error:\n{}",
stdout
)),
UserLibGenerationError(stdout) => f.write_fmt(format_args!(
"UserLib generation returned an error:\n{}",
stdout
)),
WatCompileError(e) => {
f.write_fmt(format_args!("Error while compiling WAT to WASM: {}", e))
}
}
}
}
34 changes: 34 additions & 0 deletions dfx/src/lib/error/cache.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use std::fmt;
use std::io;
use std::path::PathBuf;

/// An error happened during build.
#[derive(Debug)]
pub enum CacheErrorKind {
CannotFindUserHomeDirectory(),
CannotCreateCacheDirectory(PathBuf, io::Error),
CacheShouldBeADirectory(PathBuf),
UnknownDfxVersion(String),
}

impl fmt::Display for CacheErrorKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
use CacheErrorKind::*;

match self {
CannotFindUserHomeDirectory() => f.write_str("Cannot find the home directory."),
CannotCreateCacheDirectory(path, io_err) => f.write_fmt(format_args!(
r#"Could not create the cache folder at "{}". Error: {}"#,
path.display(),
io_err,
)),

CacheShouldBeADirectory(path) => f.write_fmt(format_args!(
r#"Cache folder "{}" should be a directory or a symlink to a directory."#,
path.display(),
)),

UnknownDfxVersion(version) => f.write_fmt(format_args!("Unknown version: {}", version)),
}
}
}
59 changes: 8 additions & 51 deletions dfx/src/lib/error.rs → dfx/src/lib/error/mod.rs
Original file line number Diff line number Diff line change
@@ -1,61 +1,18 @@
use std::fmt;
mod build;
mod cache;

#[derive(Debug)]
/// An error happened during build.
pub enum BuildErrorKind {
/// Invalid extension.
InvalidExtension(String),

/// A compiler error happened.
MotokoCompilerError(String),

/// An error happened during the generation of the Idl.
IdlGenerationError(String),

/// An error happened while generating the user library.
UserLibGenerationError(String),

/// An error happened while compiling WAT to WASM.
WatCompileError(wabt::Error),
}

impl fmt::Display for BuildErrorKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
use BuildErrorKind::*;

match self {
InvalidExtension(ext) => {
f.write_fmt(format_args!("Invalid extension: {}", ext))?;
}
MotokoCompilerError(stdout) => {
f.write_fmt(format_args!("Motoko returned an error:\n{}", stdout))?;
}
IdlGenerationError(stdout) => {
f.write_fmt(format_args!(
"IDL generation returned an error:\n{}",
stdout
))?;
}
UserLibGenerationError(stdout) => {
f.write_fmt(format_args!(
"UserLib generation returned an error:\n{}",
stdout
))?;
}
WatCompileError(e) => {
f.write_fmt(format_args!("Error while compiling WAT to WASM: {}", e))?;
}
};

Ok(())
}
}
pub use build::BuildErrorKind;
pub use cache::CacheErrorKind;

// TODO: refactor this enum into a *Kind enum and a struct DfxError.
#[derive(Debug)]
pub enum DfxError {
/// An error happened during build.
BuildError(BuildErrorKind),

/// An error happened while managing the cache.
CacheError(CacheErrorKind),

Clap(clap::Error),
Io(std::io::Error),
Reqwest(reqwest::Error),
Expand Down
2 changes: 1 addition & 1 deletion e2e/assets/print_mo/print.mo
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
actor HelloActor {
public func hello() : async () {
print("Hello, World! from DFINITY \n");
debugPrint("Hello, World! from DFINITY \n");
}
};
1 change: 1 addition & 0 deletions nix/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ let
if localCommonSrc != ""
then localCommonSrc
else builtins.fetchGit {
name = "common-sources";
url = "ssh://git@github.com/dfinity-lab/common";
rev = "b4cd9e11b5e36bbdea3cf4674c15c9f689d9f318";
};
Expand Down
Loading

0 comments on commit f32cc03

Please sign in to comment.