Skip to content

Commit

Permalink
Rename 'metadata file' to 'manifest file' (#859)
Browse files Browse the repository at this point in the history
  • Loading branch information
cnpryer authored Nov 19, 2023
1 parent 597c5f2 commit 85636b9
Show file tree
Hide file tree
Showing 17 changed files with 276 additions and 245 deletions.
8 changes: 4 additions & 4 deletions crates/huak-package-manager/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ pub enum Error {
PEP440Error(#[from] pep440_rs::Pep440Error),
#[error("a problem occurred with PEP508 parsing: {0}")]
PEP508Error(#[from] pep508_rs::Pep508Error),
#[error("a metadata file already exists")]
MetadataFileFound,
#[error("a metadata file could not be found")]
MetadataFileNotFound,
#[error("a manifest file already exists")]
ManifestFileFound,
#[error("a manifest file could not be found")]
ManifestFileNotFound,
#[error("a package version could not be found")]
PackageVersionNotFound,
#[error("a project already exists")]
Expand Down
6 changes: 3 additions & 3 deletions crates/huak-package-manager/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ mod environment;
mod error;
mod fs;
mod git;
mod metadata;
mod manifest;
pub mod ops;
mod package;
mod python_environment;
Expand All @@ -65,9 +65,9 @@ pub use environment::{env_path_string, env_path_values, Environment};
pub use error::{Error, HuakResult};
pub use fs::{copy_dir, last_path_component, CopyDirOptions};
pub use git::{default_python_gitignore, init as git_init};
pub use metadata::{
pub use manifest::{
default_package_entrypoint_string, default_package_test_file_contents,
default_pyproject_toml_contents, LocalMetadata,
default_pyproject_toml_contents, LocalManifest,
};
pub use package::{importable_package_name, Package};
pub use python_environment::{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,73 +3,72 @@ use huak_pyproject_toml::PyProjectToml;
use std::{ffi::OsStr, path::PathBuf, str::FromStr};
use toml_edit::Document;

const DEFAULT_METADATA_FILE_NAME: &str = "pyproject.toml";
const DEFAULT_MANIFEST_FILE_NAME: &str = "pyproject.toml";

/// A `LocalMetadata` struct used to manage local `Metadata` files such as
/// the pyproject.toml (<https://peps.python.org/pep-0621/>).
pub struct LocalMetadata {
/// The core `Metadata`.
/// A `LocalManifest` struct used to manage local manifest files such as the pyproject.toml (<https://peps.python.org/pep-0621/>).
pub struct LocalManifest {
/// The manifest's data including core metadata about the project.
/// See https://packaging.python.org/en/latest/specifications/core-metadata/.
metadata: PyProjectToml, // TODO: https://github.com/cnpryer/huak/issues/574
/// The path to the `LocalMetadata` file.
manifest_data: PyProjectToml, // TODO: https://github.com/cnpryer/huak/issues/574
/// The path to the `LocalManifest` file.
path: PathBuf,
}

impl LocalMetadata {
/// Initialize `LocalMetadata` from a path.
pub fn new<T: Into<PathBuf>>(path: T) -> HuakResult<LocalMetadata> {
impl LocalManifest {
/// Initialize `LocalManifest` from a path.
pub fn new<T: Into<PathBuf>>(path: T) -> HuakResult<LocalManifest> {
let path = path.into();

// NOTE: Currently only pyproject.toml files are supported.
if path.file_name() != Some(OsStr::new(DEFAULT_METADATA_FILE_NAME)) {
if path.file_name() != Some(OsStr::new(DEFAULT_MANIFEST_FILE_NAME)) {
return Err(Error::Unimplemented(format!(
"{} is not supported",
path.display()
)));
}
let local_metadata = pyproject_toml_metadata(path)?;
let manifest = read_local_manifest(path)?;

Ok(local_metadata)
Ok(manifest)
}

/// Create a `LocalMetadata` template.
pub fn template<T: Into<PathBuf>>(path: T) -> LocalMetadata {
LocalMetadata {
metadata: PyProjectToml {
/// Create a `LocalManifest` template.
pub fn template<T: Into<PathBuf>>(path: T) -> LocalManifest {
LocalManifest {
manifest_data: PyProjectToml {
doc: Document::from_str(&default_pyproject_toml_contents("project name"))
.expect("template pyproject.toml contents"),
},
path: path.into(),
}
}

/// Get a reference to the core `Metadata`.
/// Get a reference to the manifest data.
#[must_use]
pub fn metadata(&self) -> &PyProjectToml {
&self.metadata
pub fn manifest_data(&self) -> &PyProjectToml {
&self.manifest_data
}

/// Get a mutable reference to the core `Metadata`.
pub fn metadata_mut(&mut self) -> &mut PyProjectToml {
&mut self.metadata
/// Get a mutable reference to the manifest data.
pub fn manifest_data_mut(&mut self) -> &mut PyProjectToml {
&mut self.manifest_data
}

/// Write the `LocalMetadata` file to its path.
/// Write the `LocalManifest` file to its path.
pub fn write_file(&self) -> HuakResult<()> {
Ok(self.metadata.write_toml(&self.path)?)
Ok(self.manifest_data.write_toml(&self.path)?)
}
}

/// Create `LocalMetadata` from a pyproject.toml file.
fn pyproject_toml_metadata<T: Into<PathBuf>>(path: T) -> HuakResult<LocalMetadata> {
/// Create `LocalManifest` from a pyproject.toml file.
fn read_local_manifest<T: Into<PathBuf>>(path: T) -> HuakResult<LocalManifest> {
let path = path.into();
let pyproject_toml = PyProjectToml::read_toml(&path)?;
let local_metadata = LocalMetadata {
metadata: pyproject_toml,
let local_manifest = LocalManifest {
manifest_data: pyproject_toml,
path,
};

Ok(local_metadata)
Ok(local_manifest)
}

#[must_use]
Expand Down Expand Up @@ -115,21 +114,28 @@ mod tests {
let path = dev_resources_dir()
.join("mock-project")
.join("pyproject.toml");
let local_metadata = LocalMetadata::new(path).unwrap();
let local_manifest = LocalManifest::new(path).unwrap();

assert_eq!(
local_metadata.metadata.project_name().unwrap().to_string(),
local_manifest
.manifest_data
.project_name()
.unwrap()
.to_string(),
"mock_project"
);
assert_eq!(
*local_metadata
.metadata
*local_manifest
.manifest_data
.project_version()
.unwrap()
.to_string(),
"0.0.1".to_string()
);
assert!(local_metadata.metadata.project_dependencies().is_some());
assert!(local_manifest
.manifest_data
.project_dependencies()
.is_some());
}

#[ignore = "unsupported"]
Expand All @@ -138,10 +144,10 @@ mod tests {
let path = dev_resources_dir()
.join("mock-project")
.join("pyproject.toml");
let local_metadata = LocalMetadata::new(path).unwrap();
let local_manifest = LocalManifest::new(path).unwrap();

assert_eq!(
local_metadata.metadata.to_string(),
local_manifest.manifest_data.to_string(),
r#"[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"
Expand Down Expand Up @@ -171,10 +177,10 @@ dev = [
let path = dev_resources_dir()
.join("mock-project")
.join("pyproject.toml");
let local_metadata = LocalMetadata::new(path).unwrap();
let local_manifest = LocalManifest::new(path).unwrap();

assert_eq!(
local_metadata.metadata.project_dependencies().unwrap(),
local_manifest.manifest_data.project_dependencies().unwrap(),
vec!["click == 8.1.7".to_string()]
);
}
Expand All @@ -184,11 +190,11 @@ dev = [
let path = dev_resources_dir()
.join("mock-project")
.join("pyproject.toml");
let local_metadata = LocalMetadata::new(path).unwrap();
let local_manifest = LocalManifest::new(path).unwrap();

assert_eq!(
local_metadata
.metadata
local_manifest
.manifest_data
.project_optional_dependencies()
.unwrap()
.get("dev")
Expand All @@ -202,14 +208,14 @@ dev = [
let path = dev_resources_dir()
.join("mock-project")
.join("pyproject.toml");
let mut local_metadata = LocalMetadata::new(path).unwrap();
local_metadata
.metadata
let mut local_manifest = LocalManifest::new(path).unwrap();
local_manifest
.manifest_data
.add_project_dependency("test")
.formatted();

assert_eq!(
local_metadata.metadata.to_string(),
local_manifest.manifest_data.to_string(),
r#"[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"
Expand Down Expand Up @@ -241,16 +247,16 @@ dev = [
let path = dev_resources_dir()
.join("mock-project")
.join("pyproject.toml");
let mut local_metadata = LocalMetadata::new(path).unwrap();
let mut local_manifest = LocalManifest::new(path).unwrap();

local_metadata
.metadata
local_manifest
.manifest_data
.add_project_optional_dependency("test1", "dev");
local_metadata
.metadata
local_manifest
.manifest_data
.add_project_optional_dependency("test2", "new-group");
assert_eq!(
local_metadata.metadata.formatted().to_string(),
local_manifest.manifest_data.formatted().to_string(),
r#"[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"
Expand Down Expand Up @@ -285,11 +291,13 @@ new-group = [
let path = dev_resources_dir()
.join("mock-project")
.join("pyproject.toml");
let mut local_metadata = LocalMetadata::new(path).unwrap();
local_metadata.metadata.remove_project_dependency("click");
let mut local_manifest = LocalManifest::new(path).unwrap();
local_manifest
.manifest_data
.remove_project_dependency("click");

assert_eq!(
local_metadata.metadata.formatted().to_string(),
local_manifest.manifest_data.formatted().to_string(),
r#"[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"
Expand Down Expand Up @@ -318,13 +326,13 @@ dev = [
let path = dev_resources_dir()
.join("mock-project")
.join("pyproject.toml");
let mut local_metadata = LocalMetadata::new(path).unwrap();
let mut local_manifest = LocalManifest::new(path).unwrap();

local_metadata
.metadata
local_manifest
.manifest_data
.remove_project_optional_dependency("ruff", "dev");
assert_eq!(
local_metadata.metadata.formatted().to_string(),
local_manifest.manifest_data.formatted().to_string(),
r#"[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"
Expand Down
Loading

0 comments on commit 85636b9

Please sign in to comment.