Skip to content

Commit

Permalink
feat(hook): add bump profiles configuration
Browse files Browse the repository at this point in the history
This replace the previous implementation wich allowed
to run cog bump with an alternate config.
Now custom profile can be defined to perform different hooks
on bump. This allow to use several git branching models/workflows.

closes: #104
  • Loading branch information
oknozor committed Nov 30, 2021
1 parent b55c3ee commit 13eeed9
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 26 deletions.
12 changes: 6 additions & 6 deletions src/bin/cog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down
42 changes: 28 additions & 14 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,23 +53,23 @@ 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()
}
};

static ref REMOTE_URL: Option<String> = {
if let Ok(repo) = Repository::open(".") {
Settings::get(&repo, None).unwrap_or_default().github
Settings::get(&repo).unwrap_or_default().github
} else {
None
}
};

static ref AUTHORS: Vec<AuthorSetting> = {
if let Ok(repo) = Repository::open(".") {
Settings::get(&repo, None).unwrap_or_default().authors
Settings::get(&repo).unwrap_or_default().authors
} else {
vec![]
}
Expand Down Expand Up @@ -137,7 +137,7 @@ pub struct CocoGitto {
impl CocoGitto {
pub fn get() -> Result<Self> {
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"));
Expand Down Expand Up @@ -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<Hook> = 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<Hook> = 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)?;
Expand Down
34 changes: 28 additions & 6 deletions src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ pub(crate) struct Settings {
pub authors: AuthorSettings,
#[serde(default)]
pub commit_types: CommitsMetadataSettings,
#[serde(default)]
pub bump_profiles: HashMap<String, BumpProfile>,
}

#[derive(Debug, Deserialize, Serialize, Clone)]
Expand All @@ -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<String>,
#[serde(default)]
pub post_bump_hooks: Vec<String>,
}

impl Settings {
// Fails only if config exists and is malformed
pub(crate) fn get(repository: &Repository, config_path: Option<&str>) -> Result<Self> {
pub(crate) fn get(repository: &Repository) -> Result<Self> {
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))?;
Expand Down Expand Up @@ -114,4 +125,15 @@ impl Settings {
HookType::PostBump => &self.post_bump_hooks,
}
}

pub fn get_profile_hook(&self, profile: &str, hook_type: HookType) -> &Vec<String> {
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,
}
}
}

0 comments on commit 13eeed9

Please sign in to comment.