Echo Writes Code

main.rs

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
use clap::{ Parser };
use dedent::{ dedent };

use std::path::{ PathBuf };

#[derive(Parser)]
struct CommandLine {
  /// The name of the application.
  name: String,

  /// An identifier for the app (e.g. dev.echowritescode.cargo-strudel).
  identifier: String,

  /// The executable to package.
  executable: PathBuf,

  /// The directory to place the built macOS app in (NOT the .app folder itself).
  output_directory: PathBuf,

  /// The version string for the application. Defaults to 0.1.0.
  #[arg(short, long)]
  version: Option<String>,
}

fn main() -> std::io::Result<()> {
  let cli = CommandLine::parse();

  let executable = cli.executable.canonicalize()?;
  let output_directory = cli.output_directory.canonicalize()?;

  if !executable.is_file() {
    eprintln!("{} is not a file", executable.display());
    return Ok(());
  }

  if !output_directory.is_dir() {
    eprintln!("{} is not a directory", output_directory.display());
    return Ok(());
  }

  let app_directory = output_directory.join(format!("{}.app", cli.name));
  let basename = executable.file_name().and_then(|s| s.to_str()).expect("Invalid UTF-8 or bad pathspec");
  let info_plist = app_directory.join("Contents/Info.plist");
  let name = cli.name;
  let identifier = cli.identifier;
  let version = cli.version.unwrap_or("0.1.0".to_string());

  println!("{}", info_plist.display());

  std::fs::create_dir_all(app_directory.join("Contents/MacOS"))?;
  std::fs::copy(&executable, app_directory.join(format!("Contents/MacOS/{}", basename)))?;
  std::fs::write(info_plist, format!(dedent!(r#"
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    <plist version="1.0">
      <dict>
        <key>CFBundleDevelopmentRegion</key>
        <string>en</string>
        <key>CFBundleExecutable</key>
        <string>{}</string>
        <key>CFBundleIdentifier</key>
        <string>{}</string>
        <key>CFBundleInfoDictionaryVersion</key>
        <string>6.0</string>
        <key>CFBundleName</key>
        <string>{}</string>
        <key>CFBundlePackageType</key>
        <string>APPL</string>
        <key>CFBundleShortVersionString</key>
        <string>{}</string>
        <key>CFBundleSupportedPlatforms</key>
        <array>
          <string>MacOSX</string>
        </array>
        <key>CFBundleVersion</key>
        <string>{}</string>
        <key>CFBundleIconFile</key>
        <string></string>
        <key>NSHighResolutionCapable</key>
        <true/>
        <key>NSMainNibFile</key>
        <string></string>
        <key>NSSupportsAutomaticGraphicsSwitching</key>
        <true/>
        <key>CFBundleDisplayName</key>
        <string>{}</string>
        <key>NSRequiresAquaSystemAppearance</key>
        <string>NO</string>
      </dict>
    </plist>
  "#), basename, identifier, name, version, version, name))?;

  Ok(())
}