Skip to content

Commit

Permalink
refactor: general usability changes
Browse files Browse the repository at this point in the history
  • Loading branch information
moo1210 committed Feb 3, 2024
1 parent 59342d5 commit 780645d
Show file tree
Hide file tree
Showing 11 changed files with 66 additions and 100 deletions.
6 changes: 3 additions & 3 deletions src/commands/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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));
Expand Down
8 changes: 2 additions & 6 deletions src/commands/login.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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<PackageIndexConfig> {
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(&registry, None)?;
package_index.config()
Expand Down
4 changes: 2 additions & 2 deletions src/commands/logout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -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(&registry, None)?;
let api = package_index.config()?.api;
Expand Down
4 changes: 2 additions & 2 deletions src/commands/manifest_to_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand All @@ -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(())
Expand Down
4 changes: 2 additions & 2 deletions src/commands/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand All @@ -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.");
Expand Down
4 changes: 2 additions & 2 deletions src/commands/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -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(&registry, None)?;
Expand Down
6 changes: 3 additions & 3 deletions src/commands/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand All @@ -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,
Expand Down
65 changes: 27 additions & 38 deletions src/package_compat/compat.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -36,7 +31,7 @@ const PACKAGE_COMPATIBILITY_LAYERS: [PackageCompatibilityLayer; 3] = [
},
];

fn handle_based_on_file_path(path: PathBuf) -> anyhow::Result<Option<String>> {
fn handle_based_on_file_path(path: &Path) -> anyhow::Result<Option<String>> {
let mut best_manifest_name : Option<String> = None;
let mut best_layer_entry : Option<DirEntry> = None;
let mut best_layer_weight = 99;
Expand All @@ -48,41 +43,35 @@ fn handle_based_on_file_path(path: PathBuf) -> anyhow::Result<Option<String>> {
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;
});
}
Ok(best_manifest_name)
}

impl PackageCompatibility {
pub fn new(path: PathBuf) -> Self {
Self {
path,
}
}

pub fn load_backwards_compatible_package(self) -> anyhow::Result<Manifest> {
let best_layer_name = handle_based_on_file_path(self.path)?;
pub fn load_backwards_compatible_package(path: &Path) -> anyhow::Result<Manifest> {
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")
}
}
7 changes: 2 additions & 5 deletions src/package_compat/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

pub use compat::load_backwards_compatible_package;
51 changes: 18 additions & 33 deletions src/package_compat/wally_compat.rs
Original file line number Diff line number Diff line change
@@ -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<Manifest> {
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<Manifest> {
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)
}
7 changes: 3 additions & 4 deletions src/package_contents.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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] = &[
".*",
Expand All @@ -27,7 +26,7 @@ pub struct PackageContents {

impl PackageContents {
pub fn pack_from_path(input: &Path) -> anyhow::Result<Self> {
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();
Expand Down Expand Up @@ -94,7 +93,7 @@ impl PackageContents {
}

pub fn filtered_contents(input: &Path) -> anyhow::Result<Vec<PathBuf>> {
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;

Expand Down

0 comments on commit 780645d

Please sign in to comment.