-
-
Notifications
You must be signed in to change notification settings - Fork 66
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(config): allow configuration from package/workspace metadata #1082
base: main
Are you sure you want to change the base?
Changes from all commits
2e2446e
0cd2b1f
b4a9e5d
dc09c21
2cbc802
9ac3739
03b4e1b
77774ca
b348e2b
980c262
7f341e5
9a60435
bedc1e6
e177308
cbde073
7be03d5
ce3a5bc
62ad070
afb0392
3d5bf88
9955593
c84ea07
604a45b
ae68908
9d8a227
263cb66
4679f2b
7e349df
3fa6823
70e28a9
c37b11e
015fcd0
1b8e0e4
04a26d7
9a3aa94
471de29
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ mod update; | |
use std::path::{Path, PathBuf}; | ||
|
||
use anyhow::Context; | ||
use cargo_metadata::Metadata; | ||
use release_plz_core::CARGO_TOML; | ||
use tracing::info; | ||
|
||
|
@@ -59,7 +60,11 @@ fn local_manifest(project_manifest: Option<&Path>) -> PathBuf { | |
} | ||
} | ||
|
||
fn parse_config(config_path: Option<&Path>) -> anyhow::Result<Config> { | ||
fn parse_config( | ||
config_path: Option<&Path>, | ||
project_manifest: Option<&Path>, | ||
cargo_metadata: &Metadata, | ||
) -> anyhow::Result<Config> { | ||
let (config, path) = if let Some(config_path) = config_path { | ||
match std::fs::read_to_string(config_path) { | ||
Ok(config) => (config, config_path), | ||
|
@@ -71,22 +76,66 @@ fn parse_config(config_path: Option<&Path>) -> anyhow::Result<Config> { | |
}, | ||
} | ||
} else { | ||
match first_file_contents([ | ||
let file_contents = first_file_contents([ | ||
Path::new("release-plz.toml"), | ||
Path::new(".release-plz.toml"), | ||
])? { | ||
Some((config, path)) => (config, path), | ||
None => { | ||
info!("release-plz config file not found, using default configuration"); | ||
return Ok(Config::default()); | ||
} | ||
])?; | ||
if let Some(project_manifest) = project_manifest | ||
.or(Some(Path::new(CARGO_TOML))) | ||
.filter(|v| v.exists()) | ||
{ | ||
return match parse_config_from_metadata(cargo_metadata)? { | ||
Some(config) => { | ||
if file_contents.is_some() { | ||
return Err(anyhow::anyhow!( | ||
"configuration is both specified as file and metadata" | ||
)); | ||
} | ||
info!( | ||
"using configuration from metadata in {:?}", | ||
project_manifest | ||
); | ||
Ok(config) | ||
} | ||
None => Ok(Config::default()), | ||
}; | ||
} | ||
if let Some((config, path)) = file_contents { | ||
(config, path) | ||
} else { | ||
info!("release-plz config file not found, using default configuration"); | ||
return Ok(Config::default()); | ||
} | ||
}; | ||
|
||
info!("using release-plz config file {}", path.display()); | ||
toml::from_str(&config).with_context(|| format!("invalid config file {config_path:?}")) | ||
} | ||
|
||
fn parse_config_from_metadata(metadata: &Metadata) -> anyhow::Result<Option<Config>> { | ||
// check both workspace and package metadata | ||
for metadata in [ | ||
Some(metadata.clone().workspace_metadata), | ||
metadata.packages.first().map(|v| v.metadata.clone()), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why do you only consider the first package? |
||
] | ||
.into_iter() | ||
.filter(|v| v.clone().is_some_and(|v| !v.is_null())) | ||
{ | ||
if let Some(metadata) = metadata | ||
// safe to unwrap() since it is checked above | ||
.unwrap() | ||
.as_object() | ||
.ok_or_else(|| anyhow::anyhow!("failed to convert metadata to object"))? | ||
.get("release-plz") | ||
{ | ||
let config: Config = serde_json::from_value(metadata.clone()) | ||
.context("failed to parse config from metadata")?; | ||
return Ok(Some(config)); | ||
} | ||
} | ||
Ok(None) | ||
} | ||
|
||
/// Returns the contents of the first file that exists. | ||
/// | ||
/// If none of the files exist, returns `Ok(None)`. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
use std::path::{Path, PathBuf}; | ||
|
||
use anyhow::Context; | ||
use cargo_metadata::Metadata; | ||
use chrono::NaiveDate; | ||
use clap::builder::{NonEmptyStringValueParser, PathBufValueParser}; | ||
use git_cliff_core::config::Config as GitCliffConfig; | ||
|
@@ -101,8 +102,12 @@ impl RepoCommand for Update { | |
} | ||
|
||
impl Update { | ||
pub fn config(&self) -> anyhow::Result<Config> { | ||
super::parse_config(self.config.as_deref()) | ||
pub fn config(&self, cargo_metadata: &Metadata) -> anyhow::Result<Config> { | ||
super::parse_config( | ||
self.config.as_deref(), | ||
self.optional_project_manifest(), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. project_manifest is already inside There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oops sorry. Than you can find the project manifest in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure if we can do this. What happens if the user provided a custom path via There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, I missed this comment. |
||
cargo_metadata, | ||
) | ||
} | ||
|
||
fn dependencies_update(&self, config: &Config) -> bool { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if the project manifest exists, you ignore completely the
file_contents
, returning the default configuration on line 100, right? 🤔Imagine somebody that doesn't specify the config in the Cargo.toml metadata. Their
release-plz.toml
file is ignored. I tested this by running your branch on release-plz itself.