Преглед на файлове

feat/refact - update bundler (#459)

* update bundler and add scripts folder

* cleanup ios code

* cleanup platform and remove util dep from bundler
Tensor-Programming преди 5 години
родител
ревизия
e194289db1

+ 0 - 0
.scripts/batch_to_exe.cmd → .scripts/utils/batch_to_exe.cmd


+ 0 - 2
cli/tauri-bundler/Cargo.toml

@@ -32,8 +32,6 @@ toml = "0.5.6"
 uuid = { version = "0.8", features = ["v5"] }
 walkdir = "2"
 
-tauri-utils = {version = "0.4", path = "../../tauri-utils"}
-
 [target.'cfg(target_os = "windows")'.dependencies]
 attohttpc = { version = "0.11.1" }
 regex = { version = "1" }

+ 1 - 0
cli/tauri-bundler/src/bundle.rs

@@ -11,6 +11,7 @@ mod ios_bundle;
 mod msi_bundle;
 mod osx_bundle;
 mod path_utils;
+mod platform;
 mod rpm_bundle;
 mod settings;
 #[cfg(target_os = "windows")]

+ 21 - 14
cli/tauri-bundler/src/bundle/deb_bundle.rs

@@ -27,9 +27,9 @@ use image::png::{PNGDecoder, PNGEncoder};
 use image::{self, GenericImageView, ImageDecoder};
 use libflate::gzip;
 use md5;
+use std::process::{Command, Stdio};
 use tar;
 use walkdir::WalkDir;
-use std::process::{Command, Stdio};
 
 use std::collections::BTreeSet;
 use std::convert::TryInto;
@@ -105,9 +105,17 @@ pub fn generate_folders(settings: &Settings, package_dir: &Path) -> crate::Resul
   generate_icon_files(settings, &data_dir).chain_err(|| "Failed to create icon files")?;
   generate_desktop_file(settings, &data_dir).chain_err(|| "Failed to create desktop file")?;
 
+  generate_bootstrap_file(settings, &data_dir).chain_err(|| "Failed to generate bootstrap file")?;
+
+  Ok(data_dir)
+}
+
+fn generate_bootstrap_file(settings: &Settings, data_dir: &Path) -> crate::Result<()> {
+  let bin_name = settings.binary_name();
+  let bin_dir = data_dir.join("usr/bin");
+
   let bootstrap_file_name = format!("__{}-bootstrapper", bin_name);
-  let bootstrapper_file_path = bin_dir
-    .join(bootstrap_file_name.clone());
+  let bootstrapper_file_path = bin_dir.join(bootstrap_file_name.clone());
   let bootstrapper_file = &mut common::create_file(&bootstrapper_file_path)?;
   println!("{:?}", bootstrapper_file_path);
   write!(
@@ -119,23 +127,23 @@ export NVM_DIR=\"$([ -z \"${{XDG_CONFIG_HOME-}}\" ] && printf %s \"${{HOME}}/.nv
 
 if [ -e ~/.bash_profile ]
 then
-  source ~/.bash_profile
+    source ~/.bash_profile
 fi
 if [ -e ~/.zprofile ]
 then
-  source ~/.zprofile
+    source ~/.zprofile
 fi
 if [ -e ~/.profile ]
 then
-  source ~/.profile
+    source ~/.profile
 fi
 if [ -e ~/.bashrc ]
 then
-  source ~/.bashrc
+    source ~/.bashrc
 fi
 if [ -e ~/.zshrc ]
 then
-  source ~/.zshrc
+    source ~/.zshrc
 fi
 
 echo $PATH
@@ -145,10 +153,10 @@ source /etc/profile
 if pidof -x \"{}\" >/dev/null; then
     exit 0
 else
-Exec=/usr/bin/env /usr/bin/{} $@ & disown
+    Exec=/usr/bin/env /usr/bin/{} $@ & disown
 fi
-exit 0
-", bootstrap_file_name, bin_name
+exit 0",
+    bootstrap_file_name, bin_name
   )?;
   bootstrapper_file.flush()?;
 
@@ -158,10 +166,9 @@ exit 0
     .current_dir(&bin_dir)
     .stdout(Stdio::piped())
     .stderr(Stdio::piped())
-    .spawn()
-    .expect("Failed to chmod script");
+    .spawn()?;
 
-  Ok(data_dir)
+  Ok(())
 }
 
 /// Generate the application desktop file and store it under the `data_dir`.

+ 99 - 120
cli/tauri-bundler/src/bundle/ios_bundle.rs

@@ -22,154 +22,133 @@ use std::fs::{self, File};
 use std::io::Write;
 use std::path::{Path, PathBuf};
 
-pub fn bundle_project(settings: &Settings) -> crate::Result<Vec<PathBuf>> {
+
+pub fn bundle_project(settings: &Settings) -> ::Result<Vec<PathBuf>> {
   common::print_warning("iOS bundle support is still experimental.")?;
 
   let app_bundle_name = format!("{}.app", settings.bundle_name());
   common::print_bundling(&app_bundle_name)?;
-  let bundle_dir = settings
-    .project_out_directory()
-    .join("bundle/ios")
-    .join(&app_bundle_name);
+  let bundle_dir = settings.project_out_directory().join("bundle/ios").join(&app_bundle_name);
   if bundle_dir.exists() {
-    fs::remove_dir_all(&bundle_dir)
-      .chain_err(|| format!("Failed to remove old {}", app_bundle_name))?;
+      fs::remove_dir_all(&bundle_dir).chain_err(|| {
+          format!("Failed to remove old {}", app_bundle_name)
+      })?;
   }
-  fs::create_dir_all(&bundle_dir)
-    .chain_err(|| format!("Failed to create bundle directory at {:?}", bundle_dir))?;
+  fs::create_dir_all(&bundle_dir).chain_err(|| {
+      format!("Failed to create bundle directory at {:?}", bundle_dir)
+  })?;
 
-  settings.copy_resources(bundle_dir);
+  for src in settings.resource_files() {
+      let src = src?;
+      let dest = bundle_dir.join(common::resource_relpath(&src));
+      common::copy_file(&src, &dest).chain_err(|| {
+          format!("Failed to copy resource file {:?}", src)
+      })?;
+  }
 
-  let icon_filenames =
-    generate_icon_files(&bundle_dir, settings).chain_err(|| "Failed to create app icons")?;
-  generate_info_plist(&bundle_dir, settings, &icon_filenames)
-    .chain_err(|| "Failed to create Info.plist")?;
+  let icon_filenames = generate_icon_files(&bundle_dir, settings).chain_err(|| {
+      "Failed to create app icons"
+  })?;
+  generate_info_plist(&bundle_dir, settings, &icon_filenames).chain_err(|| {
+      "Failed to create Info.plist"
+  })?;
   let bin_path = bundle_dir.join(&settings.bundle_name());
-  common::copy_file(settings.binary_path(), &bin_path)
-    .chain_err(|| format!("Failed to copy binary from {:?}", settings.binary_path()))?;
+  common::copy_file(settings.binary_path(), &bin_path).chain_err(|| {
+      format!("Failed to copy binary from {:?}", settings.binary_path())
+  })?;
   Ok(vec![bundle_dir])
 }
 
 /// Generate the icon files and store them under the `bundle_dir`.
-fn generate_icon_files(bundle_dir: &Path, settings: &Settings) -> crate::Result<Vec<String>> {
+fn generate_icon_files(bundle_dir: &Path, settings: &Settings) -> ::Result<Vec<String>> {
   let mut filenames = Vec::new();
   {
-    let mut get_dest_path = |width: u32, height: u32, is_retina: bool| {
-      let filename = format!(
-        "icon_{}x{}{}.png",
-        width,
-        height,
-        if is_retina { "@2x" } else { "" }
-      );
-      let path = bundle_dir.join(&filename);
-      filenames.push(filename);
-      path
-    };
-    let mut sizes = BTreeSet::new();
-    // Prefer PNG files.
-    for icon_path in settings.icon_files() {
-      let icon_path = icon_path?;
-      if icon_path.extension() != Some(OsStr::new("png")) {
-        continue;
-      }
-      let decoder = PNGDecoder::new(File::open(&icon_path)?)?;
-      let width = decoder.dimensions().0.try_into()?;
-      let height = decoder.dimensions().1.try_into()?;
-      let is_retina = common::is_retina(&icon_path);
-      if !sizes.contains(&(width, height, is_retina)) {
-        sizes.insert((width, height, is_retina));
-        let dest_path = get_dest_path(width, height, is_retina);
-        common::copy_file(&icon_path, &dest_path)?;
-      }
-    }
-    // Fall back to non-PNG files for any missing sizes.
-    for icon_path in settings.icon_files() {
-      let icon_path = icon_path?;
-      if icon_path.extension() == Some(OsStr::new("png")) {
-        continue;
-      } else if icon_path.extension() == Some(OsStr::new("icns")) {
-        let icon_family = icns::IconFamily::read(File::open(&icon_path)?)?;
-        for icon_type in icon_family.available_icons() {
-          let width = icon_type.screen_width();
-          let height = icon_type.screen_height();
-          let is_retina = icon_type.pixel_density() > 1;
+      let mut get_dest_path = |width: u32, height: u32, is_retina: bool| {
+          let filename = format!("icon_{}x{}{}.png",
+                                 width,
+                                 height,
+                                 if is_retina { "@2x" } else { "" });
+          let path = bundle_dir.join(&filename);
+          filenames.push(filename);
+          path
+      };
+      let mut sizes = BTreeSet::new();
+      // Prefer PNG files.
+      for icon_path in settings.icon_files() {
+          let icon_path = icon_path?;
+          if icon_path.extension() != Some(OsStr::new("png")) {
+              continue;
+          }
+          let mut decoder = PNGDecoder::new(File::open(&icon_path)?);
+          let (width, height) = decoder.dimensions()?;
+          let is_retina = common::is_retina(&icon_path);
           if !sizes.contains(&(width, height, is_retina)) {
-            sizes.insert((width, height, is_retina));
-            let dest_path = get_dest_path(width, height, is_retina);
-            let icon = icon_family.get_icon_with_type(icon_type)?;
-            icon.write_png(File::create(dest_path)?)?;
+              sizes.insert((width, height, is_retina));
+              let dest_path = get_dest_path(width, height, is_retina);
+              common::copy_file(&icon_path, &dest_path)?;
+          }
+      }
+      // Fall back to non-PNG files for any missing sizes.
+      for icon_path in settings.icon_files() {
+          let icon_path = icon_path?;
+          if icon_path.extension() == Some(OsStr::new("png")) {
+              continue;
+          } else if icon_path.extension() == Some(OsStr::new("icns")) {
+              let icon_family = icns::IconFamily::read(File::open(&icon_path)?)?;
+              for icon_type in icon_family.available_icons() {
+                  let width = icon_type.screen_width();
+                  let height = icon_type.screen_height();
+                  let is_retina = icon_type.pixel_density() > 1;
+                  if !sizes.contains(&(width, height, is_retina)) {
+                      sizes.insert((width, height, is_retina));
+                      let dest_path = get_dest_path(width, height, is_retina);
+                      let icon = icon_family.get_icon_with_type(icon_type)?;
+                      icon.write_png(File::create(dest_path)?)?;
+                  }
+              }
+          } else {
+              let icon = try!(image::open(&icon_path));
+              let (width, height) = icon.dimensions();
+              let is_retina = common::is_retina(&icon_path);
+              if !sizes.contains(&(width, height, is_retina)) {
+                  sizes.insert((width, height, is_retina));
+                  let dest_path = get_dest_path(width, height, is_retina);
+                  let encoder = PNGEncoder::new(common::create_file(&dest_path)?);
+                  encoder.encode(&icon.raw_pixels(), width, height, icon.color())?;
+              }
           }
-        }
-      } else {
-        let icon = image::open(&icon_path)?;
-        let (width, height) = icon.dimensions();
-        let is_retina = common::is_retina(&icon_path);
-        if !sizes.contains(&(width, height, is_retina)) {
-          sizes.insert((width, height, is_retina));
-          let dest_path = get_dest_path(width, height, is_retina);
-          let encoder = PNGEncoder::new(common::create_file(&dest_path)?);
-          encoder.encode(&icon.raw_pixels(), width, height, icon.color())?;
-        }
       }
-    }
   }
   Ok(filenames)
 }
 
-fn generate_info_plist(
-  bundle_dir: &Path,
-  settings: &Settings,
-  icon_filenames: &Vec<String>,
-) -> crate::Result<()> {
+fn generate_info_plist(bundle_dir: &Path, settings: &Settings, icon_filenames: &Vec<String>) -> ::Result<()> {
   let file = &mut common::create_file(&bundle_dir.join("Info.plist"))?;
-  write!(
-    file,
-    "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
-     <!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" \
-     \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n\
-     <plist version=\"1.0\">\n\
-     <dict>\n"
-  )?;
+  write!(file,
+         "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
+          <!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" \
+          \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n\
+          <plist version=\"1.0\">\n\
+          <dict>\n")?;
+
+  write!(file, "  <key>CFBundleIdentifier</key>\n  <string>{}</string>\n", settings.bundle_identifier())?;
+  write!(file, "  <key>CFBundleDisplayName</key>\n  <string>{}</string>\n", settings.bundle_name())?;
+  write!(file, "  <key>CFBundleName</key>\n  <string>{}</string>\n", settings.bundle_name())?;
+  write!(file, "  <key>CFBundleExecutable</key>\n  <string>{}</string>\n", settings.binary_name())?;
+  write!(file, "  <key>CFBundleVersion</key>\n  <string>{}</string>\n", settings.version_string())?;
+  write!(file, "  <key>CFBundleShortVersionString</key>\n  <string>{}</string>\n", settings.version_string())?;
+  write!(file, "  <key>CFBundleDevelopmentRegion</key>\n  <string>en_US</string>\n")?;
 
-  write!(
-    file,
-    "  <key>CFBundleIdentifier</key>\n  <string>{}</string>\n",
-    settings.bundle_identifier()
-  )?;
-  write!(
-    file,
-    "  <key>CFBundleDisplayName</key>\n  <string>{}</string>\n",
-    settings.bundle_name()
-  )?;
-  write!(
-    file,
-    "  <key>CFBundleName</key>\n  <string>{}</string>\n",
-    settings.bundle_name()
-  )?;
-  write!(
-    file,
-    "  <key>CFBundleExecutable</key>\n  <string>{}</string>\n",
-    settings.binary_name()
-  )?;
-  write!(
-    file,
-    "  <key>CFBundleVersion</key>\n  <string>{}</string>\n",
-    settings.version_string()
-  )?;
-  write!(
-    file,
-    "  <key>CFBundleDevelopmentRegion</key>\n  <string>en_US</string>\n"
-  )?;
 
   if !icon_filenames.is_empty() {
-    write!(file, "  <key>CFBundleIconFiles</key>\n  <array>\n")?;
-    for filename in icon_filenames {
-      write!(file, "    <string>{}</string>\n", filename)?;
-    }
-    write!(file, "  </array>\n")?;
+      write!(file, "  <key>CFBundleIconFiles</key>\n  <array>\n")?;
+      for filename in icon_filenames {
+          write!(file, "    <string>{}</string>\n", filename)?;
+      }
+      write!(file, "  </array>\n")?;
   }
   write!(file, "  <key>LSRequiresIPhoneOS</key>\n  <true/>\n")?;
   write!(file, "</dict>\n</plist>\n")?;
   file.flush()?;
   Ok(())
-}
+}

+ 1 - 2
cli/tauri-bundler/src/bundle/osx_bundle.rs

@@ -134,8 +134,7 @@ exit 0",
     .current_dir(&bundle_dir.join("MacOS/"))
     .stdout(Stdio::piped())
     .stderr(Stdio::piped())
-    .spawn()
-    .expect("Failed to chmod script");
+    .spawn()?;
 
   Ok(())
 }

+ 52 - 0
cli/tauri-bundler/src/bundle/platform.rs

@@ -0,0 +1,52 @@
+/// Try to determine the current target triple.
+///
+/// Returns a target triple (e.g. `x86_64-unknown-linux-gnu` or `i686-pc-windows-msvc`) or an
+/// `Error::Config` if the current config cannot be determined or is not some combination of the
+/// following values:
+/// `linux, mac, windows` -- `i686, x86, armv7` -- `gnu, musl, msvc`
+///
+/// * Errors:
+///     * Unexpected system config
+pub fn target_triple() -> Result<String, crate::Error> {
+  let arch = if cfg!(target_arch = "x86") {
+    "i686"
+  } else if cfg!(target_arch = "x86_64") {
+    "x86_64"
+  } else if cfg!(target_arch = "arm") {
+    "armv7"
+  } else {
+    return Err(crate::Error::from(
+      "Unable to determine target-architecture",
+    ));
+  };
+
+  let os = if cfg!(target_os = "linux") {
+    "unknown-linux"
+  } else if cfg!(target_os = "macos") {
+    "apple-darwin"
+  } else if cfg!(target_os = "windows") {
+    "pc-windows"
+  } else if cfg!(target_os = "freebsd") {
+    "unknown-freebsd"
+  } else {
+    return Err(crate::Error::from("Unable to determine target-os"));
+  };
+
+  let os = if cfg!(target_os = "macos") || cfg!(target_os = "freebsd") {
+    String::from(os)
+  } else {
+    let env = if cfg!(target_env = "gnu") {
+      "gnu"
+    } else if cfg!(target_env = "musl") {
+      "musl"
+    } else if cfg!(target_env = "msvc") {
+      "msvc"
+    } else {
+      return Err(crate::Error::from("Unable to determine target-environment"));
+    };
+
+    format!("{}-{}", os, env)
+  };
+
+  Ok(format!("{}-{}", arch, os))
+}

+ 2 - 2
cli/tauri-bundler/src/bundle/settings.rs

@@ -1,12 +1,12 @@
 use super::category::AppCategory;
 use crate::bundle::common;
+use crate::bundle::platform::target_triple;
 
 use clap::ArgMatches;
 use error_chain::bail;
 use glob;
 use serde::Deserialize;
 use target_build_utils::TargetInfo;
-use tauri_utils::platform::target_triple;
 use toml;
 use walkdir;
 
@@ -435,7 +435,7 @@ impl Settings {
   }
 
   pub fn exception_domain(&self) -> Option<&String> {
-    return self.bundle_settings.exception_domain.as_ref()
+    return self.bundle_settings.exception_domain.as_ref();
   }
 
   // copy external binaries to a path.

+ 0 - 1
cli/tauri-bundler/src/main.rs

@@ -20,7 +20,6 @@ error_chain! {
         Walkdir(::walkdir::Error);
         StripError(std::path::StripPrefixError);
         ConvertError(std::num::TryFromIntError);
-        PlatformError(::tauri_utils::Error);
         RegexError(::regex::Error) #[cfg(windows)];
         HttpError(::attohttpc::Error) #[cfg(windows)];
     }

+ 3 - 4
tauri-utils/src/platform.rs

@@ -34,9 +34,8 @@ pub fn target_triple() -> Result<String, crate::Error> {
     return Err(crate::Error::from("Unable to determine target-os"));
   };
 
-  let s;
   let os = if cfg!(target_os = "macos") || cfg!(target_os = "freebsd") {
-    os
+    String::from(os)
   } else {
     let env = if cfg!(target_env = "gnu") {
       "gnu"
@@ -47,8 +46,8 @@ pub fn target_triple() -> Result<String, crate::Error> {
     } else {
       return Err(crate::Error::from("Unable to determine target-environment"));
     };
-    s = format!("{}-{}", os, env);
-    &s
+
+    format!("{}-{}", os, env)
   };
 
   Ok(format!("{}-{}", arch, os))