Skip to content

Commit

Permalink
refactor: remove unnecessary trait for pkg_info (#434)
Browse files Browse the repository at this point in the history
* refactor: remove unnecessary trait for pkg_info

* refactor: remove unnecessary trait for pkg_info
  • Loading branch information
halajohn authored Dec 19, 2024
1 parent e357f53 commit 766fd50
Show file tree
Hide file tree
Showing 27 changed files with 314 additions and 216 deletions.
79 changes: 48 additions & 31 deletions core/src/ten_manager/src/cmd/cmd_install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,23 +229,32 @@ 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_type.to_string()
&& dep.name == added_pkg_info.name
dep.pkg_type
== added_pkg_info.basic_info.type_and_name.pkg_type.to_string()
&& dep.name == added_pkg_info.basic_info.type_and_name.name
});

if !is_present {
dependencies.push(ManifestDependency {
pkg_type: added_pkg_info.pkg_type.to_string(),
name: added_pkg_info.name.clone(),
version: added_pkg_info.version.to_string(),
pkg_type: added_pkg_info
.basic_info
.type_and_name
.pkg_type
.to_string(),
name: added_pkg_info.basic_info.type_and_name.name.clone(),
version: added_pkg_info.basic_info.version.to_string(),
});
}
} else {
base_pkg_info.manifest.clone().unwrap().dependencies =
Some(vec![ManifestDependency {
pkg_type: added_pkg_info.pkg_type.to_string(),
name: added_pkg_info.name.clone(),
version: added_pkg_info.version.to_string(),
pkg_type: added_pkg_info
.basic_info
.type_and_name
.pkg_type
.to_string(),
name: added_pkg_info.basic_info.type_and_name.name.clone(),
version: added_pkg_info.basic_info.version.to_string(),
}]);
}

Expand Down Expand Up @@ -312,17 +321,19 @@ fn filter_compatible_pkgs_to_candidates(
existed_pkg
);

let compatible_score =
is_pkg_supports_compatible_with(&existed_pkg.supports, support);
let compatible_score = is_pkg_supports_compatible_with(
&existed_pkg.basic_info.supports,
support,
);

if compatible_score >= 0 {
existed_pkg.compatible_score = compatible_score;

tman_verbose_println!(
tman_config,
"The existed {} package {} is compatible with the current system.",
existed_pkg.pkg_type,
existed_pkg.name
existed_pkg.basic_info.type_and_name.pkg_type,
existed_pkg.basic_info.type_and_name.name
);

all_candidates
Expand All @@ -336,15 +347,16 @@ fn filter_compatible_pkgs_to_candidates(
tman_config,
"The existed {} package {} is not compatible \
with the current system.",
existed_pkg.pkg_type,
existed_pkg.name
existed_pkg.basic_info.type_and_name.pkg_type,
existed_pkg.basic_info.type_and_name.name
);
}
}
}

fn get_supports_str(pkg: &PkgInfo) -> String {
let support_items: Vec<String> = pkg
.basic_info
.supports
.iter()
.filter_map(|s| match (s.os.as_ref(), s.arch.as_ref()) {
Expand Down Expand Up @@ -380,7 +392,12 @@ appear in the dependency tree:",
Emoji("💡", "")
);
for pkg in untracked_local_pkgs {
println!(" {}:{}@{}", pkg.pkg_type, pkg.name, pkg.version);
println!(
" {}:{}@{}",
pkg.basic_info.type_and_name.pkg_type,
pkg.basic_info.type_and_name.name,
pkg.basic_info.version
);
}
}

Expand All @@ -403,24 +420,24 @@ appear in the dependency tree:",
if old_supports_str != new_supports_str {
println!(
" {}:{}@{}{} -> {}:{}@{}{}",
old_pkg.pkg_type,
old_pkg.name,
old_pkg.version,
old_pkg.basic_info.type_and_name.pkg_type,
old_pkg.basic_info.type_and_name.name,
old_pkg.basic_info.version,
old_supports_str,
new_pkg.pkg_type,
new_pkg.name,
new_pkg.version,
new_pkg.basic_info.type_and_name.pkg_type,
new_pkg.basic_info.type_and_name.name,
new_pkg.basic_info.version,
new_supports_str
);
} else {
println!(
" {}:{}@{} -> {}:{}@{}",
old_pkg.pkg_type,
old_pkg.name,
old_pkg.version,
new_pkg.pkg_type,
new_pkg.name,
new_pkg.version
old_pkg.basic_info.type_and_name.pkg_type,
old_pkg.basic_info.type_and_name.name,
old_pkg.basic_info.version,
new_pkg.basic_info.type_and_name.pkg_type,
new_pkg.basic_info.type_and_name.name,
new_pkg.basic_info.version
);
}
}
Expand Down Expand Up @@ -549,7 +566,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_.name.clone();
affected_pkg_name = app_pkg_.basic_info.type_and_name.name.clone();

// Push the app itself into the initial_input_pkgs.
initial_input_pkgs.push(get_pkg_info_from_path(&cwd)?);
Expand All @@ -567,9 +584,9 @@ pub async fn execute_cmd(
);

let extra_dependency_relationship = DependencyRelationship {
pkg_type: app_pkg_.pkg_type,
name: app_pkg_.name.clone(),
version: app_pkg_.version.clone(),
pkg_type: app_pkg_.basic_info.type_and_name.pkg_type,
name: app_pkg_.basic_info.type_and_name.name.clone(),
version: app_pkg_.basic_info.version.clone(),
dependency: PkgDependency {
pkg_type: desired_pkg_type_,
name: desired_pkg_src_name_.clone(),
Expand Down
18 changes: 12 additions & 6 deletions core/src/ten_manager/src/dep_and_candidate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ async fn process_dependencies_to_get_candidates(
);

let compatible_score = is_pkg_supports_compatible_with(
&candidate_pkg_info.supports,
&candidate_pkg_info.basic_info.supports,
support,
);

Expand Down Expand Up @@ -269,16 +269,19 @@ fn clean_up_all_candidates(
for pkg_info in pkg_infos.iter() {
// Check if the candidate is a locked one.
if let Some(locked_pkg) = locked_pkg {
if locked_pkg.version == pkg_info.1.version
if locked_pkg.basic_info.version
== pkg_info.1.basic_info.version
&& locked_pkg.hash == pkg_info.1.hash
{
locked_pkgs_map
.insert(pkg_info.1.version.clone(), pkg_info.1);
locked_pkgs_map.insert(
pkg_info.1.basic_info.version.clone(),
pkg_info.1,
);
}
}

version_map
.entry(pkg_info.1.version.clone())
.entry(pkg_info.1.basic_info.version.clone())
.and_modify(|existing_pkg_info| {
if pkg_info.1.compatible_score
> existing_pkg_info.compatible_score
Expand Down Expand Up @@ -378,7 +381,10 @@ pub fn get_pkg_info_from_candidates(
let version_parsed = Version::parse(version)?;
let pkg_info = all_candidates
.get(&pkg_type_name)
.and_then(|set| set.iter().find(|pkg| pkg.1.version == version_parsed))
.and_then(|set| {
set.iter()
.find(|pkg| pkg.1.basic_info.version == version_parsed)
})
.ok_or_else(|| {
anyhow!(
"PkgInfo not found for [{}]{}@{}",
Expand Down
6 changes: 4 additions & 2 deletions core/src/ten_manager/src/designer/addons/extensions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ fn retrieve_extension_addons(
if let Some(all_pkgs) = &state.all_pkgs {
let extensions = all_pkgs
.iter()
.filter(|pkg| pkg.pkg_type == PkgType::Extension)
.filter(|pkg| {
pkg.basic_info.type_and_name.pkg_type == PkgType::Extension
})
.map(|pkg_info_with_src| {
map_pkg_to_extension_addon(pkg_info_with_src)
})
Expand All @@ -68,7 +70,7 @@ fn map_pkg_to_extension_addon(
pkg_info_with_src: &PkgInfo,
) -> DesignerExtensionAddon {
DesignerExtensionAddon {
addon_name: pkg_info_with_src.name.clone(),
addon_name: pkg_info_with_src.basic_info.type_and_name.name.clone(),
url: pkg_info_with_src.url.clone(),
api: pkg_info_with_src.api.as_ref().map(|api| DesignerApi {
property: if api.property.is_empty() {
Expand Down
5 changes: 3 additions & 2 deletions core/src/ten_manager/src/designer/graphs/connections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,9 @@ pub async fn get_graph_connections(
}

if let Some(pkgs) = &state.all_pkgs {
if let Some(app_pkg) =
pkgs.iter().find(|pkg| pkg.pkg_type == PkgType::App)
if let Some(app_pkg) = pkgs
.iter()
.find(|pkg| pkg.basic_info.type_and_name.pkg_type == PkgType::App)
{
// If the app package has predefined graphs, find the one with the
// specified graph_name.
Expand Down
2 changes: 1 addition & 1 deletion core/src/ten_manager/src/designer/graphs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub async fn get_graphs(
if let Some(all_pkgs) = &state.all_pkgs {
if let Some(app_pkg) = all_pkgs
.iter()
.find(|pkg| pkg.pkg_type == PkgType::App)
.find(|pkg| pkg.basic_info.type_and_name.pkg_type == PkgType::App)
{
let graphs: Vec<RespGraph> = app_pkg
.get_predefined_graphs()
Expand Down
9 changes: 6 additions & 3 deletions core/src/ten_manager/src/designer/graphs/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ pub async fn update_graph(
}

if let Some(pkgs) = &mut state.all_pkgs {
if let Some(app_pkg) =
pkgs.iter_mut().find(|pkg| pkg.pkg_type == PkgType::App)
if let Some(app_pkg) = pkgs
.iter_mut()
.find(|pkg| pkg.basic_info.type_and_name.pkg_type == PkgType::App)
{
// Collect nodes into a Vec<GraphNode>.
let nodes: Vec<GraphNode> =
Expand Down Expand Up @@ -188,7 +189,9 @@ mod tests {
Some(pkgs) => {
let app_pkg = pkgs
.iter()
.find(|pkg| pkg.pkg_type == PkgType::App)
.find(|pkg| {
pkg.basic_info.type_and_name.pkg_type == PkgType::App
})
.unwrap();

let predefined_graphs =
Expand Down
2 changes: 1 addition & 1 deletion core/src/ten_manager/src/designer/manifest/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub async fn check_manifest(
if let Some(pkgs) = &mut state.all_pkgs {
if let Some(app_pkg) = pkgs
.iter_mut()
.find(|pkg| pkg.pkg_type == PkgType::App)
.find(|pkg| pkg.basic_info.type_and_name.pkg_type == PkgType::App)
{
match query.0.check_type.as_str() {
"dirty" => match app_pkg.is_manifest_equal_to_fs() {
Expand Down
2 changes: 1 addition & 1 deletion core/src/ten_manager/src/designer/manifest/dump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub async fn dump_manifest(
if let Some(pkgs) = &mut state.all_pkgs {
if let Some(app_pkg) = pkgs
.iter_mut()
.find(|pkg| pkg.pkg_type == PkgType::App)
.find(|pkg| pkg.basic_info.type_and_name.pkg_type == PkgType::App)
{
let new_manifest_str =
app_pkg.manifest.clone().unwrap().to_string();
Expand Down
5 changes: 3 additions & 2 deletions core/src/ten_manager/src/designer/property/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ pub async fn check_property(
}

if let Some(pkgs) = &mut state.all_pkgs {
if let Some(app_pkg) =
pkgs.iter_mut().find(|pkg| pkg.pkg_type == PkgType::App)
if let Some(app_pkg) = pkgs
.iter_mut()
.find(|pkg| pkg.basic_info.type_and_name.pkg_type == PkgType::App)
{
match query.0.check_type.as_str() {
"dirty" => match app_pkg.is_property_equal_to_fs() {
Expand Down
5 changes: 3 additions & 2 deletions core/src/ten_manager/src/designer/property/dump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ pub async fn dump_property(
);

if let Some(pkgs) = &mut state.all_pkgs {
if let Some(app_pkg) =
pkgs.iter_mut().find(|pkg| pkg.pkg_type == PkgType::App)
if let Some(app_pkg) = pkgs
.iter_mut()
.find(|pkg| pkg.basic_info.type_and_name.pkg_type == PkgType::App)
{
let response = ApiResponse {
status: Status::Ok,
Expand Down
23 changes: 13 additions & 10 deletions core/src/ten_manager/src/install/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ pub async fn install_pkg_info(
tman_verbose_println!(
tman_config,
"{}:{} has already been installed.\n",
pkg_info.pkg_type,
pkg_info.name
pkg_info.basic_info.type_and_name.pkg_type,
pkg_info.basic_info.type_and_name.name
);
return Ok(());
}
Expand All @@ -50,8 +50,10 @@ pub async fn install_pkg_info(

let mut found_pkg_identity_mapping = None;
for pkg_identity_mapping in pkg_identity_mappings {
if pkg_info.pkg_type == pkg_identity_mapping.pkg_type
&& pkg_info.name == pkg_identity_mapping.src_pkg_name
if pkg_info.basic_info.type_and_name.pkg_type
== pkg_identity_mapping.pkg_type
&& pkg_info.basic_info.type_and_name.name
== pkg_identity_mapping.src_pkg_name
{
found_pkg_identity_mapping = Some(pkg_identity_mapping);
}
Expand All @@ -62,7 +64,8 @@ pub async fn install_pkg_info(
path = Path::new(&base_dir)
.join(found_pkg_identity_mapping.dest_pkg_name.clone());
} else {
path = PathBuf::from(&base_dir).join(pkg_info.name.clone());
path = PathBuf::from(&base_dir)
.join(pkg_info.basic_info.type_and_name.name.clone());
}

let output_dir = path.to_string_lossy().to_string();
Expand All @@ -82,8 +85,8 @@ pub async fn install_pkg_info(
.map_err(|e| {
anyhow::anyhow!(
"Failed to check property.json for {}:{}, {}",
pkg_info.pkg_type,
pkg_info.name,
pkg_info.basic_info.type_and_name.pkg_type,
pkg_info.basic_info.type_and_name.name,
e
)
})?;
Expand All @@ -93,7 +96,7 @@ pub async fn install_pkg_info(
// NOTE: This feature is not currently in use, but a command-line argument
// could be added to copy the lib/ or include/ from the system package to
// ten_packages/system/. This would reduce the number of rpaths.
if pkg_info.pkg_type == PkgType::System {
if pkg_info.basic_info.type_and_name.pkg_type == PkgType::System {
let inclusions = vec![];
merge_folders(
Path::new(&output_dir),
Expand All @@ -112,8 +115,8 @@ pub async fn install_pkg_info(
tman_verbose_println!(
tman_config,
"Install files for {}:{}",
pkg_info.pkg_type,
pkg_info.name
pkg_info.basic_info.type_and_name.pkg_type,
pkg_info.basic_info.type_and_name.name
);
for install_path in &installed_paths.paths {
tman_verbose_println!(tman_config, "{}", install_path);
Expand Down
Loading

0 comments on commit 766fd50

Please sign in to comment.