Browse Source

feat(dadk-user): 增加目录依赖 (#79)

* 增加目录依赖

* 更新用户配置文件模版

* 更新用户文档
Jomo 4 months ago
parent
commit
e0934047cd

+ 16 - 2
dadk-config/src/common/task.rs

@@ -46,12 +46,26 @@ pub struct BuildConfig {
     /// 构建命令
     #[serde(rename = "build-command")]
     pub build_command: Option<String>,
+    /// 构建前执行的脚本
+    #[serde(rename = "pre-build")]
+    pub pre_build: Option<PathBuf>,
+    #[serde(rename = "post-build")]
+    /// 构建后执行的脚本
+    pub post_build: Option<PathBuf>,
 }
 
 impl BuildConfig {
     #[allow(dead_code)]
-    pub fn new(build_command: Option<String>) -> Self {
-        Self { build_command }
+    pub fn new(
+        build_command: Option<String>,
+        pre_build: Option<PathBuf>,
+        post_build: Option<PathBuf>,
+    ) -> Self {
+        Self {
+            build_command,
+            pre_build,
+            post_build,
+        }
     }
 
     pub fn validate(&self) -> Result<()> {

+ 6 - 0
dadk-config/templates/config/userapp_config.toml

@@ -43,6 +43,12 @@ revision = "01cdc56863"
 # (可选)构建命令
 build-command = "make install"
 
+# (可选)预构建脚本路径
+pre-build = "config/pre_build.sh"
+
+# (可选)构建后脚本路径
+post-build = "config/post_build.sh"
+
 # 安装相关信息
 [install]
 

+ 5 - 1
dadk-config/tests/test_user_config.rs

@@ -51,7 +51,11 @@ fn test_parse_dadk_user_config(ctx: &mut DadkConfigTestContext) {
                 version: "0.1.2".to_string(),
             },
         ],
-        build: BuildConfig::new(Some("make install".to_string())),
+        build: BuildConfig::new(
+            Some("make install".to_string()),
+            Some(PathBuf::from("config/pre_build.sh")),
+            Some(PathBuf::from("config/post_build.sh")),
+        ),
         install: InstallConfig::new(Some(PathBuf::from("/bin"))),
         clean: CleanConfig::new(Some("make clean".to_string())),
         envs: vec![

+ 42 - 0
dadk-user/src/executor/mod.rs

@@ -150,8 +150,12 @@ impl Executor {
 
         match self.action {
             Action::Build => {
+                // 构建前的工作
+                self.pre_build()?;
                 // 构建任务
                 self.build()?;
+                // 构建完毕后的工作
+                self.post_build()?;
             }
             Action::Install => {
                 // 把构建结果安装到DragonOS
@@ -173,6 +177,25 @@ impl Executor {
         return Ok(());
     }
 
+    fn pre_build(&mut self) -> Result<(), ExecutorError> {
+        if let Some(pre_build) = self.entity.task().build.pre_build {
+            let output = Command::new(pre_build)
+                .output()
+                .expect("Failed to execute pre_build script");
+
+            // 检查脚本执行结果
+            if output.status.success() {
+                info!("Pre-build script executed successfully");
+            } else {
+                error!("Pre-build script failed");
+                return Err(ExecutorError::TaskFailed(
+                    "Pre-build script failed".to_string(),
+                ));
+            }
+        }
+        Ok(())
+    }
+
     fn build(&mut self) -> Result<(), ExecutorError> {
         if let Some(status) = self.task_log().build_status() {
             if let Some(build_time) = self.task_log().build_time() {
@@ -197,6 +220,25 @@ impl Executor {
         return self.do_build();
     }
 
+    fn post_build(&mut self) -> Result<(), ExecutorError> {
+        if let Some(post_build) = self.entity.task().build.post_build {
+            let output = Command::new(post_build)
+                .output()
+                .expect("Failed to execute post_build script");
+
+            // 检查脚本执行结果
+            if output.status.success() {
+                info!("Post-build script executed successfully");
+            } else {
+                error!("Post-build script failed");
+                return Err(ExecutorError::TaskFailed(
+                    "Post-build script failed".to_string(),
+                ));
+            }
+        }
+        Ok(())
+    }
+
     /// # 执行build操作
     fn do_build(&mut self) -> Result<(), ExecutorError> {
         // 确认源文件就绪

+ 87 - 0
docs/user-manual/README.md

@@ -1,3 +1,90 @@
 # 用户指南
 
+## 用户程序配置文件模版
+在dadk-config/templates/config/目录下,可以找到用户程序的配置文件模版`userapp_config.toml`
+```toml
+# 用户程序名称
+name = "userapp_config"
+
+# 版本号
+version = "0.2.0"
+
+# 用户程序描述信息
+description = ""
+
+# (可选)默认: false 是否只构建一次,如果为true,DADK会在构建成功后,将构建结果缓存起来,下次构建时,直接使用缓存的构建结果
+build-once = false
+
+#  (可选) 默认: false 是否只安装一次,如果为true,DADK会在安装成功后,不再重复安装
+install-once = false
+
+# 目标架构
+# 可选值:"x86_64", "aarch64", "riscv64"
+target-arch = ["x86_64"]
+
+# 任务源
+[task-source]
+
+# 构建类型
+# 可选值:"build-from_source", "install-from-prebuilt"
+type = "build-from-source"
+
+# 构建来源
+# "build_from_source" 可选值:"git", "local", "archive"
+# "install_from_prebuilt" 可选值:"local", "archive"
+source = "git"
+
+# 路径或URL
+source-path = "https://git.mirrors.dragonos.org.cn/DragonOS-Community/test_git.git"
+
+# git标签或分支
+# 注意: branch和revision只能二选一,且source要设置为"git"
+revision = "01cdc56863"
+# branch = "test"
+
+# 构建相关信息
+[build]
+
+# (可选)构建命令
+build-command = "make install"
+
+# (可选)预构建脚本路径
+pre-build = "config/pre_build.sh"
+
+# (可选)构建后脚本路径
+post-build = "config/post_build.sh"
+
+# 安装相关信息
+[install]
+
+# (可选)安装到DragonOS的路径
+in-dragonos-path = "/bin"
+
+# 清除相关信息
+[clean]
+
+# (可选)清除命令
+clean-command = "make clean"
+
+# (可选)依赖项
+# 注意:如果没有依赖项,忽略此项,不允许只留一个[[depends]]
+[[depends]]
+name = "depend1"
+version = "0.1.1"
+
+[[depends]]
+name = "depend2"
+version = "0.1.2"
+
+# (可选)环境变量
+[[envs]]
+key = "PATH"
+value = "/usr/bin"
+
+[[envs]]
+key = "LD_LIBRARY_PATH"
+value = "/usr/lib"
+
+```
+
 TODO