Add operations to manage Target.

Target represents operations with the same destination.
This commit is contained in:
jiangjianfeng 2021-09-17 14:15:37 +08:00 committed by Zongmin.Gu
parent 6f81a58a03
commit 66997b2852
2 changed files with 86 additions and 0 deletions

@ -163,6 +163,66 @@ impl Bom {
} }
} }
impl Target {
fn get_target_management(&self, root_dir: &str) -> TargetManagement {
let dirs_to_make = self.get_dirs_to_make(root_dir);
let links_to_create = self.get_links_to_create(root_dir);
let source_managements = self.get_source_managements(root_dir);
let mut target_management = TargetManagement::default();
target_management.dirs_to_make = dirs_to_make;
target_management.links_to_create = links_to_create;
for source_management in source_managements.into_iter() {
target_management.add_source_management(source_management);
}
target_management
}
fn get_dirs_to_make(&self, root_dir: &str) -> Vec<String> {
let mut dirs_to_make = Vec::new();
let target_path = dest_in_root(root_dir, &self.target);
// mkdir: target path
dirs_to_make.push(target_path.to_string_lossy().to_string());
// mkdir: each sub dir
if let Some(ref dirs) = self.mkdirs {
for dir in dirs {
let dir_path = target_path.join(dir);
dirs_to_make.push(dir_path.to_string_lossy().to_string());
}
}
dirs_to_make
}
fn get_links_to_create(&self, root_dir: &str) -> Vec<(String, String)> {
let target_path = dest_in_root(root_dir, &self.target);
let mut links_to_create = Vec::new();
if let Some(ref links) = self.createlinks {
for link in links {
let linkname = target_path.join(&link.linkname);
links_to_create.push((link.src.clone(), linkname.to_string_lossy().to_string()));
}
}
links_to_create
}
fn get_source_managements(&self, root_dir: &str) -> Vec<SourceManagement> {
let target_path = dest_in_root(root_dir, &self.target);
let mut source_managements = Vec::new();
if let Some(ref sources) = self.copy {
let root_dir_path = PathBuf::from(root_dir);
let workspace_dir = root_dir_path
.parent()
.map(|parent| parent.to_string_lossy().to_string())
.unwrap_or(".".to_string());
for source in sources {
let source_management =
source.get_source_management(&workspace_dir, &target_path.to_string_lossy());
source_managements.push(source_management);
}
}
source_managements
}
}
impl BomManagement { impl BomManagement {
fn add_target_management(&mut self, mut target_management: TargetManagement, root_dir: &str) { fn add_target_management(&mut self, mut target_management: TargetManagement, root_dir: &str) {
// First, we need to resolve environmental variables // First, we need to resolve environmental variables
@ -207,6 +267,20 @@ impl BomManagement {
} }
} }
impl TargetManagement {
fn add_source_management(&mut self, source_management: SourceManagement) {
let SourceManagement {
dirs_to_copy,
files_to_copy,
files_autodep,
} = source_management;
self.dirs_to_copy.extend(dirs_to_copy.into_iter());
self.files_to_copy.extend(files_to_copy.into_iter());
self.files_autodep.extend(files_autodep.into_iter());
}
}
/// This function will return all included bom files in the order to deal with. /// This function will return all included bom files in the order to deal with.
/// This function operates in such a way: It starts from putting the root bom into a queue, /// This function operates in such a way: It starts from putting the root bom into a queue,
/// In each iteration of the loop, it will fetch the first bom from the head of the queue, /// In each iteration of the loop, it will fetch the first bom from the head of the queue,

@ -10,6 +10,18 @@ use std::path::PathBuf;
use std::process::{Command, Output}; use std::process::{Command, Output};
use std::vec; use std::vec;
/// convert a dest path(usually absolute) to a dest path in root directory
pub fn dest_in_root(root_dir: &str, dest: &str) -> PathBuf {
let root_path = PathBuf::from(root_dir);
let dest_path = PathBuf::from(dest);
let dest_relative = if dest_path.is_absolute() {
PathBuf::from(dest_path.strip_prefix("/").unwrap())
} else {
dest_path
};
return root_path.join(dest_relative);
}
/// find an included file in the file system. If we can find the bom file, return the path /// find an included file in the file system. If we can find the bom file, return the path
/// otherwise, the process exit with error /// otherwise, the process exit with error
/// if included dir is relative path, if will be viewed as path relative to the `current` path (where we execute command) /// if included dir is relative path, if will be viewed as path relative to the `current` path (where we execute command)