1use std::path::{Path, PathBuf};
2use std::process::Command;
3
4use anyhow::{Context, Result, bail};
5
6#[derive(Clone, Debug)]
8pub struct DmgWindow {
9 pub position: (u32, u32),
11 pub size: (u32, u32),
13 pub icon_size: u32,
15 pub app_icon_position: (u32, u32),
17 pub app_drop_link_position: (u32, u32),
19}
20
21impl Default for DmgWindow {
22 fn default() -> Self {
23 Self {
24 position: (200, 120),
25 size: (600, 400),
26 icon_size: 100,
27 app_icon_position: (150, 190),
28 app_drop_link_position: (450, 190),
29 }
30 }
31}
32
33#[derive(Clone, Debug)]
35pub struct CreateDmgOptions {
36 pub volume_name: String,
38 pub app: PathBuf,
40 pub output: PathBuf,
42 pub window: DmgWindow,
44 pub format: Option<String>,
46 pub background: Option<PathBuf>,
48 pub hide_app_extension: bool,
50 pub no_internet_enable: bool,
52}
53
54impl CreateDmgOptions {
55 pub fn new(
57 volume_name: impl Into<String>,
58 app: impl Into<PathBuf>,
59 output: impl Into<PathBuf>,
60 ) -> Self {
61 Self {
62 volume_name: volume_name.into(),
63 app: app.into(),
64 output: output.into(),
65 window: DmgWindow::default(),
66 format: None,
67 background: None,
68 hide_app_extension: false,
69 no_internet_enable: true,
70 }
71 }
72}
73
74pub fn create(options: &CreateDmgOptions) -> Result<()> {
76 crate::process::ensure_command("create-dmg")?;
77 if !options.app.is_dir() {
78 bail!("app bundle not found at {}", options.app.display());
79 }
80 if let Some(parent) = options.output.parent() {
81 crate::fs::create_dir_all(parent)?;
82 }
83 crate::fs::remove_path(&options.output)?;
84
85 let app_name = options
86 .app
87 .file_name()
88 .and_then(|name| name.to_str())
89 .context("app path has no UTF-8 filename")?;
90 let mut command = Command::new("create-dmg");
91 if let Some(format) = &options.format {
92 command.args(["--format", format]);
93 }
94 command.args(["--volname", &options.volume_name]);
95 if let Some(background) = &options.background {
96 command.arg("--background").arg(background);
97 }
98 command.args([
99 "--window-pos",
100 &options.window.position.0.to_string(),
101 &options.window.position.1.to_string(),
102 "--window-size",
103 &options.window.size.0.to_string(),
104 &options.window.size.1.to_string(),
105 "--icon-size",
106 &options.window.icon_size.to_string(),
107 "--icon",
108 app_name,
109 &options.window.app_icon_position.0.to_string(),
110 &options.window.app_icon_position.1.to_string(),
111 "--app-drop-link",
112 &options.window.app_drop_link_position.0.to_string(),
113 &options.window.app_drop_link_position.1.to_string(),
114 ]);
115 if options.hide_app_extension {
116 command.args(["--hide-extension", app_name]);
117 }
118 if options.no_internet_enable {
119 command.arg("--no-internet-enable");
120 }
121 command.arg(&options.output).arg(&options.app);
122
123 let status = command
124 .status()
125 .with_context(|| format!("create DMG {}", options.output.display()))?;
126 if !status.success() && !options.output.is_file() {
127 bail!("create-dmg failed with {status}");
128 }
129 if options.output.is_file() {
130 Ok(())
131 } else {
132 bail!("DMG was not created at {}", options.output.display())
133 }
134}
135
136pub fn sign(identity: &str, dmg: impl AsRef<Path>) -> Result<()> {
138 let dmg = dmg.as_ref();
139 let options = crate::apple::CodesignOptions {
140 identity,
141 target: dmg,
142 entitlements: None,
143 identifier: None,
144 hardened_runtime: false,
145 timestamp: true,
146 };
147 crate::apple::codesign(&options)
148}
149
150pub fn file_summary(path: impl AsRef<Path>) -> Result<String> {
152 let path = path.as_ref();
153 let metadata = std::fs::metadata(path).with_context(|| format!("stat {}", path.display()))?;
154 Ok(format!(
155 "{}\t{}",
156 crate::hash::human_size(metadata.len()),
157 path.display()
158 ))
159}