Skip to content

Commit

Permalink
review pm
Browse files Browse the repository at this point in the history
  • Loading branch information
ityuany committed Jan 29, 2025
1 parent 97fe92e commit c4fc470
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 85 deletions.
7 changes: 4 additions & 3 deletions crates/cli/src/execute_cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,11 @@ pub async fn execute_cli(cli: SnmCli, snm_config: SnmConfig) -> anyhow::Result<(
// manage end
SnmCommands::Install(_) | SnmCommands::Uninstall(_) | SnmCommands::Run(_) => {
let pm = PackageManager::from(&snm_config.workspace)?;
let handler = pm.get_ops();
let commands = match cli.command {
SnmCommands::Install(install_args) => pm.install(install_args),
SnmCommands::Uninstall(remove_args) => pm.remove(remove_args),
SnmCommands::Run(run_args) => pm.run(run_args),
SnmCommands::Install(install_args) => handler.install(install_args),
SnmCommands::Uninstall(remove_args) => handler.remove(remove_args),
SnmCommands::Run(run_args) => handler.run(run_args),
_ => unreachable!(),
}?;

Expand Down
21 changes: 11 additions & 10 deletions crates/snm_pm/src/ops/npm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,22 @@ use snm_utils::trace_if;
use tracing::trace;

use super::ops::{InstallArgs, PackageManagerOps, RemoveArgs};
use crate::pm_metadata::PackageManagerMetadata;

pub struct NpmCommandLine<'a> {
pub metadata: &'a PackageManagerMetadata,
pub struct NpmCommandLine {
pub name: String,
}

impl<'a> NpmCommandLine<'a> {
pub fn new(pm: &'a PackageManagerMetadata) -> Self {
Self { metadata: pm }
impl NpmCommandLine {
pub fn new() -> Self {
Self {
name: String::from("npm"),
}
}
}

impl<'a> PackageManagerOps for NpmCommandLine<'a> {
impl PackageManagerOps for NpmCommandLine {
fn install(&self, args: InstallArgs) -> anyhow::Result<Vec<String>> {
let mut command = vec![self.metadata.name.clone()];
let mut command = vec![self.name.clone()];

if args.frozen {
command.push(String::from("ci"));
Expand Down Expand Up @@ -57,7 +58,7 @@ impl<'a> PackageManagerOps for NpmCommandLine<'a> {
}

fn remove(&self, args: RemoveArgs) -> anyhow::Result<Vec<String>> {
let command = vec![self.metadata.name.clone(), String::from("uninstall")]
let command = vec![self.name.clone(), String::from("uninstall")]
.into_iter()
.chain(args.package_spec)
.collect();
Expand All @@ -68,7 +69,7 @@ impl<'a> PackageManagerOps for NpmCommandLine<'a> {
trace_if!(|| trace!(r#"Ops run args:{:?}"#, &args));

let command = vec![
self.metadata.name.clone(),
self.name.clone(),
String::from("run"),
args.command,
String::from("--"),
Expand Down
31 changes: 14 additions & 17 deletions crates/snm_pm/src/ops/pnpm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,22 @@ use snm_utils::trace_if;
use tracing::trace;

use super::ops::{InstallArgs, PackageManagerOps, RemoveArgs};
use crate::pm_metadata::PackageManagerMetadata;

pub struct PnpmCommandLine<'a> {
pub metadata: &'a PackageManagerMetadata,
pub struct PnpmCommandLine {
pub name: String,
}

impl<'a> PnpmCommandLine<'a> {
pub fn new(pm: &'a PackageManagerMetadata) -> Self {
Self { metadata: pm }
impl PnpmCommandLine {
pub fn new() -> Self {
Self {
name: String::from("pnpm"),
}
}
}

impl<'a> PackageManagerOps for PnpmCommandLine<'a> {
impl PackageManagerOps for PnpmCommandLine {
fn install(&self, args: InstallArgs) -> anyhow::Result<Vec<String>> {
let mut command = vec![self.metadata.name.clone()];
let mut command = vec![self.name.clone()];

if args.package_spec.is_empty() {
command.push(String::from("install"));
Expand Down Expand Up @@ -55,7 +56,7 @@ impl<'a> PackageManagerOps for PnpmCommandLine<'a> {
}

fn remove(&self, args: RemoveArgs) -> anyhow::Result<Vec<String>> {
let command = vec![self.metadata.name.clone(), String::from("remove")]
let command = vec![self.name.clone(), String::from("remove")]
.into_iter()
.chain(args.package_spec)
.collect();
Expand All @@ -65,14 +66,10 @@ impl<'a> PackageManagerOps for PnpmCommandLine<'a> {
fn run(&self, args: super::ops::RunArgs) -> anyhow::Result<Vec<String>> {
trace_if!(|| trace!(r#"Ops run args:{:?}"#, &args));

let command = vec![
self.metadata.name.clone(),
String::from("run"),
args.command,
]
.into_iter()
.chain(args.passthrough_args.clone())
.collect();
let command = vec![self.name.clone(), String::from("run"), args.command]
.into_iter()
.chain(args.passthrough_args.clone())
.collect();

trace_if!(|| trace!(r#"Ops run cmd:{:?}"#, command));

Expand Down
31 changes: 14 additions & 17 deletions crates/snm_pm/src/ops/yarn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,22 @@ use snm_utils::trace_if;
use tracing::trace;

use super::ops::{InstallArgs, PackageManagerOps, RemoveArgs};
use crate::pm_metadata::PackageManagerMetadata;

pub struct YarnCommandLine<'a> {
pub metadata: &'a PackageManagerMetadata,
pub struct YarnCommandLine {
pub name: String,
}

impl<'a> YarnCommandLine<'a> {
pub fn new(pm: &'a PackageManagerMetadata) -> Self {
Self { metadata: pm }
impl YarnCommandLine {
pub fn new() -> Self {
Self {
name: String::from("yarn"),
}
}
}

impl<'a> PackageManagerOps for YarnCommandLine<'a> {
impl PackageManagerOps for YarnCommandLine {
fn install(&self, args: InstallArgs) -> anyhow::Result<Vec<String>> {
let mut command = vec![self.metadata.name.clone(), String::from("install")];
let mut command = vec![self.name.clone(), String::from("install")];

if args.package_spec.is_empty() {
command.push(String::from("install"));
Expand Down Expand Up @@ -53,7 +54,7 @@ impl<'a> PackageManagerOps for YarnCommandLine<'a> {
}

fn remove(&self, args: RemoveArgs) -> anyhow::Result<Vec<String>> {
let command = vec![self.metadata.name.clone(), String::from("remove")]
let command = vec![self.name.clone(), String::from("remove")]
.into_iter()
.chain(args.package_spec)
.collect();
Expand All @@ -63,14 +64,10 @@ impl<'a> PackageManagerOps for YarnCommandLine<'a> {
fn run(&self, args: super::ops::RunArgs) -> anyhow::Result<Vec<String>> {
trace_if!(|| trace!(r#"Ops run args:{:?}"#, &args));

let command = vec![
self.metadata.name.clone(),
String::from("run"),
args.command,
]
.into_iter()
.chain(args.passthrough_args.clone())
.collect();
let command = vec![self.name.clone(), String::from("run"), args.command]
.into_iter()
.chain(args.passthrough_args.clone())
.collect();

trace_if!(|| trace!(r#"Ops run cmd:{:?}"#, command));
Ok(command)
Expand Down
31 changes: 14 additions & 17 deletions crates/snm_pm/src/ops/yarn_berry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,22 @@ use snm_utils::trace_if;
use tracing::trace;

use super::ops::{InstallArgs, PackageManagerOps, RemoveArgs};
use crate::pm_metadata::PackageManagerMetadata;

pub struct YarnBerryCommandLine<'a> {
pub metadata: &'a PackageManagerMetadata,
pub struct YarnBerryCommandLine {
pub name: String,
}

impl<'a> YarnBerryCommandLine<'a> {
pub fn new(pm: &'a PackageManagerMetadata) -> Self {
Self { metadata: pm }
impl YarnBerryCommandLine {
pub fn new() -> Self {
Self {
name: String::from("yarn"),
}
}
}

impl<'a> PackageManagerOps for YarnBerryCommandLine<'a> {
impl PackageManagerOps for YarnBerryCommandLine {
fn install(&self, args: InstallArgs) -> anyhow::Result<Vec<String>> {
let mut command = vec![self.metadata.name.clone(), String::from("install")];
let mut command = vec![self.name.clone(), String::from("install")];

if args.package_spec.is_empty() {
command.push(String::from("install"));
Expand Down Expand Up @@ -53,7 +54,7 @@ impl<'a> PackageManagerOps for YarnBerryCommandLine<'a> {
}

fn remove(&self, args: RemoveArgs) -> anyhow::Result<Vec<String>> {
let command = vec![self.metadata.name.clone(), String::from("remove")]
let command = vec![self.name.clone(), String::from("remove")]
.into_iter()
.chain(args.package_spec)
.collect();
Expand All @@ -63,14 +64,10 @@ impl<'a> PackageManagerOps for YarnBerryCommandLine<'a> {
fn run(&self, args: super::ops::RunArgs) -> anyhow::Result<Vec<String>> {
trace_if!(|| trace!(r#"Ops run args:{:?}"#, &args));

let command = vec![
self.metadata.name.clone(),
String::from("run"),
args.command,
]
.into_iter()
.chain(args.passthrough_args.clone())
.collect();
let command = vec![self.name.clone(), String::from("run"), args.command]
.into_iter()
.chain(args.passthrough_args.clone())
.collect();

trace_if!(|| trace!(r#"Ops run cmd:{:?}"#, command));
Ok(command)
Expand Down
48 changes: 27 additions & 21 deletions crates/snm_pm/src/pm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ use snm_utils::consts::ENV_KEY_FOR_SNM_PM;

use crate::{
ops::{
npm::NpmCommandLine,
ops::{InstallArgs, PackageManagerOps, RemoveArgs, RunArgs},
pnpm::PnpmCommandLine,
yarn::YarnCommandLine,
npm::NpmCommandLine, ops::PackageManagerOps, pnpm::PnpmCommandLine, yarn::YarnCommandLine,
yarn_berry::YarnBerryCommandLine,
},
package_json::PackageJson,
Expand All @@ -25,29 +22,38 @@ pub enum PackageManager {
}

impl PackageManager {
fn execute<F, T>(&self, callback: F) -> T
where
F: Fn(&dyn PackageManagerOps) -> T,
{
// fn execute<F, T>(&self, callback: F) -> T
// where
// F: Fn(&dyn PackageManagerOps) -> T,
// {
// match self {
// Self::Npm(metadata) => callback(&NpmCommandLine::new(metadata)),
// Self::Yarn(metadata) => callback(&YarnCommandLine::new(metadata)),
// Self::YarnBerry(metadata) => callback(&YarnBerryCommandLine::new(metadata)),
// Self::Pnpm(metadata) => callback(&PnpmCommandLine::new(metadata)),
// }
// }

pub fn get_ops(&self) -> Box<dyn PackageManagerOps> {
match self {
Self::Npm(metadata) => callback(&NpmCommandLine::new(metadata)),
Self::Yarn(metadata) => callback(&YarnCommandLine::new(metadata)),
Self::YarnBerry(metadata) => callback(&YarnBerryCommandLine::new(metadata)),
Self::Pnpm(metadata) => callback(&PnpmCommandLine::new(metadata)),
Self::Npm(_) => Box::new(NpmCommandLine::new()),
Self::Yarn(_) => Box::new(YarnCommandLine::new()),
Self::YarnBerry(_) => Box::new(YarnBerryCommandLine::new()),
Self::Pnpm(_) => Box::new(PnpmCommandLine::new()),
}
}

pub fn install(&self, args: InstallArgs) -> anyhow::Result<Vec<String>> {
self.execute(|pm| pm.install(args.clone()))
}
// pub fn install(&self, args: InstallArgs) -> anyhow::Result<Vec<String>> {
// self.execute(|pm| pm.install(args.clone()))
// }

pub fn remove(&self, args: RemoveArgs) -> anyhow::Result<Vec<String>> {
self.execute(|pm| pm.remove(args.clone()))
}
// pub fn remove(&self, args: RemoveArgs) -> anyhow::Result<Vec<String>> {
// self.execute(|pm| pm.remove(args.clone()))
// }

pub fn run(&self, args: RunArgs) -> anyhow::Result<Vec<String>> {
self.execute(|pm| pm.run(args.clone()))
}
// pub fn run(&self, args: RunArgs) -> anyhow::Result<Vec<String>> {
// self.execute(|pm| pm.run(args.clone()))
// }
}

impl PackageManager {
Expand Down

0 comments on commit c4fc470

Please sign in to comment.