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

https://github.com/tauri-apps/tauri · Rust · 107 lines · 80 code · 16 blank · 11 comment · 4 complexity · 3ba851d59a749f01f8c19a3c9b812efa MD5 · raw file

  1. use super::common;
  2. use super::deb_bundle;
  3. use super::path_utils;
  4. use crate::Settings;
  5. use handlebars::Handlebars;
  6. use lazy_static::lazy_static;
  7. use std::collections::BTreeMap;
  8. use std::fs::{remove_dir_all, write};
  9. use std::path::PathBuf;
  10. use std::process::{Command, Stdio};
  11. // Create handlebars template for shell script
  12. lazy_static! {
  13. static ref HANDLEBARS: Handlebars<'static> = {
  14. let mut handlebars = Handlebars::new();
  15. handlebars
  16. .register_template_string("appimage", include_str!("templates/appimage"))
  17. .expect("Failed to register template for handlebars");
  18. handlebars
  19. };
  20. }
  21. /// Bundles the project.
  22. /// Returns a vector of PathBuf that shows where the AppImage was created.
  23. pub fn bundle_project(settings: &Settings) -> crate::Result<Vec<PathBuf>> {
  24. // prerequisite: check if mksquashfs (part of squashfs-tools) is installed
  25. Command::new("mksquashfs")
  26. .arg("-version")
  27. .stdout(Stdio::piped())
  28. .stderr(Stdio::piped())
  29. .status()
  30. .expect("mksquashfs is not installed. Please install squashfs-tools and try again.");
  31. // generate the deb binary name
  32. let arch = match settings.binary_arch() {
  33. "x86" => "i386",
  34. "x86_64" => "amd64",
  35. other => other,
  36. };
  37. let package_base_name = format!(
  38. "{}_{}_{}",
  39. settings.main_binary_name(),
  40. settings.version_string(),
  41. arch
  42. );
  43. let base_dir = settings.project_out_directory().join("bundle/appimage_deb");
  44. let package_dir = base_dir.join(&package_base_name);
  45. // generate deb_folder structure
  46. deb_bundle::generate_data(settings, &package_dir)?;
  47. let output_path = settings.project_out_directory().join("bundle/appimage");
  48. if output_path.exists() {
  49. remove_dir_all(&output_path)?;
  50. }
  51. std::fs::create_dir_all(output_path.clone())?;
  52. let app_dir_path = output_path.join(format!("{}.AppDir", settings.main_binary_name()));
  53. let appimage_path = output_path.join(format!("{}.AppImage", settings.main_binary_name()));
  54. path_utils::create(app_dir_path, true)?;
  55. let upcase_app_name = settings.main_binary_name().to_uppercase();
  56. // setup data to insert into shell script
  57. let mut sh_map = BTreeMap::new();
  58. sh_map.insert("app_name", settings.main_binary_name());
  59. sh_map.insert("bundle_name", package_base_name.as_str());
  60. sh_map.insert("app_name_uppercase", upcase_app_name.as_str());
  61. // initialize shell script template.
  62. let temp = HANDLEBARS.render("appimage", &sh_map)?;
  63. // create the shell script file in the target/ folder.
  64. let sh_file = output_path.join("build_appimage.sh");
  65. common::print_bundling(&appimage_path.file_name().unwrap().to_str().unwrap())?;
  66. write(&sh_file, temp)?;
  67. // chmod script for execution
  68. Command::new("chmod")
  69. .arg("777")
  70. .arg(&sh_file)
  71. .current_dir(output_path.clone())
  72. .stdout(Stdio::piped())
  73. .stderr(Stdio::piped())
  74. .output()
  75. .expect("Failed to chmod script");
  76. // execute the shell script to build the appimage.
  77. let mut cmd = Command::new(&sh_file);
  78. cmd.current_dir(output_path);
  79. common::execute_with_verbosity(&mut cmd, &settings).map_err(|_| {
  80. crate::Error::ShellScriptError(format!(
  81. "error running appimage.sh{}",
  82. if settings.is_verbose() {
  83. ""
  84. } else {
  85. ", try running with --verbose to see command output"
  86. }
  87. ))
  88. })?;
  89. remove_dir_all(&package_dir)?;
  90. Ok(vec![appimage_path])
  91. }