Skip to content

Commit 674005c

Browse files
authored
dadk支持预编译源+代码源支持从Archive导入 (#10)
* 基本完成了DADK的预编译源与代码源的archive代码,暂时只支持tar.gz的代码以及同名文件 * 通过cd *来匹配解压后的文件,暂时解决了解压后名字与压缩包名字不同的问题 * 通过ArichiveType类型枚举和正则表达式完成了重构,支持灵活添加对不同类型的压缩文件的支持 * dadk支持预编译源+代码源支持从Archive导入 * dadk支持预编译源+代码源支持从Archive导入 * 如果taskname 包括空格就修改为下划线 * 修改了一部分系统指令为rust代码 * 将除了git外的命令行指令替换为了rust库实现,修复了一些bug,封装了FileUtils * 修改了一处unwarp为expect。下载文件增加超时检测 * 修改了一处unwarp为expect。下载文件增加超时检测 * 将bash tar改为直接tar调用
1 parent 9cbc420 commit 674005c

File tree

12 files changed

+333
-89
lines changed

12 files changed

+333
-89
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,7 @@ Cargo.lock
99
# These are backup files generated by rustfmt
1010
**/*.rs.bk
1111

12-
/run/
12+
/run/
13+
14+
/bin
15+
/user

Cargo.toml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "dadk"
3-
authors = ["longjin <longjin@DragonOS.org>"]
4-
version = "0.1.1"
3+
authors = ["longjin <longjin@DragonOS.org>", "chikejian <chikejian@DragonOS.org>"]
4+
version = "0.1.2"
55
edition = "2021"
66
description = "DragonOS Application Development Kit\nDragonOS应用开发工具"
77
license = "GPL-2.0-only"
@@ -19,7 +19,9 @@ doc = true
1919
clap = { version = "4.2.4", features = ["derive"] }
2020
lazy_static = "1.4.0"
2121
log = "0.4.17"
22+
regex = "1.9.1"
2223
reqwest = { version = "0.11", features = ["blocking", "json"] }
2324
serde = { version = "1.0.160", features = ["serde_derive"] }
2425
serde_json = "1.0.96"
2526
simple_logger = "4.1.0"
27+
zip = "0.6"

src/console/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
//! # DADK控制台
2-
//!
2+
//!
33
//! DADK控制台能够让用户通过命令行交互的方式使用DADK。
4-
//!
4+
//!
55
//! ## 创建配置文件
6-
//!
6+
//!
77
//! DADK控制台提供了一个命令,用于创建一个配置文件。您可以通过以下命令创建一个配置文件:
8-
//!
8+
//!
99
//! ```bash
1010
//! dadk new
1111
//! ```
12-
//!
12+
//!
1313
1414
pub mod clean;
1515
pub mod elements;

src/executor/cache.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,20 @@ impl CacheDir {
132132

133133
pub fn build_dir_env_key(entity: &Rc<SchedEntity>) -> Result<String, ExecutorError> {
134134
let name_version_env = entity.task().name_version_env();
135-
return Ok(format!("{}_{}", Self::DADK_BUILD_CACHE_DIR_ENV_KEY_PREFIX,name_version_env));
135+
return Ok(format!(
136+
"{}_{}",
137+
Self::DADK_BUILD_CACHE_DIR_ENV_KEY_PREFIX,
138+
name_version_env
139+
));
136140
}
137141

138142
pub fn source_dir_env_key(entity: &Rc<SchedEntity>) -> Result<String, ExecutorError> {
139143
let name_version_env = entity.task().name_version_env();
140-
return Ok(format!("{}_{}", Self::DADK_SOURCE_CACHE_DIR_ENV_KEY_PREFIX,name_version_env));
144+
return Ok(format!(
145+
"{}_{}",
146+
Self::DADK_SOURCE_CACHE_DIR_ENV_KEY_PREFIX,
147+
name_version_env
148+
));
141149
}
142150

143151
pub fn need_source_cache(entity: &Rc<SchedEntity>) -> bool {
@@ -154,11 +162,10 @@ impl CacheDir {
154162
}
155163
} else if let TaskType::InstallFromPrebuilt(ps) = task_type {
156164
match ps {
157-
crate::parser::task::PrebuiltSource::Archive(_) => return true,
165+
crate::parser::task::PrebuiltSource::Archive(_) => return false,
158166
crate::parser::task::PrebuiltSource::Local(_) => return false,
159167
}
160168
}
161-
162169
unimplemented!("Not fully implemented task type: {:?}", task_type);
163170
}
164171

src/executor/mod.rs

Lines changed: 39 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ use std::{
77
sync::RwLock,
88
};
99

10-
use log::{error, info, warn, debug};
10+
use log::{debug, error, info, warn};
1111

1212
use crate::{
1313
console::{clean::CleanLevel, Action},
1414
executor::cache::CacheDir,
1515
parser::task::{CodeSource, PrebuiltSource, TaskEnv, TaskType},
1616
scheduler::{SchedEntities, SchedEntity},
17-
utils::stdio::StdioUtils,
17+
utils::file::FileUtils,
1818
};
1919

2020
use self::cache::CacheDirType;
@@ -107,7 +107,11 @@ impl Executor {
107107
// 清理构建结果
108108
let r = self.clean();
109109
if let Err(e) = r {
110-
error!("Failed to clean task {}: {:?}", self.entity.task().name_version(), e);
110+
error!(
111+
"Failed to clean task {}: {:?}",
112+
self.entity.task().name_version(),
113+
e
114+
);
111115
}
112116
}
113117
_ => {
@@ -165,36 +169,8 @@ impl Executor {
165169

166170
// 拷贝构建结果到安装路径
167171
let build_dir: PathBuf = self.build_dir.path.clone();
168-
169-
let cmd = Command::new("cp")
170-
.arg("-r")
171-
.arg(build_dir.to_string_lossy().to_string() + "/.")
172-
.arg(install_path)
173-
.stdout(Stdio::null())
174-
.stderr(Stdio::piped())
175-
.spawn()
176-
.map_err(|e| {
177-
ExecutorError::InstallError(format!(
178-
"Failed to install, error message: {}",
179-
e.to_string()
180-
))
181-
})?;
182-
183-
let output = cmd.wait_with_output().map_err(|e| {
184-
ExecutorError::InstallError(format!(
185-
"Failed to install, error message: {}",
186-
e.to_string()
187-
))
188-
})?;
189-
190-
if !output.status.success() {
191-
let err_msg = StdioUtils::tail_n_str(StdioUtils::stderr_to_lines(&output.stderr), 10);
192-
return Err(ExecutorError::InstallError(format!(
193-
"Failed to install, error message: {}",
194-
err_msg
195-
)));
196-
}
197-
172+
FileUtils::copy_dir_all(&build_dir, &install_path)
173+
.map_err(|e| ExecutorError::InstallError(e))?;
198174
info!("Task {} installed.", self.entity.task().name_version());
199175

200176
return Ok(());
@@ -292,7 +268,6 @@ impl Executor {
292268
if let Some(local_path) = self.entity.task().source_path() {
293269
return local_path;
294270
}
295-
296271
return self.source_dir.as_ref().unwrap().path.clone();
297272
}
298273

@@ -308,7 +283,15 @@ impl Executor {
308283
self.action
309284
),
310285
},
311-
_ => None,
286+
287+
TaskType::InstallFromPrebuilt(_) => match self.action {
288+
Action::Build => self.entity.task().build.build_command.clone(),
289+
Action::Clean(_) => self.entity.task().clean.clean_command.clone(),
290+
_ => unimplemented!(
291+
"create_command: Action {:?} not supported yet.",
292+
self.action
293+
),
294+
},
312295
};
313296

314297
if raw_cmd.is_none() {
@@ -359,14 +342,13 @@ impl Executor {
359342

360343
fn prepare_input(&self) -> Result<(), ExecutorError> {
361344
// 拉取源文件
362-
if self.source_dir.is_none() {
363-
return Ok(());
364-
}
365345
let task = self.entity.task();
366-
let source_dir = self.source_dir.as_ref().unwrap();
367-
368346
match &task.task_type {
369347
TaskType::BuildFromSource(cs) => {
348+
if self.source_dir.is_none() {
349+
return Ok(());
350+
}
351+
let source_dir = self.source_dir.as_ref().unwrap();
370352
match cs {
371353
CodeSource::Git(git) => {
372354
git.prepare(source_dir)
@@ -375,15 +357,29 @@ impl Executor {
375357
// 本地源文件,不需要拉取
376358
CodeSource::Local(_) => return Ok(()),
377359
// 在线压缩包,需要下载
378-
CodeSource::Archive(_) => todo!(),
360+
CodeSource::Archive(archive) => {
361+
archive
362+
.download_unzip(source_dir)
363+
.map_err(|e| ExecutorError::PrepareEnvError(e))?;
364+
}
379365
}
380366
}
381367
TaskType::InstallFromPrebuilt(pb) => {
382368
match pb {
383369
// 本地源文件,不需要拉取
384-
PrebuiltSource::Local(_) => return Ok(()),
370+
PrebuiltSource::Local(local_source) => {
371+
let local_path = local_source.path();
372+
let target_path = &self.build_dir.path;
373+
FileUtils::copy_dir_all(&local_path, &target_path)
374+
.map_err(|e| ExecutorError::TaskFailed(e))?; // let mut cmd = "cp -r ".to_string();
375+
return Ok(());
376+
}
385377
// 在线压缩包,需要下载
386-
PrebuiltSource::Archive(_) => todo!(),
378+
PrebuiltSource::Archive(archive) => {
379+
archive
380+
.download_unzip(&self.build_dir)
381+
.map_err(|e| ExecutorError::PrepareEnvError(e))?;
382+
}
387383
}
388384
}
389385
}

0 commit comments

Comments
 (0)