Skip to content

Commit

Permalink
Merge pull request #110 from dherman/rename-commands
Browse files Browse the repository at this point in the history
Change `notion install` to `notion fetch`
  • Loading branch information
dherman authored Aug 5, 2018
2 parents b5a625c + 6288243 commit 2c974c2
Show file tree
Hide file tree
Showing 16 changed files with 199 additions and 178 deletions.
66 changes: 33 additions & 33 deletions crates/notion-core/src/catalog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ use tempfile::NamedTempFile;
use toml;

use config::{Config, ToolConfig};
use installer::node::NodeInstaller;
use installer::yarn::YarnInstaller;
use installer::{Install, Installed};
use distro::node::NodeDistro;
use distro::yarn::YarnDistro;
use distro::{Distro, Fetched};
use notion_fail::{Fallible, NotionError, NotionFail, ResultExt};
use path::{self, user_catalog_file};
use semver::{Version, VersionReq};
Expand Down Expand Up @@ -60,18 +60,18 @@ impl LazyCatalog {
}
}

pub struct Collection<I: Install> {
pub struct Collection<D: Distro> {
/// The currently activated Node version, if any.
pub default: Option<Version>,

// A sorted collection of the available versions in the catalog.
pub versions: BTreeSet<Version>,

pub phantom: PhantomData<I>,
pub phantom: PhantomData<D>,
}

pub type NodeCollection = Collection<NodeInstaller>;
pub type YarnCollection = Collection<YarnInstaller>;
pub type NodeCollection = Collection<NodeDistro>;
pub type YarnCollection = Collection<YarnDistro>;

/// The catalog of tool versions available locally.
pub struct Catalog {
Expand Down Expand Up @@ -102,8 +102,8 @@ impl Catalog {

/// Sets the default Node version to one matching the specified semantic versioning requirements.
pub fn set_default_node(&mut self, matching: &VersionReq, config: &Config) -> Fallible<()> {
let installed = self.install_node(matching, config)?;
let version = Some(installed.into_version());
let fetched = self.fetch_node(matching, config)?;
let version = Some(fetched.into_version());

if self.node.default != version {
self.node.default = version;
Expand All @@ -113,17 +113,17 @@ impl Catalog {
Ok(())
}

/// Installs a Node version matching the specified semantic versioning requirements.
pub fn install_node(&mut self, matching: &VersionReq, config: &Config) -> Fallible<Installed> {
let installer = self.node.resolve_remote(&matching, config.node.as_ref())?;
let installed = installer.install(&self.node).unknown()?;
/// Fetches a Node version matching the specified semantic versioning requirements.
pub fn fetch_node(&mut self, matching: &VersionReq, config: &Config) -> Fallible<Fetched> {
let distro = self.node.resolve_remote(&matching, config.node.as_ref())?;
let fetched = distro.fetch(&self.node).unknown()?;

if let &Installed::Now(ref version) = &installed {
if let &Fetched::Now(ref version) = &fetched {
self.node.versions.insert(version.clone());
self.save()?;
}

Ok(installed)
Ok(fetched)
}

/// Uninstalls a specific Node version from the local catalog.
Expand Down Expand Up @@ -152,8 +152,8 @@ impl Catalog {
// And potentially share code between node and yarn
/// Sets the default Yarn version to one matching the specified semantic versioning requirements.
pub fn set_default_yarn(&mut self, matching: &VersionReq, config: &Config) -> Fallible<()> {
let installed = self.install_yarn(matching, config)?;
let version = Some(installed.into_version());
let fetched = self.fetch_yarn(matching, config)?;
let version = Some(fetched.into_version());

if self.yarn.default != version {
self.yarn.default = version;
Expand All @@ -163,17 +163,17 @@ impl Catalog {
Ok(())
}

/// Installs a Yarn version matching the specified semantic versioning requirements.
pub fn install_yarn(&mut self, matching: &VersionReq, config: &Config) -> Fallible<Installed> {
let installer = self.yarn.resolve_remote(&matching, config.yarn.as_ref())?;
let installed = installer.install(&self.yarn).unknown()?;
/// Fetches a Yarn version matching the specified semantic versioning requirements.
pub fn fetch_yarn(&mut self, matching: &VersionReq, config: &Config) -> Fallible<Fetched> {
let distro = self.yarn.resolve_remote(&matching, config.yarn.as_ref())?;
let fetched = distro.fetch(&self.yarn).unknown()?;

if let &Installed::Now(ref version) = &installed {
if let &Fetched::Now(ref version) = &fetched {
self.yarn.versions.insert(version.clone());
self.save()?;
}

Ok(installed)
Ok(fetched)
}

/// Uninstalls a specific Yarn version from the local catalog.
Expand Down Expand Up @@ -229,7 +229,7 @@ impl NotionFail for NoYarnVersionFoundError {
}
}

impl<I: Install> Collection<I> {
impl<D: Distro> Collection<D> {
/// Tests whether this Collection contains the specified Tool version.
pub fn contains(&self, version: &Version) -> bool {
self.versions.contains(version)
Expand All @@ -246,9 +246,9 @@ impl<I: Install> Collection<I> {
}
}

pub trait Resolve<I: Install> {
pub trait Resolve<D: Distro> {
/// Resolves the specified semantic versioning requirements from a remote distributor.
fn resolve_remote(&self, matching: &VersionReq, config: Option<&ToolConfig<I>>) -> Fallible<I> {
fn resolve_remote(&self, matching: &VersionReq, config: Option<&ToolConfig<D>>) -> Fallible<D> {
match config {
Some(ToolConfig {
resolve: Some(ref plugin),
Expand All @@ -259,7 +259,7 @@ pub trait Resolve<I: Install> {
}

/// Resolves the specified semantic versioning requirements from the public distributor (e.g. `https://nodejs.org`).
fn resolve_public(&self, matching: &VersionReq) -> Fallible<I>;
fn resolve_public(&self, matching: &VersionReq) -> Fallible<D>;
}

/// Thrown when the public registry for Node or Yarn could not be downloaded.
Expand All @@ -286,8 +286,8 @@ impl NotionFail for RegistryFetchError {
}
}

impl Resolve<NodeInstaller> for NodeCollection {
fn resolve_public(&self, matching: &VersionReq) -> Fallible<NodeInstaller> {
impl Resolve<NodeDistro> for NodeCollection {
fn resolve_public(&self, matching: &VersionReq) -> Fallible<NodeDistro> {
let index: Index = match read_cached_opt().unknown()? {
Some(serial) => serial,
None => {
Expand Down Expand Up @@ -341,7 +341,7 @@ impl Resolve<NodeInstaller> for NodeCollection {
.next()
.map(|(k, _)| k.clone());
if let Some(version) = version {
NodeInstaller::public(version)
NodeDistro::public(version)
} else {
throw!(NoNodeVersionFoundError {
matching: matching.clone(),
Expand All @@ -350,9 +350,9 @@ impl Resolve<NodeInstaller> for NodeCollection {
}
}

impl Resolve<YarnInstaller> for YarnCollection {
impl Resolve<YarnDistro> for YarnCollection {
/// Resolves the specified semantic versioning requirements from the public distributor.
fn resolve_public(&self, matching: &VersionReq) -> Fallible<YarnInstaller> {
fn resolve_public(&self, matching: &VersionReq) -> Fallible<YarnDistro> {
let spinner = progress_spinner(&format!(
"Fetching public registry: {}",
PUBLIC_YARN_VERSION_INDEX
Expand All @@ -369,7 +369,7 @@ impl Resolve<YarnInstaller> for YarnCollection {

if let Some(matching_version) = matching_version {
let version = Version::parse(&matching_version).unwrap();
YarnInstaller::public(version)
YarnDistro::public(version)
} else {
throw!(NoYarnVersionFoundError {
matching: matching.clone(),
Expand Down
14 changes: 7 additions & 7 deletions crates/notion-core/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ use std::str::FromStr;
use lazycell::LazyCell;
use toml;

use installer::Install;
use installer::node::NodeInstaller;
use installer::yarn::YarnInstaller;
use distro::Distro;
use distro::node::NodeDistro;
use distro::yarn::YarnDistro;
use notion_fail::{Fallible, NotionError, ResultExt};
use path::user_config_file;
use plugin;
Expand Down Expand Up @@ -37,18 +37,18 @@ impl LazyConfig {

/// Notion configuration settings.
pub struct Config {
pub node: Option<ToolConfig<NodeInstaller>>,
pub yarn: Option<ToolConfig<YarnInstaller>>,
pub node: Option<ToolConfig<NodeDistro>>,
pub yarn: Option<ToolConfig<YarnDistro>>,
}

/// Notion configuration settings relating to the Node executable.
pub struct ToolConfig<I: Install> {
pub struct ToolConfig<D: Distro> {
/// The plugin for resolving Node versions, if any.
pub resolve: Option<plugin::ResolvePlugin>,
/// The plugin for listing the set of Node versions available on the remote server, if any.
pub ls_remote: Option<plugin::LsRemote>,

pub phantom: PhantomData<I>,
pub phantom: PhantomData<D>,
}

impl Config {
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! Provides types for installing tools into the Notion catalog.
//! Provides types for fetching tool distributions into the Notion catalog.

mod error;
pub mod node;
Expand All @@ -10,43 +10,43 @@ use semver::Version;
use std::fs::File;

/// The result of a requested installation.
pub enum Installed {
pub enum Fetched {
/// Indicates that the given tool was already installed.
Already(Version),
/// Indicates that the given tool was not already installed but has now been installed.
Now(Version),
}

impl Installed {
impl Fetched {
/// Consumes this value and produces the installed version.
pub fn into_version(self) -> Version {
match self {
Installed::Already(version) | Installed::Now(version) => version,
Fetched::Already(version) | Fetched::Now(version) => version,
}
}

/// Produces a reference to the installed version.
pub fn version(&self) -> &Version {
match self {
&Installed::Already(ref version) | &Installed::Now(ref version) => version,
&Fetched::Already(ref version) | &Fetched::Now(ref version) => version,
}
}
}

pub trait Install: Sized {
/// Provision an `Installer` from the public distributor (e.g. `https://nodejs.org`).
pub trait Distro: Sized {
/// Provision a distribution from the public distributor (e.g. `https://nodejs.org`).
fn public(version: Version) -> Fallible<Self>;

/// Provision an `Installer` from a remote distributor.
/// Provision a distribution from a remote distributor.
fn remote(version: Version, url: &str) -> Fallible<Self>;

/// Provision an `Installer` from the filesystem.
/// Provision a distribution from the filesystem.
fn cached(version: Version, file: File) -> Fallible<Self>;

/// Produces a reference to this installer's Tool version.
/// Produces a reference to this distro's Tool version.
fn version(&self) -> &Version;

/// Installs this version of the Tool. (It is left to the responsibility of the `Collection`
/// to update its state after installation succeeds.)
fn install(self, catalog: &Collection<Self>) -> Fallible<Installed>;
/// Fetches this version of the Tool. (It is left to the responsibility of the `Collection`
/// to update its state after fetching succeeds.)
fn fetch(self, catalog: &Collection<Self>) -> Fallible<Fetched>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
use std::fs::{rename, File};
use std::string::ToString;

use super::{Install, Installed};
use super::{Distro, Fetched};
use catalog::NodeCollection;
use installer::error::DownloadError;
use distro::error::DownloadError;
use node_archive::{self, Archive};
use path;
use style::{progress_bar, Action};
Expand All @@ -15,54 +15,54 @@ use semver::Version;

const PUBLIC_NODE_SERVER_ROOT: &'static str = "https://nodejs.org/dist/";

/// A provisioned Node installer.
pub struct NodeInstaller {
/// A provisioned Node distribution.
pub struct NodeDistro {
archive: Box<Archive>,
version: Version,
}

impl Install for NodeInstaller {
/// Provision an `Installer` from the public Node distributor (`https://nodejs.org`).
impl Distro for NodeDistro {
/// Provision a Node distribution from the public Node distributor (`https://nodejs.org`).
fn public(version: Version) -> Fallible<Self> {
let archive_file = path::node_archive_file(&version.to_string());
let url = format!("{}v{}/{}", PUBLIC_NODE_SERVER_ROOT, version, &archive_file);
NodeInstaller::remote(version, &url)
NodeDistro::remote(version, &url)
}

/// Provision an `Installer` from a remote distributor.
/// Provision a Node distribution from a remote distributor.
fn remote(version: Version, url: &str) -> Fallible<Self> {
let archive_file = path::node_archive_file(&version.to_string());
let cache_file = path::node_cache_dir()?.join(&archive_file);

if cache_file.is_file() {
return NodeInstaller::cached(version, File::open(cache_file).unknown()?);
return NodeDistro::cached(version, File::open(cache_file).unknown()?);
}

Ok(NodeInstaller {
Ok(NodeDistro {
archive: node_archive::fetch(url, &cache_file)
.with_context(DownloadError::for_version(version.to_string()))?,
version: version,
})
}

/// Provision an `Installer` from the filesystem.
/// Provision a Node distribution from the filesystem.
fn cached(version: Version, file: File) -> Fallible<Self> {
Ok(NodeInstaller {
Ok(NodeDistro {
archive: node_archive::load(file).unknown()?,
version: version,
})
}

/// Produces a reference to this installer's Node version.
/// Produces a reference to this distribution's Node version.
fn version(&self) -> &Version {
&self.version
}

/// Installs this version of Node. (It is left to the responsibility of the `NodeCollection`
/// to update its state after installation succeeds.)
fn install(self, collection: &NodeCollection) -> Fallible<Installed> {
/// Fetches this version of Node. (It is left to the responsibility of the `NodeCollection`
/// to update its state after fetching succeeds.)
fn fetch(self, collection: &NodeCollection) -> Fallible<Fetched> {
if collection.contains(&self.version) {
return Ok(Installed::Already(self.version));
return Ok(Fetched::Already(self.version));
}

let dest = path::node_versions_dir()?;
Expand All @@ -87,6 +87,6 @@ impl Install for NodeInstaller {
).unknown()?;

bar.finish_and_clear();
Ok(Installed::Now(self.version))
Ok(Fetched::Now(self.version))
}
}
Loading

0 comments on commit 2c974c2

Please sign in to comment.