1 | // SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd. |
2 | // |
3 | // SPDX-License-Identifier: GPL-3.0-or-later |
4 | |
5 | #ifndef CONFIGUTIL_H |
6 | #define CONFIGUTIL_H |
7 | |
8 | #include "services/project/projectservice.h" |
9 | |
10 | #include <QObject> |
11 | #include <QVector> |
12 | #include <QMap> |
13 | |
14 | namespace config { |
15 | |
16 | enum ConfigType { |
17 | Unknown = 0, |
18 | Debug, |
19 | Release |
20 | }; |
21 | |
22 | enum StepType { |
23 | Build = 0, |
24 | Clean, |
25 | StepCount |
26 | }; |
27 | |
28 | struct StepItem { |
29 | StepType type = Build; |
30 | QString targetName; |
31 | QStringList targetList; |
32 | QString arguments; |
33 | |
34 | friend QDataStream &operator<<(QDataStream &stream, const StepItem &data) |
35 | { |
36 | stream << data.type; |
37 | stream << data.targetName; |
38 | stream << data.targetList; |
39 | stream << data.arguments; |
40 | |
41 | return stream; |
42 | } |
43 | |
44 | friend QDataStream &operator>>(QDataStream &stream, StepItem &data) |
45 | { |
46 | int type = 0; |
47 | stream >> type; |
48 | data.type = static_cast<StepType>(type); |
49 | stream >> data.targetName; |
50 | stream >> data.targetList; |
51 | stream >> data.arguments; |
52 | |
53 | return stream; |
54 | } |
55 | }; |
56 | |
57 | struct EnvironmentItem { |
58 | bool enable; |
59 | QMap<QString, QString> environments; |
60 | |
61 | friend QDataStream &operator<<(QDataStream &stream, const EnvironmentItem &data) |
62 | { |
63 | stream << data.enable; |
64 | stream << data.environments; |
65 | |
66 | return stream; |
67 | } |
68 | |
69 | friend QDataStream &operator>>(QDataStream &stream, EnvironmentItem &data) |
70 | { |
71 | stream >> data.enable; |
72 | stream >> data.environments; |
73 | |
74 | return stream; |
75 | } |
76 | }; |
77 | |
78 | struct RunParam { |
79 | QString targetName; |
80 | QString targetPath; |
81 | QString arguments; |
82 | QString workDirectory; |
83 | EnvironmentItem env; |
84 | |
85 | friend QDataStream &operator<<(QDataStream &stream, const RunParam &data) |
86 | { |
87 | stream << data.targetName; |
88 | stream << data.targetPath; |
89 | stream << data.arguments; |
90 | stream << data.workDirectory; |
91 | stream << data.env; |
92 | |
93 | return stream; |
94 | } |
95 | |
96 | friend QDataStream &operator>>(QDataStream &stream, RunParam &data) |
97 | { |
98 | stream >> data.targetName; |
99 | stream >> data.targetPath; |
100 | stream >> data.arguments; |
101 | stream >> data.workDirectory; |
102 | stream >> data.env; |
103 | |
104 | return stream; |
105 | } |
106 | }; |
107 | |
108 | struct RunConfigure { |
109 | QString defaultTargetName; |
110 | QVector<RunParam> params; |
111 | |
112 | friend QDataStream &operator<<(QDataStream &stream, const RunConfigure &data) |
113 | { |
114 | stream << data.defaultTargetName; |
115 | stream << data.params; |
116 | |
117 | return stream; |
118 | } |
119 | |
120 | friend QDataStream &operator>>(QDataStream &stream, RunConfigure &data) |
121 | { |
122 | stream >> data.defaultTargetName; |
123 | stream >> data.params; |
124 | |
125 | return stream; |
126 | } |
127 | }; |
128 | |
129 | struct BuildConfigure { |
130 | ConfigType type = Unknown; |
131 | QString directory; |
132 | QVector<StepItem> steps; |
133 | EnvironmentItem env; |
134 | RunConfigure runConfigure; |
135 | |
136 | friend QDataStream &operator<<(QDataStream &stream, const BuildConfigure &data) |
137 | { |
138 | stream << data.type; |
139 | stream << data.directory; |
140 | stream << data.steps; |
141 | stream << data.env; |
142 | stream << data.runConfigure; |
143 | |
144 | return stream; |
145 | } |
146 | |
147 | friend QDataStream &operator>>(QDataStream &stream, BuildConfigure &data) |
148 | { |
149 | int type = 0; |
150 | stream >> type; |
151 | data.type = static_cast<ConfigType>(type); |
152 | stream >> data.directory; |
153 | stream >> data.steps; |
154 | stream >> data.env; |
155 | stream >> data.runConfigure; |
156 | |
157 | return stream; |
158 | } |
159 | }; |
160 | |
161 | struct ConfigureParam { |
162 | QString kit; |
163 | QString language; |
164 | QString workspace; |
165 | ConfigType defaultType = Unknown; |
166 | ConfigType tempSelType = Unknown; |
167 | QVector<BuildConfigure> buildConfigures; |
168 | |
169 | friend QDataStream &operator<<(QDataStream &stream, const ConfigureParam &data) |
170 | { |
171 | stream << data.kit; |
172 | stream << data.language; |
173 | stream << data.workspace; |
174 | int type = data.defaultType; |
175 | stream << type; |
176 | type = data.tempSelType; |
177 | stream << type; |
178 | stream << data.buildConfigures; |
179 | |
180 | return stream; |
181 | } |
182 | |
183 | friend QDataStream &operator>>(QDataStream &stream, ConfigureParam &data) |
184 | { |
185 | stream >> data.kit; |
186 | stream >> data.language; |
187 | stream >> data.workspace; |
188 | int type = 0; |
189 | stream >> type; |
190 | data.defaultType = static_cast<ConfigType>(type); |
191 | stream >> type; |
192 | data.tempSelType = static_cast<ConfigType>(type); |
193 | stream >> data.buildConfigures; |
194 | |
195 | return stream; |
196 | } |
197 | |
198 | void clear() |
199 | { |
200 | kit.clear(); |
201 | language.clear(); |
202 | workspace.clear(); |
203 | defaultType = Unknown; |
204 | tempSelType = Unknown; |
205 | buildConfigures.clear(); |
206 | } |
207 | }; |
208 | |
209 | class ConfigUtilPrivate; |
210 | class ConfigUtil final: public QObject |
211 | { |
212 | Q_OBJECT |
213 | public: |
214 | static ConfigUtil *instance(); |
215 | |
216 | ConfigUtil(const ConfigUtil &) = delete; |
217 | ConfigUtil &operator=(const ConfigUtil &) = delete; |
218 | |
219 | QString getConfigPath(const QString &workspace); |
220 | |
221 | ConfigureParam *getConfigureParamPointer(); |
222 | |
223 | QString getNameFromType(ConfigType type); |
224 | ConfigType getTypeFromName(QString name); |
225 | |
226 | dpfservice::ProjectInfo createProjectInfo(const ConfigureParam *param); |
227 | bool isNeedConfig(const QString &workspace, ConfigureParam ¶m); |
228 | void checkConfigInfo(const QString &buildType, const QString &directory); |
229 | void configProject(const ConfigureParam *param); |
230 | |
231 | void readConfig(const QString &filePath, ConfigureParam ¶m); |
232 | void saveConfig(const QString &filePath, const ConfigureParam ¶m); |
233 | bool updateProjectInfo(dpfservice::ProjectInfo &info, const ConfigureParam *param); |
234 | signals: |
235 | void configureDone(const dpfservice::ProjectInfo &info); |
236 | |
237 | private: |
238 | explicit ConfigUtil(QObject *parent = nullptr); |
239 | ~ConfigUtil(); |
240 | |
241 | ConfigUtilPrivate *const d; |
242 | }; |
243 | |
244 | } //namespace config |
245 | |
246 | #endif // CONFIGUTIL_H |
247 | |