-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #95 from jontze/feat/config-file-and-custom-prefix…
…-support Add support for config file and custom commit prefixes
- Loading branch information
Showing
12 changed files
with
545 additions
and
150 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
--- | ||
# Configuration for conventional-versioning | ||
kind: Node # Node | Cargo | ||
output: Plain # Human | Plain | Json | Yaml | Yml | Toml | ||
prefixes: | ||
patch: | ||
- "fix" # e.g. fix: ... | fix(scope): ... | ||
- "chore" | ||
minor: | ||
- "feat" | ||
major: | ||
# Commits with a "!" or "BREAKING CHANGE:" | ||
# will always be considered | ||
# as a major change | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
use super::shared::{OutputFormat, SemVerKindArg}; | ||
use clap::Parser; | ||
|
||
#[derive(Parser, Debug)] | ||
#[command(author, about, version)] | ||
pub(crate) struct Args { | ||
/// Path to the repository. Default is the current directory. | ||
#[arg(short = 'r', long, value_hint = clap::ValueHint::DirPath, env = "CONVENTIONAL_VERSIONING_REPO")] | ||
pub repo: Option<std::path::PathBuf>, | ||
/// Path to the configuration file. By default, the OS specific | ||
/// user configuration directories are checked. | ||
/// WARNING: If you use the `--config` option, all other args will be ignored, besides `--repo`. | ||
#[arg(short = 'c', long, value_hint = clap::ValueHint::FilePath, env = "CONVENTIONAL_VERSIONING_CONFIG")] | ||
pub config: Option<std::path::PathBuf>, | ||
/// SemVer kind. Default is the Node SemVer variant. | ||
#[arg( | ||
short = 'k', | ||
long, | ||
default_value = "node", | ||
env = "CONVENTIONAL_VERSIONING_KIND" | ||
)] | ||
pub kind: Option<SemVerKindArg>, | ||
/// Output format. Default is the human readable format | ||
#[arg( | ||
short = 'o', | ||
long, | ||
default_value = "human", | ||
env = "CONVENTIONAL_VERSIONING_OUTPUT" | ||
)] | ||
pub out: Option<OutputFormat>, | ||
/// Commit scopes that cause a patch version bump. | ||
#[arg(short = 'p', long, env = "CONVENTIONAL_VERSIONING_PATCH")] | ||
pub patch_scope: Option<Vec<String>>, | ||
/// Commit scopes that cause a minor version bump. | ||
#[arg(short = 'm', long, env = "CONVENTIONAL_VERSIONING_MINOR")] | ||
pub minor_scope: Option<Vec<String>>, | ||
/// Commit scopes that cause a major version bump. | ||
#[arg(short = 'M', long, env = "CONVENTIONAL_VERSIONING_MAJOR")] | ||
pub major_scope: Option<Vec<String>>, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
use miette::miette; | ||
|
||
use super::shared::{OutputFormat, Prefixes, SemVerKindArg}; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Serialize, Deserialize, Default, Debug)] | ||
pub(crate) struct Settings { | ||
pub kind: SemVerKindArg, | ||
pub output: OutputFormat, | ||
pub prefixes: Prefixes, | ||
} | ||
|
||
pub(crate) fn read_config_at_path<P, TConfig>(path: P) -> miette::Result<TConfig> | ||
where | ||
P: AsRef<std::path::Path>, | ||
TConfig: for<'de> serde::Deserialize<'de>, | ||
{ | ||
let file = std::fs::File::open(path).map_err(|err| miette!(err.to_string()))?; | ||
let reader = std::io::BufReader::new(file); | ||
let settings = serde_yaml::from_reader(reader).map_err(|err| miette!(err.to_string()))?; | ||
Ok(settings) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
use clap::Parser; | ||
use miette::miette; | ||
use std::path::{Path, PathBuf}; | ||
|
||
use file::{read_config_at_path, Settings}; | ||
|
||
mod args; | ||
mod file; | ||
mod shared; | ||
|
||
pub(crate) use shared::OutputFormat; | ||
pub(crate) use shared::Prefixes; | ||
pub(crate) use shared::SemVerKindArg as SemVerKind; | ||
|
||
const DEFAULT_REPO_PATH: &str = "."; | ||
|
||
#[derive(Debug)] | ||
pub(crate) struct Config { | ||
prefixes: Prefixes, | ||
output: OutputFormat, | ||
kind: SemVerKind, | ||
repo_path: PathBuf, | ||
} | ||
|
||
impl Config { | ||
pub(crate) fn new() -> miette::Result<Self> { | ||
let args = args::Args::parse(); | ||
|
||
let settings: Option<miette::Result<Settings>> = args.config.map(read_config_at_path); | ||
|
||
match settings { | ||
// If config path provided, read from file | ||
Some(settings) => { | ||
if let Ok(settings) = settings { | ||
Ok(Self { | ||
kind: settings.kind, | ||
output: settings.output, | ||
prefixes: settings.prefixes, | ||
repo_path: args | ||
.repo | ||
.map_or(Path::new(DEFAULT_REPO_PATH).to_path_buf(), |p| p), | ||
}) | ||
} else { | ||
Err(miette!( | ||
"Failed to read configuration file at provided path." | ||
)) | ||
} | ||
} | ||
// Otherwise, use command line args | ||
None => Ok(Self { | ||
kind: args.kind.unwrap_or(SemVerKind::Node), | ||
output: args.out.unwrap_or(OutputFormat::Human), | ||
prefixes: Prefixes { | ||
patch: args.patch_scope.unwrap_or_else(Vec::new), | ||
minor: args.minor_scope.unwrap_or_else(Vec::new), | ||
major: args.major_scope.unwrap_or_else(Vec::new), | ||
}, | ||
repo_path: args | ||
.repo | ||
.map_or(Path::new(DEFAULT_REPO_PATH).to_path_buf(), |p| p), | ||
}), | ||
} | ||
} | ||
|
||
pub(crate) fn repo_path(&self) -> &PathBuf { | ||
&self.repo_path | ||
} | ||
|
||
pub(crate) fn semver_kind(&self) -> SemVerKind { | ||
self.kind | ||
} | ||
|
||
pub(crate) fn output(&self) -> OutputFormat { | ||
self.output | ||
} | ||
|
||
pub(crate) fn prefixes(&self) -> &Prefixes { | ||
&self.prefixes | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
use clap::ValueEnum; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, ValueEnum, Serialize, Deserialize)] | ||
pub(crate) enum SemVerKindArg { | ||
Node, | ||
Cargo, | ||
} | ||
|
||
impl Default for SemVerKindArg { | ||
fn default() -> Self { | ||
Self::Cargo | ||
} | ||
} | ||
|
||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, ValueEnum, Serialize, Deserialize)] | ||
pub(crate) enum OutputFormat { | ||
Human, | ||
Plain, | ||
Json, | ||
Yaml, | ||
Yml, | ||
Toml, | ||
} | ||
|
||
impl Default for OutputFormat { | ||
fn default() -> Self { | ||
Self::Plain | ||
} | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Default, Debug)] | ||
pub(crate) struct Prefixes { | ||
pub patch: Vec<String>, | ||
pub minor: Vec<String>, | ||
pub major: Vec<String>, | ||
} | ||
|
||
impl Prefixes { | ||
pub(crate) fn is_empty(&self) -> bool { | ||
self.patch.is_empty() && self.minor.is_empty() && self.major.is_empty() | ||
} | ||
|
||
pub(crate) fn is_patch(&self, commit_prefix: impl ToString) -> bool { | ||
let commit_prefix = commit_prefix.to_string(); | ||
self.patch | ||
.iter() | ||
.any(|patch_prefix| patch_prefix.eq(&commit_prefix)) | ||
} | ||
|
||
pub(crate) fn is_minor(&self, commit_prefix: impl ToString) -> bool { | ||
let commit_prefix = commit_prefix.to_string(); | ||
self.minor | ||
.iter() | ||
.any(|minor_prefix| minor_prefix.eq(&commit_prefix)) | ||
} | ||
|
||
pub(crate) fn is_major(&self, commit_prefix: impl ToString) -> bool { | ||
let commit_prefix = commit_prefix.to_string(); | ||
self.major | ||
.iter() | ||
.any(|major_prefix| major_prefix.eq(&commit_prefix)) | ||
} | ||
} |
Oops, something went wrong.