diff --git a/src/bin/cog.rs b/src/bin/cog.rs index 58e60da5..cc4254c4 100644 --- a/src/bin/cog.rs +++ b/src/bin/cog.rs @@ -59,10 +59,10 @@ fn main() -> Result<()> { }; let pre = subcommand.value_of("pre"); - let hooks_config = subcommand.value_of("config"); + let hooks_profile = subcommand.value_of("hook-profile"); // TODO mode to cli - cocogitto.create_version(increment, pre, hooks_config)? + cocogitto.create_version(increment, pre, hooks_profile)? } VERIFY => { let subcommand = matches.subcommand_matches(VERIFY).unwrap(); @@ -325,10 +325,10 @@ fn app<'a, 'b>() -> App<'a, 'b> { .takes_value(true), ) .arg( - Arg::with_name("config") - .help("Specify path to hooks config file") - .short("c") - .long("config") + Arg::with_name("hook-profile") + .help("Specify the bump profile hooks to run") + .short("hp") + .long("hook-profile") .takes_value(true), ) .display_order(6); diff --git a/src/lib.rs b/src/lib.rs index d1ba717f..66f4ce53 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -53,7 +53,7 @@ lazy_static! { // unwrapping in case of error. pub static ref COMMITS_METADATA: CommitsMetadata = { if let Ok(repo) = Repository::open(".") { - Settings::get(&repo, None).unwrap_or_default().commit_types() + Settings::get(&repo).unwrap_or_default().commit_types() } else { Settings::default().commit_types() } @@ -61,7 +61,7 @@ lazy_static! { static ref REMOTE_URL: Option = { if let Ok(repo) = Repository::open(".") { - Settings::get(&repo, None).unwrap_or_default().github + Settings::get(&repo).unwrap_or_default().github } else { None } @@ -69,7 +69,7 @@ lazy_static! { static ref AUTHORS: Vec = { if let Ok(repo) = Repository::open(".") { - Settings::get(&repo, None).unwrap_or_default().authors + Settings::get(&repo).unwrap_or_default().authors } else { vec![] } @@ -137,7 +137,7 @@ pub struct CocoGitto { impl CocoGitto { pub fn get() -> Result { let repository = Repository::open(&std::env::current_dir()?)?; - let settings = Settings::get(&repository, None)?; + let settings = Settings::get(&repository)?; let changelog_path = settings .changelog_path .unwrap_or_else(|| PathBuf::from("CHANGELOG.md")); @@ -594,17 +594,31 @@ impl CocoGitto { &self, hook_type: HookType, next_version: &str, - hooks_config: Option<&str>, + hook_profile: Option<&str>, ) -> Result<()> { - let settings = Settings::get(&self.repository, hooks_config)?; - - let hooks: Vec = settings - .get_hooks(hook_type) - .iter() - .map(|s| s.parse()) - .enumerate() - .map(|(idx, result)| result.context(format!("Cannot parse hook at index {}", idx))) - .try_collect()?; + let settings = Settings::get(&self.repository)?; + + let hooks: Vec = match hook_profile { + Some(profile) => settings + .get_profile_hook(profile, hook_type) + .iter() + .map(|s| s.parse()) + .enumerate() + .map(|(idx, result)| { + result.context(format!( + "Cannot parse bump profile {} hook at index {}", + profile, idx + )) + }) + .try_collect()?, + None => settings + .get_hooks(hook_type) + .iter() + .map(|s| s.parse()) + .enumerate() + .map(|(idx, result)| result.context(format!("Cannot parse hook at index {}", idx))) + .try_collect()?, + }; for mut hook in hooks { hook.insert_version(next_version)?; diff --git a/src/settings.rs b/src/settings.rs index ba1eb440..d279bd64 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -28,6 +28,8 @@ pub(crate) struct Settings { pub authors: AuthorSettings, #[serde(default)] pub commit_types: CommitsMetadataSettings, + #[serde(default)] + pub bump_profiles: HashMap, } #[derive(Debug, Deserialize, Serialize, Clone)] @@ -40,21 +42,30 @@ impl Default for Settings { fn default() -> Self { Settings { changelog_path: Some(PathBuf::from("CHANGELOG.md")), + github: Default::default(), + pre_bump_hooks: Default::default(), + post_bump_hooks: Default::default(), + authors: Default::default(), commit_types: Default::default(), - pre_bump_hooks: vec![], - post_bump_hooks: vec![], - authors: vec![], - github: None, + bump_profiles: Default::default(), } } } +#[derive(Debug, Deserialize, Serialize, Default)] +pub struct BumpProfile { + #[serde(default)] + pub pre_bump_hooks: Vec, + #[serde(default)] + pub post_bump_hooks: Vec, +} + impl Settings { // Fails only if config exists and is malformed - pub(crate) fn get(repository: &Repository, config_path: Option<&str>) -> Result { + pub(crate) fn get(repository: &Repository) -> Result { match repository.get_repo_dir() { Some(repo_path) => { - let settings_path = repo_path.join(config_path.unwrap_or(CONFIG_PATH)); + let settings_path = repo_path.join(CONFIG_PATH); if settings_path.exists() { let mut s = Config::new(); s.merge(File::from(settings_path))?; @@ -114,4 +125,15 @@ impl Settings { HookType::PostBump => &self.post_bump_hooks, } } + + pub fn get_profile_hook(&self, profile: &str, hook_type: HookType) -> &Vec { + let profile = self + .bump_profiles + .get(profile) + .expect("Bump profile not found"); + match hook_type { + HookType::PreBump => &profile.pre_bump_hooks, + HookType::PostBump => &profile.post_bump_hooks, + } + } }