Skip to content

Commit

Permalink
refactor: refine tman install (#513)
Browse files Browse the repository at this point in the history
  • Loading branch information
halajohn authored Jan 6, 2025
1 parent 65a660d commit 8b590d5
Show file tree
Hide file tree
Showing 12 changed files with 71 additions and 122 deletions.
11 changes: 8 additions & 3 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 @@ -27,7 +27,8 @@ pub struct CheckGraphCommand {
pub fn create_sub_cmd(_args_cfg: &crate::cmd_line::ArgsCfg) -> Command {
Command::new("graph")
.about(
"Check whether the graph content of the predefined graph or start_graph command is correct",
"Check whether the graph content of the predefined graph or \
start_graph command is correct",
)
.arg(
Arg::new("APP_DIR")
Expand All @@ -37,7 +38,8 @@ pub fn create_sub_cmd(_args_cfg: &crate::cmd_line::ArgsCfg) -> Command {
default, the predefined graph will be read from the first \
one in the list.",
)
.required(true).num_args(1..),
.required(true)
.num_args(1..),
)
.arg(
Arg::new("PREDEFINED_GRAPH_NAME")
Expand Down Expand Up @@ -145,7 +147,10 @@ fn get_graphs_to_be_checked(command: &CheckGraphCommand) -> Result<Vec<Graph>> {
let first_app_path = path::Path::new(&command.app_dir[0]);
let first_app_property = parse_property_in_folder(first_app_path)?;
if first_app_property.is_none() {
return Err(anyhow::anyhow!("The property.json is not found in the first app, which is required to retrieve the predefined graphs."));
return Err(anyhow::anyhow!(
"The property.json is not found in the first app, which is \
required to retrieve the predefined graphs."
));
}

let predefined_graphs = first_app_property
Expand Down
9 changes: 7 additions & 2 deletions core/src/ten_manager/src/cmd/cmd_create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,11 @@ pub fn create_sub_cmd(args_cfg: &crate::cmd_line::ArgsCfg) -> Command {
.arg(
Arg::new("TEMPLATE_DATA")
.long("template-data")
.help("The placeholders used within the template and their corresponding values. The format is key-value pairs, e.g., `--template-data key=value`")
.help(
"The placeholders used within the template and their \
corresponding values. The format is key-value pairs, e.g., \
`--template-data key=value`",
)
.value_name("KEY=VALUE")
.action(ArgAction::Append),
)
Expand Down Expand Up @@ -120,7 +124,8 @@ pub fn parse_sub_cmd(sub_cmd_args: &ArgMatches) -> Result<CreateCommand> {

if cmd.template_data.contains_key("package_name") {
return Err(anyhow!(
"The 'package_name' is set via the command line as '{}', and cannot be modified through '--template-data'.",
"The 'package_name' is set via the command line as '{}', \
and cannot be modified through '--template-data'.",
cmd.pkg_name
));
}
Expand Down
3 changes: 2 additions & 1 deletion core/src/ten_manager/src/cmd/cmd_designer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ pub async fn execute_cmd(
let cwd = get_cwd()?.to_str().unwrap_or_default().to_string();

println!(
"{} Doesn't specify the base directory, use current working directory instead: {}",
"{} Doesn't specify the base directory, use current working \
directory instead: {}",
Emoji("💡", "!"),
&cwd
);
Expand Down
43 changes: 14 additions & 29 deletions core/src/ten_manager/src/cmd/cmd_install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,14 +208,8 @@ pub async fn execute_cmd(
// folder).
let mut all_existing_local_pkgs: Vec<PkgInfo> = vec![];

// The initial value of extra_dependencies is the package specified to be
// installed via the command line (if any).
let mut extra_dependencies_specified_in_cmd_line = vec![];

// `extra_dependency_relationships` contain TEN packages, and each TEN
// package is the main entity depended upon by its corresponding
// extra_dependencies."
let mut extra_dependency_relationships = vec![];
let mut dep_relationship_from_cmd_line: Option<DependencyRelationship> =
None;

let mut app_pkg: Option<PkgInfo> = None;
let mut installing_pkg_type: Option<PkgType> = None;
Expand All @@ -225,6 +219,11 @@ pub async fn execute_cmd(
// Case 1: tman install <package_type> <package_name>

let installing_pkg_type_: PkgType = package_type_str.parse()?;

// First, check that the package we want to install can be installed
// within the current directory structure.
is_package_installable_in_path(&cwd, &installing_pkg_type_)?;

let (installing_pkg_name_, installing_pkg_version_req_) =
parse_pkg_name_version_req(
command_data.package_name.as_ref().unwrap(),
Expand All @@ -233,31 +232,23 @@ pub async fn execute_cmd(
installing_pkg_type = Some(installing_pkg_type_);
installing_pkg_name = Some(installing_pkg_name_.clone());

// First, check that the package we want to install can be installed
// within the current directory structure.
is_package_installable_in_path(&cwd, &installing_pkg_type_)?;

// If it is not a standalone install, then the `cwd` must be within
// the base directory of a TEN app.
// The `cwd` must be the base directory of a TEN app.
let app_pkg_ = get_pkg_info_from_path(&cwd, true)?;
affected_pkg_name = app_pkg_.basic_info.type_and_name.name.clone();

// Push the app itself into the initial_input_pkgs.
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 existing packages into all_candidates only if the compatible
// score of the package is >= 0.
filter_compatible_pkgs_to_candidates(
tman_config,
&all_existing_local_pkgs,
&mut all_candidates,
&command_data.support,
);

let extra_dependency_relationship = DependencyRelationship {
dep_relationship_from_cmd_line = Some(DependencyRelationship {
type_and_name: PkgTypeAndName {
pkg_type: app_pkg_.basic_info.type_and_name.pkg_type,
name: app_pkg_.basic_info.type_and_name.name.clone(),
Expand All @@ -272,17 +263,9 @@ pub async fn execute_cmd(
path: None,
base_dir: None,
},
};
extra_dependency_relationships.push(extra_dependency_relationship);
});

app_pkg = Some(app_pkg_);

let dep = PkgDependency::new(
installing_pkg_type_,
installing_pkg_name_.clone(),
installing_pkg_version_req_,
);
extra_dependencies_specified_in_cmd_line.push(dep);
} else {
// Case 2: tman install

Expand Down Expand Up @@ -353,8 +336,10 @@ pub async fn execute_cmd(
tman_config,
&command_data.support,
initial_pkgs_to_find_candidates,
dep_relationship_from_cmd_line
.as_ref()
.map(|rel| &rel.dependency),
all_candidates,
&extra_dependencies_specified_in_cmd_line,
locked_pkgs.as_ref(),
)
.await?;
Expand All @@ -366,7 +351,7 @@ pub async fn execute_cmd(
tman_config,
&affected_pkg_name,
&affected_pkg_type,
&extra_dependency_relationships,
dep_relationship_from_cmd_line.as_ref(),
&all_candidates,
locked_pkgs.as_ref(),
)?;
Expand Down
13 changes: 8 additions & 5 deletions core/src/ten_manager/src/cmd/cmd_modify/cmd_modify_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,30 +32,33 @@ pub fn create_sub_cmd(_args_cfg: &crate::cmd_line::ArgsCfg) -> Command {
.long("app-dir")
.help("Specify the app directory")
.required(true)
.num_args(1)
.num_args(1),
)
.arg(
Arg::new("PREDEFINED_GRAPH_NAME")
.long("predefined-graph-name")
.help("Specify the predefined graph name to be modified")
.required(true)
.num_args(1)
.num_args(1),
)
.arg(
Arg::new("MODIFICATION")
.long("modification")
.short('m')
.help("The path=JsonString to modify in the selected graph. E.g. .name=\"test\"")
.help(
"The path=JsonString to modify in the selected graph. \
E.g. .name=\"test\"",
)
.required(true)
.num_args(1)
.num_args(1),
)
.arg(
Arg::new("INPLACE")
.long("inplace")
.short('i')
.help("Overwrite the original property.json file")
.required(false)
.action(clap::ArgAction::SetTrue)
.action(clap::ArgAction::SetTrue),
)
}

Expand Down
5 changes: 4 additions & 1 deletion core/src/ten_manager/src/cmd/cmd_package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ pub struct PackageCommand {}
pub fn create_sub_cmd(_args_cfg: &crate::cmd_line::ArgsCfg) -> Command {
Command::new("package")
.about("Create a package file")
.after_help("Switch to the base directory of the TEN package you want to package, then simply run 'tman package' directly in that directory.")
.after_help(
"Switch to the base directory of the TEN package you want \
to package, then simply run 'tman package' directly in that directory.",
)
}

pub fn parse_sub_cmd(
Expand Down
5 changes: 4 additions & 1 deletion core/src/ten_manager/src/cmd/cmd_publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ pub struct PublishCommand {}
pub fn create_sub_cmd(_args_cfg: &crate::cmd_line::ArgsCfg) -> Command {
Command::new("publish")
.about("Publish a package")
.after_help("Switch to the base directory of the TEN package you want to publish, then simply run 'tman publish' directly in that directory.")
.after_help(
"Switch to the base directory of the TEN package you want to \
publish, then simply run 'tman publish' directly in that directory.",
)
}

pub fn parse_sub_cmd(
Expand Down
6 changes: 3 additions & 3 deletions core/src/ten_manager/src/dep_and_candidate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,22 +351,22 @@ pub async fn get_all_candidates_from_deps(
tman_config: &TmanConfig,
support: &PkgSupport,
mut pkgs_to_be_searched: Vec<PkgInfo>,
extra_dep: Option<&PkgDependency>,
mut all_candidates: HashMap<PkgTypeAndName, HashMap<PkgBasicInfo, PkgInfo>>,
extra_dependencies: &Vec<PkgDependency>,
locked_pkgs: Option<&HashMap<PkgTypeAndName, PkgInfo>>,
) -> Result<HashMap<PkgTypeAndName, HashMap<PkgBasicInfo, PkgInfo>>> {
let mut merged_dependencies = HashMap::new();
let mut processed_pkgs = HashSet::<PkgBasicInfo>::new();

// If there is extra dependencies (ex: specified in the command line),
// handle those dependencies, too.
if !extra_dependencies.is_empty() {
if extra_dep.is_some() {
let mut new_pkgs_to_be_searched: Vec<PkgInfo> = vec![];

process_dependencies_to_get_candidates(
tman_config,
support,
extra_dependencies,
&vec![extra_dep.unwrap().clone()],
&mut merged_dependencies,
&mut all_candidates,
&mut new_pkgs_to_be_searched,
Expand Down
25 changes: 0 additions & 25 deletions core/src/ten_manager/src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,28 +92,3 @@ pub fn check_is_app_folder(path: &Path) -> Result<()> {

Ok(())
}

pub fn check_is_package_folder(path: &Path) -> Result<()> {
match ten_rust::pkg_info::manifest::parse_manifest_in_folder(path) {
Ok(_) => Ok(()),
Err(err) => Err(err),
}
}

#[cfg(test)]
mod tests {
// use super::*;
// use crate::constants::PROPERTY_JSON_FILENAME;

// #[test]
// fn it_merge_files() -> Result<()> {
// let source = Path::new("/home/wei/MyData/Temp/tests/A");
// let destination = Path::new("/home/wei/MyData/Temp/tests/B");
// let inclusions = vec![
// "xxx/manifest.json".to_string(),
// PROPERTY_JSON_FILENAME.to_string(),
// ];

// merge_folders(source, destination, &inclusions)
// }
}
32 changes: 7 additions & 25 deletions core/src/ten_manager/src/install/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ use ten_rust::pkg_info::{
use super::{config::TmanConfig, registry::get_package};
use crate::{
cmd::cmd_install::{InstallCommand, LocalInstallMode},
fs::{
check_is_app_folder, check_is_package_folder, copy_folder_recursively,
},
fs::{check_is_app_folder, copy_folder_recursively},
log::tman_verbose_println,
manifest_lock::{
parse_manifest_lock_in_folder, write_pkg_lockfile, ManifestLock,
Expand Down Expand Up @@ -199,26 +197,10 @@ pub fn is_package_installable_in_path(
) -> Result<()> {
match installing_pkg_type {
PkgType::App => {
// The app must not be installed into a TEN package folder.
if check_is_package_folder(cwd).is_ok() {
return Err(anyhow!(
"There is already a TEN package in the current folder. The TEN APP must be installed in a separate folder."
.to_string(),
));
}
}

PkgType::Extension => {
let manifest_path = cwd.join(MANIFEST_JSON_FILENAME);
if !manifest_path.exists() {
// An extension can be independently installed in a non-TEN
// directory. This is mainly to allow developers to easily
// develop, compile, test, and release an extension.
return Ok(());
}

// Otherwise, the extension must be installed in a TEN app folder.
check_is_app_folder(cwd)?;
return Err(anyhow!(
"The package type 'app' is not allowed to be installed. \
Use the 'create' command instead."
));
}

_ => {
Expand Down Expand Up @@ -413,7 +395,7 @@ pub fn filter_compatible_pkgs_to_candidates(
tman_verbose_println!(
tman_config,
"The existed {} package {} is not compatible \
with the current system.",
with the current system.",
existed_pkg.basic_info.type_and_name.pkg_type,
existed_pkg.basic_info.type_and_name.name
);
Expand Down Expand Up @@ -455,7 +437,7 @@ pub fn compare_solver_results_with_existed_pkgs(
if !untracked_local_pkgs.is_empty() {
println!(
"{} The following local packages do not \
appear in the dependency tree:",
appear in the dependency tree:",
Emoji("💡", "")
);
for pkg in untracked_local_pkgs {
Expand Down
Loading

0 comments on commit 8b590d5

Please sign in to comment.