Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor method 'add_cli' to reduce duplicated code #307

Merged
merged 1 commit into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 14 additions & 37 deletions crates/moon/src/cli/deps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,47 +85,24 @@ pub fn add_cli(cli: UniversalFlags, cmd: AddSubcommand) -> anyhow::Result<i32> {
let package_path = cmd.package_path;

let parts: Vec<&str> = package_path.splitn(2, '@').collect();

let author_pkg: Vec<&str> = parts[0].splitn(2, '/').collect();
if author_pkg.len() != 2 {
bail!("package path must be in the form of <author>/<package_name>[@<version>]");
}
let username = author_pkg[0];
let pkgname = author_pkg[1];
let pkg_name = ModuleName {
username: username.to_string(),
pkgname: pkgname.to_string(),
};

if parts.len() == 2 {
let version: &str = parts[1];
let version = version.parse()?;

let parts: Vec<&str> = parts[0].splitn(2, '/').collect();
if parts.len() != 2 {
bail!("package path must be in the form of <author>/<package_name>[@<version>]");
}
let username = parts[0];
let pkgname = parts[1];

let registry_config = RegistryConfig::load();
let name = ModuleName {
username: username.to_string(),
pkgname: pkgname.to_string(),
};
mooncake::pkg::add::add(
&source_dir,
&target_dir,
&name,
&version,
&registry_config,
false,
)
mooncake::pkg::add::add(&source_dir, &target_dir, &pkg_name, &version, false)
} else {
let parts: Vec<&str> = parts[0].splitn(2, '/').collect();
if parts.len() < 2 {
bail!("package path must be in the form of <author>/<package_name>[@<version>]");
}
let username = parts[0];
let pkgname = parts[1];

let registry_config = RegistryConfig::load();
mooncake::pkg::add::add_latest(
&source_dir,
&target_dir,
username,
pkgname,
&registry_config,
false,
)
mooncake::pkg::add::add_latest(&source_dir, &target_dir, &pkg_name, false)
}
}

Expand Down
33 changes: 9 additions & 24 deletions crates/mooncake/src/pkg/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,59 +17,45 @@
// For inquiries, you can contact us via e-mail at [email protected].

use colored::Colorize;
use moonutil::common::{read_module_desc_file_in_dir, write_module_json_to_file, MOONBITLANG_CORE};
use moonutil::dependency::DependencyInfo;
use moonutil::module::convert_module_to_mod_json;
use moonutil::mooncakes::{ModuleName, ModuleSource, RegistryConfig};
use moonutil::mooncakes::{ModuleName, ModuleSource};
use semver::Version;
use std::path::Path;
use std::rc::Rc;

use moonutil::common::{read_module_desc_file_in_dir, write_module_json_to_file, MOONBITLANG_CORE};

use crate::registry::{self, Registry, RegistryList};
use crate::resolver::resolve_single_root_with_defaults;

pub fn add_latest(
source_dir: &Path,
target_dir: &Path,
username: &str,
pkgname: &str,
registry_config: &RegistryConfig,
pkg_name: &ModuleName,
quiet: bool,
) -> anyhow::Result<i32> {
if format!("{username}/{pkgname}") == MOONBITLANG_CORE {
if pkg_name.to_string() == MOONBITLANG_CORE {
eprintln!(
"{}: no need to add `{}` as dependency",
MOONBITLANG_CORE,
"Warning".yellow().bold()
);
std::process::exit(0);
}

let registry = registry::OnlineRegistry::mooncakes_io();
let pkg_name = ModuleName {
username: username.to_string(),
pkgname: pkgname.to_string(),
};
let latest_version = registry
.get_latest_version(&pkg_name)
.get_latest_version(pkg_name)
.ok_or_else(|| {
anyhow::anyhow!(
"could not find the latest version of {}/{}. Please consider running `moon update` to update the index.",
username,
pkgname
"could not find the latest version of {}. Please consider running `moon update` to update the index.",
pkg_name.to_string()
)
})?
.version
.clone()
.unwrap();
add(
source_dir,
target_dir,
&pkg_name,
&latest_version,
registry_config,
quiet,
)
add(source_dir, target_dir, pkg_name, &latest_version, quiet)
}

#[test]
Expand All @@ -83,7 +69,6 @@ pub fn add(
_target_dir: &Path,
pkg_name: &ModuleName,
version: &Version,
_registry_config: &RegistryConfig,
quiet: bool,
) -> anyhow::Result<i32> {
let mut m = read_module_desc_file_in_dir(source_dir)?;
Expand Down
Loading