From 45a5b6a1a646fd55bfb31895b46e85f852b44072 Mon Sep 17 00:00:00 2001 From: Tim-Paik Date: Mon, 4 Apr 2022 23:53:17 +0800 Subject: [PATCH] update cli --- neutauri_bundler/Cargo.toml | 4 +++ neutauri_bundler/src/bundle.rs | 49 +++++++++++++++++++++++++++ neutauri_bundler/src/main.rs | 61 ++-------------------------------- 3 files changed, 56 insertions(+), 58 deletions(-) create mode 100644 neutauri_bundler/src/bundle.rs diff --git a/neutauri_bundler/Cargo.toml b/neutauri_bundler/Cargo.toml index a0d2b2a..9e49aaa 100644 --- a/neutauri_bundler/Cargo.toml +++ b/neutauri_bundler/Cargo.toml @@ -11,3 +11,7 @@ new_mime_guess = "4.0" serde = {version = "1.0", features = ["derive"]} toml = "0.5" wry = "0.12" + +[profile.release.package.wry] +debug = true +debug-assertions = true \ No newline at end of file diff --git a/neutauri_bundler/src/bundle.rs b/neutauri_bundler/src/bundle.rs new file mode 100644 index 0000000..f885186 --- /dev/null +++ b/neutauri_bundler/src/bundle.rs @@ -0,0 +1,49 @@ +use crate::data; +use std::{fs, io::Write}; + +#[cfg(windows)] +const RUNTIME_DATA: &[u8] = include_bytes!("../../target/release/neutauri_runtime.exe"); +#[cfg(not(windows))] +const RUNTIME_DATA: &[u8] = include_bytes!("../../target/release/neutauri_runtime"); + +fn options() -> fs::OpenOptions { + #[cfg(not(windows))] + use std::os::unix::prelude::OpenOptionsExt; + let mut options = fs::OpenOptions::new(); + options.write(true); + options.create(true); + options.truncate(true); + #[cfg(not(windows))] + options.mode(0o755); + options +} + +pub fn bundle(config_path: String) -> std::io::Result<()> { + let config_path = std::path::Path::new(&config_path).canonicalize()?; + let config: data::Config = toml::from_str(fs::read_to_string(&config_path)?.as_str()) + .map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))?; + let source = match config_path.parent() { + Some(path) => path.join(&config.source).canonicalize()?, + None => config.source.canonicalize()?, + }; + let target = match config_path.parent() { + Some(path) => data::normalize_path(&path.join(&config.target)), + None => data::normalize_path(&config.target), + }; + let target = if target.extension() == None && cfg!(windows) { + target.with_extension("exe") + } else { + target + }; + if target.extension() == Some(std::ffi::OsStr::new("neu")) { + data::pack(config_path)?; + return Ok(()); + } + let data = data::Data::build_from_dir(source, config.window_attr()?, config.webview_attr()?)?; + let mut f = options().open(&target)?; + f.write_all(RUNTIME_DATA)?; + f.write_all(&data)?; + f.sync_all()?; + f.flush()?; + Ok(()) +} diff --git a/neutauri_bundler/src/main.rs b/neutauri_bundler/src/main.rs index 7354f2e..d0f4014 100644 --- a/neutauri_bundler/src/main.rs +++ b/neutauri_bundler/src/main.rs @@ -1,65 +1,10 @@ -use std::{fs, io::Write}; - +mod bundle; mod data; -#[cfg(windows)] -const RUNTIME_DATA: &[u8] = include_bytes!("../../target/release/neutauri_runtime.exe"); -#[cfg(not(windows))] -const RUNTIME_DATA: &[u8] = include_bytes!("../../target/release/neutauri_runtime"); - -#[cfg(not(windows))] -fn options() -> fs::OpenOptions { - use std::os::unix::prelude::OpenOptionsExt; - let mut options = fs::OpenOptions::new(); - options.write(true); - options.create(true); - options.truncate(true); - options.mode(0o755); - options -} - -#[cfg(windows)] -fn options() -> fs::OpenOptions { - let mut options = fs::OpenOptions::new(); - options.write(true); - options.create(true); - options.truncate(true); - options -} - -fn main() -> wry::Result<()> { +fn main() -> std::io::Result<()> { let arg = std::env::args() .nth(1) .unwrap_or_else(|| "neutauri.toml".into()); - if arg == "--help" || arg == "-h" { - println!("Usage: neutauri_bundler [neutauri.toml]"); - return Ok(()); - } - let config_path = std::path::Path::new(&arg).canonicalize()?; - let config: data::Config = toml::from_str(fs::read_to_string(&config_path)?.as_str()) - .map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))?; - let source = match config_path.parent() { - Some(path) => path.join(&config.source).canonicalize()?, - None => config.source.canonicalize()?, - }; - let target = match config_path.parent() { - Some(path) => data::normalize_path(&path.join(&config.target)), - None => data::normalize_path(&config.target), - }; - let target = if target.extension() == None && cfg!(windows) { - target.with_extension("exe") - } else { - target - }; - if target.extension() == Some(std::ffi::OsStr::new("neu")) { - data::pack(arg)?; - return Ok(()); - } - let data = data::Data::build_from_dir(source, config.window_attr()?, config.webview_attr()?)?; - let mut f = options().open(&target)?; - f.write_all(RUNTIME_DATA)?; - f.write_all(&data)?; - f.sync_all()?; - f.flush()?; + bundle::bundle(arg)?; Ok(()) }