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