Skip to content

Commit

Permalink
feat: add support for local dependency in extension
Browse files Browse the repository at this point in the history
  • Loading branch information
halajohn committed Jan 7, 2025
1 parent 8b463f1 commit 75f1be5
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 108 deletions.
4 changes: 2 additions & 2 deletions core/src/ten_manager/src/cmd/cmd_check/cmd_check_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use clap::{Arg, ArgMatches, Command};
use console::Emoji;

use ten_rust::pkg_info::{
get_all_existed_pkgs_info_of_app, graph::Graph, localhost,
get_all_installed_pkgs_info_of_app, graph::Graph, localhost,
property::parse_property_in_folder, PkgInfo,
};

Expand Down Expand Up @@ -107,7 +107,7 @@ fn get_existed_pkgs_of_all_apps(

for app in &command.app_dir {
let app_path = path::Path::new(app);
let app_existed_pkgs = get_all_existed_pkgs_info_of_app(app_path)?;
let app_existed_pkgs = get_all_installed_pkgs_info_of_app(app_path)?;

let app_property = parse_property_in_folder(app_path)?;
let app_uri = if let Some(property) = app_property {
Expand Down
92 changes: 51 additions & 41 deletions core/src/ten_manager/src/cmd/cmd_install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ use crate::{
},
log::tman_verbose_println,
manifest_lock::parse_manifest_lock_in_folder,
package_info::tman_get_all_existed_pkgs_info_of_app,
package_info::tman_get_all_installed_pkgs_info_of_app,
solver::{
introducer::extract_introducer_relations_from_raw_solver_results,
solve::{solve_all, DependencyRelationship},
Expand Down Expand Up @@ -155,6 +155,26 @@ fn get_locked_pkgs(app_dir: &Path) -> Option<HashMap<PkgTypeAndName, PkgInfo>> {
}
}

fn add_cwd_pkg_to_initial_pkg_to_find_candidates_and_all_candidates(
cwd: &Path,
initial_pkgs_to_find_candidates: &mut Vec<PkgInfo>,
all_candidates: &mut HashMap<
PkgTypeAndName,
HashMap<PkgBasicInfo, PkgInfo>,
>,
) -> Result<PkgInfo> {
let cwd_pkg = get_pkg_info_from_path(cwd, true)?;

initial_pkgs_to_find_candidates.push(cwd_pkg.clone());

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

Ok(cwd_pkg)
}

pub async fn execute_cmd(
tman_config: &TmanConfig,
command_data: InstallCommand,
Expand Down Expand Up @@ -204,8 +224,8 @@ pub async fn execute_cmd(
// *) If some of these packages are not compatible with packages in
// the dependency tree, then users will be questioned whether to overwrite
// them with the new packages or quit the installation.
let mut all_existing_local_pkgs: Vec<PkgInfo> = vec![];
let mut all_compatible_existing_local_pkgs: HashMap<
let mut all_installed_pkgs: Vec<PkgInfo> = vec![];
let mut all_compatible_installed_pkgs: HashMap<
PkgTypeAndName,
HashMap<PkgBasicInfo, PkgInfo>,
> = HashMap::new();
Expand Down Expand Up @@ -235,24 +255,22 @@ pub async fn execute_cmd(
installing_pkg_name = Some(installing_pkg_name_.clone());

// The `cwd` must be the base directory of a TEN app.
let app_pkg_ = get_pkg_info_from_path(&cwd, true)?;
cwd_pkg_name = app_pkg_.basic_info.type_and_name.name.clone();

initial_pkgs_to_find_candidates.push(app_pkg_.clone());
let app_pkg_ =
add_cwd_pkg_to_initial_pkg_to_find_candidates_and_all_candidates(
&cwd,
&mut initial_pkgs_to_find_candidates,
&mut all_candidates,
)?;

all_existing_local_pkgs =
tman_get_all_existed_pkgs_info_of_app(tman_config, &cwd)?;
cwd_pkg_name = app_pkg_.basic_info.type_and_name.name.clone();

all_candidates
.entry((&app_pkg_).into())
.or_default()
.insert((&app_pkg_).into(), app_pkg_.clone());
all_installed_pkgs =
tman_get_all_installed_pkgs_info_of_app(tman_config, &cwd)?;

filter_compatible_pkgs_to_candidates(
tman_config,
&all_existing_local_pkgs,
// &mut all_candidates,
&mut all_compatible_existing_local_pkgs,
&all_installed_pkgs,
&mut all_compatible_installed_pkgs,
&command_data.support,
);

Expand Down Expand Up @@ -287,23 +305,20 @@ pub async fn execute_cmd(
// dependencies on a specific version of an app, so the app also
// needs to be included in the package list for dependency tree
// calculation.
let app_pkg_ = get_pkg_info_from_path(&cwd, true)?;

initial_pkgs_to_find_candidates.push(app_pkg_.clone());

all_existing_local_pkgs =
tman_get_all_existed_pkgs_info_of_app(tman_config, &cwd)?;
add_cwd_pkg_to_initial_pkg_to_find_candidates_and_all_candidates(
&cwd,
&mut initial_pkgs_to_find_candidates,
&mut all_candidates,
)?;

all_candidates
.entry((&app_pkg_).into())
.or_default()
.insert((&app_pkg_).into(), app_pkg_.clone());
all_installed_pkgs =
tman_get_all_installed_pkgs_info_of_app(tman_config, &cwd)?;

filter_compatible_pkgs_to_candidates(
tman_config,
&all_existing_local_pkgs,
// &mut all_candidates,
&mut all_compatible_existing_local_pkgs,
&all_installed_pkgs,
&mut all_compatible_installed_pkgs,
&command_data.support,
);
}
Expand All @@ -318,20 +333,16 @@ pub async fn execute_cmd(
fs::create_dir_all(path)?;
}

let extension_pkg_ = get_pkg_info_from_path(&cwd, true)?;

initial_pkgs_to_find_candidates.push(extension_pkg_.clone());

all_candidates
.entry((&extension_pkg_).into())
.or_default()
.insert((&extension_pkg_).into(), extension_pkg_.clone());
add_cwd_pkg_to_initial_pkg_to_find_candidates_and_all_candidates(
&cwd,
&mut initial_pkgs_to_find_candidates,
&mut all_candidates,
)?;

filter_compatible_pkgs_to_candidates(
tman_config,
&initial_pkgs_to_find_candidates,
// &mut all_candidates,
&mut all_compatible_existing_local_pkgs,
&mut all_compatible_installed_pkgs,
&command_data.support,
);
}
Expand Down Expand Up @@ -361,8 +372,7 @@ pub async fn execute_cmd(
dep_relationship_from_cmd_line
.as_ref()
.map(|rel| &rel.dependency),
// &all_existing_local_pkgs,
&all_compatible_existing_local_pkgs,
&all_compatible_installed_pkgs,
all_candidates,
locked_pkgs.as_ref(),
)
Expand Down Expand Up @@ -421,7 +431,7 @@ pub async fn execute_cmd(
// or replaced.
let has_conflict = compare_solver_results_with_existed_pkgs(
&remaining_solver_results,
&all_existing_local_pkgs,
&all_installed_pkgs,
);

if has_conflict && !tman_config.assume_yes {
Expand Down
67 changes: 11 additions & 56 deletions core/src/ten_manager/src/dep_and_candidate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,7 @@ async fn process_non_local_dependency_to_get_candidate(
tman_config: &TmanConfig,
support: &PkgSupport,
dependency: &PkgDependency,
// all_existing_local_pkgs: &[PkgInfo],
all_compatible_existing_local_pkgs: &HashMap<
all_compatible_installed_pkgs: &HashMap<
PkgTypeAndName,
HashMap<PkgBasicInfo, PkgInfo>,
>,
Expand Down Expand Up @@ -180,39 +179,17 @@ async fn process_non_local_dependency_to_get_candidate(
candidate_pkg_infos.push(candidate_pkg_info);
}

// =-=-=
// Find packages from the all_candidates that meet the specified
// criteria and add them to the candidate_pkg_infos.
//
// Since `all_candidates` includes the ten packages installed
// locally, 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.into()) {
// for candidate in candidates {
// if dependency.version_req.matches(&candidate.0.version) {
// tman_verbose_println!(
// tman_config,
// "Collect candidate: {:?}",
// candidate
// );

// candidate_pkg_infos.push(candidate.1.clone());
// }
// }
// }

// =-=-=
// Find packages from the installed packages that meet the specified
// criteria and add them to the candidate_pkg_infos. This action ensures
// that the dependencies within those packages are processed.
if let Some(candidates) =
all_compatible_existing_local_pkgs.get(&dependency.into())
all_compatible_installed_pkgs.get(&dependency.into())
{
for candidate in candidates {
if dependency.version_req.matches(&candidate.0.version) {
tman_verbose_println!(
tman_config,
"Collect candidate from existing local package: {:?}",
"Collect candidate from an already installed package: {:?}",
candidate
);

Expand All @@ -221,23 +198,6 @@ async fn process_non_local_dependency_to_get_candidate(
}
}

// =-=-=
// for candidate in all_existing_local_pkgs {
// if dependency.type_and_name == candidate.basic_info.type_and_name
// && dependency
// .version_req
// .matches(&candidate.basic_info.version)
// {
// tman_verbose_println!(
// tman_config,
// "Collect candidate from already installed package: {:?}",
// candidate
// );

// candidate_pkg_infos.push(candidate.clone());
// }
// }

// Filter suitable candidate packages according to `supports`.
for mut candidate_pkg_info in candidate_pkg_infos {
tman_verbose_println!(
Expand Down Expand Up @@ -296,8 +256,7 @@ async fn process_dependencies_to_get_candidates(
support: &PkgSupport,
input_dependencies: &Vec<PkgDependency>,
merged_dependencies: &mut HashMap<PkgTypeAndName, MergedVersionReq>,
// all_existing_local_pkgs: &[PkgInfo],
all_compatible_existing_local_pkgs: &HashMap<
all_compatible_installed_pkgs: &HashMap<
PkgTypeAndName,
HashMap<PkgBasicInfo, PkgInfo>,
>,
Expand Down Expand Up @@ -328,8 +287,7 @@ async fn process_dependencies_to_get_candidates(
tman_config,
support,
dependency,
// all_existing_local_pkgs,
all_compatible_existing_local_pkgs,
all_compatible_installed_pkgs,
all_candidates,
new_pkgs_to_be_searched,
)
Expand Down Expand Up @@ -399,8 +357,7 @@ pub async fn get_all_candidates_from_deps(
support: &PkgSupport,
mut pkgs_to_be_searched: Vec<PkgInfo>,
extra_dep: Option<&PkgDependency>,
// all_existing_local_pkgs: &[PkgInfo],
all_compatible_existing_local_pkgs: &HashMap<
all_compatible_installed_pkgs: &HashMap<
PkgTypeAndName,
HashMap<PkgBasicInfo, PkgInfo>,
>,
Expand All @@ -420,8 +377,7 @@ pub async fn get_all_candidates_from_deps(
support,
&vec![extra_dep.unwrap().clone()],
&mut merged_dependencies,
// all_existing_local_pkgs,
all_compatible_existing_local_pkgs,
all_compatible_installed_pkgs,
&mut all_candidates,
&mut new_pkgs_to_be_searched,
)
Expand All @@ -446,8 +402,7 @@ pub async fn get_all_candidates_from_deps(
support,
&pkg_to_be_search.dependencies,
&mut merged_dependencies,
// all_existing_local_pkgs,
all_compatible_existing_local_pkgs,
all_compatible_installed_pkgs,
&mut all_candidates,
&mut new_pkgs_to_be_searched,
)
Expand Down
4 changes: 2 additions & 2 deletions core/src/ten_manager/src/designer/get_all_pkgs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
use anyhow::{anyhow, Result};

use super::DesignerState;
use crate::package_info::tman_get_all_existed_pkgs_info_of_app;
use crate::package_info::tman_get_all_installed_pkgs_info_of_app;

pub fn get_all_pkgs(state: &mut DesignerState) -> Result<()> {
use std::path::PathBuf;

if state.all_pkgs.is_none() {
if let Some(base_dir) = &state.base_dir {
let app_path = PathBuf::from(base_dir);
match tman_get_all_existed_pkgs_info_of_app(
match tman_get_all_installed_pkgs_info_of_app(
&state.tman_config,
&app_path,
) {
Expand Down
6 changes: 3 additions & 3 deletions core/src/ten_manager/src/package_info/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ use anyhow::Result;

use crate::config::TmanConfig;
use ten_rust::pkg_info::{
get_all_existed_pkgs_info_of_app_to_hashmap, PkgInfo,
get_all_installed_pkgs_info_of_app_to_hashmap, PkgInfo,
};

pub fn tman_get_all_existed_pkgs_info_of_app(
pub fn tman_get_all_installed_pkgs_info_of_app(
tman_config: &TmanConfig,
app_path: &Path,
) -> Result<Vec<PkgInfo>> {
let pkgs_info = get_all_existed_pkgs_info_of_app_to_hashmap(app_path)?;
let pkgs_info = get_all_installed_pkgs_info_of_app_to_hashmap(app_path)?;
crate::log::tman_verbose_println!(tman_config, "{:?}", pkgs_info);
Ok(pkgs_info.into_values().collect())
}
8 changes: 4 additions & 4 deletions core/src/ten_rust/src/pkg_info/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ fn collect_pkg_info_from_path<'a>(
}
}

pub fn get_all_existed_pkgs_info_of_app_to_hashmap(
pub fn get_all_installed_pkgs_info_of_app_to_hashmap(
app_path: &Path,
) -> Result<HashMap<PkgTypeAndName, PkgInfo>> {
let mut pkgs_info: HashMap<PkgTypeAndName, PkgInfo> = HashMap::new();
Expand Down Expand Up @@ -286,10 +286,10 @@ pub fn get_all_existed_pkgs_info_of_app_to_hashmap(
Ok(pkgs_info)
}

pub fn get_all_existed_pkgs_info_of_app(
pub fn get_all_installed_pkgs_info_of_app(
app_path: &Path,
) -> Result<Vec<PkgInfo>> {
let result = get_all_existed_pkgs_info_of_app_to_hashmap(app_path)?;
let result = get_all_installed_pkgs_info_of_app_to_hashmap(app_path)?;
Ok(result.into_values().collect())
}

Expand Down Expand Up @@ -383,7 +383,7 @@ pub fn ten_rust_check_graph_for_app(
}

let mut pkgs_of_app: HashMap<String, Vec<PkgInfo>> = HashMap::new();
let pkgs_info = get_all_existed_pkgs_info_of_app(app_path)?;
let pkgs_info = get_all_installed_pkgs_info_of_app(app_path)?;
pkgs_of_app.insert(app_uri.to_string(), pkgs_info);

// `Graph::from_str` calls `validate`, and `validate` checks that there are
Expand Down

0 comments on commit 75f1be5

Please sign in to comment.