Skip to content

Commit

Permalink
Merge pull request #111 from dherman/rename-more-commands
Browse files Browse the repository at this point in the history
Changes to command UX
  • Loading branch information
dherman authored Aug 8, 2018
2 parents aa6939f + 3cc581b commit 371d87c
Show file tree
Hide file tree
Showing 13 changed files with 143 additions and 209 deletions.
10 changes: 4 additions & 6 deletions crates/notion-core/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use std::time::{SystemTime, UNIX_EPOCH};

use monitor::LazyMonitor;
use notion_fail::{Fallible, NotionError};
use session::ActivityKind;
use plugin::Publish;
use session::ActivityKind;

// the Event data that is serialized to JSON and sent the plugin
#[derive(Serialize)]
Expand Down Expand Up @@ -124,13 +124,11 @@ impl EventLog {

pub fn publish(&mut self, plugin: Option<&Publish>) {
match plugin {
Some(&Publish::Url(_)) => { unimplemented!() }
Some(&Publish::Url(_)) => unimplemented!(),
Some(&Publish::Bin(ref command)) => {
self.monitor
.get_mut(command)
.send_events(&self.events);
self.monitor.get_mut(command).send_events(&self.events);
}
None => { }
None => {}
}
}
}
2 changes: 1 addition & 1 deletion crates/notion-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ extern crate winfolder;

pub mod catalog;
pub mod config;
mod distro;
pub mod env;
mod event;
mod distro;
pub mod manifest;
pub mod monitor;
mod package_info;
Expand Down
3 changes: 1 addition & 2 deletions crates/notion-core/src/monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ impl LazyMonitor {

/// Forces creating a monitor and returns a mutable reference to it.
pub fn get_mut(&mut self, command: &str) -> &mut Monitor {
self.monitor
.borrow_mut_with(|| Monitor::new(command))
self.monitor.borrow_mut_with(|| Monitor::new(command))
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/notion-core/src/serial/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl EventsConfig {
Some(p.into_publish()?)
} else {
None
}
},
})
}
}
Expand Down
15 changes: 14 additions & 1 deletion crates/notion-core/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use semver::{Version, VersionReq};
#[derive(Eq, PartialEq, Ord, PartialOrd, Clone, Copy)]
pub enum ActivityKind {
Fetch,
Install,
Uninstall,
Current,
Deactivate,
Expand All @@ -38,6 +39,7 @@ impl Display for ActivityKind {
fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> {
let s = match self {
&ActivityKind::Fetch => "fetch",
&ActivityKind::Install => "install",
&ActivityKind::Uninstall => "uninstall",
&ActivityKind::Current => "current",
&ActivityKind::Deactivate => "deactivate",
Expand Down Expand Up @@ -181,6 +183,14 @@ impl Session {
catalog.fetch_yarn(matching, config)
}

/// Sets the default Yarn version to one matching the specified semantic versioning
/// requirements.
pub fn set_default_yarn(&mut self, matching: &VersionReq) -> Fallible<()> {
let catalog = self.catalog.get_mut()?;
let config = self.config.get()?;
catalog.set_default_yarn(matching, config)
}

pub fn add_event_start(&mut self, activity_kind: ActivityKind) {
self.event_log.add_event_start(activity_kind)
}
Expand All @@ -206,5 +216,8 @@ impl Session {

fn publish_plugin(config: &LazyConfig) -> Fallible<Option<&Publish>> {
let config = config.get()?;
Ok(config.events.as_ref().and_then(|events| events.publish.as_ref()))
Ok(config
.events
.as_ref()
.and_then(|events| events.publish.as_ref()))
}
67 changes: 0 additions & 67 deletions src/command/default.rs

This file was deleted.

10 changes: 8 additions & 2 deletions src/command/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use notion_core::serial::version::parse_requirements;
use notion_core::session::{ActivityKind, Session};
use notion_fail::Fallible;

use {CliParseError, Notion};
use command::{Command, CommandName, Help};
use {CliParseError, Notion};

#[derive(Debug, Deserialize)]
pub(crate) struct Args {
Expand Down Expand Up @@ -37,7 +37,13 @@ Options:
Fetch::Help
}

fn parse(_: Notion, Args { arg_toolchain, arg_version }: Args) -> Fallible<Self> {
fn parse(
_: Notion,
Args {
arg_toolchain,
arg_version,
}: Args,
) -> Fallible<Self> {
match &arg_toolchain[..] {
"node" => Ok(Fetch::Node(parse_requirements(&arg_version)?)),
"yarn" => Ok(Fetch::Yarn(parse_requirements(&arg_version)?)),
Expand Down
7 changes: 3 additions & 4 deletions src/command/help.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use notion_core::session::{ActivityKind, Session};
use notion_fail::Fallible;

use command::{Command, CommandName, Config, Current, Deactivate, Default, Fetch, Shim,
Uninstall, Use, Version};
use command::{Command, CommandName, Config, Current, Deactivate, Fetch, Install, Shim, Use,
Version};
use {CliParseError, Notion};

#[derive(Debug, Deserialize)]
Expand Down Expand Up @@ -59,11 +59,10 @@ Options:
Help::Command(CommandName::Config) => Config::USAGE,
Help::Command(CommandName::Current) => Current::USAGE,
Help::Command(CommandName::Deactivate) => Deactivate::USAGE,
Help::Command(CommandName::Default) => Default::USAGE,
Help::Command(CommandName::Help) => Help::USAGE,
Help::Command(CommandName::Version) => Version::USAGE,
Help::Command(CommandName::Fetch) => Fetch::USAGE,
Help::Command(CommandName::Uninstall) => Uninstall::USAGE,
Help::Command(CommandName::Install) => Install::USAGE,
Help::Command(CommandName::Shim) => Shim::USAGE,
}
);
Expand Down
78 changes: 78 additions & 0 deletions src/command/install.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
use semver::VersionReq;

use notion_core::serial::version::parse_requirements;
use notion_core::session::{ActivityKind, Session};
use notion_fail::Fallible;

use Notion;
use command::{Command, CommandName, Help};

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

pub(crate) enum Install {
Help,
Node(VersionReq),
Yarn(VersionReq),
Other { name: String, version: VersionReq },
}

impl Command for Install {
type Args = Args;

const USAGE: &'static str = "
Install a global package
Usage:
notion install <package> <version>
notion install -h | --help
Options:
-h, --help Display this message
";

fn help() -> Self {
Install::Help
}

fn parse(
_: Notion,
Args {
arg_package,
arg_version,
}: Args,
) -> Fallible<Self> {
match &arg_package[..] {
"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(),
version: parse_requirements(&arg_version)?,
}),
}
}

fn run(self, session: &mut Session) -> Fallible<bool> {
session.add_event_start(ActivityKind::Install);
match self {
Install::Help => {
Help::Command(CommandName::Install).run(session)?;
}
Install::Node(requirements) => {
session.set_default_node(&requirements)?;
}
Install::Yarn(requirements) => {
session.set_default_yarn(&requirements)?;
}
Install::Other {
name: _,
version: _,
} => unimplemented!(),
};
session.add_event_end(ActivityKind::Install, 0);
Ok(true)
}
}
19 changes: 7 additions & 12 deletions src/command/mod.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
mod config;
mod current;
mod deactivate;
mod default;
mod help;
mod fetch;
mod help;
mod install;
mod shim;
mod uninstall;
mod use_;
mod version;

pub(crate) use self::config::Config;
pub(crate) use self::current::Current;
pub(crate) use self::deactivate::Deactivate;
pub(crate) use self::default::Default;
pub(crate) use self::help::Help;
pub(crate) use self::fetch::Fetch;
pub(crate) use self::help::Help;
pub(crate) use self::install::Install;
pub(crate) use self::shim::Shim;
pub(crate) use self::uninstall::Uninstall;
pub(crate) use self::use_::Use;
pub(crate) use self::version::Version;

Expand All @@ -35,12 +33,11 @@ use std::str::FromStr;
#[derive(Debug, Deserialize, Clone, Copy)]
pub(crate) enum CommandName {
Fetch,
Uninstall,
Install,
Use,
Config,
Current,
Deactivate,
Default,
Shim,
Help,
Version,
Expand All @@ -53,11 +50,10 @@ impl Display for CommandName {
"{}",
match *self {
CommandName::Fetch => "fetch",
CommandName::Uninstall => "uninstall",
CommandName::Install => "install",
CommandName::Use => "use",
CommandName::Config => "config",
CommandName::Deactivate => "deactivate",
CommandName::Default => "default",
CommandName::Current => "current",
CommandName::Shim => "shim",
CommandName::Help => "help",
Expand All @@ -73,12 +69,11 @@ impl FromStr for CommandName {
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(match s {
"fetch" => CommandName::Fetch,
"uninstall" => CommandName::Uninstall,
"install" => CommandName::Install,
"use" => CommandName::Use,
"config" => CommandName::Config,
"current" => CommandName::Current,
"deactivate" => CommandName::Deactivate,
"default" => CommandName::Default,
"shim" => CommandName::Shim,
"help" => CommandName::Help,
"version" => CommandName::Version,
Expand Down
Loading

0 comments on commit 371d87c

Please sign in to comment.