cargo-crapapp 0.2.0

Cargo subcommand for building CRAP app installers and self-contained app bundles
Documentation
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
use std::fs;
use std::path::{Path, PathBuf};
use std::process::Command;

use anyhow::{Context, Result, bail};
use image::ImageReader;
use minijinja::{Environment, context};
use serde_json::{Value, json};

use crate::services::build_manifest::BuildManifest;
use crate::services::manifest_file::{EulaFile, WindowsInstaller};
use crate::services::payload_file::PayloadFile;
use crate::services::platform_manifest::{PlatformManifest, WindowsPlatformManifest};
use crate::services::target_manifest::Shortcut;

const SETUP_CARGO_TOML: &str = include_str!("../../assets/windows-installer/Cargo.toml.j2");
const SETUP_BUILD_RS: &str = include_str!("../../assets/windows-installer/build.rs.j2");
const SETUP_RS: &str = include_str!("../../assets/windows-installer/main.rs.j2");
const UNINSTALL_RS: &str = include_str!("../../assets/windows-installer/uninstall.rs.j2");
const INSTALL_ICON: &[u8] = include_bytes!("../../assets/windows-installer/install.ico");
const LIBCRAPAPP_VERSION: &str = "0.2.0";
const SETUP_CONFIG: &str = "setup-config.json";
const DISPLAY_ICON_SIZE: u32 = 256;

pub struct WindowsBundler<'a, M: PlatformManifest> {
    build_manifest: &'a BuildManifest<M>,
    build_dir: &'a Path,
}

impl<'a, M: PlatformManifest> WindowsBundler<'a, M> {
    pub fn new(build_manifest: &'a BuildManifest<M>, build_dir: &'a Path) -> Self {
        Self {
            build_manifest,
            build_dir,
        }
    }

    pub fn bundle(&self) -> Result<()> {
        let windows = self.windows_platform()?;

        for target in &windows.targets {
            let output_dir = self.build_dir.join(&windows.platform).join(&target.target);
            let setup_source_dir = output_dir.join("setup-src");
            let setup_output = output_dir.join("setup.exe");

            remove_dir_if_exists(&setup_source_dir)?;
            fs::create_dir_all(&output_dir)
                .with_context(|| format!("failed to create {}", output_dir.display()))?;
            fs::create_dir_all(setup_source_dir.join("src")).with_context(|| {
                format!(
                    "failed to create setup project at {}",
                    setup_source_dir.display()
                )
            })?;

            self.write_setup_project(windows, &target.files, &target.shortcuts, &setup_source_dir)?;
            self.build_uninstaller(&target.target, &setup_source_dir)?;
            self.write_setup_rs_with_uninstaller(windows, &target.target, &setup_source_dir)?;
            self.build_setup(&target.target, &setup_source_dir)?;
            self.copy_setup_output(&target.target, &setup_source_dir, &setup_output)?;
            self.clean_setup_source(&setup_source_dir)?;
        }

        Ok(())
    }

    fn windows_platform(&self) -> Result<&WindowsPlatformManifest> {
        self.build_manifest
            .platforms
            .iter()
            .find_map(|platform| platform.as_windows())
            .context("windows platform is not configured")
    }

    fn write_setup_project(
        &self,
        platform: &WindowsPlatformManifest,
        files: &[PayloadFile],
        shortcuts: &[Shortcut],
        setup_source_dir: &Path,
    ) -> Result<()> {
        if files.is_empty() {
            bail!("windows bundle has no files to package");
        }

        fs::write(
            setup_source_dir.join("Cargo.toml"),
            setup_cargo_toml(platform)?,
        )
        .with_context(|| "failed to write setup Cargo.toml")?;
        fs::write(
            setup_source_dir.join("build.rs"),
            render_static_template("build.rs.j2", SETUP_BUILD_RS)?,
        )
        .with_context(|| "failed to write setup build.rs")?;
        self.write_setup_build_input(platform, files, shortcuts, None, setup_source_dir)?;
        write_setup_assets(setup_source_dir, platform.display_icon_source.as_deref())?;
        fs::write(
            setup_source_dir.join("src").join("main.rs"),
            render_setup_template("main.rs.j2", SETUP_RS, platform)?,
        )
        .with_context(|| "failed to write setup main.rs")?;
        fs::write(
            setup_source_dir.join("src").join("uninstall.rs"),
            render_setup_template("uninstall.rs.j2", UNINSTALL_RS, platform)?,
        )
        .with_context(|| "failed to write uninstall.rs")?;

        Ok(())
    }

    fn build_uninstaller(&self, target: &str, setup_source_dir: &Path) -> Result<()> {
        remove_dir_if_exists(&setup_source_dir.join("target"))?;

        let status = Command::new("cargo")
            .arg("build")
            .arg("--release")
            .arg("--target")
            .arg(target)
            .arg("--bin")
            .arg("uninstall")
            .current_dir(setup_source_dir)
            .status()
            .with_context(|| format!("failed to build uninstall.exe for {target}"))?;

        if !status.success() {
            bail!("uninstall.exe build failed for {target}");
        }

        Ok(())
    }

    fn write_setup_rs_with_uninstaller(
        &self,
        platform: &WindowsPlatformManifest,
        target: &str,
        setup_source_dir: &Path,
    ) -> Result<()> {
        let uninstaller = fs::canonicalize(release_exe_path(target, setup_source_dir, "uninstall"))
            .with_context(|| format!("failed to find built uninstall.exe for {target}"))?;
        let embedded_uninstaller = setup_source_dir.join("assets").join("uninstall.exe");
        fs::copy(&uninstaller, &embedded_uninstaller).with_context(|| {
            format!(
                "failed to copy {} to {}",
                uninstaller.display(),
                embedded_uninstaller.display()
            )
        })?;
        let embedded_uninstaller = fs::canonicalize(&embedded_uninstaller).with_context(|| {
            format!(
                "failed to find copied uninstall.exe at {}",
                embedded_uninstaller.display()
            )
        })?;

        remove_dir_if_exists(&setup_source_dir.join("target"))?;

        let target_manifest = platform
            .targets
            .iter()
            .find(|target_manifest| target_manifest.target == target)
            .context("failed to find target manifest")?;

        fs::write(
            setup_source_dir.join("build.rs"),
            render_static_template("build.rs.j2", SETUP_BUILD_RS)?,
        )
        .with_context(|| "failed to write setup build.rs with embedded uninstaller payload")?;

        self.write_setup_build_input(
            platform,
            &target_manifest.files,
            &target_manifest.shortcuts,
            Some(&embedded_uninstaller),
            setup_source_dir,
        )?;

        Ok(())
    }

    fn write_setup_build_input(
        &self,
        platform: &WindowsPlatformManifest,
        files: &[PayloadFile],
        shortcuts: &[Shortcut],
        uninstaller_source: Option<&Path>,
        setup_source_dir: &Path,
    ) -> Result<()> {
        let setup_config = json!({
            "app_name": self.build_manifest.app_name,
            "app_version": self.build_manifest.version,
            "display_name": self.build_manifest.build.display_name,
            "publisher": self.build_manifest.build.publisher,
            "variables": platform.variables.iter().map(ToString::to_string).collect::<Vec<_>>(),
            "uninstaller_source": uninstaller_source
                .map(|path| path.display().to_string())
                .unwrap_or_default(),
            "payload": files
                .iter()
                .map(setup_payload_file)
                .collect::<Result<Vec<_>>>()?,
            "display_icon": platform.display_icon,
            "display_icon_rgba": setup_display_icon(platform.display_icon_source.as_deref())?,
            "associated_files": platform.associated_files,
            "shortcuts": shortcuts,
            "eulas": platform.eulas
                .iter()
                .map(setup_eula_file)
                .collect::<Result<Vec<_>>>()?,
        });

        fs::write(
            setup_source_dir.join(SETUP_CONFIG),
            serde_json::to_string_pretty(&setup_config)
                .context("failed to serialize setup config")?,
        )
        .with_context(|| "failed to write setup config")?;

        Ok(())
    }

    fn build_setup(&self, target: &str, setup_source_dir: &Path) -> Result<()> {
        remove_dir_if_exists(&setup_source_dir.join("target"))?;

        let status = Command::new("cargo")
            .arg("build")
            .arg("--release")
            .arg("--target")
            .arg(target)
            .arg("--bin")
            .arg("setup")
            .current_dir(setup_source_dir)
            .status()
            .with_context(|| format!("failed to build setup.exe for {target}"))?;

        if !status.success() {
            bail!("setup.exe build failed for {target}");
        }

        Ok(())
    }

    fn copy_setup_output(
        &self,
        target: &str,
        setup_source_dir: &Path,
        output_file: &Path,
    ) -> Result<()> {
        copy_release_exe(target, setup_source_dir, output_file, "setup")
    }

    fn clean_setup_source(&self, setup_source_dir: &Path) -> Result<()> {
        fs::remove_dir_all(setup_source_dir).with_context(|| {
            format!(
                "failed to remove generated setup project {}",
                setup_source_dir.display()
            )
        })
    }
}

fn remove_dir_if_exists(path: &Path) -> Result<()> {
    if path.exists() {
        fs::remove_dir_all(path).with_context(|| format!("failed to remove {}", path.display()))?;
    }

    Ok(())
}

fn copy_release_exe(
    target: &str,
    setup_source_dir: &Path,
    output_file: &Path,
    name: &str,
) -> Result<()> {
    let source = release_exe_path(target, setup_source_dir, name);
    let destination = if output_file.extension().is_some() {
        output_file.to_path_buf()
    } else {
        output_file.join(format!("{name}.exe"))
    };

    fs::copy(&source, &destination).with_context(|| {
        format!(
            "failed to copy {} to {}",
            source.display(),
            destination.display()
        )
    })?;

    Ok(())
}

fn release_exe_path(target: &str, setup_source_dir: &Path, name: &str) -> PathBuf {
    setup_source_dir
        .join("target")
        .join(target)
        .join("release")
        .join(format!("{name}.exe"))
}

fn setup_cargo_toml(platform: &WindowsPlatformManifest) -> Result<String> {
    let dependencies = libcrapapp_dependency(platform)?;
    let environment = Environment::new();
    let template = environment
        .template_from_str(SETUP_CARGO_TOML)
        .context("failed to parse setup Cargo.toml template")?;

    template
        .render(context! {
            libcrapapp_dependency => dependencies.runtime,
            libcrapapp_build_dependency => dependencies.build,
        })
        .context("failed to render setup Cargo.toml template")
}

fn render_static_template(name: &str, source: &str) -> Result<String> {
    let environment = Environment::new();
    let template = environment
        .template_from_str(source)
        .with_context(|| format!("failed to parse {name} template"))?;

    template
        .render(context! {})
        .with_context(|| format!("failed to render {name} template"))
}

fn render_setup_template(
    name: &str,
    source: &str,
    platform: &WindowsPlatformManifest,
) -> Result<String> {
    let environment = Environment::new();
    let template = environment
        .template_from_str(source)
        .with_context(|| format!("failed to parse {name} template"))?;

    template
        .render(context! {
            gui_installer => matches!(
                platform.installer.unwrap_or_default(),
                WindowsInstaller::Gui
            ),
        })
        .with_context(|| format!("failed to render {name} template"))
}

fn libcrapapp_dependency(platform: &WindowsPlatformManifest) -> Result<LibcrapappDependency> {
    let manifest_dir = Path::new(env!("CARGO_MANIFEST_DIR"));
    let source = if manifest_dir.join("src").join("lib.rs").is_file() {
        let manifest_dir = fs::canonicalize(manifest_dir)
            .with_context(|| format!("failed to find {}", manifest_dir.display()))?;

        format!(
            r#"package = "cargo-crapapp", path = "{}""#,
            manifest_dir.display()
        )
    } else {
        format!(r#"package = "cargo-crapapp", version = "{LIBCRAPAPP_VERSION}""#)
    };
    let feature = platform.installer.unwrap_or_default().cargo_feature();

    Ok(LibcrapappDependency {
        runtime: format!(r#"{{ {source}, default-features = false, features = ["{feature}"] }}"#),
        build: format!(r#"{{ {source}, default-features = false }}"#),
    })
}

struct LibcrapappDependency {
    runtime: String,
    build: String,
}

fn write_setup_assets(setup_source_dir: &Path, _display_icon_source: Option<&str>) -> Result<()> {
    let assets_dir = setup_source_dir.join("assets");
    fs::create_dir_all(&assets_dir)
        .with_context(|| format!("failed to create {}", assets_dir.display()))?;
    let install_icon = assets_dir.join("install.ico");

    fs::write(&install_icon, INSTALL_ICON).with_context(|| "failed to write setup install icon")?;

    Ok(())
}

fn setup_payload_file(file: &PayloadFile) -> Result<Value> {
    let source = fs::canonicalize(&file.source)
        .with_context(|| format!("failed to find payload source {}", &file.source))?;

    Ok(json!({
        "source": source.display().to_string(),
        "destination": file.destination,
        "executable": file.executable,
    }))
}

fn setup_eula_file(eula: &EulaFile) -> Result<Value> {
    let path = Path::new(eula.path());
    let text = fs::read_to_string(path)
        .with_context(|| format!("failed to read EULA file {}", path.display()))?;
    let name = path
        .file_name()
        .and_then(|name| name.to_str())
        .unwrap_or_else(|| eula.path());

    Ok(json!({
        "name": name,
        "text": text,
        "required": eula.required(),
    }))
}

fn setup_display_icon(source: Option<&str>) -> Result<Option<Value>> {
    let Some(source) = source else {
        return Ok(None);
    };
    let path = Path::new(source);
    let supported = path
        .extension()
        .and_then(|extension| extension.to_str())
        .is_some_and(|extension| {
            extension.eq_ignore_ascii_case("ico") || extension.eq_ignore_ascii_case("png")
        });

    if !supported {
        return Ok(None);
    }

    let icon = ImageReader::open(path)
        .with_context(|| format!("failed to open display icon {}", path.display()))?
        .decode()
        .with_context(|| format!("failed to decode display icon {}", path.display()))?
        .resize_exact(
            DISPLAY_ICON_SIZE,
            DISPLAY_ICON_SIZE,
            image::imageops::FilterType::Lanczos3,
        )
        .to_rgba8();

    Ok(Some(json!({
        "width": DISPLAY_ICON_SIZE,
        "height": DISPLAY_ICON_SIZE,
        "rgba": icon.into_raw(),
    })))
}