Skip to content

Commit

Permalink
Merge pull request #274 from charlespierce/consolidate_binaries
Browse files Browse the repository at this point in the history
Consolidate Tools into a single Binary
  • Loading branch information
dherman authored Feb 28, 2019
2 parents 64bc1cf + 2b7ca1e commit 08fa783
Show file tree
Hide file tree
Showing 26 changed files with 265 additions and 348 deletions.
28 changes: 2 additions & 26 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,8 @@ smoke-tests = []
intercept-globals = ["notion-core/intercept-globals"]

[[bin]]
name = "notion"
path = "src/notion.rs"

[[bin]]
name = "node"
path = "src/node.rs"

[[bin]]
name = "npm"
path = "src/npm.rs"

[[bin]]
name = "npx"
path = "src/npx.rs"

[[bin]]
name = "yarn"
path = "src/yarn.rs"

[[bin]]
name = "launchbin"
path = "src/launchbin.rs"

[[bin]]
name = "launchscript"
path = "src/launchscript.rs"
name = "shim"
path = "src/shim.rs"

[dependencies]
docopt = "0.8"
Expand Down
8 changes: 2 additions & 6 deletions crates/notion-core/src/path/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,8 @@ pub fn notion_file() -> Fallible<PathBuf> {
Ok(notion_home()?.join("notion"))
}

pub fn launchbin_file() -> Fallible<PathBuf> {
Ok(notion_home()?.join("launchbin"))
}

pub fn launchscript_file() -> Fallible<PathBuf> {
Ok(notion_home()?.join("launchscript"))
pub fn shim_executable() -> Fallible<PathBuf> {
Ok(notion_home()?.join("shim"))
}

pub fn create_file_symlink(src: PathBuf, dst: PathBuf) -> Result<(), io::Error> {
Expand Down
8 changes: 2 additions & 6 deletions crates/notion-core/src/path/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,8 @@ pub fn node_image_3p_bin_dir(_node: &str, _npm: &str) -> Fallible<PathBuf> {
unimplemented!("global 3rd party executables not yet implemented for Windows")
}

pub fn launchbin_file() -> Fallible<PathBuf> {
Ok(notion_home()?.join("launchbin.exe"))
}

pub fn launchscript_file() -> Fallible<PathBuf> {
Ok(notion_home()?.join("launchscript.exe"))
pub fn shim_executable() -> Fallible<PathBuf> {
Ok(notion_home()?.join("shim.exe"))
}

pub fn notion_file() -> Fallible<PathBuf> {
Expand Down
4 changes: 2 additions & 2 deletions crates/notion-core/src/shim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ fn is_3p_shim(name: &str) -> bool {
}

pub fn create(shim_name: &str) -> Fallible<ShimResult> {
let launchbin = path::launchbin_file()?;
let executable = path::shim_executable()?;
let shim = path::shim_file(shim_name)?;
match path::create_file_symlink(launchbin, shim) {
match path::create_file_symlink(executable, shim) {
Ok(_) => Ok(ShimResult::Created),
Err(err) => {
if err.kind() == io::ErrorKind::AlreadyExists {
Expand Down
52 changes: 33 additions & 19 deletions crates/notion-core/src/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ use std::env;
use std::fmt::{self, Display, Formatter};

use console::style;
use failure::Fail;
use indicatif::{ProgressBar, ProgressStyle};
use notion_fail::NotionError;
use term_size;

const NOTION_DEV: &'static str = "NOTION_DEV";

/// Represents the context from which an error is being reported.
pub enum ErrorContext {
/// An error reported from the `notion` executable.
Expand All @@ -18,13 +20,27 @@ pub enum ErrorContext {
}

/// Displays an error to stderr.
pub fn display_error<E: Display>(cx: ErrorContext, err: &E) {
pub fn display_error(cx: ErrorContext, err: &NotionError) {
display_error_prefix(cx);
if err.is_user_friendly() {
display_user_friendly_error(err);
} else {
display_internal_error(err);
}
}

/// Displays a user-friendly error to stderr
fn display_user_friendly_error(err: &NotionError) {
eprintln!("{}", err);

if env::var(NOTION_DEV).is_ok() {
eprintln!();
display_development_details(err);
}
}

/// Displays an error to stderr with a styled prefix.
pub fn display_error_prefix(cx: ErrorContext) {
fn display_error_prefix(cx: ErrorContext) {
match cx {
ErrorContext::Notion => {
// Since the command here was `notion`, it would be redundant to say that this was
Expand All @@ -42,25 +58,12 @@ pub fn display_error_prefix(cx: ErrorContext) {
}

/// Displays a generic message for internal errors to stderr.
pub fn display_unknown_error<E: Fail>(cx: ErrorContext, err: &E) {
display_error_prefix(cx);
fn display_internal_error(err: &NotionError) {
eprintln!("an internal error occurred");
eprintln!();

if env::var("NOTION_DEV").is_ok() {
eprintln!("{} {:?}", style("details:").yellow().bold(), err);
eprintln!();

let backtrace = err.backtrace();

// For now, we require RUST_BACKTRACE for this to work.
// See: https://github.com/notion-cli/notion/issues/75

if backtrace.is_some() && env::var("RUST_BACKTRACE").is_ok() {
eprintln!("{:?}", backtrace.unwrap());
} else {
eprintln!("Run with NOTION_DEV=1 and RUST_BACKTRACE=1 for a backtrace.");
}
if env::var(NOTION_DEV).is_ok() {
display_development_details(err);
} else {
eprintln!("Notion is still a pre-alpha project, so we expect to run into some bugs,");
eprintln!("but we'd love to hear about them so we can fix them!");
Expand All @@ -78,6 +81,17 @@ pub fn display_unknown_error<E: Fail>(cx: ErrorContext, err: &E) {
}
}

fn display_development_details(err: &NotionError) {
eprintln!("{} {:?}", style("details:").yellow().bold(), err);
eprintln!();

// If `RUST_BACKTRACE` is set, then the backtrace will be included in the above output
// If not, we should let the user know how to see the backtrace
if env::var("RUST_BACKTRACE").is_err() {
eprintln!("Run with NOTION_DEV=1 and RUST_BACKTRACE=1 for a backtrace.");
}
}

#[derive(Eq, PartialEq, Ord, PartialOrd, Clone, Copy)]
pub enum Action {
Fetching,
Expand Down
34 changes: 20 additions & 14 deletions crates/notion-core/src/tool/binary.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::env::{args_os, ArgsOs};
use std::ffi::OsStr;
use std::env::ArgsOs;
use std::ffi::{OsStr, OsString};
use std::process::Command;

use super::{arg0, command_for, Tool};
use super::{command_for, Tool};
use crate::error::ErrorDetails;
use crate::path;
use crate::session::{ActivityKind, Session};
Expand All @@ -12,27 +12,33 @@ use notion_fail::{throw, Fallible};
/// Represents a delegated binary executable.
pub struct Binary(Command);

/// Represents the arguments needed for a binary executable
/// Both the executable name and the arguments to pass to it
pub struct BinaryArgs {
pub executable: OsString,
pub args: ArgsOs,
}

impl Tool for Binary {
fn new(session: &mut Session) -> Fallible<Self> {
session.add_event_start(ActivityKind::Binary);
type Arguments = BinaryArgs;

let mut args = args_os();
let exe = arg0(&mut args)?;
fn new(params: BinaryArgs, session: &mut Session) -> Fallible<Self> {
session.add_event_start(ActivityKind::Binary);

// first try to use the project toolchain
if let Some(project) = session.project()? {
// check if the executable is a direct dependency
if project.has_direct_bin(&exe)? {
if project.has_direct_bin(&params.executable)? {
// use the full path to the file
let mut path_to_bin = project.local_bin_dir();
path_to_bin.push(&exe);
path_to_bin.push(&params.executable);

// if we're in a pinned project, use the project's platform.
if let Some(ref platform) = session.project_platform()? {
let image = platform.checkout(session)?;
return Ok(Self::from_components(
&path_to_bin.as_os_str(),
args,
params.args,
&image.path()?,
));
}
Expand All @@ -42,7 +48,7 @@ impl Tool for Binary {
let image = platform.checkout(session)?;
return Ok(Self::from_components(
&path_to_bin.as_os_str(),
args,
params.args,
&image.path()?,
));
}
Expand All @@ -62,18 +68,18 @@ impl Tool for Binary {
let node_str = image.node.runtime.to_string();
let npm_str = image.node.npm.to_string();
let mut third_p_bin_dir = path::node_image_3p_bin_dir(&node_str, &npm_str)?;
third_p_bin_dir.push(&exe);
third_p_bin_dir.push(&params.executable);
return Ok(Self::from_components(
&third_p_bin_dir.as_os_str(),
args,
params.args,
&image.path()?,
));
};

// at this point, there is no project or user toolchain
// the user is executing a Notion shim that doesn't have a way to execute it
throw!(ErrorDetails::NoToolChain {
shim_name: exe.to_string_lossy().to_string(),
shim_name: params.executable.to_string_lossy().to_string(),
});
}

Expand Down
Loading

0 comments on commit 08fa783

Please sign in to comment.