Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(bundler) remove error_chain and add thiserror and anyhow. #623

Merged
merged 25 commits into from
May 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
470298b
Publish (#547)
github-actions[bot] Apr 2, 2020
c395bc9
chore(tauri.js): version updates (#556) (#557)
github-actions[bot] Apr 6, 2020
c579313
Publish (#588)
github-actions[bot] May 9, 2020
cf19a41
hotfix(tauri.js) proxy dev-server websocket connection so HMR works (…
lucasfernog May 12, 2020
9273e89
Publish (#606)
github-actions[bot] May 21, 2020
519e8a7
Publish (#608)
github-actions[bot] May 21, 2020
4013883
chore(api): "version updates" (#614) (#615)
github-actions[bot] May 23, 2020
5a160d3
Merge branch 'master' of https://github.com/tauri-apps/tauri into dev
tensor-programming May 26, 2020
9c4ce46
Merge branch 'dev' of https://github.com/tauri-apps/tauri into dev
tensor-programming May 28, 2020
b047d83
add anyhow and thiserror to bundler.
tensor-programming May 28, 2020
c937c0a
remove error chain
tensor-programming May 28, 2020
587c32c
cleanup
tensor-programming May 28, 2020
5b0b65f
fix import paths.
tensor-programming May 28, 2020
35f8654
remove bail calls.
tensor-programming May 28, 2020
18dce9c
add more consistent error descriptions.
tensor-programming May 28, 2020
2a11867
add cfg for non-linux errors.
tensor-programming May 28, 2020
67035e0
fix linux cfg
tensor-programming May 28, 2020
23c53b5
fix maskfile
tensor-programming May 28, 2020
d828856
add runas and loopback command
tensor-programming May 28, 2020
d5bd4e8
cleanup maskfile clean.
tensor-programming May 28, 2020
cb44e32
fix cfg
tensor-programming May 28, 2020
fb920fe
export print info and fix cfg.
tensor-programming May 28, 2020
f1a3d08
Merge branch 'dev' into feat/advanced-error-handling
tensor-programming May 29, 2020
179d5cc
fix maskfile's logic
tensor-programming May 29, 2020
b7fa6f2
Merge branch 'feat/advanced-error-handling' of https://github.com/tau…
tensor-programming May 29, 2020
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
6 changes: 5 additions & 1 deletion cli/tauri-bundler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,17 @@ ar = "0.8.0"
chrono = "0.4"
clap = "^2"
dirs = "2.0.2"
error-chain = "0.12"
glob = "0.3.0"
icns = "0.3"
image = "0.23.4"
libflate = "1.0"
md5 = "0.7.0"
msi = "0.2"

# error handling
anyhow = "1.0"
thiserror = "1.0"

serde_json = "1.0"
serde = { version = "1.0", features = ["derive"] }
strsim = "0.10.0"
Expand All @@ -38,6 +41,7 @@ handlebars = { version = "3.0" }
[target.'cfg(target_os = "windows")'.dependencies]
attohttpc = { version = "0.13.0" }
regex = { version = "1" }
runas = "0.2"

[target.'cfg(not(target_os = "linux"))'.dependencies]
zip = { version = "0.5" }
Expand Down
2 changes: 1 addition & 1 deletion cli/tauri-bundler/src/bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ mod tauri_config;
#[cfg(target_os = "windows")]
mod wix;

pub use self::common::{print_error, print_finished};
pub use self::common::{print_error, print_finished, print_info};
pub use self::settings::{BuildArtifact, PackageType, Settings};

use std::path::PathBuf;
Expand Down
10 changes: 3 additions & 7 deletions cli/tauri-bundler/src/bundle/appimage_bundle.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use super::common;
use super::deb_bundle;
use super::path_utils;
use crate::ResultExt;
use crate::Settings;

use handlebars::Handlebars;
Expand Down Expand Up @@ -46,8 +45,7 @@ pub fn bundle_project(settings: &Settings) -> crate::Result<Vec<PathBuf>> {

let output_path = settings.project_out_directory().join("bundle/appimage");
if output_path.exists() {
remove_dir_all(&output_path)
.chain_err(|| format!("Failed to remove old {}", package_base_name))?;
remove_dir_all(&output_path)?;
}
std::fs::create_dir_all(output_path.clone())?;
let app_dir_path = output_path.join(format!("{}.AppDir", settings.binary_name()));
Expand All @@ -63,14 +61,12 @@ pub fn bundle_project(settings: &Settings) -> crate::Result<Vec<PathBuf>> {
sh_map.insert("app_name_uppercase", upcase.as_str());

// initialize shell script template.
let temp = HANDLEBARS
.render("appimage", &sh_map)
.or_else(|e| Err(e.to_string()))?;
let temp = HANDLEBARS.render("appimage", &sh_map)?;

// create the shell script file in the target/ folder.
let sh_file = output_path.join("build_appimage");
common::print_bundling(format!("{:?}", &appimage_path).as_str())?;
write(&sh_file, temp).or_else(|e| Err(e.to_string()))?;
write(&sh_file, temp)?;

// chmod script for execution
Command::new("chmod")
Expand Down
57 changes: 35 additions & 22 deletions cli/tauri-bundler/src/bundle/common.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
use crate::ResultExt;

use std;
use std::ffi::OsStr;
use std::fs::{self, File};
use std::io::{self, BufWriter, Write};
use std::path::{Component, Path, PathBuf};

use error_chain::bail;
use term;
use walkdir;

Expand All @@ -27,9 +24,9 @@ pub fn is_retina<P: AsRef<Path>>(path: P) -> bool {
/// needed.
pub fn create_file(path: &Path) -> crate::Result<BufWriter<File>> {
if let Some(parent) = path.parent() {
fs::create_dir_all(&parent).chain_err(|| format!("Failed to create directory {:?}", parent))?;
fs::create_dir_all(&parent)?;
}
let file = File::create(path).chain_err(|| format!("Failed to create file {:?}", path))?;
let file = File::create(path)?;
Ok(BufWriter::new(file))
}

Expand Down Expand Up @@ -58,14 +55,20 @@ fn symlink_file(src: &Path, dst: &Path) -> io::Result<()> {
/// is a directory or doesn't exist.
pub fn copy_file(from: &Path, to: &Path) -> crate::Result<()> {
if !from.exists() {
bail!("{:?} does not exist", from);
return Err(crate::Error::GenericError(format!(
"{:?} does not exist",
from
)));
}
if !from.is_file() {
bail!("{:?} is not a file", from);
return Err(crate::Error::GenericError(format!(
"{:?} is not a file",
from
)));
}
let dest_dir = to.parent().expect("No data in parent");
fs::create_dir_all(dest_dir).chain_err(|| format!("Failed to create {:?}", dest_dir))?;
fs::copy(from, to).chain_err(|| format!("Failed to copy {:?} to {:?}", from, to))?;
fs::create_dir_all(dest_dir)?;
fs::copy(from, to)?;
Ok(())
}

Expand All @@ -75,16 +78,25 @@ pub fn copy_file(from: &Path, to: &Path) -> crate::Result<()> {
/// already exists.
pub fn copy_dir(from: &Path, to: &Path) -> crate::Result<()> {
if !from.exists() {
bail!("{:?} does not exist", from);
return Err(crate::Error::GenericError(format!(
"{:?} does not exist",
from
)));
}
if !from.is_dir() {
bail!("{:?} is not a directory", from);
return Err(crate::Error::GenericError(format!(
"{:?} is not a Directory",
from
)));
}
if to.exists() {
bail!("{:?} already exists", to);
return Err(crate::Error::GenericError(format!(
"{:?} already exists",
from
)));
}
let parent = to.parent().expect("No data in parent");
fs::create_dir_all(parent).chain_err(|| format!("Failed to create {:?}", parent))?;
fs::create_dir_all(parent)?;
for entry in walkdir::WalkDir::new(from) {
let entry = entry?;
debug_assert!(entry.path().starts_with(from));
Expand Down Expand Up @@ -212,7 +224,7 @@ pub fn print_info(message: &str) -> crate::Result<()> {
}

/// Prints an error to stderr, in the same format that `cargo` uses.
pub fn print_error(error: &crate::Error) -> crate::Result<()> {
pub fn print_error(error: &anyhow::Error) -> crate::Result<()> {
if let Some(mut output) = term::stderr() {
safe_term_attr(&mut output, term::Attr::Bold)?;
output.fg(term::color::RED)?;
Expand All @@ -221,24 +233,25 @@ pub fn print_error(error: &crate::Error) -> crate::Result<()> {
safe_term_attr(&mut output, term::Attr::Bold)?;
writeln!(output, " {}", error)?;
output.reset()?;
for cause in error.iter().skip(1) {
for cause in error.chain().skip(1) {
writeln!(output, " Caused by: {}", cause)?;
}
if let Some(backtrace) = error.backtrace() {
writeln!(output, "{:?}", backtrace)?;
}
// Add Backtrace once its stable.
// if let Some(backtrace) = error.backtrace() {
// writeln!(output, "{:?}", backtrace)?;
// }
output.flush()?;
std::process::exit(1)
} else {
let mut output = io::stderr();
write!(output, "error:")?;
writeln!(output, " {}", error)?;
for cause in error.iter().skip(1) {
for cause in error.chain().skip(1) {
writeln!(output, " Caused by: {}", cause)?;
}
if let Some(backtrace) = error.backtrace() {
writeln!(output, "{:?}", backtrace)?;
}
// if let Some(backtrace) = error.backtrace() {
// writeln!(output, "{:?}", backtrace)?;
// }
output.flush()?;
std::process::exit(1)
}
Expand Down
40 changes: 23 additions & 17 deletions cli/tauri-bundler/src/bundle/deb_bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@
// generate postinst or prerm files.

use super::common;
use crate::{ResultExt, Settings};
use crate::Settings;

use anyhow::Context;
use ar;
use icns;
use image::png::PngDecoder;
Expand Down Expand Up @@ -55,34 +56,34 @@ pub fn bundle_project(settings: &Settings) -> crate::Result<Vec<PathBuf>> {
let package_dir = base_dir.join(&package_base_name);
if package_dir.exists() {
fs::remove_dir_all(&package_dir)
.chain_err(|| format!("Failed to remove old {}", package_base_name))?;
.with_context(|| format!("Failed to remove old {}", package_base_name))?;
}
let package_path = base_dir.join(package_name);

let data_dir =
generate_folders(settings, &package_dir).chain_err(|| "Failed to build folders")?;
generate_folders(settings, &package_dir).with_context(|| "Failed to build folders")?;
// Generate control files.
let control_dir = package_dir.join("control");
generate_control_file(settings, arch, &control_dir, &data_dir)
.chain_err(|| "Failed to create control file")?;
generate_md5sums(&control_dir, &data_dir).chain_err(|| "Failed to create md5sums file")?;
.with_context(|| "Failed to create control file")?;
generate_md5sums(&control_dir, &data_dir).with_context(|| "Failed to create md5sums file")?;

// Generate `debian-binary` file; see
// http://www.tldp.org/HOWTO/Debian-Binary-Package-Building-HOWTO/x60.html#AEN66
let debian_binary_path = package_dir.join("debian-binary");
create_file_with_data(&debian_binary_path, "2.0\n")
.chain_err(|| "Failed to create debian-binary file")?;
.with_context(|| "Failed to create debian-binary file")?;

// Apply tar/gzip/ar to create the final package file.
let control_tar_gz_path =
tar_and_gzip_dir(control_dir).chain_err(|| "Failed to tar/gzip control directory")?;
tar_and_gzip_dir(control_dir).with_context(|| "Failed to tar/gzip control directory")?;
let data_tar_gz_path =
tar_and_gzip_dir(data_dir).chain_err(|| "Failed to tar/gzip data directory")?;
tar_and_gzip_dir(data_dir).with_context(|| "Failed to tar/gzip data directory")?;
create_archive(
vec![debian_binary_path, control_tar_gz_path, data_tar_gz_path],
&package_path,
)
.chain_err(|| "Failed to create package archive")?;
.with_context(|| "Failed to create package archive")?;
Ok(vec![package_path])
}

Expand All @@ -94,19 +95,20 @@ pub fn generate_folders(settings: &Settings, package_dir: &Path) -> crate::Resul
let bin_dir = data_dir.join("usr/bin");

common::copy_file(settings.binary_path(), &binary_dest)
.chain_err(|| "Failed to copy binary file")?;
transfer_resource_files(settings, &data_dir).chain_err(|| "Failed to copy resource files")?;
.with_context(|| "Failed to copy binary file")?;
transfer_resource_files(settings, &data_dir).with_context(|| "Failed to copy resource files")?;

settings
.copy_binaries(&bin_dir)
.chain_err(|| "Failed to copy external binaries")?;
.with_context(|| "Failed to copy external binaries")?;

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_icon_files(settings, &data_dir).with_context(|| "Failed to create icon files")?;
generate_desktop_file(settings, &data_dir).with_context(|| "Failed to create desktop file")?;

let use_bootstrapper = settings.debian_use_bootstrapper();
if use_bootstrapper {
generate_bootstrap_file(settings, &data_dir).chain_err(|| "Failed to generate bootstrap file")?;
generate_bootstrap_file(settings, &data_dir)
.with_context(|| "Failed to generate bootstrap file")?;
}

Ok(data_dir)
Expand Down Expand Up @@ -192,9 +194,13 @@ fn generate_desktop_file(settings: &Settings, data_dir: &Path) -> crate::Result<
}
let use_bootstrapper = settings.debian_use_bootstrapper();
write!(
file,
file,
"Exec={}\n",
if use_bootstrapper { format!("__{}-bootstrapper", bin_name) } else { bin_name.to_string() }
if use_bootstrapper {
format!("__{}-bootstrapper", bin_name)
} else {
bin_name.to_string()
}
)?;
write!(file, "Icon={}\n", bin_name)?;
write!(file, "Name={}\n", settings.bundle_name())?;
Expand Down
38 changes: 27 additions & 11 deletions cli/tauri-bundler/src/bundle/dmg_bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ use super::common;
use super::osx_bundle;
use crate::Settings;

use anyhow::Context;

use std::fs::{self, write};
use std::path::PathBuf;
use std::process::{Command, Stdio};

use crate::ResultExt;

// create script files to bundle project and execute bundle_script.
pub fn bundle_project(settings: &Settings) -> crate::Result<Vec<PathBuf>> {
// generate the app.app folder
Expand All @@ -26,9 +26,10 @@ pub fn bundle_project(settings: &Settings) -> crate::Result<Vec<PathBuf>> {

let support_directory_path = output_path.join("support");
if output_path.exists() {
fs::remove_dir_all(&output_path).chain_err(|| format!("Failed to remove old {}", dmg_name))?;
fs::remove_dir_all(&output_path)
.with_context(|| format!("Failed to remove old {}", dmg_name))?;
}
fs::create_dir_all(&support_directory_path).chain_err(|| {
fs::create_dir_all(&support_directory_path).with_context(|| {
format!(
"Failed to create output directory at {:?}",
support_directory_path
Expand All @@ -42,9 +43,18 @@ pub fn bundle_project(settings: &Settings) -> crate::Result<Vec<PathBuf>> {
common::print_bundling(format!("{:?}", &dmg_path.clone()).as_str())?;

// write the scripts
write(&bundle_script_path, include_str!("templates/dmg/bundle_dmg")).or_else(|e| Err(e.to_string()))?;
write(support_directory_path.join("template.applescript"), include_str!("templates/dmg/template.applescript"))?;
write(&license_script_path, include_str!("templates/dmg/dmg-license.py"))?;
write(
&bundle_script_path,
include_str!("templates/dmg/bundle_dmg"),
)?;
write(
support_directory_path.join("template.applescript"),
include_str!("templates/dmg/template.applescript"),
)?;
write(
&license_script_path,
include_str!("templates/dmg/dmg-license.py"),
)?;

// chmod script for execution
Command::new("chmod")
Expand All @@ -63,11 +73,15 @@ pub fn bundle_project(settings: &Settings) -> crate::Result<Vec<PathBuf>> {
"--volicon",
"../../../../icons/icon.icns",
"--icon",
&bundle_name, "180", "170",
&bundle_name,
"180",
"170",
"--app-drop-link",
"480", "170",
"480",
"170",
"--window-size",
"660", "400",
"660",
"400",
"--hide-extension",
&bundle_name,
];
Expand All @@ -86,7 +100,9 @@ pub fn bundle_project(settings: &Settings) -> crate::Result<Vec<PathBuf>> {
.expect("Failed to execute shell script");

if !status.success() {
Err(crate::Error::from("error running bundle_dmg.sh"))
Err(crate::Error::ShellScriptError(
"error running bundle_dmg.sh".to_owned(),
))
} else {
fs::rename(bundle_dir.join(dmg_name.clone()), dmg_path.clone())?;
Ok(vec![bundle_path, dmg_path])
Expand Down
Loading