1 use std::path::PathBuf;
2
3 use serde::Deserialize;
4
5 use crate::common::{
6 target_arch::TargetArch,
7 task::{BuildConfig, CleanConfig, Dependency, InstallConfig, TaskEnv, TaskSource},
8 };
9
10 use anyhow::Result;
11
12 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
13 pub enum UserCleanLevel {
14 /// 清理所有用户程序构建缓存
15 All,
16 /// 只在用户程序源码目录下清理
17 InSrc,
18 /// 只清理用户程序输出目录
19 Output,
20 }
21
22 #[derive(Debug, Deserialize, PartialEq)]
23 /// 用户程序配置文件
24 pub struct UserConfigFile {
25 /// 包名
26 pub name: String,
27 /// 版本
28 pub version: String,
29 /// 包的描述
30 pub description: String,
31 /// 任务类型
32 #[serde(rename = "task-source")]
33 pub task_source: TaskSource,
34 /// 依赖的包
35 #[serde(default = "default_empty_dep")]
36 pub depends: Vec<Dependency>,
37 /// 构建配置
38 pub build: BuildConfig,
39 /// 安装配置
40 pub install: InstallConfig,
41 /// 清理配置
42 pub clean: CleanConfig,
43 /// 环境变量
44 #[serde(default = "default_empty_env")]
45 pub envs: Vec<TaskEnv>,
46
47 /// (可选) 是否只构建一次,如果为true,DADK会在构建成功后,将构建结果缓存起来,下次构建时,直接使用缓存的构建结果。
48 #[serde(rename = "build-once", default = "default_false")]
49 pub build_once: bool,
50
51 /// (可选) 是否只安装一次,如果为true,DADK会在安装成功后,不再重复安装。
52 #[serde(rename = "install-once", default = "default_false")]
53 pub install_once: bool,
54
55 #[serde(rename = "target-arch")]
56 pub target_arch: Vec<TargetArch>,
57 }
58
59 impl UserConfigFile {
load(path: &PathBuf) -> Result<Self>60 pub fn load(path: &PathBuf) -> Result<Self> {
61 let content = std::fs::read_to_string(path)?;
62 Self::load_from_str(&content)
63 }
64
load_from_str(content: &str) -> Result<Self>65 pub fn load_from_str(content: &str) -> Result<Self> {
66 let config: UserConfigFile = toml::from_str(content)?;
67 Ok(config)
68 }
69 }
70
default_empty_env() -> Vec<TaskEnv>71 fn default_empty_env() -> Vec<TaskEnv> {
72 vec![]
73 }
74
default_empty_dep() -> Vec<Dependency>75 fn default_empty_dep() -> Vec<Dependency> {
76 vec![]
77 }
78
default_false() -> bool79 fn default_false() -> bool {
80 false
81 }
82