Skip to content

Commit

Permalink
fix: reorganize command deps
Browse files Browse the repository at this point in the history
  • Loading branch information
Roger-luo committed Feb 10, 2023
1 parent 2d6558d commit 1c1407e
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 60 deletions.
12 changes: 8 additions & 4 deletions src/bin/ion/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
use clap::{ArgMatches, Command};
use ion::config::Config;
use ion::errors::CliResult;

pub mod auth;
pub mod bump;
pub mod clone;
Expand All @@ -14,6 +10,14 @@ pub mod script;
pub mod summon;
pub mod template;

pub use pkg::PackageSpecList;
pub use clap::parser::ArgMatches;
pub use clap::{arg, Command, ValueHint};
pub use ion::config::Config;
pub use ion::errors::CliResult;
pub use ion::utils::{assert_julia_version, Julia};


pub fn builtin() -> Vec<Command> {
vec![
auth::cli(),
Expand Down
9 changes: 2 additions & 7 deletions src/bin/ion/commands/pkg/add.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
use crate::commands::pkg::package_spec_list;
use clap::parser::ArgMatches;
use clap::{arg, Command, ValueHint};
use ion::config::Config;
use ion::errors::CliResult;
use ion::utils::Julia;
use crate::commands::*;

pub fn cli() -> Command {
Command::new("add")
Expand All @@ -14,7 +9,7 @@ pub fn cli() -> Command {
}

pub fn exec(config: &mut Config, matches: &ArgMatches) -> CliResult {
format!("using Pkg; Pkg.add([{}])", package_spec_list(matches))
format!("using Pkg; Pkg.add({})", PackageSpecList::new(matches))
.julia_exec(config, matches.get_flag("global"))?;
Ok(())
}
9 changes: 2 additions & 7 deletions src/bin/ion/commands/pkg/develop.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
use crate::commands::pkg::package_spec_list;
use clap::parser::ArgMatches;
use clap::{arg, Command, ValueHint};
use ion::config::Config;
use ion::errors::CliResult;
use ion::utils::Julia;
use crate::commands::*;

pub fn cli() -> Command {
Command::new("develop")
Expand All @@ -18,7 +13,7 @@ pub fn cli() -> Command {
}

pub fn exec(config: &mut Config, matches: &ArgMatches) -> CliResult {
format!("using Pkg; Pkg.develop([{}])", package_spec_list(matches))
format!("using Pkg; Pkg.develop({})", PackageSpecList::new(matches))
.julia_exec(config, matches.get_flag("global"))?;
Ok(())
}
9 changes: 2 additions & 7 deletions src/bin/ion/commands/pkg/free.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
use crate::commands::pkg::package_spec_list;
use clap::parser::ArgMatches;
use clap::{arg, Command};
use ion::config::Config;
use ion::errors::CliResult;
use ion::utils::Julia;
use crate::commands::*;

pub fn cli() -> Command {
Command::new("free")
Expand All @@ -14,7 +9,7 @@ pub fn cli() -> Command {
}

pub fn exec(config: &mut Config, matches: &ArgMatches) -> CliResult {
format!("using Pkg; Pkg.free([{}])", package_spec_list(matches))
format!("using Pkg; Pkg.free({})", PackageSpecList::new(matches))
.julia_exec(config, matches.get_flag("global"))?;
Ok(())
}
2 changes: 1 addition & 1 deletion src/bin/ion/commands/pkg/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ pub mod update;
pub mod utils;
pub mod why;

pub use utils::package_spec_list;
pub use utils::PackageSpecList;
9 changes: 2 additions & 7 deletions src/bin/ion/commands/pkg/remove.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
use crate::commands::pkg::package_spec_list;
use clap::parser::ArgMatches;
use clap::{arg, Command};
use ion::config::Config;
use ion::errors::CliResult;
use ion::utils::Julia;
use crate::commands::*;

pub fn cli() -> Command {
Command::new("remove")
Expand All @@ -15,7 +10,7 @@ pub fn cli() -> Command {
}

pub fn exec(config: &mut Config, matches: &ArgMatches) -> CliResult {
format!("using Pkg; Pkg.rm([{}])", package_spec_list(matches))
format!("using Pkg; Pkg.rm({})", PackageSpecList::new(matches))
.julia_exec(config, matches.get_flag("global"))?;
Ok(())
}
16 changes: 3 additions & 13 deletions src/bin/ion/commands/pkg/update.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
use crate::commands::pkg::package_spec_list;
use clap::parser::ArgMatches;
use clap::{arg, Command};
use ion::config::Config;
use ion::errors::CliResult;
use ion::utils::Julia;
use crate::commands::*;

pub fn cli() -> Command {
Command::new("update")
Expand All @@ -14,12 +9,7 @@ pub fn cli() -> Command {
}

pub fn exec(config: &mut Config, matches: &ArgMatches) -> CliResult {
let cmd = if matches.args_present() {
format!("using Pkg; Pkg.update([{}])", package_spec_list(matches))
} else {
"using Pkg; Pkg.update()".into()
};
cmd.julia_exec(config, matches.get_flag("global"))?;

format!("using Pkg; Pkg.update({})", PackageSpecList::new(matches))
.julia_exec(config, matches.get_flag("global"))?;
Ok(())
}
32 changes: 25 additions & 7 deletions src/bin/ion/commands/pkg/utils.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,30 @@
use std::fmt::Display;

use clap::parser::ArgMatches;
use ion::PackageSpec;

pub fn package_spec_list(matches: &ArgMatches) -> String {
let packages = matches.get_many::<String>("PACKAGE").into_iter().flatten();
pub struct PackageSpecList {
pub list: Vec<PackageSpec>,
}

impl PackageSpecList {
pub fn new(matches: &ArgMatches) -> Self {
let packages = matches
.get_many::<String>("PACKAGE").into_iter().flatten();
Self {
list: packages
.map(PackageSpec::new)
.collect::<Vec<_>>(),
}
}
}

packages
.map(PackageSpec::new)
.map(|p| format!("{p}"))
.collect::<Vec<_>>()
.join(",")
impl Display for PackageSpecList {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if self.list.len() == 0 {
write!(f, "")
} else {
write!(f, "[{}]", self.list.iter().map(|p| format!("{p}")).collect::<Vec<_>>().join(","))
}
}
}
9 changes: 2 additions & 7 deletions src/bin/ion/commands/pkg/why.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
use crate::commands::pkg::package_spec_list;
use clap::parser::ArgMatches;
use clap::{arg, Command, ValueHint};
use ion::config::Config;
use ion::errors::CliResult;
use ion::utils::{assert_julia_version, Julia};
use crate::commands::*;

pub fn cli() -> Command {
Command::new("why")
Expand All @@ -19,7 +14,7 @@ pub fn cli() -> Command {

pub fn exec(config: &mut Config, matches: &ArgMatches) -> CliResult {
assert_julia_version(config, ">=1.9.0-beta")?;
format!("using Pkg; Pkg.why([{}])", package_spec_list(matches))
format!("using Pkg; Pkg.why({})", PackageSpecList::new(matches))
.julia_exec(config, matches.get_flag("global"))?;
Ok(())
}

0 comments on commit 1c1407e

Please sign in to comment.