cargo-bundle 0.10.0

Wrap rust executables in OS-specific app bundles
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
// An OSX package is laid out like:
//
// foobar.app    # Actually a directory
//     Contents      # A further subdirectory
//         Info.plist     # An xml file containing the app's metadata
//         MacOS          # A directory to hold executable binary files
//             foobar          # The main binary executable of the app
//             foobar_helper   # A helper application, possibly provitidng a CLI
//         Resources      # Data files such as images, sounds, translations and nib files
//             en.lproj        # Folder containing english translation strings/data
//         Frameworks     # A directory containing private frameworks (shared libraries)
//         PlugIns        # A directory containing Plugins
//         ...            # Any other optional files the developer wants to place here
//
// See https://developer.apple.com/go/?id=bundle-structure for a full
// explanation.
//
// Currently, cargo-bundle does not support Frameworks, nor does it support placing arbitrary
// files into the `Contents` directory of the bundle.

use super::common::{self, read_file};
use crate::Settings;
use anyhow::Context;
use image::imageops::FilterType::Lanczos3;
use image::{self, GenericImageView};
use std::cmp::min;
use std::ffi::OsStr;
use std::fs::{self, File};
use std::io::prelude::*;
use std::io::{self, BufWriter};
use std::path::{Path, PathBuf};

pub fn bundle_project(settings: &Settings) -> crate::Result<Vec<PathBuf>> {
    let app_bundle_name = format!("{}.app", settings.bundle_name());
    common::print_bundling(&app_bundle_name)?;
    let app_bundle_path = settings
        .project_out_directory()
        .join("bundle/osx")
        .join(&app_bundle_name);
    if app_bundle_path.exists() {
        fs::remove_dir_all(&app_bundle_path)
            .with_context(|| format!("Failed to remove old {app_bundle_name}"))?;
    }
    let bundle_directory = app_bundle_path.join("Contents");
    fs::create_dir_all(&bundle_directory)
        .with_context(|| format!("Failed to create bundle directory at {bundle_directory:?}"))?;

    let resources_dir = bundle_directory.join("Resources");

    let bundle_icon_file: Option<PathBuf> = {
        create_icns_file(&resources_dir, settings).with_context(|| "Failed to create app icon")?
    };

    create_info_plist(&bundle_directory, bundle_icon_file, settings)
        .with_context(|| "Failed to create Info.plist")?;

    let copied = copy_frameworks_to_bundle(&bundle_directory, settings)
        .with_context(|| "Failed to bundle frameworks")?;

    copy_plugins_to_bundle(&bundle_directory, settings)
        .with_context(|| "Failed to bundle plugins")?;

    for src in settings.resource_files() {
        let src = src?;
        let dest = resources_dir.join(common::resource_relpath(&src));
        common::copy_file(&src, &dest)
            .with_context(|| format!("Failed to copy resource file {src:?}"))?;
    }

    copy_binary_to_bundle(&bundle_directory, settings)
        .with_context(|| format!("Failed to copy binary from {:?}", settings.binary_path()))?;

    if copied > 0 {
        add_rpath(&bundle_directory, settings)?;
    }

    Ok(vec![app_bundle_path])
}

#[allow(dead_code)]
#[derive(Debug, Default)]
struct DylibInfo {
    dylibs: Vec<PathBuf>,
    rpaths: Vec<PathBuf>,
}

impl DylibInfo {
    fn inspect(dylib_path: &Path) -> crate::Result<Self> {
        use std::process::Command;
        let out = Command::new("otool").arg("-l").arg(dylib_path).output()?;

        if !out.status.success() {
            anyhow::bail!("otool command failed with status: {}", out.status);
        }

        let mut dylibs = Vec::new();
        let mut rpaths = Vec::new();
        enum NextAction {
            Unknown,
            FindDylib,
            FindRpath,
        }

        let mut next_action = NextAction::Unknown;

        let lines = String::from_utf8_lossy(&out.stdout);
        for line in lines.lines() {
            if let Some((w0, w1)) = line.trim_start().split_once(" ") {
                match next_action {
                    NextAction::Unknown => {
                        if w0 == "cmd" {
                            if w1 == "LC_LOAD_DYLIB" {
                                next_action = NextAction::FindDylib;
                            } else if w1 == "LC_RPATH" {
                                next_action = NextAction::FindRpath;
                            }
                        }
                    }
                    NextAction::FindDylib => {
                        if w0 == "name" {
                            dylibs.push(Self::extract_path_from_line(w1, "name", "LC_LOAD_DYLIB")?);
                            next_action = NextAction::Unknown;
                        } else if w0 == "Load" {
                            next_action = NextAction::Unknown; //just to avoid unexpected output
                        }
                    }
                    NextAction::FindRpath => {
                        if w0 == "path" {
                            rpaths.push(Self::extract_path_from_line(w1, "path", "LC_RPATH")?);
                            next_action = NextAction::Unknown;
                        } else if w0 == "Load" {
                            next_action = NextAction::Unknown; //just to avoid unexpected output
                        }
                    }
                }
            }
        }
        Ok(Self { dylibs, rpaths })
    }

    fn extract_path_from_line(
        line: &str,
        field_name: &str,
        context: &str,
    ) -> crate::Result<PathBuf> {
        if let Some(trail) = line.find('(') {
            if trail > 0 {
                let name = &line[..trail];
                Ok(PathBuf::from(name.trim_end()))
            } else {
                anyhow::bail!("unexpected otool output - empty {field_name} field");
            }
        } else {
            anyhow::bail!("unexpected otool output - expect {field_name} field after {context}");
        }
    }

    fn has_rpath<T: AsRef<Path>>(&self, path: T) -> bool {
        self.rpaths.iter().any(|s| s.as_path() == path.as_ref())
    }
}

fn copy_binary_to_bundle(bundle_directory: &Path, settings: &Settings) -> crate::Result<()> {
    let dest_dir = bundle_directory.join("MacOS");
    common::copy_file(
        settings.binary_path(),
        &dest_dir.join(settings.binary_name()),
    )
}
trait PlistEntryFormatter {
    fn format_plist_entry(&self) -> String;
}

impl<T: AsRef<str>> PlistEntryFormatter for T {
    fn format_plist_entry(&self) -> String {
        let input = self.as_ref();
        input.replace("&", "&amp;")
        // add other necessary modifications here...
    }
}

const FRAMEWORKS_RPATH: &str = "@executable_path/../Frameworks";

fn add_rpath(bundle_directory: &Path, settings: &Settings) -> crate::Result<()> {
    let bin = bundle_directory.join("MacOS").join(settings.binary_name());

    let dyinfo = DylibInfo::inspect(&bin)?;

    if dyinfo.has_rpath(FRAMEWORKS_RPATH) {
        //rpath already in dylib
        return Ok(());
    }

    if !std::process::Command::new("install_name_tool")
        .arg("-add_rpath")
        .arg(FRAMEWORKS_RPATH)
        .arg(bin)
        .status()?
        .success()
    {
        anyhow::bail!("failed to execute install_name_tool");
    }

    Ok(())
}

fn create_info_plist(
    bundle_dir: &Path,
    bundle_icon_file: Option<PathBuf>,
    settings: &Settings,
) -> crate::Result<()> {
    let build_number = chrono::Utc::now().format("%Y%m%d.%H%M%S");
    let file = &mut common::create_file(&bundle_dir.join("Info.plist"))?;
    write!(
        file,
        "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
            <!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" \
            \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n\
            <plist version=\"1.0\">\n\
            <dict>\n"
    )?;
    write!(
        file,
        "  <key>CFBundleDevelopmentRegion</key>\n  \
            <string>English</string>\n"
    )?;
    write!(
        file,
        "  <key>CFBundleDisplayName</key>\n  <string>{}</string>\n",
        settings.bundle_name().format_plist_entry()
    )?;
    write!(
        file,
        "  <key>CFBundleExecutable</key>\n  <string>{}</string>\n",
        settings.binary_name()
    )?;
    if let Some(path) = bundle_icon_file {
        write!(
            file,
            "  <key>CFBundleIconFile</key>\n  <string>{}</string>\n",
            path.file_name().unwrap().to_string_lossy()
        )?;
    }
    write!(
        file,
        "  <key>CFBundleIdentifier</key>\n  <string>{}</string>\n",
        settings.bundle_identifier()
    )?;
    write!(
        file,
        "  <key>CFBundleInfoDictionaryVersion</key>\n  \
            <string>6.0</string>\n"
    )?;
    write!(
        file,
        "  <key>CFBundleName</key>\n  <string>{}</string>\n",
        settings.bundle_name().format_plist_entry()
    )?;
    write!(
        file,
        "  <key>CFBundlePackageType</key>\n  <string>APPL</string>\n"
    )?;
    write!(
        file,
        "  <key>CFBundleShortVersionString</key>\n  <string>{}</string>\n",
        settings.version_string()
    )?;
    if !settings.osx_url_schemes().is_empty() {
        write!(
            file,
            "  <key>CFBundleURLTypes</key>\n  \
               <array>\n    \
                   <dict>\n      \
                       <key>CFBundleURLName</key>\n      \
                       <string>{}</string>\n      \
                       <key>CFBundleTypeRole</key>\n      \
                       <string>Viewer</string>\n      \
                       <key>CFBundleURLSchemes</key>\n      \
                       <array>\n",
            settings.bundle_name().format_plist_entry()
        )?;
        for scheme in settings.osx_url_schemes() {
            writeln!(
                file,
                "        <string>{}</string>",
                scheme.format_plist_entry()
            )?;
        }
        write!(
            file,
            "      </array>\n    \
                </dict>\n  \
             </array>\n"
        )?;
    }
    write!(
        file,
        "  <key>CFBundleVersion</key>\n  <string>{build_number}</string>\n"
    )?;
    write!(file, "  <key>CSResourcesFileMapped</key>\n  <true/>\n")?;
    if let Some(category) = settings.app_category() {
        write!(
            file,
            "  <key>LSApplicationCategoryType</key>\n  \
                <string>{}</string>\n",
            category
                .osx_application_category_type()
                .format_plist_entry()
        )?;
    }
    if let Some(version) = settings.osx_minimum_system_version() {
        write!(
            file,
            "  <key>LSMinimumSystemVersion</key>\n  \
                <string>{version}</string>\n"
        )?;
    }
    write!(file, "  <key>LSRequiresCarbon</key>\n  <true/>\n")?;
    write!(file, "  <key>NSHighResolutionCapable</key>\n  <true/>\n")?;
    if let Some(copyright) = settings.copyright_string() {
        write!(
            file,
            "  <key>NSHumanReadableCopyright</key>\n  \
                <string>{}</string>\n",
            copyright.format_plist_entry()
        )?;
    }
    for plist in settings.osx_info_plist_exts() {
        let plist = plist?;
        let contents = read_file(&plist)?;
        write!(file, "{:}", contents.format_plist_entry())?
    }
    write!(file, "</dict>\n</plist>\n")?;
    file.flush()?;
    Ok(())
}

fn copy_framework_from(dest_dir: &Path, framework: &str, src_dir: &Path) -> crate::Result<bool> {
    let src_name = format!("{framework}.framework");
    let src_path = src_dir.join(&src_name);
    if src_path.exists() {
        common::copy_dir(&src_path, &dest_dir.join(&src_name))?;
        Ok(true)
    } else {
        Ok(false)
    }
}

fn copy_frameworks_to_bundle(bundle_directory: &Path, settings: &Settings) -> crate::Result<i32> {
    let frameworks = settings.osx_frameworks();
    if frameworks.is_empty() {
        return Ok(0);
    }
    let mut copied = 0;
    let dest_dir = bundle_directory.join("Frameworks");
    fs::create_dir_all(bundle_directory)
        .with_context(|| format!("Failed to create Frameworks directory at {dest_dir:?}"))?;
    for framework in frameworks.iter() {
        if framework.ends_with(".framework") {
            let src_path = PathBuf::from(framework);
            let src_name = src_path.file_name().unwrap();
            common::copy_dir(&src_path, &dest_dir.join(src_name))?;
            copied += 1;
            continue;
        } else if framework.ends_with(".dylib") {
            let src_path = PathBuf::from(framework);
            let src_name = src_path.file_name().unwrap();
            common::copy_file(&src_path, &dest_dir.join(src_name))?;
            copied += 1;
            continue;
        } else if framework.contains('/') {
            anyhow::bail!(
                "Framework path should have .framework extension: {}",
                framework
            );
        }
        if let Some(home_dir) = dirs::home_dir()
            && copy_framework_from(&dest_dir, framework, &home_dir.join("Library/Frameworks/"))?
        {
            copied += 1;
            continue;
        }
        if copy_framework_from(&dest_dir, framework, &PathBuf::from("/Library/Frameworks/"))?
            || copy_framework_from(
                &dest_dir,
                framework,
                &PathBuf::from("/Network/Library/Frameworks/"),
            )?
            || copy_framework_from(
                &dest_dir,
                framework,
                &PathBuf::from("/System/Library/Frameworks/"),
            )?
        {
            copied += 1;
            continue;
        }
        anyhow::bail!("Could not locate {}.framework", framework);
    }
    Ok(copied)
}

fn copy_plugins_to_bundle(bundle_directory: &Path, settings: &Settings) -> crate::Result<()> {
    let plugins = settings.osx_plugins();
    if plugins.is_empty() {
        return Ok(());
    }
    let dest_dir = bundle_directory.join("PlugIns");
    fs::create_dir_all(bundle_directory)
        .with_context(|| format!("Failed to create PlugIns directory at {dest_dir:?}"))?;
    for plugin in plugins.iter() {
        let src_path = PathBuf::from(plugin);
        let src_name = src_path.file_name().unwrap();
        common::copy_dir(&src_path, &dest_dir.join(src_name))?;
    }
    Ok(())
}

/// Given a list of icon files, try to produce an ICNS file in the resources
/// directory and return the path to it.  Returns `Ok(None)` if no usable icons
/// were provided.
fn create_icns_file(
    resources_dir: &PathBuf,
    settings: &Settings,
) -> crate::Result<Option<PathBuf>> {
    if settings.icon_files().count() == 0 {
        return Ok(None);
    }

    // If one of the icon files is already an ICNS file, just use that.
    for icon_path in settings.icon_files() {
        let icon_path = icon_path?;
        if icon_path.extension() == Some(OsStr::new("icns")) {
            let mut dest_path = resources_dir.to_path_buf();
            dest_path.push(icon_path.file_name().unwrap());
            common::copy_file(&icon_path, &dest_path)?;
            return Ok(Some(dest_path));
        }
    }

    // Otherwise, read available images and pack them into a new ICNS file.
    let mut family = icns::IconFamily::new();

    fn add_icon_to_family(
        icon: image::DynamicImage,
        density: u32,
        family: &mut icns::IconFamily,
    ) -> io::Result<()> {
        // Try to add this image to the icon family.  Ignore images whose sizes
        // don't map to any ICNS icon type; print warnings and skip images that
        // fail to encode.
        match icns::IconType::from_pixel_size_and_density(icon.width(), icon.height(), density) {
            Some(icon_type) => {
                if !family.has_icon_with_type(icon_type) {
                    let icon = make_icns_image(icon)?;
                    family.add_icon_with_type(&icon, icon_type)?;
                }
                Ok(())
            }
            None => Err(io::Error::new(
                io::ErrorKind::InvalidData,
                "No matching IconType",
            )),
        }
    }

    let mut images_to_resize: Vec<(image::DynamicImage, u32, u32)> = vec![];
    for icon_path in settings.icon_files() {
        let icon_path = icon_path?;
        if icon_path.extension() == Some(OsStr::new("svg")) {
            continue; // TODO: convert svg to appropriate format?
        }
        let icon = image::open(&icon_path)?;
        let density = if common::is_retina(&icon_path) { 2 } else { 1 };
        let (w, h) = icon.dimensions();
        let orig_size = min(w, h);
        let next_size_down = 2f32.powf((orig_size as f32).log2().floor()) as u32;
        if orig_size > next_size_down {
            images_to_resize.push((icon, next_size_down, density));
        } else {
            add_icon_to_family(icon, density, &mut family)?;
        }
    }

    for (icon, next_size_down, density) in images_to_resize {
        let icon = icon.resize_exact(next_size_down, next_size_down, Lanczos3);
        add_icon_to_family(icon, density, &mut family)?;
    }

    if !family.is_empty() {
        fs::create_dir_all(resources_dir)?;
        let mut dest_path = resources_dir.clone();
        dest_path.push(settings.bundle_name());
        dest_path.set_extension("icns");
        let icns_file = BufWriter::new(File::create(&dest_path)?);
        family.write(icns_file)?;
        return Ok(Some(dest_path));
    }

    anyhow::bail!("No usable icon files found.");
}

/// Converts an image::DynamicImage into an icns::Image.
fn make_icns_image(img: image::DynamicImage) -> io::Result<icns::Image> {
    let pixel_format = match img.color() {
        image::ColorType::Rgba8 => icns::PixelFormat::RGBA,
        image::ColorType::Rgb8 => icns::PixelFormat::RGB,
        image::ColorType::La8 => icns::PixelFormat::GrayAlpha,
        image::ColorType::L8 => icns::PixelFormat::Gray,
        _ => {
            let msg = format!("unsupported ColorType: {:?}", img.color());
            return Err(io::Error::new(io::ErrorKind::InvalidData, msg));
        }
    };
    icns::Image::from_data(pixel_format, img.width(), img.height(), img.into_bytes())
}