Skip to content

Commit

Permalink
Merge pull request #116 from dherman/tool-vs-toolchain
Browse files Browse the repository at this point in the history
Update terminology to match current thinking for MVP
  • Loading branch information
dherman authored Aug 15, 2018
2 parents f326b39 + 5380e82 commit acb48fd
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 91 deletions.
16 changes: 9 additions & 7 deletions crates/notion-core/src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ impl Project {
}

/// Returns true if the input binary name is a direct dependency of the input project
pub fn has_local_bin(&self, bin_name: &OsStr) -> Fallible<bool> {
pub fn has_direct_bin(&self, bin_name: &OsStr) -> Fallible<bool> {
let dep_bins = self.dependent_bins.get(&self)?;
if let Some(bin_name_str) = bin_name.to_str() {
if dep_bins.contains_key(bin_name_str) {
Expand Down Expand Up @@ -241,18 +241,20 @@ pub mod tests {
let project_path = fixture_path("basic");
let test_project = Project::for_dir(&project_path).unwrap().unwrap();
// eslint, rsvp, bin-1, and bin-2 are direct dependencies
assert!(test_project.has_local_bin(&OsStr::new("eslint")).unwrap());
assert!(test_project.has_local_bin(&OsStr::new("rsvp")).unwrap());
assert!(test_project.has_local_bin(&OsStr::new("bin-1")).unwrap());
assert!(test_project.has_local_bin(&OsStr::new("bin-2")).unwrap());
assert!(test_project.has_direct_bin(&OsStr::new("eslint")).unwrap());
assert!(test_project.has_direct_bin(&OsStr::new("rsvp")).unwrap());
assert!(test_project.has_direct_bin(&OsStr::new("bin-1")).unwrap());
assert!(test_project.has_direct_bin(&OsStr::new("bin-2")).unwrap());
}

#[test]
fn local_bin_false() {
let project_path = fixture_path("basic");
let test_project = Project::for_dir(&project_path).unwrap().unwrap();
// tsc and tsserver are installed, but not direct deps
assert!(!test_project.has_local_bin(&OsStr::new("tsc")).unwrap());
assert!(!test_project.has_local_bin(&OsStr::new("tsserver")).unwrap());
assert!(!test_project.has_direct_bin(&OsStr::new("tsc")).unwrap());
assert!(!test_project
.has_direct_bin(&OsStr::new("tsserver"))
.unwrap());
}
}
18 changes: 8 additions & 10 deletions crates/notion-core/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,9 @@ impl Session {
}

/// Produces the version of Node for the current session. If there is an
/// active project with Notion settings, this will ensure a compatible
/// version of Node is installed before returning. If there is no active
/// project with Notion settings, this produces the global version, which
/// may be `None`.
/// active pinned project, this will ensure that project's Node version is
/// installed before returning. If there is no active pinned project, this
/// produces the user version, which may be `None`.
pub fn current_node(&mut self) -> Fallible<Option<Version>> {
if let Some(ref project) = self.project {
let requirements = &project.manifest().node;
Expand All @@ -124,10 +123,10 @@ impl Session {
return Ok(Some(fetched.into_version()));
}

self.global_node()
self.user_node()
}

pub fn global_node(&self) -> Fallible<Option<Version>> {
pub fn user_node(&self) -> Fallible<Option<Version>> {
match env::var("NOTION_NODE_VERSION") {
Ok(s) => Ok(Some(Version::parse(&s[..]).unknown()?)),
Err(VarError::NotPresent) => Ok(self.catalog()?.node.default.clone()),
Expand All @@ -152,10 +151,9 @@ impl Session {
}

/// Produces the version of Yarn for the current session. If there is an
/// active project with Notion settings, this will ensure a compatible
/// version of Yarn is installed before returning. If there is no active
/// project with Notion settings, this produces the global version, which
/// may be `None`.
/// active pinned project, this will ensure that project's Yarn version is
/// installed before returning. If there is no active pinned project, this
/// produces the user version, which may be `None`.
pub fn current_yarn(&mut self) -> Fallible<Option<Version>> {
if let Some(ref project) = self.project {
let requirements = &project.manifest().yarn.clone().unwrap();
Expand Down
2 changes: 1 addition & 1 deletion crates/notion-core/src/tool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ impl Tool for Binary {
// we are in a Node project

// if this project has this as a local executable, use that
if project.has_local_bin(&exe)? {
if project.has_direct_bin(&exe)? {
// use the full path to the file
let mut path_to_bin = project.local_bin_dir();
path_to_bin.push(&exe);
Expand Down
55 changes: 29 additions & 26 deletions src/command/current.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,30 @@ use command::{Command, CommandName, Help};

#[derive(Debug, Deserialize)]
pub(crate) struct Args {
flag_local: bool,
flag_global: bool,
flag_project: bool,
flag_user: bool,
}

pub(crate) enum Current {
Help,
Local,
Global,
Project,
User,
All,
}

impl Command for Current {
type Args = Args;

const USAGE: &'static str = "
Display the currently activated toolchain
Display the currently activated Node version
Usage:
notion current [options]
Options:
-h, --help Display this message
-l, --local Display local toolchain
-g, --global Display global toolchain
-p, --project Display the current project's Node version
-u, --user Display the user's Node version
";

fn help() -> Self {
Expand All @@ -41,14 +41,14 @@ Options:
fn parse(
_: Notion,
Args {
flag_local,
flag_global,
flag_project,
flag_user,
}: Args,
) -> Fallible<Current> {
Ok(if !flag_local && flag_global {
Current::Local
} else if flag_local && !flag_global {
Current::Global
Ok(if !flag_project && flag_user {
Current::User
} else if flag_project && !flag_user {
Current::Project
} else {
Current::All
})
Expand All @@ -59,31 +59,34 @@ Options:

let result = match self {
Current::Help => Help::Command(CommandName::Current).run(session),
Current::Local => Ok(local(&session)?
Current::Project => Ok(project_node_version(&session)?
.map(|version| {
println!("v{}", version);
})
.is_some()),
Current::Global => Ok(global(session)?
Current::User => Ok(user_node_version(session)?
.map(|version| {
println!("v{}", version);
})
.is_some()),
Current::All => {
let (local, global) = (local(&session)?, global(&session)?);
let (project, user) = (
project_node_version(&session)?,
user_node_version(&session)?,
);

let global_active = local.is_none() && global.is_some();
let any = local.is_some() || global.is_some();
let user_active = project.is_none() && user.is_some();
let any = project.is_some() || user.is_some();

for version in local {
println!("local: v{} (active)", version);
for version in project {
println!("project: v{} (active)", version);
}

for version in global {
for version in user {
println!(
"global: v{}{}",
"user: v{}{}",
version,
if global_active { " (active)" } else { "" }
if user_active { " (active)" } else { "" }
);
}

Expand All @@ -95,7 +98,7 @@ Options:
}
}

fn local(session: &Session) -> Fallible<Option<String>> {
fn project_node_version(session: &Session) -> Fallible<Option<String>> {
let project = session.project();
let project = match project {
Some(ref project) => project,
Expand All @@ -109,6 +112,6 @@ fn local(session: &Session) -> Fallible<Option<String>> {
Ok(catalog.node.resolve_local(&req).map(|v| v.to_string()))
}

fn global(session: &Session) -> Fallible<Option<String>> {
Ok(session.global_node()?.clone().map(|v| v.to_string()))
fn user_node_version(session: &Session) -> Fallible<Option<String>> {
Ok(session.user_node()?.clone().map(|v| v.to_string()))
}
14 changes: 7 additions & 7 deletions src/command/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use {CliParseError, Notion};

#[derive(Debug, Deserialize)]
pub(crate) struct Args {
arg_toolchain: String,
arg_tool: String,
arg_version: String,
}

Expand All @@ -23,10 +23,10 @@ impl Command for Fetch {
type Args = Args;

const USAGE: &'static str = "
Fetch a toolchain to the local machine
Fetch a tool to the local machine
Usage:
notion fetch <toolchain> <version>
notion fetch <tool> <version>
notion fetch -h | --help
Options:
Expand All @@ -40,17 +40,17 @@ Options:
fn parse(
_: Notion,
Args {
arg_toolchain,
arg_tool,
arg_version,
}: Args,
) -> Fallible<Self> {
match &arg_toolchain[..] {
match &arg_tool[..] {
"node" => Ok(Fetch::Node(parse_requirements(&arg_version)?)),
"yarn" => Ok(Fetch::Yarn(parse_requirements(&arg_version)?)),
ref toolchain => {
ref tool => {
throw!(CliParseError {
usage: None,
error: format!("no such toolchain: `{}`", toolchain),
error: format!("no such tool: `{}`", tool),
});
}
}
Expand Down
14 changes: 7 additions & 7 deletions src/command/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use command::{Command, CommandName, Help};

#[derive(Debug, Deserialize)]
pub(crate) struct Args {
arg_package: String,
arg_tool: String,
arg_version: String,
}

Expand All @@ -24,10 +24,10 @@ impl Command for Install {
type Args = Args;

const USAGE: &'static str = "
Install a global package
Install a tool in the user toolchain
Usage:
notion install <package> <version>
notion install <tool> <version>
notion install -h | --help
Options:
Expand All @@ -41,15 +41,15 @@ Options:
fn parse(
_: Notion,
Args {
arg_package,
arg_tool,
arg_version,
}: Args,
) -> Fallible<Self> {
match &arg_package[..] {
match &arg_tool[..] {
"node" => Ok(Install::Node(parse_requirements(&arg_version)?)),
"yarn" => Ok(Install::Yarn(parse_requirements(&arg_version)?)),
ref package => Ok(Install::Other {
name: package.to_string(),
ref tool => Ok(Install::Other {
name: tool.to_string(),
version: parse_requirements(&arg_version)?,
}),
}
Expand Down
Loading

0 comments on commit acb48fd

Please sign in to comment.