Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 66 additions & 3 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ include!("src/versions.rs");
fn download_filename() -> String {
format!("electrum-{}-x86_64.AppImage", &VERSION)
}

#[cfg(all(target_os = "macos"))]
fn download_filename() -> String {
format!("electrum-{}.dmg", &VERSION)
}
// other platforms are currently unsupported

fn get_expected_sha256() -> Result<sha256::Hash, ()> {
Expand All @@ -33,6 +38,7 @@ fn main() {
if !download_dir.exists() {
fs::create_dir_all(&download_dir).unwrap();
}

let filepath = download_dir.join("electrum.AppImage");

if !filepath.exists() {
Expand All @@ -53,9 +59,66 @@ fn main() {
.read_to_end(&mut downloaded_bytes)
.unwrap();

let downloaded_hash = sha256::Hash::hash(&downloaded_bytes);
assert_eq!(expected_hash, downloaded_hash);
fs::write(&filepath, downloaded_bytes).unwrap();
#[cfg(all(target_os = "linux", target_arch = "x86_64"))]
{
let downloaded_hash = sha256::Hash::hash(&downloaded_bytes);
assert_eq!(expected_hash, downloaded_hash);
fs::write(&filepath, downloaded_bytes).unwrap();
}

#[cfg(target_os = "macos")]
{
let dmg_path = download_dir.join("electrum.dmg");
fs::write(&dmg_path, &downloaded_bytes).unwrap();
Comment on lines +69 to +72

// Mount the DMG (use -nobrowse to keep it out of Finder)
let output = std::process::Command::new("hdiutil")
.args(["attach", dmg_path.to_str().unwrap(), "-nobrowse"])
.output()
.expect("failed to run hdiutil attach");
assert!(
output.status.success(),
"hdiutil attach failed: {}",
String::from_utf8_lossy(&output.stderr)
);

// Parse mount point from hdiutil output (last column of last line)
let stdout = String::from_utf8_lossy(&output.stdout);
let mount_point = stdout
.lines()
.last()
.and_then(|line| line.split('\t').last())
.map(|s| s.trim())
.expect("failed to parse mount point from hdiutil output");

// Copy the entire Electrum.app bundle out of the DMG
let app_dest = download_dir.join("Electrum.app");
let app_src = Path::new(mount_point).join("Electrum.app");
let cp_output = std::process::Command::new("cp")
.args(["-R", app_src.to_str().unwrap(), app_dest.to_str().unwrap()])
.output()
.expect("failed to copy Electrum.app");
assert!(
Comment on lines +97 to +101
cp_output.status.success(),
"cp -R failed: {}",
String::from_utf8_lossy(&cp_output.stderr)
);

// Symlink electrum.AppImage -> the executable inside the app bundle
std::os::unix::fs::symlink(
Path::new("Electrum.app").join("Contents").join("MacOS").join("run_electrum"),
&filepath,
)
.expect("failed to create symlink to run_electrum");

// Detach the mounted DMG
let _ = std::process::Command::new("hdiutil")
.args(["detach", mount_point])
.output();

// Clean up the DMG file
let _ = fs::remove_file(&dmg_path);
}

// chmod +x
let mut perms = fs::metadata(&filepath).unwrap().permissions();
Expand Down
Loading