From 780645df218b0042a300b8b744cbee34ee0a2c72 Mon Sep 17 00:00:00 2001 From: "Carter Schuller (MeowSpark LLC)" Date: Sat, 3 Feb 2024 04:52:58 -0600 Subject: [PATCH] refactor: general usability changes --- src/commands/install.rs | 6 +-- src/commands/login.rs | 8 +--- src/commands/logout.rs | 4 +- src/commands/manifest_to_json.rs | 4 +- src/commands/publish.rs | 4 +- src/commands/search.rs | 4 +- src/commands/update.rs | 6 +-- src/package_compat/compat.rs | 65 +++++++++++++----------------- src/package_compat/mod.rs | 7 +--- src/package_compat/wally_compat.rs | 51 +++++++++-------------- src/package_contents.rs | 7 ++-- 11 files changed, 66 insertions(+), 100 deletions(-) diff --git a/src/commands/install.rs b/src/commands/install.rs index a6fb5be..cea4f5a 100644 --- a/src/commands/install.rs +++ b/src/commands/install.rs @@ -8,14 +8,14 @@ use structopt::StructOpt; use crate::installation::InstallationContext; use crate::lockfile::{LockPackage, Lockfile}; -use crate::manifest::Manifest; +use crate::package_compat; use crate::package_id::PackageId; use crate::package_source::{PackageSource, PackageSourceMap, Registry, TestRegistry}; use crate::resolution::resolve; use super::GlobalOptions; -/// Install all of the dependencies of this project. +/// Install all of the dependencies of this project. (cross-compatible with other package formats) #[derive(Debug, StructOpt)] pub struct InstallSubcommand { /// Path to the project to install dependencies for. @@ -25,7 +25,7 @@ pub struct InstallSubcommand { impl InstallSubcommand { pub fn run(self, global: GlobalOptions) -> anyhow::Result<()> { - let manifest = Manifest::load(&self.project_path)?; + let manifest = package_compat::load_backwards_compatible_package(&self.project_path)?; let lockfile = Lockfile::load(&self.project_path)? .unwrap_or_else(|| Lockfile::from_manifest(&manifest)); diff --git a/src/commands/login.rs b/src/commands/login.rs index 2125185..8211392 100644 --- a/src/commands/login.rs +++ b/src/commands/login.rs @@ -9,11 +9,7 @@ use reqwest::Url; use serde::Deserialize; use structopt::StructOpt; -use crate::{ - auth::AuthStore, - manifest::Manifest, - package_index::{PackageIndex, PackageIndexConfig}, -}; +use crate::{auth::AuthStore, package_compat, package_index::{PackageIndex, PackageIndexConfig}}; /// Log into a registry. #[derive(Debug, StructOpt)] @@ -111,7 +107,7 @@ fn prompt_github_auth(api: url::Url, github_oauth_id: &str) -> anyhow::Result<() } fn fetch_package_index_config(project_path: &Path) -> anyhow::Result { - let manifest = Manifest::load(project_path)?; + let manifest = package_compat::load_backwards_compatible_package(project_path)?; let registry = Url::parse(&manifest.package.registry)?; let package_index = PackageIndex::new(®istry, None)?; package_index.config() diff --git a/src/commands/logout.rs b/src/commands/logout.rs index d208447..ab96452 100644 --- a/src/commands/logout.rs +++ b/src/commands/logout.rs @@ -2,7 +2,7 @@ use std::path::PathBuf; use structopt::StructOpt; -use crate::{auth::AuthStore, manifest::Manifest, package_index::PackageIndex}; +use crate::{auth::AuthStore, package_compat, package_index::PackageIndex}; /// Log out of a registry. #[derive(Debug, StructOpt)] @@ -14,7 +14,7 @@ pub struct LogoutSubcommand { impl LogoutSubcommand { pub fn run(self) -> anyhow::Result<()> { - let manifest = Manifest::load(&self.project_path)?; + let manifest = package_compat::load_backwards_compatible_package(&self.project_path)?; let registry = url::Url::parse(&manifest.package.registry)?; let package_index = PackageIndex::new(®istry, None)?; let api = package_index.config()?.api; diff --git a/src/commands/manifest_to_json.rs b/src/commands/manifest_to_json.rs index b457fd9..8224699 100644 --- a/src/commands/manifest_to_json.rs +++ b/src/commands/manifest_to_json.rs @@ -2,7 +2,7 @@ use std::path::PathBuf; use structopt::StructOpt; -use crate::manifest::Manifest; +use crate::package_compat; /// Print a gooey manifest as a line of JSON. /// @@ -16,7 +16,7 @@ pub struct ManifestToJsonSubcommand { impl ManifestToJsonSubcommand { pub fn run(self) -> anyhow::Result<()> { - let manifest = Manifest::load(&self.project_path)?; + let manifest = package_compat::load_backwards_compatible_package(&self.project_path)?; println!("{}", serde_json::to_string(&manifest)?); Ok(()) diff --git a/src/commands/publish.rs b/src/commands/publish.rs index 06cbc56..5df2124 100644 --- a/src/commands/publish.rs +++ b/src/commands/publish.rs @@ -6,7 +6,7 @@ use ubyte::ToByteUnit; use url::Url; use crate::{ - auth::AuthStore, manifest::Manifest, package_contents::PackageContents, + auth::AuthStore, package_compat, package_contents::PackageContents, package_index::PackageIndex, GlobalOptions, }; @@ -26,7 +26,7 @@ pub struct PublishSubcommand { impl PublishSubcommand { pub fn run(self, global: GlobalOptions) -> anyhow::Result<()> { - let manifest = Manifest::load(&self.project_path)?; + let manifest = package_compat::load_backwards_compatible_package(&self.project_path)?; if manifest.package.private { bail!("Cannot publish private package."); diff --git a/src/commands/search.rs b/src/commands/search.rs index 7b5abd3..41c30f8 100644 --- a/src/commands/search.rs +++ b/src/commands/search.rs @@ -7,7 +7,7 @@ use reqwest::{blocking::Client, header::AUTHORIZATION}; use serde::Deserialize; use structopt::StructOpt; -use crate::{auth::AuthStore, manifest::Manifest, package_index::PackageIndex}; +use crate::{auth::AuthStore, package_compat, package_index::PackageIndex}; /// Search a registry for packages matching a query. #[derive(Debug, StructOpt)] @@ -22,7 +22,7 @@ pub struct SearchSubcommand { impl SearchSubcommand { pub fn run(self) -> anyhow::Result<()> { - let manifest = Manifest::load(&self.project_path)?; + let manifest = package_compat::load_backwards_compatible_package(&self.project_path)?; let registry = url::Url::parse(&manifest.package.registry)?; let auth_store = AuthStore::load()?; let package_index = PackageIndex::new(®istry, None)?; diff --git a/src/commands/update.rs b/src/commands/update.rs index e56b9bd..9ad7e3e 100644 --- a/src/commands/update.rs +++ b/src/commands/update.rs @@ -5,7 +5,7 @@ use std::time::Duration; use crate::installation::InstallationContext; use crate::lockfile::Lockfile; -use crate::manifest::Manifest; +use crate::package_compat; use crate::package_id::PackageId; use crate::package_name::PackageName; use crate::package_req::PackageReq; @@ -15,7 +15,7 @@ use crossterm::style::{Attribute, Color, SetAttribute, SetForegroundColor}; use indicatif::{ProgressBar, ProgressStyle}; use structopt::StructOpt; -/// Update all of the dependencies of this project. +/// Update all of the dependencies of this project. (cross-compatible with other package formats) #[derive(Debug, StructOpt)] pub struct UpdateSubcommand { /// Path to the project to publish. @@ -29,7 +29,7 @@ pub struct UpdateSubcommand { impl UpdateSubcommand { pub fn run(self, global: GlobalOptions) -> anyhow::Result<()> { - let manifest = Manifest::load(&self.project_path)?; + let manifest = package_compat::load_backwards_compatible_package(&self.project_path)?; let lockfile = match Lockfile::load(&self.project_path)? { Some(lockfile) => lockfile, diff --git a/src/package_compat/compat.rs b/src/package_compat/compat.rs index b665a7a..26ccbb5 100644 --- a/src/package_compat/compat.rs +++ b/src/package_compat/compat.rs @@ -1,21 +1,16 @@ use std::ffi::OsString; use std::fs::DirEntry; -use std::path::PathBuf; +use std::path::{Path}; use crate::manifest::{Manifest, MANIFEST_FILE_NAME}; -use crate::package_compat::WALLY_MANIFEST_FILE_NAME; -use crate::package_compat::wally_compat::WallyPackageCompatibility; +use crate::package_compat::{wally_compat, WALLY_MANIFEST_FILE_NAME}; use crate::package_compat::wally_manifest::WallyManifest; -#[derive(Debug)] -pub struct PackageCompatibility { - /// The path of the package to open. - path: PathBuf, -} +/// package cross compatibility layer management #[derive(Debug)] -struct PackageCompatibilityLayer { +struct PackageCompatibilityLayer<'a> { /// The packages manifest name - manifest_name: *const str, + manifest_name: &'a str, /// Weight of layer weight: u8, @@ -36,7 +31,7 @@ const PACKAGE_COMPATIBILITY_LAYERS: [PackageCompatibilityLayer; 3] = [ }, ]; -fn handle_based_on_file_path(path: PathBuf) -> anyhow::Result> { +fn handle_based_on_file_path(path: &Path) -> anyhow::Result> { let mut best_manifest_name : Option = None; let mut best_layer_entry : Option = None; let mut best_layer_weight = 99; @@ -48,7 +43,7 @@ fn handle_based_on_file_path(path: PathBuf) -> anyhow::Result> { PACKAGE_COMPATIBILITY_LAYERS.iter().find(|layer| { layer.manifest_name == name.as_os_str().to_str().unwrap_or("") && layer.weight < best_layer_weight }).map(|layer| { - best_manifest_name = Some(layer.manifest_name.parse().unwrap()); + best_manifest_name = Some(layer.manifest_name.to_string()); best_layer_entry = Some(pos_best_layer); best_layer_weight = layer.weight; }); @@ -56,33 +51,27 @@ fn handle_based_on_file_path(path: PathBuf) -> anyhow::Result> { Ok(best_manifest_name) } -impl PackageCompatibility { - pub fn new(path: PathBuf) -> Self { - Self { - path, - } - } - pub fn load_backwards_compatible_package(self) -> anyhow::Result { - let best_layer_name = handle_based_on_file_path(self.path)?; +pub fn load_backwards_compatible_package(path: &Path) -> anyhow::Result { + let best_layer_name = handle_based_on_file_path(path)?; - match best_layer_name { - Some(layer) => { - match layer.as_str() { - MANIFEST_FILE_NAME => { - let manifest = Manifest::load(&self.path)?; - Ok(manifest) - }, // load manifest normally, return normal manifest. - WALLY_MANIFEST_FILE_NAME => { - let wally_manifest = WallyManifest::load(&self.path)?; - let wally_compat = WallyPackageCompatibility::new(wally_manifest); - let manifest = wally_compat.load_as_backwards_compatible_package()?; - Ok(manifest) - }, // load wally manifest & pass to wally_compat, then return normal manifest. - _ => anyhow::bail!("Unknown package compatibility layer {}", layer), - } - }, - None => anyhow::bail!("No available package compatibility layer found") - } + match best_layer_name { + Some(layer) => { + match layer.as_str() { + MANIFEST_FILE_NAME => { + let manifest = Manifest::load(&path)?; + log::debug!("Using normal manifest"); + Ok(manifest) + }, // load manifest normally, return normal manifest. + WALLY_MANIFEST_FILE_NAME => { + let wally_manifest = WallyManifest::load(&path)?; + let manifest = wally_compat::load_as_backwards_compatible_package(wally_manifest)?; + log::debug!("Using Wally manifest"); + Ok(manifest) + }, // load wally manifest & pass to wally_compat, then return normal manifest. + _ => anyhow::bail!("Unknown package compatibility layer {}", layer), + } + }, + None => anyhow::bail!("No available package compatibility layer found") } } \ No newline at end of file diff --git a/src/package_compat/mod.rs b/src/package_compat/mod.rs index b45bb5c..86bbfef 100644 --- a/src/package_compat/mod.rs +++ b/src/package_compat/mod.rs @@ -5,9 +5,6 @@ mod wally_package_req; mod compat; mod wally_compat; -pub use compat::PackageCompatibility; - use wally_manifest::*; -use wally_package_id::WallyPackageId; -use wally_package_name::WallyPackageName; -use wally_package_req::WallyPackageReq; \ No newline at end of file + +pub use compat::load_backwards_compatible_package; \ No newline at end of file diff --git a/src/package_compat/wally_compat.rs b/src/package_compat/wally_compat.rs index 0ae930d..5a584ab 100644 --- a/src/package_compat/wally_compat.rs +++ b/src/package_compat/wally_compat.rs @@ -1,44 +1,29 @@ use crate::manifest::{Manifest, Realm}; use crate::package_compat::wally_manifest::{WallyManifest, WallyRealm}; -use crate::package_compat::WallyPackage; -use crate::package_id::PackageId; use crate::package_name::PackageName; -#[derive(Debug)] -pub struct WallyPackageCompatibility { - /// The path of the package to open. - manifest: WallyManifest, -} -impl WallyPackageCompatibility { - pub fn new(manifest: WallyManifest) -> Self { - Self { - manifest, - } +pub fn load_as_backwards_compatible_package(manifest: WallyManifest) -> anyhow::Result { + let mut new_manifest = Manifest::new()?; + + new_manifest.package.name = PackageName::new(manifest.package.name.scope(), manifest.package.name.name())?; + match manifest.package.realm { + WallyRealm::Server => {new_manifest.package.realm = Realm::Server} + WallyRealm::Shared => {new_manifest.package.realm = Realm::Shared} + WallyRealm::Dev => {new_manifest.package.realm = Realm::Dev} } - pub fn load_as_backwards_compatible_package(&self) -> anyhow::Result { - let mut manifest = Manifest::new()?; - manifest.package.name = PackageName::new(self.manifest.package.name.scope(), self.manifest.package.name.name())?; - match self.manifest.package.realm { - WallyRealm::Server => {manifest.package.realm = Realm::Server} - WallyRealm::Shared => {manifest.package.realm = Realm::Shared} - WallyRealm::Dev => {manifest.package.realm = Realm::Dev} - } - - - // TODO: This will obviously not be a 1:1 clone in the near future - manifest.package.version = self.manifest.package.version.clone(); - manifest.package.registry = self.manifest.package.registry.clone(); - manifest.package.description = self.manifest.package.description.clone(); - manifest.package.license = self.manifest.package.license.clone(); - manifest.package.authors = self.manifest.package.authors.clone(); - manifest.package.include = self.manifest.package.include.clone(); - manifest.package.exclude = self.manifest.package.exclude.clone(); - manifest.package.private = self.manifest.package.private.clone(); + // TODO: This will obviously not be a 1:1 clone in the near future + new_manifest.package.version = manifest.package.version.clone(); + new_manifest.package.registry = manifest.package.registry.clone(); + new_manifest.package.description = manifest.package.description.clone(); + new_manifest.package.license = manifest.package.license.clone(); + new_manifest.package.authors = manifest.package.authors.clone(); + new_manifest.package.include = manifest.package.include.clone(); + new_manifest.package.exclude = manifest.package.exclude.clone(); + new_manifest.package.private = manifest.package.private.clone(); - Ok(manifest) - } + Ok(new_manifest) } \ No newline at end of file diff --git a/src/package_contents.rs b/src/package_contents.rs index a9f5f07..727e9ae 100644 --- a/src/package_contents.rs +++ b/src/package_contents.rs @@ -7,8 +7,7 @@ use globset::{Glob, GlobSet, GlobSetBuilder}; use serde_json::json; use walkdir::WalkDir; use zip::{write::FileOptions, ZipArchive, ZipWriter}; - -use crate::manifest::Manifest; +use crate::package_compat; static EXCLUDED_GLOBS: &[&str] = &[ ".*", @@ -27,7 +26,7 @@ pub struct PackageContents { impl PackageContents { pub fn pack_from_path(input: &Path) -> anyhow::Result { - let manifest = Manifest::load(input)?; + let manifest = package_compat::load_backwards_compatible_package(input)?; let package_name = manifest.package.name.name(); let mut data = Vec::new(); @@ -94,7 +93,7 @@ impl PackageContents { } pub fn filtered_contents(input: &Path) -> anyhow::Result> { - let manifest = Manifest::load(input)?; + let manifest = package_compat::load_backwards_compatible_package(input)?; let includes = manifest.package.include; let mut excludes = manifest.package.exclude;