dadk_config/common/
task.rs

1use anyhow::{Error, Result};
2use serde::{Deserialize, Serialize};
3use std::path::PathBuf;
4
5#[derive(Debug, Serialize, Deserialize, PartialEq)]
6pub struct TaskSource {
7    #[serde(rename = "type")]
8    pub source_type: TaskSourceType,
9    pub source: Source,
10    #[serde(rename = "source-path")]
11    pub source_path: String,
12    /// 把压缩包中的哪个目录作为根目录(可选)
13    ///
14    /// 仅当 source = "archive" 时生效
15    #[serde(rename = "archive-rootdir")]
16    pub archive_rootdir: Option<String>,
17    /// 分支(可选,如果为空,则拉取master)branch和revision只能同时指定一个
18    pub branch: Option<String>,
19    /// 特定的提交的hash值(可选,如果为空,则拉取branch的最新提交)
20    pub revision: Option<String>,
21}
22
23/// # 任务类型
24#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
25pub enum TaskSourceType {
26    /// 从源码构建
27    #[serde(rename = "build-from-source")]
28    BuildFromSource,
29    /// 从预编译包安装
30    #[serde(rename = "install-from-prebuilt")]
31    InstallFromPrebuilt,
32}
33
34/// # 来源类型
35#[derive(Debug, Serialize, Deserialize, PartialEq)]
36pub enum Source {
37    /// 从Git仓库获取
38    #[serde(rename = "git")]
39    Git,
40    /// 从本地目录获取
41    #[serde(rename = "local")]
42    Local,
43    /// 从在线压缩包获取
44    #[serde(rename = "archive")]
45    Archive,
46}
47
48/// @brief 构建配置
49#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
50pub struct BuildConfig {
51    /// 构建命令
52    #[serde(rename = "build-command")]
53    pub build_command: Option<String>,
54    /// 构建前执行的脚本
55    #[serde(rename = "pre-build")]
56    pub pre_build: Option<PathBuf>,
57    #[serde(rename = "post-build")]
58    /// 构建后执行的脚本
59    pub post_build: Option<PathBuf>,
60}
61
62impl BuildConfig {
63    #[allow(dead_code)]
64    pub fn new(
65        build_command: Option<String>,
66        pre_build: Option<PathBuf>,
67        post_build: Option<PathBuf>,
68    ) -> Self {
69        Self {
70            build_command,
71            pre_build,
72            post_build,
73        }
74    }
75
76    pub fn validate(&self) -> Result<()> {
77        return Ok(());
78    }
79
80    pub fn trim(&mut self) {
81        if let Some(build_command) = &mut self.build_command {
82            *build_command = build_command.trim().to_string();
83        }
84    }
85}
86
87#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
88pub struct InstallConfig {
89    /// 安装到DragonOS内的目录
90    #[serde(rename = "in-dragonos-path")]
91    pub in_dragonos_path: Option<PathBuf>,
92}
93
94impl InstallConfig {
95    #[allow(dead_code)]
96    pub fn new(in_dragonos_path: Option<PathBuf>) -> Self {
97        Self { in_dragonos_path }
98    }
99
100    pub fn validate(&self) -> Result<()> {
101        if self.in_dragonos_path.is_none() {
102            return Ok(());
103        }
104        if self.in_dragonos_path.as_ref().unwrap().is_relative() {
105            return Err(Error::msg(
106                "InstallConfig: in_dragonos_path should be an Absolute path",
107            ));
108        }
109        return Ok(());
110    }
111
112    pub fn trim(&mut self) {}
113}
114/// # 清理配置
115#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
116pub struct CleanConfig {
117    /// 清理命令
118    #[serde(rename = "clean-command")]
119    pub clean_command: Option<String>,
120}
121
122impl CleanConfig {
123    #[allow(dead_code)]
124    pub fn new(clean_command: Option<String>) -> Self {
125        Self { clean_command }
126    }
127
128    pub fn validate(&self) -> Result<()> {
129        return Ok(());
130    }
131
132    pub fn trim(&mut self) {
133        if let Some(clean_command) = &mut self.clean_command {
134            *clean_command = clean_command.trim().to_string();
135        }
136    }
137}
138
139/// @brief 依赖项
140#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
141pub struct Dependency {
142    #[serde(default = "default_empty_string")]
143    pub name: String,
144    #[serde(default = "default_empty_string")]
145    pub version: String,
146}
147
148impl Dependency {
149    #[allow(dead_code)]
150    pub fn new(name: String, version: String) -> Self {
151        Self { name, version }
152    }
153
154    pub fn validate(&self) -> Result<()> {
155        if self.name.is_empty() {
156            return Err(Error::msg("name is empty"));
157        }
158        if self.version.is_empty() {
159            return Err(Error::msg("version is empty"));
160        }
161        return Ok(());
162    }
163
164    pub fn trim(&mut self) {
165        self.name = self.name.trim().to_string();
166        self.version = self.version.trim().to_string();
167    }
168
169    pub fn name_version(&self) -> String {
170        return format!("{}-{}", self.name, self.version);
171    }
172}
173
174/// # 任务环境变量
175///
176/// 任务执行时的环境变量.这个环境变量是在当前任务执行时设置的,不会影响到其他任务
177#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
178pub struct TaskEnv {
179    #[serde(default = "default_empty_string")]
180    pub key: String,
181    #[serde(default = "default_empty_string")]
182    pub value: String,
183}
184
185impl TaskEnv {
186    #[allow(dead_code)]
187    pub fn new(key: String, value: String) -> Self {
188        Self { key, value }
189    }
190
191    pub fn key(&self) -> &str {
192        &self.key
193    }
194
195    pub fn value(&self) -> &str {
196        &self.value
197    }
198
199    pub fn trim(&mut self) {
200        self.key = self.key.trim().to_string();
201        self.value = self.value.trim().to_string();
202    }
203
204    pub fn validate(&self) -> Result<()> {
205        if self.key.is_empty() {
206            return Err(Error::msg("Env: key is empty"));
207        }
208        return Ok(());
209    }
210}
211
212fn default_empty_string() -> String {
213    "".to_string()
214}