cargo-nw 0.1.1

Cargo subcommand for building NW application deployment packages (redistributables) for Windows, MacOS and Linux.
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
mod innosetup;
mod iss;

use crate::prelude::*;
use async_std::fs;
use async_std::path::Path;
use async_std::path::PathBuf;
use chrono::Datelike;
use console::style;
use fs_extra::dir;
use image::imageops::FilterType;
use image::GenericImageView;
use std::collections::HashMap;
use winres_edit::*;

pub struct Windows {
    ctx: Arc<Context>,
    tpl: Tpl,
    // nwjs_root_folder: PathBuf,
    target_folder: PathBuf,
    setup_icon_file: PathBuf,
    pub app_exe_file: String,
}

impl Windows {
    pub fn new(ctx: Arc<Context>) -> Windows {
        let nwjs_root_folder = ctx.build_folder.join(&ctx.app_snake_name);
        // let nwjs_root_folder = ctx.build_folder.clone(); // ctx.build_folder.join(&ctx.manifest.application.title);
        let target_folder = if ctx.manifest.package.use_app_nw.unwrap_or(false) {
            nwjs_root_folder
        } else {
            nwjs_root_folder.join("app.nw")
        };
        let app_name = ctx.manifest.application.name.clone();

        let setup_icon_file = if let Some(crate::manifest::Windows {
            setup_icon: Some(setup_icon),
            ..
        }) = &ctx.manifest.windows
        {
            ctx.app_root_folder.join(setup_icon)
        } else {
            ctx.cache_folder.join(format!("{app_name}-setup.ico"))
        };

        let app_exe_file = match ctx.manifest.windows {
            Some(crate::manifest::Windows {
                executable: Some(ref executable),
                ..
            }) => executable.clone(),
            _ => {
                format!("{}.exe", ctx.manifest.application.name)
            }
        };

        let tpl = create_installer_tpl(&ctx, &target_folder);

        Windows {
            ctx,
            tpl,
            app_exe_file,
            // nwjs_root_folder,
            target_folder,
            setup_icon_file,
        }
    }
}

#[async_trait]
impl Installer for Windows {
    async fn init(&self, _targets: &TargetSet) -> Result<()> {
        Ok(())
    }

    async fn check(&self, targets: &TargetSet) -> Result<()> {
        if targets.contains(&Target::InnoSetup)
            && !std::path::Path::new(iss::INNO_SETUP_COMPIL32).exists()
        {
            println!();
            println!("fatal: unable to locate: `{}`", iss::INNO_SETUP_COMPIL32);
            println!("please download innosetup 6 at:");
            println!("https://jrsoftware.org/isdl.php");
            println!();
            return Err("missing InnoSetup compiler".into());
        }

        Ok(())
    }

    async fn create(&self, targets: &TargetSet) -> Result<Vec<PathBuf>> {
        self.copy_nwjs_folder().await?;
        self.copy_app_data().await?;
        self.update_resources().await?;

        // let tpl = create_installer_tpl(
        //     &self.ctx,
        //     &self.nwjs_root_folder
        // );

        execute_actions(Stage::Package, &self.ctx, &self.tpl, &self.target_folder).await?;

        let mut files = Vec::new();

        if !self.ctx.dry_run && targets.contains(&Target::Archive) {
            log_info!("Windows", "creating archive");

            // let filename = Path::new(&format!("{}.zip",self.ctx.app_snake_name)).to_path_buf();
            let level = self
                .ctx
                .manifest
                .package
                .archive
                .clone()
                .unwrap_or_default();
            let filename = Path::new(&format!("{}.zip", self.ctx.app_snake_name)).to_path_buf();
            let target_file = self.ctx.output_folder.join(filename);
            compress_folder(&self.target_folder, &target_file, level)?;

            files.push(target_file);
            // files.push(filename);
        }

        #[cfg(any(target_os = "windows", feature = "multiplatform"))]
        if !self.ctx.dry_run && targets.contains(&Target::InnoSetup) {
            self.create_innosetup_icon(&self.setup_icon_file).await?;
            let wizard_image_files = self.create_innosetup_images().await?;

            let setup_script = iss::ISS::new(
                self.ctx.clone(),
                self.target_folder.clone(),
                self.setup_icon_file.clone(),
                wizard_image_files,
            );

            let filename = setup_script.create().await?;
            files.push(filename);
        }

        Ok(files)
    }

    fn tpl(&self) -> Tpl {
        self.tpl.clone()
    }

    fn target_folder(&self) -> PathBuf {
        self.target_folder.clone()
    }
}

impl Windows {
    async fn copy_nwjs_folder(&self) -> Result<()> {
        let mut options = dir::CopyOptions::new();
        options.content_only = true;
        options.skip_exist = true;

        log_info!("Integrating", "NW binaries");

        dir::copy(
            Path::new(&self.ctx.deps.nwjs.target()),
            &self.target_folder,
            &options,
        )?;

        fs::rename(
            self.target_folder.join("nw.exe"),
            self.target_folder.join(&self.app_exe_file),
        )
        .await?;

        if self.ctx.manifest.nwjs.ffmpeg.unwrap_or(false) {
            log_info!("Integrating", "FFMPEG binaries");
            fs::copy(
                Path::new(&self.ctx.deps.ffmpeg.as_ref().unwrap().target()).join("ffmpeg.dll"),
                self.target_folder.join("ffmpeg.dll"),
            )
            .await?;
        }

        Ok(())
    }

    async fn copy_app_data(&self) -> Result<()> {
        log_info!("Integrating", "application data");

        // let tpl = self.ctx.tpl_clone();
        copy_folder_with_filters(
            &self.ctx.app_root_folder,
            &self.target_folder,
            (&self.tpl, &self.ctx.include, &self.ctx.exclude).try_into()?,
            CopyOptions::new(self.ctx.manifest.package.hidden.unwrap_or(false)),
        )
        .await?;

        self.ctx.update_package_json(&self.target_folder).await?;

        Ok(())
    }

    fn get_resource_strings(&self) -> Vec<(String, String)> {
        let windows = self.ctx.manifest.windows.as_ref();
        let application = &self.ctx.manifest.application;
        let description = &self.ctx.manifest.description;

        let mut list: HashMap<&str, String> = [
            ("ProductVersion", &application.version),
            ("ProductName", &application.title),
            ("FileVersion", &application.version),
            ("FileDescription", &description.short),
            ("InternalName", &application.title),
            ("CompanyName", &application.organization),
            (
                "LegalCopyright",
                &format!(
                    "Copyright © {} {}",
                    chrono::Utc::now().year(),
                    application.organization
                ),
            ),
        ]
        .into_iter()
        .map(|(k, v)| (k, v.to_string()))
        .collect();

        if let Some(copyright) = &application.copyright {
            list.insert("LegalCopyright", copyright.into());
        }

        list.insert("OriginalFilename", self.app_exe_file.clone());

        if let Some(crate::manifest::Windows {
            resources: Some(resources),
            ..
        }) = windows
        {
            for resource in resources {
                match resource {
                    WindowsResourceString::ProductName(value) => {
                        list.insert("ProductName", value.into());
                    }
                    WindowsResourceString::ProductVersion(value) => {
                        list.insert("ProductVersion", value.into());
                    }
                    WindowsResourceString::FileVersion(value) => {
                        list.insert("FileVersion", value.into());
                    }
                    WindowsResourceString::FileDescription(value) => {
                        list.insert("FileDescription", value.into());
                    }
                    WindowsResourceString::CompanyName(value) => {
                        list.insert("CompanyName", value.into());
                    }
                    WindowsResourceString::LegalCopyright(value) => {
                        list.insert("LegalCopyright", value.into());
                    }
                    WindowsResourceString::LegalTrademarks(value) => {
                        list.insert("LegalTrademarks", value.into());
                    }
                    WindowsResourceString::InternalName(value) => {
                        list.insert("InternalName", value.into());
                    }
                    WindowsResourceString::Custom { name, value } => {
                        list.insert(name, value.into());
                    }
                }
            }
        }

        list.into_iter()
            .map(|(k, v)| (self.tpl.transform(k), self.tpl.transform(&v)))
            .collect()
    }

    async fn create_innosetup_images(&self) -> Result<(Vec<PathBuf>, Vec<PathBuf>)> {
        log_info!("InnoSetup", "generating wizard image files");

        let mut small_files = Vec::new();
        let small_file_bmp = self.ctx.cache_folder.join("innosetup-wizard-small.bmp");
        let small_file_png = self
            .ctx
            .setup_resources_folder
            .join("innosetup-wizard-small.png");
        let mut small_src = image::open(&small_file_png)
            .unwrap_or_else(|err| panic!("Unable to open '{}': {err}", small_file_png.display()));
        if !small_file_bmp.exists().await {
            small_src.save(&small_file_bmp).unwrap_or_else(|err| {
                panic!("Unable to save '{}': {err}", small_file_bmp.display())
            });
        }
        small_files.push(small_file_bmp.clone());

        let mut large_files = Vec::new();
        let large_file_bmp = self.ctx.cache_folder.join("innosetup-wizard-large.bmp");
        let large_file_png = self
            .ctx
            .setup_resources_folder
            .join("innosetup-wizard-large.png");
        let mut large_src = image::open(&large_file_png)
            .unwrap_or_else(|err| panic!("Unable to open '{}': {err}", large_file_png.display()));
        if !large_file_bmp.exists().await {
            large_src.save(&large_file_bmp).unwrap_or_else(|err| {
                panic!("Unable to save '{}': {err}", large_file_bmp.display())
            });
        }
        large_files.push(large_file_bmp.clone());

        if let Some(innosetup) = &self.ctx.manifest.innosetup {
            if innosetup.resize_wizard_files.unwrap_or(false) {
                return Ok((small_files, large_files));
            }
        }

        // https://jrsoftware.org/ishelp/index.php?topic=setup_wizardsmallimagefile
        let small_resolutions = [
            (138, 140),
            (119, 123),
            (110, 106),
            (92, 97),
            (83, 80),
            (64, 68),
            (55, 55),
        ];

        // https://jrsoftware.org/ishelp/index.php?topic=setup_wizardsmallimagefile
        let large_resolutions = [
            (410, 797),
            (355, 700),
            (328, 604),
            (273, 556),
            (246, 459),
            (192, 386),
            (164, 314),
        ];

        cfg_if! {
            if #[cfg(debug_assertions)] {
                let resize_filter_type = FilterType::Triangle;
            } else {
                let resize_filter_type = FilterType::Lanczos3;
            }
        }

        for (width, height) in small_resolutions.iter() {
            let dimensions = small_src.dimensions();
            if *width < dimensions.0 && *height < dimensions.1 {
                let filename = self
                    .ctx
                    .cache_folder
                    .join(format!("innosetup-wizard-small-{}x{}.bmp", *width, *height));
                if !filename.exists().await {
                    small_src = small_src.resize(*width, *height, resize_filter_type);
                    small_src.save(&filename).unwrap_or_else(|err| {
                        panic!("Unable to save '{}': {err}", filename.display())
                    });
                }
                small_files.push(filename);
            }
        }

        for (width, height) in large_resolutions.iter() {
            let dimensions = large_src.dimensions();
            if *width < dimensions.0 && *height < dimensions.1 {
                let filename = self
                    .ctx
                    .cache_folder
                    .join(format!("innosetup-wizard-large-{}x{}.bmp", *width, *height));
                if !filename.exists().await {
                    large_src = large_src.resize(*width, *height, resize_filter_type);
                    large_src.save(&filename).unwrap_or_else(|err| {
                        panic!("Unable to save '{}': {err}", filename.display())
                    });
                }
                large_files.push(filename);
            }
        }

        Ok((small_files, large_files))
    }

    async fn create_innosetup_icon(&self, ico_file: &PathBuf) -> Result<()> {
        log_info!("InnoSetup", "generating icons");

        if Path::new(ico_file).exists().await {
            return Ok(());
        }

        let app_icon_png = find_file(
            &self.ctx.setup_resources_folder,
            &self.ctx.images.innosetup_icon(),
        )
        .await?;

        let mut src = image::open(&app_icon_png)
            .unwrap_or_else(|err| panic!("Unable to open '{app_icon_png:?}': {err}"));
        let dimensions = src.dimensions();
        if dimensions.0 != 1024 || dimensions.1 != 1024 {
            println!();
            println!(
                "WARNING: {}",
                app_icon_png.file_name().unwrap().to_str().unwrap()
            );
            println!(
                "         ^^^ icon dimensions are {}x{}; must be 1024x1024",
                dimensions.0, dimensions.1
            );
            println!();
        }

        cfg_if! {
            if #[cfg(debug_assertions)] {
                let resize_filter_type = FilterType::Triangle;
            } else {
                let resize_filter_type = FilterType::Lanczos3;
            }
        }

        let mut icon_dir = ico::IconDir::new(ico::ResourceType::Icon);

        let sizes = vec![256, 128, 64, 32, 16];
        for size in sizes {
            let dest = src.resize(size, size, resize_filter_type);

            let image_data = dest.as_rgba8().expect("Unable to get RGBA8 image data");
            let image_ico = ico::IconImage::from_rgba_data(
                image_data.width(),
                image_data.height(),
                image_data.as_raw().clone(),
            );
            icon_dir.add_entry(ico::IconDirEntry::encode(&image_ico).unwrap());
            src = dest;
        }

        let ico_file_fd = std::fs::File::create(ico_file)?;
        icon_dir.write(ico_file_fd).unwrap();

        Ok(())
    }

    async fn update_resources(&self) -> Result<()> {
        log_info!("Windows", "updating resources");
        let strings = self.get_resource_strings();

        let mut version = self
            .ctx
            .manifest
            .application
            .version
            .trim()
            .split('.')
            .map(|s| s.parse::<u16>().unwrap())
            .collect::<Vec<u16>>();

        if version.len() > 4 {
            return Err(format!(
                "invalid version format '{}' ... must be '1.2.3' or '1.2.3.4'",
                self.ctx.manifest.application.version
            )
            .into());
        }
        if version.len() < 4 {
            version.resize(4, 0);
        }
        let version: [u16; 4] = version
            .clone()
            .try_into()
            .map_err(|_| format!("Unable to parse version '{version:?}'"))?;

        // ~~~

        let app_icon_png = find_file(
            &self.ctx.setup_resources_folder,
            &self.ctx.images.windows_application(),
        )
        .await?;

        let mut app_icon_image = image::open(&app_icon_png)
            .unwrap_or_else(|err| panic!("Unable to open '{app_icon_png:?}': {err}"));

        if app_icon_image.width() < 256 || app_icon_image.height() < 256 {
            log_warn!(
                "Resources",
                "{}",
                style(
                    "application icon image size should be at least 256x256 (1024x1024 for MacOS)"
                )
                .red()
            );
        }
        if app_icon_image.width() > 256 || app_icon_image.height() > 256 {
            app_icon_image = app_icon_image.resize(256, 256, FilterType::Lanczos3);
        }

        let app_icon_image_data = app_icon_image
            .as_rgba8()
            .expect("Unable to get RGBA8 image data");
        let app_icon_image_ico = ico::IconImage::from_rgba_data(
            app_icon_image_data.width(),
            app_icon_image_data.height(),
            app_icon_image_data.as_raw().clone(),
        );
        let app_icon_encoded = ico::IconDirEntry::encode(&app_icon_image_ico).unwrap();
        // let app_res_file = self.ctx.build_folder.join(&self.app_exe_file);
        let app_res_file = self.target_folder.join(&self.app_exe_file);
        // let app_res_file = std::path::PathBuf::from(app_res_file.as_path());
        let mut resources = Resources::new(&std::path::PathBuf::from(app_res_file.as_path()));
        resources.load().unwrap_or_else(|err| {
            panic!(
                "Unable to load resources from '{}': {err}",
                app_res_file.display()
            )
        });
        resources.open().unwrap_or_else(|err| {
            panic!(
                "Unable to open resource file '{}' for updates: {err}",
                app_res_file.display()
            )
        });

        resources
            .find(resource_type::ICON, Id::Integer(1))
            .expect("unable to find main icon")
            .replace(app_icon_encoded.data())?
            .update()?;

        resources
            .get_version_info()?
            .expect("Unable to get version info")
            .set_file_version(&version)
            .set_product_version(&version)
            .insert_strings(
                &strings
                    .iter()
                    .map(|v| (v.0.as_str(), v.1.as_str()))
                    .collect::<Vec<_>>(),
            )
            // .remove_string("LastChange")
            .update()?;

        resources.close();

        Ok(())
    }
}