/cli/tauri-bundler/src/bundle/dmg_bundle.rs

https://github.com/tauri-apps/tauri · Rust · 132 lines · 106 code · 16 blank · 10 comment · 8 complexity · 9c6156f6fb8eade4ca07a37641e71e5c MD5 · raw file

  1. use super::common;
  2. use super::osx_bundle;
  3. use crate::Settings;
  4. use anyhow::Context;
  5. use std::env;
  6. use std::fs::{self, write};
  7. use std::path::PathBuf;
  8. use std::process::{Command, Stdio};
  9. /// Bundles the project.
  10. /// Returns a vector of PathBuf that shows where the DMG was created.
  11. pub fn bundle_project(settings: &Settings) -> crate::Result<Vec<PathBuf>> {
  12. // generate the .app bundle
  13. osx_bundle::bundle_project(settings)?;
  14. // get the target path
  15. let output_path = settings.project_out_directory().join("bundle/dmg");
  16. let package_base_name = format!(
  17. "{}_{}_{}",
  18. settings.main_binary_name(),
  19. settings.version_string(),
  20. match settings.binary_arch() {
  21. "x86_64" => "x64",
  22. other => other,
  23. }
  24. );
  25. let dmg_name = format!("{}.dmg", &package_base_name);
  26. let dmg_path = output_path.join(&dmg_name.clone());
  27. let bundle_name = &format!("{}.app", &package_base_name);
  28. let bundle_dir = settings.project_out_directory().join("bundle/osx");
  29. let bundle_path = bundle_dir.join(&bundle_name.clone());
  30. let support_directory_path = output_path.join("support");
  31. if output_path.exists() {
  32. fs::remove_dir_all(&output_path)
  33. .with_context(|| format!("Failed to remove old {}", dmg_name))?;
  34. }
  35. fs::create_dir_all(&support_directory_path).with_context(|| {
  36. format!(
  37. "Failed to create output directory at {:?}",
  38. support_directory_path
  39. )
  40. })?;
  41. // create paths for script
  42. let bundle_script_path = output_path.join("bundle_dmg.sh");
  43. let license_script_path = support_directory_path.join("dmg-license.py");
  44. common::print_bundling(format!("{:?}", &dmg_path.clone()).as_str())?;
  45. // write the scripts
  46. write(
  47. &bundle_script_path,
  48. include_str!("templates/dmg/bundle_dmg"),
  49. )?;
  50. write(
  51. support_directory_path.join("template.applescript"),
  52. include_str!("templates/dmg/template.applescript"),
  53. )?;
  54. write(
  55. &license_script_path,
  56. include_str!("templates/dmg/dmg-license.py"),
  57. )?;
  58. // chmod script for execution
  59. Command::new("chmod")
  60. .arg("777")
  61. .arg(&bundle_script_path)
  62. .arg(&license_script_path)
  63. .current_dir(output_path)
  64. .stdout(Stdio::piped())
  65. .stderr(Stdio::piped())
  66. .output()
  67. .expect("Failed to chmod script");
  68. let mut args = vec![
  69. "--volname",
  70. &package_base_name,
  71. "--volicon",
  72. "../../../../icons/icon.icns",
  73. "--icon",
  74. &bundle_name,
  75. "180",
  76. "170",
  77. "--app-drop-link",
  78. "480",
  79. "170",
  80. "--window-size",
  81. "660",
  82. "400",
  83. "--hide-extension",
  84. &bundle_name,
  85. ];
  86. if let Some(license_path) = settings.osx_license() {
  87. args.push("--eula");
  88. args.push(license_path);
  89. }
  90. // Issue #592 - Building MacOS dmg files on CI
  91. // https://github.com/tauri-apps/tauri/issues/592
  92. if let Some(value) = env::var_os("CI") {
  93. if value == "true" {
  94. args.push("--skip-jenkins");
  95. }
  96. }
  97. // execute the bundle script
  98. let mut cmd = Command::new(&bundle_script_path);
  99. cmd
  100. .current_dir(bundle_dir.clone())
  101. .args(args)
  102. .args(vec![dmg_name.as_str(), bundle_name.as_str()]);
  103. common::print_info("running bundle_dmg.sh")?;
  104. common::execute_with_verbosity(&mut cmd, &settings).map_err(|_| {
  105. crate::Error::ShellScriptError(format!(
  106. "error running bundle_dmg.sh{}",
  107. if settings.is_verbose() {
  108. ""
  109. } else {
  110. ", try running with --verbose to see command output"
  111. }
  112. ))
  113. })?;
  114. fs::rename(bundle_dir.join(dmg_name), dmg_path.clone())?;
  115. Ok(vec![bundle_path, dmg_path])
  116. }