-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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(package): Extract package version from PEP 621 compliant pyproject.toml #3950
feat(package): Extract package version from PEP 621 compliant pyproject.toml #3950
Conversation
I'm afraid my noob level rust is showing, I've tried a few different variations on the following but can't seem to get it to work: fn get_pyproject_version(context: &Context, config: &PackageConfig) -> Option<String> {
let file_contents = context.read_file_from_pwd("pyproject.toml")?;
let pyproject_toml: toml::Value = toml::from_str(&file_contents).ok()?;
if let Some(poetry_version) = pyproject_toml
.get("tool")?
.get("poetry")?
.get("version")?
.as_str()
{
format_version(poetry_version, config.version_format);
}
if let Some(pep621_version) = pyproject_toml.get("project")?.get("version")?.as_str() {
format_version(pep621_version, config.version_format);
}
None
} I get the following test failures: So both a poetry pyproject.toml and a pep621 pyproject.toml are returning [tool.poetry]
version = "0.1.0" If so, grabbing the version and formatting it. If not, check for a: [project]
version = "0.1.0" If so, grabbing the version and formatting it. And if version is a table here, the And finally if we get here and haven't found anything, return a Obviously my understanding is wrong otherwise I wouldn't be here! Can anyone help me out? 🙂 |
When encountering pyproject_toml
.get("tool")
.and_then(|tool| tool.get("poetry"))
.and_then(|poetry| poetry.get("version"))
.and_then(|version| version.as_str()) |
@davidkna Worked perfectly! Thanks for the help |
The way I've written this atm is it will prioritise a PEP 621 pyproject.toml so in the weird case where someone has both a Poetry is the odd one out here and if/when it eventually adopts PEP 621, poetry pyproject.toml files will replace |
Thank you for your contribution @FollowTheProcess and thanks for reviewing @davidkna. |
Description
This PR adds a function to the package module to extract the package version from a PEP 621 compliant
pyproject.toml
Motivation and Context
Closes #3949
Screenshots (if appropriate):
How Has This Been Tested?
Ran
cargo test
,cargo clippy --all-targets --all-features
andcargo fmt
Checklist: