Skip to content

Commit

Permalink
feat(monorepo): add config opt to disable global global-tag-opt (#264)
Browse files Browse the repository at this point in the history
* feat(monorepo): add config opt to disable global global-tag-opt

* refactor: use builder pattern for hook run options

* chore: remove codesee

* chore: add andre161292 to contributors list

Closes #259
  • Loading branch information
oknozor committed Feb 18, 2023
1 parent 5670cd8 commit 96aa3b6
Show file tree
Hide file tree
Showing 9 changed files with 363 additions and 197 deletions.
20 changes: 0 additions & 20 deletions .github/workflows/codesee-arch-diagram.yml

This file was deleted.

3 changes: 2 additions & 1 deletion cog.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,6 @@ authors = [
{ signature = "Stephen Connolly", username = "stephenc" },
{ signature = "Luca Trevisani", username = "lucatrv" },
{ signature = "Racci", username = "DaRacci" },
{ signature = "Conrad Hoffmann", username = "bitfehler" }
{ signature = "Conrad Hoffmann", username = "bitfehler" },
{ signature = "andre161292", username = "andre161292" }
]
94 changes: 76 additions & 18 deletions src/command/bump/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,72 @@ mod monorepo;
mod package;
mod standard;

struct HookRunOptions<'a> {
hook_type: HookType,
current_tag: Option<&'a HookVersion>,
next_version: Option<&'a HookVersion>,
hook_profile: Option<&'a str>,
package_name: Option<&'a str>,
package: Option<&'a MonoRepoPackage>,
}

impl<'a> HookRunOptions<'a> {
pub fn post_bump() -> Self {
Self {
hook_type: HookType::PostBump,
current_tag: None,
next_version: None,
hook_profile: None,
package_name: None,
package: None,
}
}

pub fn pre_bump() -> Self {
Self {
hook_type: HookType::PreBump,
current_tag: None,
next_version: None,
hook_profile: None,
package_name: None,
package: None,
}
}

pub fn current_tag<'b>(mut self, version: Option<&'b HookVersion>) -> Self
where
'b: 'a,
{
self.current_tag = version;
self
}

pub fn next_version<'b>(mut self, version: &'b HookVersion) -> Self
where
'b: 'a,
{
self.next_version = Some(version);
self
}

pub fn hook_profile<'b>(mut self, profile: Option<&'b str>) -> Self
where
'b: 'a,
{
self.hook_profile = profile;
self
}

pub fn package<'b>(mut self, name: &'b str, package: &'b MonoRepoPackage) -> Self
where
'b: 'a,
{
self.package_name = Some(name);
self.package = Some(package);
self
}
}

fn ensure_tag_is_greater_than_previous(current: &Tag, next: &Tag) -> Result<()> {
if next <= current {
let comparison = format!("{current} <= {next}").red();
Expand Down Expand Up @@ -148,20 +214,12 @@ impl CocoGitto {
Ok(release)
}

fn run_hooks(
&self,
hook_type: HookType,
current_tag: Option<&HookVersion>,
next_version: &HookVersion,
hook_profile: Option<&str>,
package_name: Option<&str>,
package: Option<&MonoRepoPackage>,
) -> Result<()> {
fn run_hooks(&self, options: HookRunOptions) -> Result<()> {
let settings = Settings::get(&self.repository)?;

let hooks: Vec<Hook> = match (package, hook_profile) {
let hooks: Vec<Hook> = match (options.package, options.hook_profile) {
(None, Some(profile)) => settings
.get_profile_hooks(profile, hook_type)
.get_profile_hooks(profile, options.hook_type)
.iter()
.map(|s| s.parse())
.enumerate()
Expand All @@ -173,7 +231,7 @@ impl CocoGitto {
.try_collect()?,

(Some(package), Some(profile)) => {
let hooks = package.get_profile_hooks(profile, hook_type);
let hooks = package.get_profile_hooks(profile, options.hook_type);

hooks
.iter()
Expand All @@ -187,14 +245,14 @@ impl CocoGitto {
.try_collect()?
}
(Some(package), None) => package
.get_hooks(hook_type)
.get_hooks(options.hook_type)
.iter()
.map(|s| s.parse())
.enumerate()
.map(|(idx, result)| result.context(format!("Cannot parse hook at index {idx}")))
.try_collect()?,
(None, None) => settings
.get_hooks(hook_type)
.get_hooks(options.hook_type)
.iter()
.map(|s| s.parse())
.enumerate()
Expand All @@ -203,12 +261,12 @@ impl CocoGitto {
};

if !hooks.is_empty() {
let hook_type = match hook_type {
let hook_type = match options.hook_type {
HookType::PreBump => "pre-bump",
HookType::PostBump => "post-bump",
};

match package_name {
match options.package_name {
None => {
let msg = format!("[{hook_type}]").underline().white().bold();
info!("{msg}")
Expand All @@ -224,15 +282,15 @@ impl CocoGitto {
}

for mut hook in hooks {
hook.insert_versions(current_tag, next_version)?;
hook.insert_versions(options.current_tag, options.next_version)?;
let command = hook.to_string();
let command = if command.chars().count() > 78 {
&command[0..command.len()]
} else {
&command
};
info!("[{command}]");
let package_path = package.map(|p| p.path.as_path());
let package_path = options.package.map(|p| p.path.as_path());
hook.run(package_path).context(hook.to_string())?;
println!();
}
Expand Down
131 changes: 91 additions & 40 deletions src/command/bump/monorepo.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use crate::command::bump::{ensure_tag_is_greater_than_previous, tag_or_fallback_to_zero};
use crate::command::bump::{
ensure_tag_is_greater_than_previous, tag_or_fallback_to_zero, HookRunOptions,
};

use crate::conventional::changelog::template::{
MonoRepoContext, PackageBumpContext, PackageContext,
Expand All @@ -9,12 +11,11 @@ use crate::conventional::version::{Increment, IncrementCommand};

use crate::git::tag::Tag;
use crate::hook::HookVersion;
use crate::settings::HookType;
use crate::{settings, CocoGitto, SETTINGS};
use anyhow::Result;
use colored::*;

use log::info;
use log::{info, warn};
use semver::Prerelease;
use tera::Tera;

Expand Down Expand Up @@ -47,7 +48,14 @@ impl CocoGitto {
) -> Result<()> {
match increment {
IncrementCommand::Auto => {
self.create_monorepo_version_auto(pre_release, hooks_config, annotated, dry_run)
if SETTINGS.generate_mono_repository_global_tag {
self.create_monorepo_version_auto(pre_release, hooks_config, annotated, dry_run)
} else {
if annotated.is_some() {
warn!("--annotated flag is not supported for package bumps without a global tag");
}
self.create_all_package_version_auto(pre_release, hooks_config, dry_run)
}
}
_ => self.create_monorepo_version_manual(
increment,
Expand All @@ -59,6 +67,59 @@ impl CocoGitto {
}
}

pub fn create_all_package_version_auto(
&mut self,
pre_release: Option<&str>,
hooks_config: Option<&str>,
dry_run: bool,
) -> Result<()> {
self.pre_bump_checks()?;
// Get package bumps
let bumps = self.get_packages_bumps(pre_release)?;

if dry_run {
for bump in bumps {
println!("{}", bump.new_version.prefixed_tag)
}
return Ok(());
}

let hook_result = self.run_hooks(HookRunOptions::pre_bump().hook_profile(hooks_config));

self.repository.add_all()?;
self.unwrap_or_stash_and_exit(&Tag::default(), hook_result);
self.bump_packages(pre_release, hooks_config, &bumps)?;

let sign = self.repository.gpg_sign();
self.repository
.commit("chore(version): bump packages", sign)?;

for bump in &bumps {
self.repository.create_tag(&bump.new_version.prefixed_tag)?;
}

// Run per package post hooks
for bump in bumps {
let package = SETTINGS
.packages
.get(&bump.package_name)
.expect("package exists");

self.run_hooks(
HookRunOptions::post_bump()
.current_tag(bump.old_version.as_ref())
.next_version(&bump.new_version)
.hook_profile(hooks_config)
.package(&bump.package_name, package),
)?;
}

// Run global post hooks
self.run_hooks(HookRunOptions::post_bump().hook_profile(hooks_config))?;

Ok(())
}

fn create_monorepo_version_auto(
&mut self,
pre_release: Option<&str>,
Expand Down Expand Up @@ -143,12 +204,10 @@ impl CocoGitto {
let next_version = HookVersion::new(tag.clone());

let hook_result = self.run_hooks(
HookType::PreBump,
current.as_ref(),
&next_version,
hooks_config,
None,
None,
HookRunOptions::pre_bump()
.current_tag(current.as_ref())
.next_version(&next_version)
.hook_profile(hooks_config),
);

self.repository.add_all()?;
Expand Down Expand Up @@ -184,23 +243,20 @@ impl CocoGitto {
.get(&bump.package_name)
.expect("package exists");
self.run_hooks(
HookType::PostBump,
bump.old_version.as_ref(),
&bump.new_version,
hooks_config,
Some(&bump.package_name),
Some(package),
HookRunOptions::post_bump()
.current_tag(bump.old_version.as_ref())
.next_version(&bump.new_version)
.hook_profile(hooks_config)
.package(&bump.package_name, package),
)?;
}

// Run global post hooks
self.run_hooks(
HookType::PostBump,
current.as_ref(),
&next_version,
hooks_config,
None,
None,
HookRunOptions::post_bump()
.current_tag(current.as_ref())
.next_version(&next_version)
.hook_profile(hooks_config),
)?;

Ok(())
Expand Down Expand Up @@ -267,12 +323,10 @@ impl CocoGitto {
let next_version = HookVersion::new(tag.clone());

let hook_result = self.run_hooks(
HookType::PreBump,
current.as_ref(),
&next_version,
hooks_config,
None,
None,
HookRunOptions::pre_bump()
.current_tag(current.as_ref())
.next_version(&next_version)
.hook_profile(hooks_config),
);

self.repository.add_all()?;
Expand All @@ -296,12 +350,10 @@ impl CocoGitto {

// Run global post hooks
self.run_hooks(
HookType::PostBump,
current.as_ref(),
&next_version,
hooks_config,
None,
None,
HookRunOptions::post_bump()
.current_tag(current.as_ref())
.next_version(&next_version)
.hook_profile(hooks_config),
)?;

Ok(())
Expand Down Expand Up @@ -432,12 +484,11 @@ impl CocoGitto {
let new_version = HookVersion::new(tag.clone());

let hook_result = self.run_hooks(
HookType::PreBump,
old_version.as_ref(),
&new_version,
hooks_config,
Some(package_name),
Some(package),
HookRunOptions::pre_bump()
.current_tag(old_version.as_ref())
.next_version(&new_version)
.hook_profile(hooks_config)
.package(package_name, package),
);

self.repository.add_all()?;
Expand Down
Loading

0 comments on commit 96aa3b6

Please sign in to comment.