Skip to content

Commit

Permalink
Merge pull request #1230 from mikrostew/yarn3-hooks
Browse files Browse the repository at this point in the history
Add "format" to yarn.index hook
  • Loading branch information
mikrostew authored Jun 1, 2022
2 parents 97f4d47 + eee41af commit ba0a393
Show file tree
Hide file tree
Showing 11 changed files with 372 additions and 40 deletions.
20 changes: 20 additions & 0 deletions crates/volta-core/fixtures/hooks/format_github.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"node": {
"index": {
"prefix": "http://localhost/node/index/",
"format": "github"
}
},
"npm": {
"index": {
"prefix": "http://localhost/npm/index/",
"format": "github"
}
},
"yarn": {
"index": {
"prefix": "http://localhost/yarn/index/",
"format": "github"
}
}
}
20 changes: 20 additions & 0 deletions crates/volta-core/fixtures/hooks/format_npm.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"node": {
"index": {
"prefix": "http://localhost/node/index/",
"format": "npm"
}
},
"npm": {
"index": {
"prefix": "http://localhost/npm/index/",
"format": "npm"
}
},
"yarn": {
"index": {
"prefix": "http://localhost/yarn/index/",
"format": "npm"
}
}
}
14 changes: 14 additions & 0 deletions crates/volta-core/src/error/kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,11 @@ pub enum ErrorKind {
version: String,
},

/// Thrown when a format other than "npm" or "github" is given for yarn.index in the hooks
InvalidRegistryFormat {
format: String,
},

/// Thrown when a tool name is invalid per npm's rules.
InvalidToolName {
name: String,
Expand Down Expand Up @@ -811,6 +816,14 @@ To {action} the package '{version}', please use an explicit version such as '{ve
write!(f, "{}\n\n{}", error, wrapped_cta)
}

ErrorKind::InvalidRegistryFormat { format } => write!(
f,
"Unrecognized index registry format: '{}'
Please specify either 'npm' or 'github' for the format.",
format
),

ErrorKind::InvalidToolName { name, errors } => {
let indentation = " ";
let wrapped = match text_width() {
Expand Down Expand Up @@ -1438,6 +1451,7 @@ impl ErrorKind {
ErrorKind::InvalidHookOutput { .. } => ExitCode::ExecutionFailure,
ErrorKind::InvalidInvocation { .. } => ExitCode::InvalidArguments,
ErrorKind::InvalidInvocationOfBareVersion { .. } => ExitCode::InvalidArguments,
ErrorKind::InvalidRegistryFormat { .. } => ExitCode::ConfigurationError,
ErrorKind::InvalidToolName { .. } => ExitCode::InvalidArguments,
ErrorKind::LockAcquireError => ExitCode::FileSystemError,
ErrorKind::NoBundledNpm { .. } => ExitCode::ConfigurationError,
Expand Down
150 changes: 132 additions & 18 deletions crates/volta-core/src/hook/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::path::Path;
use crate::error::{Context, ErrorKind, Fallible};
use crate::layout::volta_home;
use crate::project::Project;
use crate::tool::{Node, Npm, Tool, Yarn};
use crate::tool::{Node, Npm, Tool};
use lazycell::LazyCell;
use log::debug;

Expand Down Expand Up @@ -50,7 +50,7 @@ impl LazyHookConfig {
pub struct HookConfig {
node: Option<ToolHooks<Node>>,
npm: Option<ToolHooks<Npm>>,
yarn: Option<ToolHooks<Yarn>>,
yarn: Option<YarnHooks>,
events: Option<EventHooks>,
}

Expand All @@ -66,6 +66,16 @@ pub struct ToolHooks<T: Tool> {
phantom: PhantomData<T>,
}

/// Volta hooks for Yarn
pub struct YarnHooks {
/// The hook for resolving the URL for a distro version
pub distro: Option<tool::DistroHook>,
/// The hook for resolving the URL for the latest version
pub latest: Option<tool::MetadataHook>,
/// The hook for resolving the Tool Index URL
pub index: Option<tool::YarnIndexHook>,
}

impl<T: Tool> ToolHooks<T> {
/// Extends this ToolHooks with another, giving precendence to the current instance
fn merge(self, other: Self) -> Self {
Expand All @@ -78,6 +88,17 @@ impl<T: Tool> ToolHooks<T> {
}
}

impl YarnHooks {
/// Extends this YarnHooks with another, giving precendence to the current instance
fn merge(self, other: Self) -> Self {
Self {
distro: self.distro.or(other.distro),
latest: self.latest.or(other.latest),
index: self.index.or(other.index),
}
}
}

macro_rules! merge_hooks {
($self:ident, $other:ident, $field:ident) => {
match ($self.$field, $other.$field) {
Expand All @@ -97,7 +118,7 @@ impl HookConfig {
self.npm.as_ref()
}

pub fn yarn(&self) -> Option<&ToolHooks<Yarn>> {
pub fn yarn(&self) -> Option<&YarnHooks> {
self.yarn.as_ref()
}

Expand Down Expand Up @@ -199,6 +220,26 @@ impl HookConfig {
}
}

/// Format of the registry used for Yarn (Npm or Github)
#[derive(PartialEq, Debug)]
pub enum RegistryFormat {
Npm,
Github,
}

impl RegistryFormat {
pub fn from_str(raw_format: &str) -> Fallible<RegistryFormat> {
match raw_format {
"npm" => Ok(RegistryFormat::Npm),
"github" => Ok(RegistryFormat::Github),
other => Err(ErrorKind::InvalidRegistryFormat {
format: String::from(other),
}
.into()),
}
}
}

/// Volta hooks related to events.
pub struct EventHooks {
/// The hook for publishing events, if any.
Expand All @@ -217,7 +258,7 @@ impl EventHooks {
#[cfg(test)]
pub mod tests {

use super::{tool, HookConfig, Publish};
use super::{tool, HookConfig, Publish, RegistryFormat};
use std::path::PathBuf;

fn fixture_path(fixture_dir: &str) -> PathBuf {
Expand Down Expand Up @@ -284,9 +325,12 @@ pub mod tests {
);
assert_eq!(
yarn.index,
Some(tool::MetadataHook::Bin {
bin: "/bin/to/yarn/index".to_string(),
base_path: fixture_dir,
Some(tool::YarnIndexHook {
format: RegistryFormat::Github,
metadata: tool::MetadataHook::Bin {
bin: "/bin/to/yarn/index".to_string(),
base_path: fixture_dir,
},
})
);
assert_eq!(
Expand Down Expand Up @@ -335,9 +379,10 @@ pub mod tests {
);
assert_eq!(
yarn.index,
Some(tool::MetadataHook::Prefix(
"http://localhost/yarn/index/".to_string()
))
Some(tool::YarnIndexHook {
format: RegistryFormat::Github,
metadata: tool::MetadataHook::Prefix("http://localhost/yarn/index/".to_string())
})
);
}

Expand Down Expand Up @@ -380,8 +425,71 @@ pub mod tests {
);
assert_eq!(
yarn.index,
Some(tool::MetadataHook::Template(
"http://localhost/yarn/index/{{version}}/".to_string()
Some(tool::YarnIndexHook {
format: RegistryFormat::Github,
metadata: tool::MetadataHook::Template(
"http://localhost/yarn/index/{{version}}/".to_string()
)
})
);
}

#[test]
fn test_from_str_format_npm() {
let fixture_dir = fixture_path("hooks");
let format_npm_file = fixture_dir.join("format_npm.json");
let hooks = HookConfig::from_file(&format_npm_file).unwrap().unwrap();
let yarn = hooks.yarn.unwrap();
let node = hooks.node.unwrap();
let npm = hooks.npm.unwrap();
assert_eq!(
yarn.index,
Some(tool::YarnIndexHook {
format: RegistryFormat::Npm,
metadata: tool::MetadataHook::Prefix("http://localhost/yarn/index/".to_string())
})
);
// node and npm don't have format
assert_eq!(
node.index,
Some(tool::MetadataHook::Prefix(
"http://localhost/node/index/".to_string()
))
);
assert_eq!(
npm.index,
Some(tool::MetadataHook::Prefix(
"http://localhost/npm/index/".to_string()
))
);
}

#[test]
fn test_from_str_format_github() {
let fixture_dir = fixture_path("hooks");
let format_github_file = fixture_dir.join("format_github.json");
let hooks = HookConfig::from_file(&format_github_file).unwrap().unwrap();
let yarn = hooks.yarn.unwrap();
let node = hooks.node.unwrap();
let npm = hooks.npm.unwrap();
assert_eq!(
yarn.index,
Some(tool::YarnIndexHook {
format: RegistryFormat::Github,
metadata: tool::MetadataHook::Prefix("http://localhost/yarn/index/".to_string())
})
);
// node and npm don't have format
assert_eq!(
node.index,
Some(tool::MetadataHook::Prefix(
"http://localhost/node/index/".to_string()
))
);
assert_eq!(
npm.index,
Some(tool::MetadataHook::Prefix(
"http://localhost/npm/index/".to_string()
))
);
}
Expand Down Expand Up @@ -436,9 +544,12 @@ pub mod tests {
);
assert_eq!(
yarn.index,
Some(tool::MetadataHook::Template(
"http://localhost/yarn/index/{{version}}/".to_string()
))
Some(tool::YarnIndexHook {
format: RegistryFormat::Github,
metadata: tool::MetadataHook::Template(
"http://localhost/yarn/index/{{version}}/".to_string()
)
})
);
assert_eq!(
merged_hooks.events.expect("No events config found").publish,
Expand Down Expand Up @@ -492,9 +603,12 @@ pub mod tests {
);
assert_eq!(
yarn.index,
Some(tool::MetadataHook::Template(
"http://localhost/yarn/index/{{version}}/".to_string()
))
Some(tool::YarnIndexHook {
format: RegistryFormat::Github,
metadata: tool::MetadataHook::Template(
"http://localhost/yarn/index/{{version}}/".to_string()
)
})
);
assert_eq!(
merged_hooks.events.expect("No events config found").publish,
Expand Down
Loading

0 comments on commit ba0a393

Please sign in to comment.