1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
use crate::prelude::*;
use async_std::{
fs,
path::{Path, PathBuf},
};
use console::style;
use std::time::Instant;
pub struct Builder {
pub ctx: Arc<Context>,
// #[cfg(feature = "unix")]
// platform : Platform,
}
impl Builder {
// pub fn new(ctx: Arc<Context>, platform : Platform) -> Self {
pub fn new(ctx: Arc<Context>) -> Self {
Builder {
ctx,
// platform
}
}
#[allow(clippy::borrowed_box)]
pub async fn execute(
self: &Arc<Self>,
targets: &TargetSet,
// installer: &Box<dyn Installer>,
installer: &Box<dyn Installer>,
) -> Result<()> {
// println!("{:#?}", self.ctx.manifest);
// return Ok(());
if targets.is_empty() {
return Err("no build targets selected".into());
}
installer.check(targets).await?;
let tpl = installer.tpl();
self.ctx.clean().await?;
self.ctx.deps.ensure().await?;
self.ctx.ensure_folders().await?;
installer.init(targets).await?;
// let installer = create_installer(&self.ctx);
self.process_dependencies(&tpl, installer.target_folder())
.await?;
// return Ok(());
if let Some(builds) = &self.ctx.manifest.package.build {
log_info!("Build", "building...");
println!();
for build in builds.iter() {
match build {
Build::WASM {
clean,
purge,
name,
outdir,
args,
dev,
env,
} => {
// invoke cargo clean
if clean.unwrap_or(false) {
log_info!("WasmPack", "cargo clean");
cmd!("cargo clean").dir(&self.ctx.app_root_folder).run()?;
}
// delete the entire target folder
if purge.unwrap_or(false) && self.ctx.cargo_target_folder.exists().await {
log_info!("WasmPack", "purging target folder");
fs::remove_dir_all(&self.ctx.cargo_target_folder).await?;
}
let outdir = outdir.clone().unwrap_or_else(|| "app/wasm".to_string());
let name = name.as_ref().unwrap_or(&self.ctx.manifest.application.name);
let mut argv = vec!["wasm-pack", "build"];
if dev.unwrap_or(false) {
argv.push("--dev");
}
argv.extend_from_slice(&[
"--target",
"web",
"--out-name",
name.as_str(),
"--out-dir",
outdir.as_str(),
]);
if let Some(args) = args {
argv.extend(args.split(' ').collect::<Vec<_>>());
}
log_info!("WasmPack", "building WASM target");
execute(
&self.ctx,
&argv.into(),
&self.ctx.app_root_folder,
env,
&None,
&None,
&None,
&tpl,
)
.await?;
}
Build::NPM {
clean,
clean_package_lock,
args,
dev,
env,
} => {
let node_modules_folder = self.ctx.app_root_folder.join("node_modules");
if clean.unwrap_or(false) && node_modules_folder.exists().await {
log_info!("NPM", "removing node_modules folder");
fs::remove_dir_all(&node_modules_folder).await?;
}
let package_lock_file = self.ctx.app_root_folder.join("package-lock.json");
if clean_package_lock.unwrap_or(false) && package_lock_file.exists().await {
log_info!("NPM", "removing NPM package lock");
fs::remove_file(&package_lock_file).await?;
}
let mut argv = vec!["npm", "install"];
if !dev.unwrap_or(false) {
argv.extend_from_slice(&["--omit", "dev"]);
}
if let Some(args) = args {
argv.extend(args.split(' ').collect::<Vec<_>>());
}
log_info!("NPM", "installing");
// let argv = argv.iter().map(|s|s.to_string()).collect();
execute(
&self.ctx,
&argv.into(),
&self.ctx.app_root_folder,
env,
&None,
&None,
&None,
&tpl,
)
.await?;
}
Build::Custom(ec) => {
log_info!("Build", "executing `{}`", ec.display(&tpl));
execute_with_context(&self.ctx, ec, None, &tpl).await?;
}
}
}
println!();
}
let ts_start = Instant::now();
log_info!(
"Build",
"building {} version {}",
style(&self.ctx.manifest.application.title).cyan(),
style(&self.ctx.manifest.application.version).cyan()
);
let target_list = targets
.iter()
.map(|v| v.to_string())
.collect::<Vec<String>>()
.join(", ");
log_info!(
"Build",
"redistributable type: {}",
style(target_list).cyan()
);
let target_folder = installer.target_folder();
// self.execute_actions(Stage::Build, &installer).await?;
execute_actions(Stage::Build, &self.ctx, &tpl, &target_folder).await?;
// if let Some(actions) = &self.ctx.manifest.action {
// let actions = actions
// .iter()
// .filter(|action|action.stage.map(|stage|stage == Stage::Build).unwrap_or(false))
// .collect::<Vec<_>>();
// for action in actions {
// action.execute(&self.ctx,&self.ctx.tpl(),&self.ctx.project_root_folder,&target_folder).await?;
// }
// }
// installer execution
let files = installer.create(targets).await?;
if self.ctx.dry_run {
log_warn!("Integration", "dry-run completed successfully");
return Ok(());
} else if files.is_empty() {
return Err(Error::Warning("build produced no output".into()));
}
let duration = ts_start.elapsed();
let files: Vec<(_, _)> = files
.iter()
.map(|f| (f.file_name().unwrap().to_owned(), f))
.collect();
if let Some(signatures) = &self.ctx.manifest.package.signatures {
log_info!("Build", "generating signatures (SHA)");
for (_, path) in files.iter() {
generate_signatures(path, signatures).await?;
}
}
for (_file, path) in files.iter() {
let package_size = (std::fs::metadata(path)?.len() as f64) / 1024.0 / 1024.0;
let path = if let Ok(path) = path.strip_prefix(&self.ctx.root_folder) {
path
} else {
path
};
log_info!(
"Package",
"{} - {}",
style(path.to_str().unwrap()).cyan(),
style(format!("{package_size:.2}Mb")).cyan()
);
}
log_info!(
"Finished",
"build completed in {:.0}s",
duration.as_millis() as f64 / 1000.0
);
// let target_folder = installer.target_folder();
execute_actions(Stage::Deploy, &self.ctx, &tpl, &target_folder).await?;
// self.execute_actions(Stage::Deploy, &installer).await?;
// self.execute_actions(Stage::Deploy, &target_folder,&target_folder).await?;
// if let Some(actions) = &self.ctx.manifest.action {
// let actions = actions
// .iter()
// .filter(|action|action.stage.map(|stage|stage == Stage::Deploy).unwrap_or(false))
// .collect::<Vec<_>>();
// for action in actions {
// action.execute(&self.ctx,&self.ctx.tpl(),&self.ctx.project_root_folder,&target_folder).await?;
// }
// }
println!();
Ok(())
}
async fn process_dependencies(&self, tpl: &Tpl, target_folder: PathBuf) -> Result<()> {
if let Some(deps) = &self.ctx.manifest.dependencies {
fs::create_dir_all(&self.ctx.dependencies_folder).await?;
for dep in deps.iter() {
self.process_dependency(dep, tpl, &target_folder).await?;
}
}
Ok(())
}
async fn process_dependency(
&self,
dep: &Dependency,
tpl: &Tpl,
target_folder: &Path,
) -> Result<()> {
let mut name = dep.name.clone(); //.map_or("dependency".to_string(),|n|n);
if let Some(platform) = &dep.platform {
if !platform.contains(&self.ctx.platform) {
log_info!(
"Dependency",
"skipping `{}` on platform `{}`",
name.unwrap_or_else(|| "dependency".to_string()),
self.ctx.platform.to_string()
);
return Ok(());
}
}
if let Some(arch) = &dep.arch {
if !arch.contains(&self.ctx.arch) {
log_info!(
"Dependency",
"skipping `{}` on arch `{}`",
name.unwrap_or_else(|| "dependency".to_string()),
self.ctx.arch.to_string()
);
return Ok(());
}
}
let (rebuild, dep_build_folder, status) = if let Some(git) = &dep.git {
let repo = Path::new(&git.url).file_name().unwrap().to_str().unwrap();
let repo_folder = self.ctx.dependencies_folder.join(repo);
let status_file = self.ctx.dependencies_folder.join(format!("{repo}.status"));
name = name.or_else(|| Some(repo.to_string()));
if repo_folder.is_dir().await {
log_info!("Git", "pulling `{}`", name.as_ref().unwrap());
cmd("git", ["pull"]).dir(&repo_folder).run()?;
} else {
let args = if let Some(branch) = &git.branch {
log_info!("Git", "cloning `{}` ({})", name.as_ref().unwrap(), branch);
vec!["clone", "-b", branch.as_str(), &git.url]
} else {
log_info!("Git", "cloning `{}`", name.as_ref().unwrap());
vec!["clone", &git.url]
};
cmd("git", args).dir(&self.ctx.dependencies_folder).run()?;
}
let status_data = cmd("git", ["show", "--summary"]).dir(&repo_folder).read()?;
if status_file.exists().await {
let last_status_data = fs::read_to_string(&status_file).await?;
let rebuild = status_data != last_status_data;
(rebuild, repo_folder, Some((status_file, status_data)))
} else {
(true, repo_folder, Some((status_file, status_data)))
}
} else {
(true, self.ctx.dependencies_folder.clone(), None)
};
let name = name.unwrap_or_else(|| "...".into());
if rebuild {
log_info!("Dependency", "building `{}`", name);
for ec in dep.run.iter() {
execute_with_context(&self.ctx, ec, Some(dep_build_folder.as_path()), tpl).await?;
}
} else {
log_info!("Dependency", "skipping `{}` (build is up to date)", name);
}
// let tpl = self.ctx.tpl_clone();
for copy_settings in dep.copy.iter() {
copy(tpl, copy_settings, &dep_build_folder, target_folder).await?;
}
if let Some((status_file, status_data)) = status {
fs::write(status_file, status_data).await?;
}
Ok(())
}
}