dmg_bundle.rs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. let app_name = settings.bundle_name();
  15. // get the target path
  16. let output_path = settings.project_out_directory().join("bundle/dmg");
  17. let dmg_name = format!("{}.dmg", app_name.clone());
  18. let dmg_path = output_path.join(&dmg_name.clone());
  19. let bundle_name = &format!("{}.app", app_name);
  20. let bundle_dir = settings.project_out_directory().join("bundle/osx");
  21. let bundle_path = bundle_dir.join(&bundle_name.clone());
  22. let support_directory_path = output_path.join("support");
  23. if output_path.exists() {
  24. fs::remove_dir_all(&output_path)
  25. .with_context(|| format!("Failed to remove old {}", dmg_name))?;
  26. }
  27. fs::create_dir_all(&support_directory_path).with_context(|| {
  28. format!(
  29. "Failed to create output directory at {:?}",
  30. support_directory_path
  31. )
  32. })?;
  33. // create paths for script
  34. let bundle_script_path = output_path.join("bundle_dmg.sh");
  35. let license_script_path = support_directory_path.join("dmg-license.py");
  36. common::print_bundling(format!("{:?}", &dmg_path.clone()).as_str())?;
  37. // write the scripts
  38. write(
  39. &bundle_script_path,
  40. include_str!("templates/dmg/bundle_dmg"),
  41. )?;
  42. write(
  43. support_directory_path.join("template.applescript"),
  44. include_str!("templates/dmg/template.applescript"),
  45. )?;
  46. write(
  47. &license_script_path,
  48. include_str!("templates/dmg/dmg-license.py"),
  49. )?;
  50. // chmod script for execution
  51. Command::new("chmod")
  52. .arg("777")
  53. .arg(&bundle_script_path)
  54. .arg(&license_script_path)
  55. .current_dir(output_path.clone())
  56. .stdout(Stdio::piped())
  57. .stderr(Stdio::piped())
  58. .output()
  59. .expect("Failed to chmod script");
  60. let mut args = vec![
  61. "--volname",
  62. &app_name,
  63. "--volicon",
  64. "../../../../icons/icon.icns",
  65. "--icon",
  66. &bundle_name,
  67. "180",
  68. "170",
  69. "--app-drop-link",
  70. "480",
  71. "170",
  72. "--window-size",
  73. "660",
  74. "400",
  75. "--hide-extension",
  76. &bundle_name,
  77. ];
  78. if let Some(license_path) = settings.osx_license() {
  79. args.push("--eula");
  80. args.push(license_path);
  81. }
  82. // Issue #592 - Building MacOS dmg files on CI
  83. // https://github.com/tauri-apps/tauri/issues/592
  84. match env::var_os("CI") {
  85. Some(value) => {
  86. if value == "true" {
  87. args.push("--skip-jenkins");
  88. }
  89. }
  90. None => (),
  91. }
  92. // execute the bundle script
  93. let status = Command::new(&bundle_script_path)
  94. .current_dir(bundle_dir.clone())
  95. .args(args)
  96. .args(vec![dmg_name.as_str(), bundle_name.as_str()])
  97. .status()
  98. .expect("Failed to execute shell script");
  99. if !status.success() {
  100. Err(crate::Error::ShellScriptError(
  101. "error running bundle_dmg.sh".to_owned(),
  102. ))
  103. } else {
  104. fs::rename(bundle_dir.join(dmg_name.clone()), dmg_path.clone())?;
  105. Ok(vec![bundle_path, dmg_path])
  106. }
  107. }