chore: reorganize manifest module

This commit is contained in:
daimond113 2024-07-24 11:55:15 +02:00
parent f0cd53a2c9
commit b10e7667f0
No known key found for this signature in database
GPG key ID: 3A8ECE51328B513C
14 changed files with 201 additions and 166 deletions

View file

@ -4,8 +4,7 @@ use clap::Args;
use colored::Colorize; use colored::Colorize;
use inquire::validator::Validation; use inquire::validator::Validation;
use pesde::{ use pesde::{
errors::ManifestReadError, manifest::ScriptName, names::PackageName, Project, errors::ManifestReadError, names::PackageName, scripts::ScriptName, Project, DEFAULT_INDEX_NAME,
DEFAULT_INDEX_NAME,
}; };
use std::{path::Path, str::FromStr}; use std::{path::Path, str::FromStr};

View file

@ -2,7 +2,7 @@ use crate::cli::{reqwest_client, IsUpToDate};
use anyhow::Context; use anyhow::Context;
use clap::Args; use clap::Args;
use indicatif::MultiProgress; use indicatif::MultiProgress;
use pesde::{lockfile::Lockfile, manifest::TargetKind, Project}; use pesde::{lockfile::Lockfile, manifest::target::TargetKind, Project};
use std::{collections::HashSet, sync::Arc, time::Duration}; use std::{collections::HashSet, sync::Arc, time::Duration};
#[derive(Debug, Args)] #[derive(Debug, Args)]

View file

@ -1,7 +1,7 @@
use anyhow::Context; use anyhow::Context;
use clap::Args; use clap::Args;
use colored::Colorize; use colored::Colorize;
use pesde::{manifest::Target, Project, MANIFEST_FILE_NAME, MAX_ARCHIVE_SIZE}; use pesde::{manifest::target::Target, Project, MANIFEST_FILE_NAME, MAX_ARCHIVE_SIZE};
use std::path::Component; use std::path::Component;
#[derive(Debug, Args)] #[derive(Debug, Args)]

View file

@ -2,7 +2,7 @@ use std::path::{Component, Path};
use full_moon::{ast::luau::ExportedTypeDeclaration, visitors::Visitor}; use full_moon::{ast::luau::ExportedTypeDeclaration, visitors::Visitor};
use crate::manifest::Target; use crate::manifest::target::Target;
struct TypeVisitor { struct TypeVisitor {
types: Vec<String>, types: Vec<String>,

View file

@ -1,9 +1,9 @@
use crate::{ use crate::{
linking::generator::get_file_types, linking::generator::get_file_types,
lockfile::DownloadedGraph, lockfile::DownloadedGraph,
manifest::{ScriptName, Target}, manifest::target::Target,
names::PackageNames, names::PackageNames,
scripts::execute_script, scripts::{execute_script, ScriptName},
source::{PackageRef, VersionId}, source::{PackageRef, VersionId},
Project, PACKAGES_CONTAINER_NAME, Project, PACKAGES_CONTAINER_NAME,
}; };

View file

@ -1,5 +1,9 @@
use crate::{ use crate::{
manifest::{DependencyType, OverrideKey, Target, TargetKind}, manifest::{
overrides::OverrideKey,
target::{Target, TargetKind},
DependencyType,
},
names::{PackageName, PackageNames}, names::{PackageName, PackageNames},
source::{DependencySpecifiers, PackageRef, PackageRefs, VersionId}, source::{DependencySpecifiers, PackageRef, PackageRefs, VersionId},
}; };

96
src/manifest/mod.rs Normal file
View file

@ -0,0 +1,96 @@
use std::collections::{BTreeMap, BTreeSet};
use relative_path::RelativePathBuf;
use semver::Version;
use serde::{Deserialize, Serialize};
use crate::{
manifest::{overrides::OverrideKey, target::Target},
names::{PackageName, PackageNames},
source::{DependencySpecifiers, VersionId},
};
pub mod overrides;
pub mod target;
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Manifest {
pub name: PackageName,
pub version: Version,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub license: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub authors: Option<Vec<String>>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub repository: Option<String>,
pub target: Target,
#[serde(default)]
pub private: bool,
#[serde(default, skip_serializing)]
pub scripts: BTreeMap<String, RelativePathBuf>,
#[serde(default)]
pub indices: BTreeMap<String, url::Url>,
#[cfg(feature = "wally-compat")]
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
pub wally_indices: BTreeMap<String, url::Url>,
#[serde(default, skip_serializing)]
pub overrides: BTreeMap<OverrideKey, DependencySpecifiers>,
#[serde(default)]
pub includes: BTreeSet<String>,
#[cfg(feature = "patches")]
#[serde(default, skip_serializing)]
pub patches: BTreeMap<PackageNames, BTreeMap<VersionId, RelativePathBuf>>,
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
pub dependencies: BTreeMap<String, DependencySpecifiers>,
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
pub peer_dependencies: BTreeMap<String, DependencySpecifiers>,
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
pub dev_dependencies: BTreeMap<String, DependencySpecifiers>,
}
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[serde(rename_all = "snake_case")]
pub enum DependencyType {
Standard,
Dev,
Peer,
}
impl Manifest {
pub fn all_dependencies(
&self,
) -> Result<
BTreeMap<String, (DependencySpecifiers, DependencyType)>,
errors::AllDependenciesError,
> {
let mut all_deps = BTreeMap::new();
for (deps, ty) in [
(&self.dependencies, DependencyType::Standard),
(&self.peer_dependencies, DependencyType::Peer),
(&self.dev_dependencies, DependencyType::Dev),
] {
for (alias, spec) in deps {
if all_deps.insert(alias.clone(), (spec.clone(), ty)).is_some() {
return Err(errors::AllDependenciesError::AliasConflict(alias.clone()));
}
}
}
Ok(all_deps)
}
}
pub mod errors {
use thiserror::Error;
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum AllDependenciesError {
#[error("another specifier is already using the alias {0}")]
AliasConflict(String),
}
}

58
src/manifest/overrides.rs Normal file
View file

@ -0,0 +1,58 @@
use serde_with::{DeserializeFromStr, SerializeDisplay};
use std::{
fmt::{Display, Formatter},
str::FromStr,
};
#[derive(
Debug, DeserializeFromStr, SerializeDisplay, Clone, PartialEq, Eq, Hash, PartialOrd, Ord,
)]
pub struct OverrideKey(pub Vec<Vec<String>>);
impl FromStr for OverrideKey {
type Err = errors::OverrideKeyFromStr;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let overrides = s
.split(',')
.map(|overrides| overrides.split('>').map(|s| s.to_string()).collect())
.collect::<Vec<Vec<String>>>();
if overrides.is_empty() {
return Err(errors::OverrideKeyFromStr::Empty);
}
Ok(Self(overrides))
}
}
impl Display for OverrideKey {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{}",
self.0
.iter()
.map(|overrides| {
overrides
.iter()
.map(|o| o.as_str())
.collect::<Vec<_>>()
.join(">")
})
.collect::<Vec<_>>()
.join(",")
)
}
}
pub mod errors {
use thiserror::Error;
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum OverrideKeyFromStr {
#[error("empty override key")]
Empty,
}
}

View file

@ -1,13 +1,7 @@
use crate::{
names::{PackageName, PackageNames},
source::{DependencySpecifiers, VersionId},
};
use relative_path::RelativePathBuf; use relative_path::RelativePathBuf;
use semver::Version;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_with::{DeserializeFromStr, SerializeDisplay};
use std::{ use std::{
collections::{BTreeMap, BTreeSet}, collections::BTreeSet,
fmt::{Display, Formatter}, fmt::{Display, Formatter},
str::FromStr, str::FromStr,
}; };
@ -176,155 +170,9 @@ impl Display for Target {
} }
} }
#[derive(
Debug, DeserializeFromStr, SerializeDisplay, Clone, PartialEq, Eq, Hash, PartialOrd, Ord,
)]
pub struct OverrideKey(pub Vec<Vec<String>>);
impl FromStr for OverrideKey {
type Err = errors::OverrideKeyFromStr;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let overrides = s
.split(',')
.map(|overrides| overrides.split('>').map(|s| s.to_string()).collect())
.collect::<Vec<Vec<String>>>();
if overrides.is_empty() {
return Err(errors::OverrideKeyFromStr::Empty);
}
Ok(Self(overrides))
}
}
impl Display for OverrideKey {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{}",
self.0
.iter()
.map(|overrides| {
overrides
.iter()
.map(|o| o.as_str())
.collect::<Vec<_>>()
.join(">")
})
.collect::<Vec<_>>()
.join(",")
)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum ScriptName {
#[cfg(feature = "roblox")]
RobloxSyncConfigGenerator,
#[cfg(feature = "wally-compat")]
SourcemapGenerator,
}
impl Display for ScriptName {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
#[cfg(feature = "roblox")]
ScriptName::RobloxSyncConfigGenerator => write!(f, "roblox_sync_config_generator"),
#[cfg(feature = "wally-compat")]
ScriptName::SourcemapGenerator => write!(f, "sourcemap_generator"),
}
}
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Manifest {
pub name: PackageName,
pub version: Version,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub license: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub authors: Option<Vec<String>>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub repository: Option<String>,
pub target: Target,
#[serde(default)]
pub private: bool,
#[serde(default, skip_serializing)]
pub scripts: BTreeMap<String, RelativePathBuf>,
#[serde(default)]
pub indices: BTreeMap<String, url::Url>,
#[cfg(feature = "wally-compat")]
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
pub wally_indices: BTreeMap<String, url::Url>,
#[serde(default, skip_serializing)]
pub overrides: BTreeMap<OverrideKey, DependencySpecifiers>,
#[serde(default)]
pub includes: BTreeSet<String>,
#[cfg(feature = "patches")]
#[serde(default, skip_serializing)]
pub patches: BTreeMap<PackageNames, BTreeMap<VersionId, RelativePathBuf>>,
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
pub dependencies: BTreeMap<String, DependencySpecifiers>,
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
pub peer_dependencies: BTreeMap<String, DependencySpecifiers>,
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
pub dev_dependencies: BTreeMap<String, DependencySpecifiers>,
}
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[serde(rename_all = "snake_case")]
pub enum DependencyType {
Standard,
Dev,
Peer,
}
impl Manifest {
pub fn all_dependencies(
&self,
) -> Result<
BTreeMap<String, (DependencySpecifiers, DependencyType)>,
errors::AllDependenciesError,
> {
let mut all_deps = BTreeMap::new();
for (deps, ty) in [
(&self.dependencies, DependencyType::Standard),
(&self.peer_dependencies, DependencyType::Peer),
(&self.dev_dependencies, DependencyType::Dev),
] {
for (alias, spec) in deps {
if all_deps.insert(alias.clone(), (spec.clone(), ty)).is_some() {
return Err(errors::AllDependenciesError::AliasConflict(alias.clone()));
}
}
}
Ok(all_deps)
}
}
pub mod errors { pub mod errors {
use thiserror::Error; use thiserror::Error;
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum OverrideKeyFromStr {
#[error("empty override key")]
Empty,
}
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum AllDependenciesError {
#[error("another specifier is already using the alias {0}")]
AliasConflict(String),
}
#[derive(Debug, Error)] #[derive(Debug, Error)]
#[non_exhaustive] #[non_exhaustive]
pub enum TargetValidatePublishError { pub enum TargetValidatePublishError {

View file

@ -6,6 +6,27 @@ use std::{
thread::spawn, thread::spawn,
}; };
use std::fmt::{Display, Formatter};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum ScriptName {
#[cfg(feature = "roblox")]
RobloxSyncConfigGenerator,
#[cfg(feature = "wally-compat")]
SourcemapGenerator,
}
impl Display for ScriptName {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
#[cfg(feature = "roblox")]
ScriptName::RobloxSyncConfigGenerator => write!(f, "roblox_sync_config_generator"),
#[cfg(feature = "wally-compat")]
ScriptName::SourcemapGenerator => write!(f, "sourcemap_generator"),
}
}
}
pub fn execute_script<A: IntoIterator<Item = S>, S: AsRef<OsStr>, P: AsRef<Path>>( pub fn execute_script<A: IntoIterator<Item = S>, S: AsRef<OsStr>, P: AsRef<Path>>(
script_name: Option<&str>, script_name: Option<&str>,
script_path: &Path, script_path: &Path,

View file

@ -1,5 +1,8 @@
use crate::{ use crate::{
manifest::{DependencyType, Target, TargetKind}, manifest::{
target::{Target, TargetKind},
DependencyType,
},
names::PackageNames, names::PackageNames,
Project, Project,
}; };
@ -246,6 +249,6 @@ pub mod errors {
Version(#[from] semver::Error), Version(#[from] semver::Error),
#[error("malformed target")] #[error("malformed target")]
Target(#[from] crate::manifest::errors::TargetKindFromStr), Target(#[from] crate::manifest::target::errors::TargetKindFromStr),
} }
} }

View file

@ -7,7 +7,10 @@ use pkg_ref::PesdePackageRef;
use specifier::PesdeDependencySpecifier; use specifier::PesdeDependencySpecifier;
use crate::{ use crate::{
manifest::{DependencyType, Target, TargetKind}, manifest::{
target::{Target, TargetKind},
DependencyType,
},
names::{PackageName, PackageNames}, names::{PackageName, PackageNames},
source::{hash, DependencySpecifiers, PackageSource, ResolveResult, VersionId}, source::{hash, DependencySpecifiers, PackageSource, ResolveResult, VersionId},
util::authenticate_conn, util::authenticate_conn,

View file

@ -4,7 +4,10 @@ use semver::Version;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::{ use crate::{
manifest::{DependencyType, Target, TargetKind}, manifest::{
target::{Target, TargetKind},
DependencyType,
},
names::PackageName, names::PackageName,
source::{pesde::PesdePackageSource, DependencySpecifiers, PackageRef, PackageSources}, source::{pesde::PesdePackageSource, DependencySpecifiers, PackageRef, PackageSources},
}; };

View file

@ -1,4 +1,4 @@
use crate::{manifest::TargetKind, names::PackageName, source::DependencySpecifier}; use crate::{manifest::target::TargetKind, names::PackageName, source::DependencySpecifier};
use semver::VersionReq; use semver::VersionReq;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::fmt::Display; use std::fmt::Display;