Skip to content

Commit

Permalink
chore: refine the usage of PkgIdentity (#429)
Browse files Browse the repository at this point in the history
* chore: refine the usage of PkgIdentity

* chore: refine the usage of PkgIdentity

* chore: refine the usage of PkgIdentity

* chore: refine the usage of PkgIdentity

* chore: refine the usage of PkgIdentity

* chore: refine the usage of PkgIdentity

* chore: refine the usage of PkgIdentity

* chore: refine the usage of PkgIdentity
  • Loading branch information
halajohn authored Dec 19, 2024
1 parent e9589cd commit 8f95e8b
Show file tree
Hide file tree
Showing 33 changed files with 395 additions and 336 deletions.
7 changes: 2 additions & 5 deletions core/src/ten_manager/src/cmd/cmd_delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use console::Emoji;
use indicatif::HumanDuration;
use semver::Version;

use ten_rust::pkg_info::pkg_identity::PkgIdentity;
use ten_rust::pkg_info::pkg_type::PkgType;

use crate::log::tman_verbose_println;
Expand Down Expand Up @@ -85,10 +84,8 @@ pub async fn execute_cmd(

delete_package(
tman_config,
&PkgIdentity {
pkg_type: PkgType::from_str(&command_data.package_type)?,
name: command_data.package_name,
},
PkgType::from_str(&command_data.package_type)?,
&command_data.package_name,
&Version::parse(&command_data.version)?,
&command_data.hash,
)
Expand Down
79 changes: 35 additions & 44 deletions core/src/ten_manager/src/cmd/cmd_install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ use ten_rust::pkg_info::{
dependencies::{DependencyRelationship, PkgDependency},
find_to_be_replaced_local_pkgs, find_untracked_local_packages,
get_pkg_info_from_path,
pkg_identity::PkgIdentity,
pkg_type::PkgType,
pkg_type_and_name::PkgTypeAndName,
supports::{is_pkg_supports_compatible_with, Arch, Os, PkgSupport},
PkgInfo,
};
Expand Down Expand Up @@ -228,22 +228,22 @@ fn update_package_manifest(
base_pkg_info.manifest.as_mut().unwrap().dependencies
{
let is_present = dependencies.iter().any(|dep| {
dep.pkg_type == added_pkg_info.pkg_identity.pkg_type.to_string()
&& dep.name == added_pkg_info.pkg_identity.name
dep.pkg_type == added_pkg_info.pkg_type.to_string()
&& dep.name == added_pkg_info.name
});

if !is_present {
dependencies.push(ManifestDependency {
pkg_type: added_pkg_info.pkg_identity.pkg_type.to_string(),
name: added_pkg_info.pkg_identity.name.clone(),
pkg_type: added_pkg_info.pkg_type.to_string(),
name: added_pkg_info.name.clone(),
version: added_pkg_info.version.to_string(),
});
}
} else {
base_pkg_info.manifest.clone().unwrap().dependencies =
Some(vec![ManifestDependency {
pkg_type: added_pkg_info.pkg_identity.pkg_type.to_string(),
name: added_pkg_info.pkg_identity.name.clone(),
pkg_type: added_pkg_info.pkg_type.to_string(),
name: added_pkg_info.name.clone(),
version: added_pkg_info.version.to_string(),
}]);
}
Expand Down Expand Up @@ -298,7 +298,7 @@ fn parse_pkg_name_version(
fn filter_compatible_pkgs_to_candidates(
tman_config: &TmanConfig,
all_existing_local_pkgs: &Vec<PkgInfo>,
all_candidates: &mut HashMap<PkgIdentity, HashSet<PkgInfo>>,
all_candidates: &mut HashMap<PkgTypeAndName, HashSet<PkgInfo>>,
support: &PkgSupport,
) {
for existed_pkg in all_existing_local_pkgs.to_owned().iter_mut() {
Expand All @@ -317,12 +317,12 @@ fn filter_compatible_pkgs_to_candidates(
tman_verbose_println!(
tman_config,
"The existed {} package {} is compatible with the current system.",
existed_pkg.pkg_identity.pkg_type,
existed_pkg.pkg_identity.name
existed_pkg.pkg_type,
existed_pkg.name
);

all_candidates
.entry(existed_pkg.pkg_identity.clone())
.entry((&*existed_pkg).into())
.or_default()
.insert(existed_pkg.clone());
} else {
Expand All @@ -332,8 +332,8 @@ fn filter_compatible_pkgs_to_candidates(
tman_config,
"The existed {} package {} is not compatible \
with the current system.",
existed_pkg.pkg_identity.pkg_type,
existed_pkg.pkg_identity.name
existed_pkg.pkg_type,
existed_pkg.name
);
}
}
Expand Down Expand Up @@ -376,10 +376,7 @@ appear in the dependency tree:",
Emoji("💡", "")
);
for pkg in untracked_local_pkgs {
println!(
" {}:{}@{}",
pkg.pkg_identity.pkg_type, pkg.pkg_identity.name, pkg.version
);
println!(" {}:{}@{}", pkg.pkg_type, pkg.name, pkg.version);
}
}

Expand All @@ -402,23 +399,23 @@ appear in the dependency tree:",
if old_supports_str != new_supports_str {
println!(
" {}:{}@{}{} -> {}:{}@{}{}",
old_pkg.pkg_identity.pkg_type,
old_pkg.pkg_identity.name,
old_pkg.pkg_type,
old_pkg.name,
old_pkg.version,
old_supports_str,
new_pkg.pkg_identity.pkg_type,
new_pkg.pkg_identity.name,
new_pkg.pkg_type,
new_pkg.name,
new_pkg.version,
new_supports_str
);
} else {
println!(
" {}:{}@{} -> {}:{}@{}",
old_pkg.pkg_identity.pkg_type,
old_pkg.pkg_identity.name,
old_pkg.pkg_type,
old_pkg.name,
old_pkg.version,
new_pkg.pkg_identity.pkg_type,
new_pkg.pkg_identity.name,
new_pkg.pkg_type,
new_pkg.name,
new_pkg.version
);
}
Expand Down Expand Up @@ -458,7 +455,7 @@ pub async fn execute_cmd(
// those addons (extensions, extension_groups, ...) installed in the app
// directory are all considered initial_input_pkgs.
let mut initial_input_pkgs = vec![];
let mut all_candidates: HashMap<PkgIdentity, HashSet<PkgInfo>> =
let mut all_candidates: HashMap<PkgTypeAndName, HashSet<PkgInfo>> =
HashMap::new();

// 'all_existing_local_pkgs' contains all the packages which are already
Expand Down Expand Up @@ -499,7 +496,7 @@ pub async fn execute_cmd(
let template_data = serde_json::to_value(&command_data.template_data)?;

// The locked_pkgs comes from a lock file in the app folder.
let mut locked_pkgs: Option<HashMap<PkgIdentity, PkgInfo>> = None;
let mut locked_pkgs: Option<HashMap<PkgTypeAndName, PkgInfo>> = None;

if command_data.template_mode {
template_ctx = Some(&template_data);
Expand All @@ -522,7 +519,7 @@ pub async fn execute_cmd(
desired_pkg_src_version_,
) = parse_pkg_name_version(&command_data.package_name.unwrap())?;

desired_pkg_type = Some(desired_pkg_type_.clone());
desired_pkg_type = Some(desired_pkg_type_);
desired_pkg_src_name = Some(desired_pkg_src_name_.clone());

// First, check that the package we want to install can be installed
Expand All @@ -532,7 +529,7 @@ pub async fn execute_cmd(
is_standalone_installing =
is_installing_package_standalone(&cwd, &desired_pkg_type_)?;
if is_standalone_installing {
affected_pkg_type = desired_pkg_type_.clone();
affected_pkg_type = desired_pkg_type_;
affected_pkg_name = desired_pkg_src_name_.clone();

if let Some(desired_pkg_dest_name) = desired_pkg_dest_name {
Expand All @@ -546,7 +543,7 @@ pub async fn execute_cmd(
// If it is not a standalone install, then the `cwd` must be within
// the base directory of a TEN app.
let app_pkg_ = get_pkg_info_from_path(&cwd)?;
affected_pkg_name = app_pkg_.pkg_identity.name.clone();
affected_pkg_name = app_pkg_.name.clone();

// Push the app itself into the initial_input_pkgs.
initial_input_pkgs.push(get_pkg_info_from_path(&cwd)?);
Expand All @@ -564,13 +561,12 @@ pub async fn execute_cmd(
);

let extra_dependency_relationship = DependencyRelationship {
pkg_identity: app_pkg_.pkg_identity.clone(),
pkg_type: app_pkg_.pkg_type,
name: app_pkg_.name.clone(),
version: app_pkg_.version.clone(),
dependency: PkgDependency {
pkg_identity: PkgIdentity {
pkg_type: desired_pkg_type_.clone(),
name: desired_pkg_src_name_.clone(),
},
pkg_type: desired_pkg_type_,
name: desired_pkg_src_name_.clone(),
version_req: desired_pkg_src_version_.clone(),
version_req_str: desired_pkg_src_version_str_,
},
Expand All @@ -581,22 +577,17 @@ pub async fn execute_cmd(
}

let dep = PkgDependency::new(
desired_pkg_type_.clone(),
desired_pkg_type_,
desired_pkg_src_name_.clone(),
desired_pkg_src_version_,
);
extra_dependencies.push(dep);

if let Some(desired_pkg_dest_name) = desired_pkg_dest_name {
let pkg_identity_mapping = PkgIdentityMapping {
src_pkg_identity: PkgIdentity {
pkg_type: desired_pkg_type_.clone(),
name: desired_pkg_src_name_,
},
dest_pkg_identity: PkgIdentity {
pkg_type: desired_pkg_type_,
name: desired_pkg_dest_name.to_string(),
},
pkg_type: desired_pkg_type_,
src_pkg_name: desired_pkg_src_name_,
dest_pkg_name: desired_pkg_dest_name.to_string(),
};
pkg_identity_mappings.push(pkg_identity_mapping);
}
Expand Down
55 changes: 31 additions & 24 deletions core/src/ten_manager/src/dep_and_candidate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ use anyhow::{anyhow, Result};
use semver::{Version, VersionReq};

use ten_rust::pkg_info::dependencies::PkgDependency;
use ten_rust::pkg_info::pkg_identity::PkgIdentity;
use ten_rust::pkg_info::pkg_type::PkgType;
use ten_rust::pkg_info::pkg_type_and_name::PkgTypeAndName;
use ten_rust::pkg_info::supports::{
is_pkg_supports_compatible_with, PkgSupport,
};
Expand Down Expand Up @@ -53,19 +53,21 @@ impl MergedVersionReq {
}

struct FoundDependency {
pkg_identity: PkgIdentity,
pkg_type: PkgType,
name: String,
version_reqs: MergedVersionReq,
}

impl Hash for FoundDependency {
fn hash<H: Hasher>(&self, state: &mut H) {
self.pkg_identity.hash(state);
self.pkg_type.hash(state);
self.name.hash(state);
}
}

impl PartialEq for FoundDependency {
fn eq(&self, other: &Self) -> bool {
self.pkg_identity == other.pkg_identity
self.pkg_type == other.pkg_type && self.name == other.name
}
}

Expand All @@ -81,7 +83,8 @@ fn merge_dependency_to_dependencies(
dependency: &PkgDependency,
) -> Result<bool> {
let searched_target = FoundDependency {
pkg_identity: dependency.pkg_identity.clone(),
pkg_type: dependency.pkg_type,
name: dependency.name.clone(),
version_reqs: MergedVersionReq::default(),
};

Expand All @@ -101,7 +104,8 @@ fn merge_dependency_to_dependencies(
// This is the first time seeing this dependency.

merged_dependencies.insert(FoundDependency {
pkg_identity: dependency.pkg_identity.clone(),
pkg_type: dependency.pkg_type,
name: dependency.name.clone(),
version_reqs: MergedVersionReq::new(&dependency.version_req),
});
}
Expand Down Expand Up @@ -131,7 +135,7 @@ async fn process_dependencies_to_get_candidates(
support: &PkgSupport,
input_dependencies: &Vec<PkgDependency>,
merged_dependencies: &mut HashSet<FoundDependency>,
all_candidates: &mut HashMap<PkgIdentity, HashSet<PkgInfo>>,
all_candidates: &mut HashMap<PkgTypeAndName, HashSet<PkgInfo>>,
new_pkgs_to_be_searched: &mut Vec<PkgInfo>,
) -> Result<()> {
for dependency in input_dependencies {
Expand All @@ -154,9 +158,13 @@ async fn process_dependencies_to_get_candidates(

// Retrieve all packages from the registry that meet the specified
// criteria.
let results =
get_package_list(tman_config, &dependency.pkg_identity, &criteria)
.await?;
let results = get_package_list(
tman_config,
dependency.pkg_type,
&dependency.name,
&criteria,
)
.await?;

let mut candidate_pkg_infos: Vec<PkgInfo> = vec![];

Expand Down Expand Up @@ -186,7 +194,7 @@ async fn process_dependencies_to_get_candidates(
// searching through `all_candidates` allows those locally installed
// packages to be added to `new_pkgs_to_be_searched`, ensuring that the
// dependencies within those packages are processed.
if let Some(candidates) = all_candidates.get(&dependency.pkg_identity) {
if let Some(candidates) = all_candidates.get(&dependency.into()) {
for candidate in candidates {
if dependency.version_req.matches(&candidate.version) {
tman_verbose_println!(
Expand Down Expand Up @@ -226,7 +234,7 @@ async fn process_dependencies_to_get_candidates(
);

all_candidates
.entry(dependency.pkg_identity.clone())
.entry(dependency.into())
.or_default()
.insert(candidate_pkg_info.clone());

Expand All @@ -242,15 +250,15 @@ async fn process_dependencies_to_get_candidates(
/// suitable one is the package with the highest compatible_score. If there are
/// multiple packages with the highest score, just pick one at random.
fn clean_up_all_candidates(
all_candidates: &mut HashMap<PkgIdentity, HashSet<PkgInfo>>,
locked_pkgs: Option<&HashMap<PkgIdentity, PkgInfo>>,
all_candidates: &mut HashMap<PkgTypeAndName, HashSet<PkgInfo>>,
locked_pkgs: Option<&HashMap<PkgTypeAndName, PkgInfo>>,
) {
for (pkg_identity, pkg_infos) in all_candidates.iter_mut() {
for (pkg_type_name, pkg_infos) in all_candidates.iter_mut() {
let mut version_map: HashMap<Version, &PkgInfo> = HashMap::new();
let mut locked_pkgs_map: HashMap<Version, &PkgInfo> = HashMap::new();

let locked_pkg =
locked_pkgs.and_then(|locked_pkgs| locked_pkgs.get(pkg_identity));
locked_pkgs.and_then(|locked_pkgs| locked_pkgs.get(pkg_type_name));

for pkg_info in pkg_infos.iter() {
// Check if the candidate is a locked one.
Expand Down Expand Up @@ -286,10 +294,10 @@ pub async fn get_all_candidates_from_deps(
tman_config: &TmanConfig,
support: &PkgSupport,
mut pkgs_to_be_searched: Vec<PkgInfo>,
mut all_candidates: HashMap<PkgIdentity, HashSet<PkgInfo>>,
mut all_candidates: HashMap<PkgTypeAndName, HashSet<PkgInfo>>,
extra_dependencies: &Vec<PkgDependency>,
locked_pkgs: Option<&HashMap<PkgIdentity, PkgInfo>>,
) -> Result<HashMap<PkgIdentity, HashSet<PkgInfo>>> {
locked_pkgs: Option<&HashMap<PkgTypeAndName, PkgInfo>>,
) -> Result<HashMap<PkgTypeAndName, HashSet<PkgInfo>>> {
let mut merged_dependencies = HashSet::<FoundDependency>::new();
let mut processed_pkgs = HashSet::<PkgInfo>::new();

Expand Down Expand Up @@ -351,16 +359,15 @@ pub fn get_pkg_info_from_candidates(
pkg_type: &str,
pkg_name: &str,
version: &str,
all_candidates: &HashMap<PkgIdentity, HashSet<PkgInfo>>,
all_candidates: &HashMap<PkgTypeAndName, HashSet<PkgInfo>>,
) -> Result<PkgInfo> {
let pkg_type_enum = pkg_type.parse::<PkgType>()?;
let pkg_identity = PkgIdentity {
pkg_type: pkg_type_enum,
let pkg_type_name = PkgTypeAndName {
pkg_type: pkg_type.parse::<PkgType>()?,
name: pkg_name.to_string(),
};
let version_parsed = Version::parse(version)?;
let pkg_info = all_candidates
.get(&pkg_identity)
.get(&pkg_type_name)
.and_then(|set| set.iter().find(|pkg| pkg.version == version_parsed))
.ok_or_else(|| {
anyhow!(
Expand Down
Loading

0 comments on commit 8f95e8b

Please sign in to comment.