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: abstract pkg_basic_info concept #431

Merged
merged 2 commits into from
Dec 19, 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
22 changes: 14 additions & 8 deletions core/src/ten_manager/src/cmd/cmd_install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// Refer to the "LICENSE" file in the root directory for more information.
//
use std::{
collections::{HashMap, HashSet},
collections::HashMap,
env,
fs::{self, OpenOptions},
path::{Path, PathBuf},
Expand All @@ -20,9 +20,6 @@ use indicatif::HumanDuration;
use inquire::Confirm;
use semver::VersionReq;

use ten_rust::pkg_info::manifest::{
dependency::ManifestDependency, parse_manifest_in_folder,
};
use ten_rust::pkg_info::{
dependencies::{DependencyRelationship, PkgDependency},
find_to_be_replaced_local_pkgs, find_untracked_local_packages,
Expand All @@ -32,6 +29,10 @@ use ten_rust::pkg_info::{
supports::{is_pkg_supports_compatible_with, Arch, Os, PkgSupport},
PkgInfo,
};
use ten_rust::pkg_info::{
manifest::{dependency::ManifestDependency, parse_manifest_in_folder},
pkg_basic_info::PkgBasicInfo,
};

use crate::{
config::TmanConfig,
Expand Down Expand Up @@ -298,7 +299,10 @@ fn parse_pkg_name_version(
fn filter_compatible_pkgs_to_candidates(
tman_config: &TmanConfig,
all_existing_local_pkgs: &Vec<PkgInfo>,
all_candidates: &mut HashMap<PkgTypeAndName, HashSet<PkgInfo>>,
all_candidates: &mut HashMap<
PkgTypeAndName,
HashMap<PkgBasicInfo, PkgInfo>,
>,
support: &PkgSupport,
) {
for existed_pkg in all_existing_local_pkgs.to_owned().iter_mut() {
Expand All @@ -324,7 +328,7 @@ fn filter_compatible_pkgs_to_candidates(
all_candidates
.entry((&*existed_pkg).into())
.or_default()
.insert(existed_pkg.clone());
.insert((&*existed_pkg).into(), existed_pkg.clone());
} else {
// The existed package is not compatible with the current system, so
// it should not be considered as a candidate.
Expand Down Expand Up @@ -455,8 +459,10 @@ 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<PkgTypeAndName, HashSet<PkgInfo>> =
HashMap::new();
let mut all_candidates: HashMap<
PkgTypeAndName,
HashMap<PkgBasicInfo, PkgInfo>,
> = HashMap::new();

// 'all_existing_local_pkgs' contains all the packages which are already
// located in the app directory.
Expand Down
64 changes: 37 additions & 27 deletions core/src/ten_manager/src/dep_and_candidate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use anyhow::{anyhow, Result};
use semver::{Version, VersionReq};

use ten_rust::pkg_info::dependencies::PkgDependency;
use ten_rust::pkg_info::pkg_basic_info::PkgBasicInfo;
use ten_rust::pkg_info::pkg_type::PkgType;
use ten_rust::pkg_info::pkg_type_and_name::PkgTypeAndName;
use ten_rust::pkg_info::supports::{
Expand Down Expand Up @@ -118,9 +119,8 @@ fn merge_dependency_to_dependencies(
/// # Parameters
///
/// - `all_candidates`: A mutable reference to a map where the keys are package
/// identities (`PkgIdentity`) and the values are sets of candidate package
/// information (`HashSet<PkgInfo>`). This map will be updated with potential
/// candidate packages for each dependency.
/// type & name and the values are sets of candidate package information. This
/// map will be updated with potential candidate packages for each dependency.
///
/// # Returns
///
Expand All @@ -135,7 +135,10 @@ async fn process_dependencies_to_get_candidates(
support: &PkgSupport,
input_dependencies: &Vec<PkgDependency>,
merged_dependencies: &mut HashSet<FoundDependency>,
all_candidates: &mut HashMap<PkgTypeAndName, HashSet<PkgInfo>>,
all_candidates: &mut HashMap<
PkgTypeAndName,
HashMap<PkgBasicInfo, PkgInfo>,
>,
new_pkgs_to_be_searched: &mut Vec<PkgInfo>,
) -> Result<()> {
for dependency in input_dependencies {
Expand Down Expand Up @@ -196,14 +199,14 @@ async fn process_dependencies_to_get_candidates(
// dependencies within those packages are processed.
if let Some(candidates) = all_candidates.get(&dependency.into()) {
for candidate in candidates {
if dependency.version_req.matches(&candidate.version) {
if dependency.version_req.matches(&candidate.0.version) {
tman_verbose_println!(
tman_config,
"Collect candidate: {:?}",
candidate
);

candidate_pkg_infos.push(candidate.clone());
candidate_pkg_infos.push(candidate.1.clone());
}
}
}
Expand Down Expand Up @@ -233,10 +236,10 @@ async fn process_dependencies_to_get_candidates(
candidate_pkg_info
);

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

new_pkgs_to_be_searched.push(candidate_pkg_info);
}
Expand All @@ -250,7 +253,10 @@ 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<PkgTypeAndName, HashSet<PkgInfo>>,
all_candidates: &mut HashMap<
PkgTypeAndName,
HashMap<PkgBasicInfo, PkgInfo>,
>,
locked_pkgs: Option<&HashMap<PkgTypeAndName, PkgInfo>>,
) {
for (pkg_type_name, pkg_infos) in all_candidates.iter_mut() {
Expand All @@ -263,43 +269,47 @@ 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.version
&& locked_pkg.hash == pkg_info.hash
if locked_pkg.version == pkg_info.1.version
&& locked_pkg.hash == pkg_info.1.hash
{
locked_pkgs_map.insert(pkg_info.version.clone(), pkg_info);
locked_pkgs_map
.insert(pkg_info.1.version.clone(), pkg_info.1);
}
}

version_map
.entry(pkg_info.version.clone())
.entry(pkg_info.1.version.clone())
.and_modify(|existing_pkg_info| {
if pkg_info.compatible_score
if pkg_info.1.compatible_score
> existing_pkg_info.compatible_score
{
*existing_pkg_info = pkg_info;
*existing_pkg_info = pkg_info.1;
}
})
.or_insert(pkg_info);
.or_insert(pkg_info.1);
}

// If there is a locked one, replace the candidate with the highest
// score with the locked one.
version_map.extend(locked_pkgs_map);

*pkg_infos = version_map.into_values().cloned().collect();
*pkg_infos = version_map
.into_values()
.map(|pkg_info| (pkg_info.into(), pkg_info.clone()))
.collect();
}
}

pub async fn get_all_candidates_from_deps(
tman_config: &TmanConfig,
support: &PkgSupport,
mut pkgs_to_be_searched: Vec<PkgInfo>,
mut all_candidates: HashMap<PkgTypeAndName, HashSet<PkgInfo>>,
mut all_candidates: HashMap<PkgTypeAndName, HashMap<PkgBasicInfo, PkgInfo>>,
extra_dependencies: &Vec<PkgDependency>,
locked_pkgs: Option<&HashMap<PkgTypeAndName, PkgInfo>>,
) -> Result<HashMap<PkgTypeAndName, HashSet<PkgInfo>>> {
) -> Result<HashMap<PkgTypeAndName, HashMap<PkgBasicInfo, PkgInfo>>> {
let mut merged_dependencies = HashSet::<FoundDependency>::new();
let mut processed_pkgs = HashSet::<PkgInfo>::new();
let mut processed_pkgs = HashSet::<PkgBasicInfo>::new();

// If there is extra dependencies (ex: specified in the command line),
// handle those dependencies, too.
Expand All @@ -324,7 +334,7 @@ pub async fn get_all_candidates_from_deps(

// Process all packages to be searched.
while let Some(pkg_to_be_search) = pkgs_to_be_searched.pop() {
if processed_pkgs.contains(&pkg_to_be_search) {
if processed_pkgs.contains(&(&pkg_to_be_search).into()) {
// If this package info has already been processed, do not
// process it again.
continue;
Expand All @@ -341,7 +351,7 @@ pub async fn get_all_candidates_from_deps(
.await?;

// Remember that this package has already been processed.
processed_pkgs.insert(pkg_to_be_search);
processed_pkgs.insert((&pkg_to_be_search).into());
}

if new_pkgs_to_be_searched.is_empty() {
Expand All @@ -359,7 +369,7 @@ pub fn get_pkg_info_from_candidates(
pkg_type: &str,
pkg_name: &str,
version: &str,
all_candidates: &HashMap<PkgTypeAndName, HashSet<PkgInfo>>,
all_candidates: &HashMap<PkgTypeAndName, HashMap<PkgBasicInfo, PkgInfo>>,
) -> Result<PkgInfo> {
let pkg_type_name = PkgTypeAndName {
pkg_type: pkg_type.parse::<PkgType>()?,
Expand All @@ -368,7 +378,7 @@ 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.version == version_parsed))
.and_then(|set| set.iter().find(|pkg| pkg.1.version == version_parsed))
.ok_or_else(|| {
anyhow!(
"PkgInfo not found for [{}]{}@{}",
Expand All @@ -377,5 +387,5 @@ pub fn get_pkg_info_from_candidates(
version
)
})?;
Ok(pkg_info.clone())
Ok(pkg_info.1.clone())
}
23 changes: 13 additions & 10 deletions core/src/ten_manager/src/designer/graphs/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use std::sync::{Arc, RwLock};
use actix_web::{web, HttpResponse, Responder};
use serde::{Deserialize, Serialize};

use ten_rust::pkg_info::graph::{GraphConnection, GraphNode};
use ten_rust::pkg_info::pkg_type::PkgType;
use ten_rust::pkg_info::predefined_graphs::get_pkg_predefined_graph_from_nodes_and_connections;

Expand Down Expand Up @@ -44,21 +45,23 @@ 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.pkg_type == PkgType::App)
{
// Collect nodes into a Vec<GraphNode>.
let nodes: Vec<GraphNode> =
body.nodes.iter().cloned().map(|v| v.into()).collect();

// Collect connections into a Vec<GraphConnection>.
let connections: Vec<GraphConnection> =
body.connections.iter().cloned().map(|v| v.into()).collect();

let new_graph =
match get_pkg_predefined_graph_from_nodes_and_connections(
&graph_name,
body.auto_start,
&body.nodes.clone().into_iter().map(|v| v.into()).collect(),
&body
.connections
.clone()
.into_iter()
.map(|v| v.into())
.collect(),
&nodes,
&connections,
) {
Ok(graph) => graph,
Err(err) => {
Expand Down
2 changes: 1 addition & 1 deletion core/src/ten_manager/src/manifest_lock/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub struct ManifestLock {

type LockedPkgsInfo<'a> = &'a Vec<&'a PkgInfo>;

impl<'a> From<LockedPkgsInfo<'a>> for ManifestLock {
impl From<LockedPkgsInfo<'_>> for ManifestLock {
// Convert a complete `Resolve` to a ManifestLock which can be serialized to
// a `manifest-lock.json` file.
fn from(resolve: LockedPkgsInfo) -> Self {
Expand Down
2 changes: 1 addition & 1 deletion core/src/ten_manager/src/registry/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ fn find_file_with_criteria(
name: &String,
criteria: &SearchCriteria,
) -> Result<Vec<FoundResult>> {
let target_path = base_url.join(pkg_type.to_string()).join(&name);
let target_path = base_url.join(pkg_type.to_string()).join(name);

let mut results = Vec::<FoundResult>::new();

Expand Down
6 changes: 4 additions & 2 deletions core/src/ten_manager/src/solver/introducer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@ use std::collections::{HashMap, HashSet};
use anyhow::Result;
use regex::Regex;

use ten_rust::pkg_info::{pkg_type_and_name::PkgTypeAndName, PkgInfo};
use ten_rust::pkg_info::{
pkg_basic_info::PkgBasicInfo, pkg_type_and_name::PkgTypeAndName, PkgInfo,
};

use crate::dep_and_candidate::get_pkg_info_from_candidates;

/// Returns a map from a package to its introducer and the requested version.
pub fn extract_introducer_relations_from_raw_solver_results(
results: &[String],
all_candidates: &HashMap<PkgTypeAndName, HashSet<PkgInfo>>,
all_candidates: &HashMap<PkgTypeAndName, HashMap<PkgBasicInfo, PkgInfo>>,
) -> Result<HashMap<PkgInfo, (String, Option<PkgInfo>)>> {
let re = Regex::new(
r#"introducer\("([^"]+)","([^"]+)","([^"]+)","([^"]+)","([^"]*)","([^"]*)"\)"#,
Expand Down
Loading
Loading