xref: /DADK/dadk-config/src/common/task.rs (revision a013ae121c339d37b6fa5273770148fa161c730b)
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 
51 impl BuildConfig {
52     #[allow(dead_code)]
53     pub fn new(build_command: Option<String>) -> Self {
54         Self { build_command }
55     }
56 
57     pub fn validate(&self) -> Result<()> {
58         return Ok(());
59     }
60 
61     pub fn trim(&mut self) {
62         if let Some(build_command) = &mut self.build_command {
63             *build_command = build_command.trim().to_string();
64         }
65     }
66 }
67 
68 #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
69 pub struct InstallConfig {
70     /// 安装到DragonOS内的目录
71     #[serde(rename = "in-dragonos-path")]
72     pub in_dragonos_path: Option<PathBuf>,
73 }
74 
75 impl InstallConfig {
76     #[allow(dead_code)]
77     pub fn new(in_dragonos_path: Option<PathBuf>) -> Self {
78         Self { in_dragonos_path }
79     }
80 
81     pub fn validate(&self) -> Result<()> {
82         if self.in_dragonos_path.is_none() {
83             return Ok(());
84         }
85         if self.in_dragonos_path.as_ref().unwrap().is_relative() {
86             return Err(Error::msg(
87                 "InstallConfig: in_dragonos_path should be an Absolute path",
88             ));
89         }
90         return Ok(());
91     }
92 
93     pub fn trim(&mut self) {}
94 }
95 /// # 清理配置
96 #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
97 pub struct CleanConfig {
98     /// 清理命令
99     #[serde(rename = "clean-command")]
100     pub clean_command: Option<String>,
101 }
102 
103 impl CleanConfig {
104     #[allow(dead_code)]
105     pub fn new(clean_command: Option<String>) -> Self {
106         Self { clean_command }
107     }
108 
109     pub fn validate(&self) -> Result<()> {
110         return Ok(());
111     }
112 
113     pub fn trim(&mut self) {
114         if let Some(clean_command) = &mut self.clean_command {
115             *clean_command = clean_command.trim().to_string();
116         }
117     }
118 }
119 
120 /// @brief 依赖项
121 #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
122 pub struct Dependency {
123     #[serde(default = "default_empty_string")]
124     pub name: String,
125     #[serde(default = "default_empty_string")]
126     pub version: String,
127 }
128 
129 impl Dependency {
130     #[allow(dead_code)]
131     pub fn new(name: String, version: String) -> Self {
132         Self { name, version }
133     }
134 
135     pub fn validate(&self) -> Result<()> {
136         if self.name.is_empty() {
137             return Err(Error::msg("name is empty"));
138         }
139         if self.version.is_empty() {
140             return Err(Error::msg("version is empty"));
141         }
142         return Ok(());
143     }
144 
145     pub fn trim(&mut self) {
146         self.name = self.name.trim().to_string();
147         self.version = self.version.trim().to_string();
148     }
149 
150     pub fn name_version(&self) -> String {
151         return format!("{}-{}", self.name, self.version);
152     }
153 }
154 
155 /// # 任务环境变量
156 ///
157 /// 任务执行时的环境变量.这个环境变量是在当前任务执行时设置的,不会影响到其他任务
158 #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
159 pub struct TaskEnv {
160     #[serde(default = "default_empty_string")]
161     pub key: String,
162     #[serde(default = "default_empty_string")]
163     pub value: String,
164 }
165 
166 impl TaskEnv {
167     #[allow(dead_code)]
168     pub fn new(key: String, value: String) -> Self {
169         Self { key, value }
170     }
171 
172     pub fn key(&self) -> &str {
173         &self.key
174     }
175 
176     pub fn value(&self) -> &str {
177         &self.value
178     }
179 
180     pub fn trim(&mut self) {
181         self.key = self.key.trim().to_string();
182         self.value = self.value.trim().to_string();
183     }
184 
185     pub fn validate(&self) -> Result<()> {
186         if self.key.is_empty() {
187             return Err(Error::msg("Env: key is empty"));
188         }
189         return Ok(());
190     }
191 }
192 
193 fn default_empty_string() -> String {
194     "".to_string()
195 }
196