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

Remove autoshimming #316

Merged
merged 3 commits into from
Mar 22, 2019
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
79 changes: 2 additions & 77 deletions crates/notion-core/src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ use crate::distro::node::{load_default_npm_version, NodeVersion};
use crate::error::ErrorDetails;
use crate::manifest::{serial, Manifest};
use crate::platform::PlatformSpec;
use crate::shim;
use notion_fail::{throw, Fallible, NotionError, ResultExt};
use notion_fail::{throw, Fallible, ResultExt};

fn is_node_root(dir: &Path) -> bool {
dir.join("package.json").is_file()
Expand Down Expand Up @@ -143,26 +142,6 @@ impl Project {
Ok(false)
}

/// Automatically shim the binaries of all direct dependencies of this project and
/// return a vector of any errors which occurred while doing so.
pub fn autoshim(&self) -> Vec<NotionError> {
let dependent_binaries = self.dependent_binary_names_fault_tolerant();
let mut errors = Vec::new();

for result in dependent_binaries {
match result {
Ok(name) => {
if let Err(error) = shim::create(&name) {
errors.push(error);
}
}
Err(error) => errors.push(error),
}
}

errors
}

/// Returns a mapping of the names to paths for all the binaries installed
/// by direct dependencies of the current project.
fn dependent_binaries(&self) -> Fallible<HashMap<String, String>> {
Expand All @@ -185,33 +164,6 @@ impl Project {
Ok(dependent_bins)
}

/// Gets the names of the binaries of all direct dependencies and returns them along
/// with any errors which occurred while doing so.
fn dependent_binary_names_fault_tolerant(&self) -> Vec<Fallible<String>> {
let mut results = Vec::new();
let dependencies = &self.manifest.merged_dependencies();
let dependency_paths = dependencies
.iter()
.map(|name| self.get_dependency_path(name));

for dependency_path in dependency_paths {
match Manifest::for_dir(&dependency_path) {
Ok(dependency) => {
for (name, _path) in dependency.bin {
results.push(Result::Ok(name.clone()))
}
}
Err(error) => {
if !error.to_string().contains("directory does not exist") {
results.push(Result::Err(error))
}
}
}
}

results
}

/// Convert dependency names to the path to each project.
fn get_dependency_path(&self, name: &String) -> PathBuf {
// ISSUE(158): Add support for Yarn Plug'n'Play.
Expand Down Expand Up @@ -287,7 +239,7 @@ impl Project {

#[cfg(test)]
pub mod tests {
use std::collections::{HashMap, HashSet};
use std::collections::HashMap;
use std::ffi::OsStr;
use std::path::PathBuf;

Expand Down Expand Up @@ -316,33 +268,6 @@ pub mod tests {
assert_eq!(dep_bins, expected_bins);
}

#[test]
fn gets_binary_names() {
let project = Project::for_dir(&fixture_path("basic")).unwrap().unwrap();
let binary_names = project.dependent_binary_names_fault_tolerant();
let mut expected = HashSet::new();

expected.insert("eslint".to_string());
expected.insert("rsvp".to_string());
expected.insert("bin-1".to_string());
expected.insert("bin-2".to_string());

let mut iterator = binary_names.iter();
let mut actual = HashSet::new();

while let Some(fallible) = iterator.next() {
match fallible {
Ok(binary_name) => {
actual.insert(binary_name.clone());
}

Err(error) => panic!("encountered error {:?}", error),
}
}

assert_eq!(actual, expected);
}

#[test]
fn local_bin_true() {
let project_path = fixture_path("basic");
Expand Down
2 changes: 1 addition & 1 deletion crates/notion-core/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use std::rc::Rc;

use crate::distro::node::NodeVersion;
use crate::distro::package::{self, PackageVersion, UserTool};
use crate::distro::package::{PackageVersion, UserTool};
use crate::distro::Fetched;
use crate::error::ErrorDetails;
use crate::hook::{HookConfig, LazyHookConfig, Publish};
Expand Down
23 changes: 7 additions & 16 deletions crates/notion-core/src/tool/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ use std::process::{Command, ExitStatus};
use crate::env::UNSAFE_GLOBAL;
use crate::error::ErrorDetails;
use crate::session::Session;
use crate::style;
use crate::version::VersionSpec;
use notion_fail::{Fallible, NotionError, ResultExt};
use notion_fail::{Fallible, ResultExt};

mod binary;
mod node;
Expand All @@ -27,10 +26,6 @@ use self::npm::Npm;
use self::npx::Npx;
use self::yarn::Yarn;

fn display_tool_error(err: &NotionError) {
style::display_error(style::ErrorContext::Shim, err);
}

pub enum ToolSpec {
Node(VersionSpec),
Yarn(VersionSpec),
Expand Down Expand Up @@ -107,18 +102,18 @@ pub fn execute_tool(session: &mut Session) -> Fallible<ExitStatus> {
// all the possible `Tool` implementations and fill it dynamically,
// as they have different sizes and associated types.
match &exe.to_str() {
Some("node") => Node::new(args, session)?.exec(session),
Some("npm") => Npm::new(args, session)?.exec(session),
Some("npx") => Npx::new(args, session)?.exec(session),
Some("yarn") => Yarn::new(args, session)?.exec(session),
Some("node") => Node::new(args, session)?.exec(),
Some("npm") => Npm::new(args, session)?.exec(),
Some("npx") => Npx::new(args, session)?.exec(),
Some("yarn") => Yarn::new(args, session)?.exec(),
_ => Binary::new(
BinaryArgs {
executable: exe,
args,
},
session,
)?
.exec(session),
.exec(),
}
}

Expand All @@ -135,14 +130,10 @@ pub trait Tool: Sized {
/// Extracts the `Command` from this tool.
fn command(self) -> Command;

/// Perform any tasks which must be run after the tool runs but before exiting.
fn finalize(_session: &Session, _maybe_status: &io::Result<ExitStatus>) {}

/// Delegates the current process to this tool.
fn exec(self, session: &Session) -> Fallible<ExitStatus> {
fn exec(self) -> Fallible<ExitStatus> {
let mut command = self.command();
let status = command.status();
Self::finalize(session, &status);
status.with_context(binary_exec_error)
}
}
Expand Down
17 changes: 2 additions & 15 deletions crates/notion-core/src/tool/npm.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use std::env::{args_os, ArgsOs};
use std::ffi::{OsStr, OsString};
use std::io;
use std::process::{Command, ExitStatus};
use std::process::Command;

use super::{command_for, display_tool_error, intercept_global_installs, Tool};
use super::{command_for, intercept_global_installs, Tool};
use crate::error::ErrorDetails;
use crate::session::{ActivityKind, Session};

Expand Down Expand Up @@ -46,18 +45,6 @@ impl Tool for Npm {
fn command(self) -> Command {
self.0
}

fn finalize(session: &Session, maybe_status: &io::Result<ExitStatus>) {
if let Ok(_) = maybe_status {
if let Ok(Some(project)) = session.project() {
let errors = project.autoshim();

for error in errors {
display_tool_error(&error);
}
}
}
}
}

fn is_global_npm_install() -> bool {
Expand Down
18 changes: 2 additions & 16 deletions crates/notion-core/src/tool/yarn.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use std::env::{args_os, ArgsOs};
use std::ffi::OsStr;
use std::io;
use std::process::{Command, ExitStatus};
use std::process::Command;

use super::{command_for, display_tool_error, intercept_global_installs, Tool};
use super::{command_for, intercept_global_installs, Tool};
use crate::error::ErrorDetails;
use crate::session::{ActivityKind, Session};

Expand Down Expand Up @@ -43,19 +42,6 @@ impl Tool for Yarn {
fn command(self) -> Command {
self.0
}

/// Perform any tasks which must be run after the tool runs but before exiting.
fn finalize(session: &Session, maybe_status: &io::Result<ExitStatus>) {
if let Ok(_) = maybe_status {
if let Ok(Some(project)) = session.project() {
let errors = project.autoshim();

for error in errors {
display_tool_error(&error);
}
}
}
}
}

fn is_global_yarn_add() -> bool {
Expand Down
9 changes: 0 additions & 9 deletions src/command/pin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use structopt::StructOpt;

use notion_core::error::ErrorDetails;
use notion_core::session::{ActivityKind, Session};
use notion_core::style::{display_error, ErrorContext};
use notion_core::tool::ToolSpec;
use notion_core::version::VersionSpec;
use notion_fail::{throw, ExitCode, Fallible};
Expand Down Expand Up @@ -37,14 +36,6 @@ impl Command for Pin {
ToolSpec::Package(_name, _version) => throw!(ErrorDetails::CannotPinPackage),
}

if let Some(project) = session.project()? {
let errors = project.autoshim();

for error in errors {
display_error(ErrorContext::Notion, &error);
}
}

session.add_event_end(ActivityKind::Pin, ExitCode::Success);
Ok(ExitCode::Success)
}
Expand Down