mirror of
https://github.com/tauri-apps/tauri.git
synced 2024-11-28 12:27:16 +03:00
[WIP] Refactor(Crates) Tauri into individual Crates (#192)
* split tauri into 3 crates * fix macros * change builder into lib * cleanup package paths * add features back to lib * make build function public * add build-deps * rename and fix. * correct package name * move crates to root and refactor names * fix github action * move fixture to tauri-build * remove slash * add .vscode features * fix updater * fix updater mistake * fix(tauri) refactor buiilds * fix seperation * change get back to get * fix cfg and remove dead code warnings. * roll #160 into this pr * add credit * fix eof * chore(tauri) move assets to mod, loadAssets cfg outside its definition * chore(tauri) remove unused deps * update updater and cfg * fix(tauri) embedded-server with dead variable * add review refactors and remove cli form workgroup * chore(tauri) rename tauri to tauri-api and tauri-bundle to tauri * fix workspace and updater * rename update to updater
This commit is contained in:
parent
7bd0c2f06d
commit
e288180104
778
Cargo.lock
generated
778
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@ -1,5 +1,6 @@
|
||||
[workspace]
|
||||
members = [
|
||||
"tauri",
|
||||
"cli/tauri-cli"
|
||||
"tauri-api",
|
||||
"tauri-updater",
|
||||
]
|
||||
|
@ -1,7 +1,7 @@
|
||||
[package]
|
||||
name = "tauri-cli"
|
||||
version = "0.1.2"
|
||||
authors = ["George Burton <burtonageo@gmail.com>", "Lucas Fernandes Gonçalves Nogueira <lucas@quasar.dev>", "Daniel Thompson-Yvetot <denjell@sfosc.org>"]
|
||||
authors = ["George Burton <burtonageo@gmail.com>", "Lucas Fernandes Gonçalves Nogueira <lucas@quasar.dev>", "Daniel Thompson-Yvetot <denjell@sfosc.org>", "Tensor Programming <tensordeveloper@gmail.com>"]
|
||||
license = "MIT/Apache-2.0"
|
||||
keywords = ["bundle", "cargo", "tauri"]
|
||||
repository = "https://github.com/tauri-apps/tauri"
|
||||
@ -31,12 +31,14 @@ toml = "0.5.5"
|
||||
uuid = { version = "0.8", features = ["v5"] }
|
||||
walkdir = "2"
|
||||
|
||||
sha2 = "0.8"
|
||||
lazy_static = "1.4"
|
||||
handlebars = "2.0"
|
||||
attohttpc = "0.7.0"
|
||||
hex = "0.4"
|
||||
zip = "0.5"
|
||||
attohttpc = { version = "0.7.0" }
|
||||
|
||||
[target.'cfg(not(target_os = "linux"))'.dependencies]
|
||||
handlebars = { version = "2.0" }
|
||||
lazy_static = { version = "1.4" }
|
||||
zip = { version = "0.5" }
|
||||
sha2 = { version = "0.8" }
|
||||
hex = { version = "0.4" }
|
||||
|
||||
[dev-dependencies]
|
||||
tempfile = "3"
|
||||
@ -44,3 +46,8 @@ tempfile = "3"
|
||||
[[bin]]
|
||||
name = "cargo-tauri-cli"
|
||||
path = "src/main.rs"
|
||||
|
||||
[features]
|
||||
appimage = []
|
||||
ios = []
|
||||
dmg = []
|
@ -1,14 +1,19 @@
|
||||
#[cfg(feature = "appimage")]
|
||||
mod appimage_bundle;
|
||||
mod category;
|
||||
mod common;
|
||||
mod deb_bundle;
|
||||
#[cfg(feature = "dmg")]
|
||||
mod dmg_bundle;
|
||||
#[cfg(feature = "ios")]
|
||||
mod ios_bundle;
|
||||
#[cfg(target_os = "windows")]
|
||||
mod msi_bundle;
|
||||
mod osx_bundle;
|
||||
mod path_utils;
|
||||
mod rpm_bundle;
|
||||
mod settings;
|
||||
#[cfg(target_os = "windows")]
|
||||
mod wix;
|
||||
|
||||
pub use self::common::{print_error, print_finished};
|
||||
@ -20,15 +25,19 @@ pub fn bundle_project(settings: Settings) -> crate::Result<Vec<PathBuf>> {
|
||||
for package_type in settings.package_types()? {
|
||||
paths.append(&mut match package_type {
|
||||
PackageType::OsxBundle => osx_bundle::bundle_project(&settings)?,
|
||||
#[cfg(feature = "ios")]
|
||||
PackageType::IosBundle => ios_bundle::bundle_project(&settings)?,
|
||||
// use dmg bundler
|
||||
// PackageType::OsxBundle => dmg_bundle::bundle_project(&settings)?,
|
||||
#[cfg(target_os = "windows")]
|
||||
PackageType::WindowsMsi => msi_bundle::bundle_project(&settings)?,
|
||||
// force appimage on linux
|
||||
// PackageType::Deb => appimage_bundle::bundle_project(&settings)?,
|
||||
PackageType::Deb => deb_bundle::bundle_project(&settings)?,
|
||||
PackageType::Rpm => rpm_bundle::bundle_project(&settings)?,
|
||||
#[cfg(feature = "appimage")]
|
||||
PackageType::AppImage => appimage_bundle::bundle_project(&settings)?,
|
||||
#[cfg(feature = "dmg")]
|
||||
PackageType::Dmg => dmg_bundle::bundle_project(&settings)?,
|
||||
});
|
||||
}
|
||||
|
@ -13,11 +13,15 @@ use walkdir;
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
pub enum PackageType {
|
||||
OsxBundle,
|
||||
#[cfg(feature = "ios")]
|
||||
IosBundle,
|
||||
#[cfg(target_os = "windows")]
|
||||
WindowsMsi,
|
||||
Deb,
|
||||
Rpm,
|
||||
#[cfg(feature = "appimage")]
|
||||
AppImage,
|
||||
#[cfg(feature = "dmg")]
|
||||
Dmg,
|
||||
}
|
||||
|
||||
@ -26,11 +30,15 @@ impl PackageType {
|
||||
// Other types we may eventually want to support: apk
|
||||
match name {
|
||||
"deb" => Some(PackageType::Deb),
|
||||
#[cfg(feature = "ios")]
|
||||
"ios" => Some(PackageType::IosBundle),
|
||||
#[cfg(target_os = "windows")]
|
||||
"msi" => Some(PackageType::WindowsMsi),
|
||||
"osx" => Some(PackageType::OsxBundle),
|
||||
"rpm" => Some(PackageType::Rpm),
|
||||
#[cfg(feature = "appimage")]
|
||||
"appimage" => Some(PackageType::AppImage),
|
||||
#[cfg(feature = "dmg")]
|
||||
"dmg" => Some(PackageType::Dmg),
|
||||
_ => None,
|
||||
}
|
||||
@ -39,11 +47,15 @@ impl PackageType {
|
||||
pub fn short_name(&self) -> &'static str {
|
||||
match *self {
|
||||
PackageType::Deb => "deb",
|
||||
#[cfg(feature = "ios")]
|
||||
PackageType::IosBundle => "ios",
|
||||
#[cfg(target_os = "windows")]
|
||||
PackageType::WindowsMsi => "msi",
|
||||
PackageType::OsxBundle => "osx",
|
||||
PackageType::Rpm => "rpm",
|
||||
#[cfg(feature = "appimage")]
|
||||
PackageType::AppImage => "appimage",
|
||||
#[cfg(feature = "dmg")]
|
||||
PackageType::Dmg => "dmg",
|
||||
}
|
||||
}
|
||||
@ -55,7 +67,9 @@ impl PackageType {
|
||||
|
||||
const ALL_PACKAGE_TYPES: &[PackageType] = &[
|
||||
PackageType::Deb,
|
||||
#[cfg(feature = "ios")]
|
||||
PackageType::IosBundle,
|
||||
#[cfg(target_os = "windows")]
|
||||
PackageType::WindowsMsi,
|
||||
PackageType::OsxBundle,
|
||||
PackageType::Rpm,
|
||||
@ -328,8 +342,10 @@ impl Settings {
|
||||
};
|
||||
match target_os {
|
||||
"macos" => Ok(vec![PackageType::OsxBundle]),
|
||||
#[cfg(feature = "ios")]
|
||||
"ios" => Ok(vec![PackageType::IosBundle]),
|
||||
"linux" => Ok(vec![PackageType::Deb]), // TODO: Do Rpm too, once it's implemented.
|
||||
#[cfg(target_os = "windows")]
|
||||
"windows" => Ok(vec![PackageType::WindowsMsi]),
|
||||
os => bail!("Native {} bundles not yet supported.", os),
|
||||
}
|
||||
|
@ -27,10 +27,7 @@ serde_derive = "1.0"
|
||||
tiny_http = "0.6"
|
||||
phf = "0.8.0"
|
||||
includedir = "0.5.0"
|
||||
|
||||
[dependencies.tauri]
|
||||
path = "../../../../tauri"
|
||||
features = [ "all-api", "edge" ]
|
||||
tauri = { path = "../../../../tauri", features = [ "all-api", "edge" ] }
|
||||
|
||||
[features]
|
||||
dev-server = [ "tauri/dev-server" ]
|
||||
|
@ -10,7 +10,7 @@ module.exports = function () {
|
||||
ctx: {},
|
||||
tauri: {
|
||||
embeddedServer: {
|
||||
active: false
|
||||
active: true
|
||||
},
|
||||
bundle: {
|
||||
active: true
|
||||
|
22
tauri-api/Cargo.toml
Normal file
22
tauri-api/Cargo.toml
Normal file
@ -0,0 +1,22 @@
|
||||
[package]
|
||||
name = "tauri-api"
|
||||
version = "0.2.0"
|
||||
authors = ["Lucas Fernandes Gonçalves Nogueira <lucas@quasar.dev>", "Daniel Thompson-Yvetot <denjell@sfosc.org>", "Tensor Programming <tensordeveloper@gmail.com>"]
|
||||
license = "MIT"
|
||||
homepage = "https://tauri-apps.org"
|
||||
repository = "https://github.com/tauri-apps/tauri"
|
||||
description = "Make tiny, secure apps for all desktop platforms with Tauri"
|
||||
edition = "2018"
|
||||
exclude = ["test/fixture/**"]
|
||||
|
||||
[dependencies]
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
dirs = "2.0.2"
|
||||
ignore = "0.4.10"
|
||||
zip = "0.5.3"
|
||||
tempdir = "0.3"
|
||||
semver = "0.9"
|
||||
tempfile = "3"
|
||||
either = "1.5.3"
|
||||
tar = "0.4"
|
||||
flate2 = "1"
|
23
tauri/src/command.rs → tauri-api/src/command.rs
Executable file → Normal file
23
tauri/src/command.rs → tauri-api/src/command.rs
Executable file → Normal file
@ -1,9 +1,5 @@
|
||||
use web_view::WebView;
|
||||
|
||||
use std::process::{Child, Command, Stdio};
|
||||
|
||||
use crate::execute_promise;
|
||||
|
||||
pub fn get_output(cmd: String, args: Vec<String>, stdout: Stdio) -> Result<String, String> {
|
||||
Command::new(cmd)
|
||||
.args(args)
|
||||
@ -57,22 +53,3 @@ pub fn spawn_relative_command(
|
||||
let cmd = relative_command(command)?;
|
||||
Ok(Command::new(cmd).args(args).stdout(stdout).spawn()?)
|
||||
}
|
||||
|
||||
pub fn call<T: 'static>(
|
||||
webview: &mut WebView<'_, T>,
|
||||
command: String,
|
||||
args: Vec<String>,
|
||||
callback: String,
|
||||
error: String,
|
||||
) {
|
||||
execute_promise(
|
||||
webview,
|
||||
|| {
|
||||
get_output(command, args, Stdio::piped())
|
||||
.map_err(|err| format!("`{}`", err))
|
||||
.map(|output| format!("`{}`", output))
|
||||
},
|
||||
callback,
|
||||
error,
|
||||
);
|
||||
}
|
@ -2,6 +2,7 @@ use tempfile;
|
||||
|
||||
mod utils;
|
||||
use ignore::Walk;
|
||||
use serde::Serialize;
|
||||
use std::fs;
|
||||
use std::fs::metadata;
|
||||
use utils::get_dir_name_from_path;
|
0
tauri/src/dir/utils.rs → tauri-api/src/dir/utils.rs
Executable file → Normal file
0
tauri/src/dir/utils.rs → tauri-api/src/dir/utils.rs
Executable file → Normal file
6
tauri-api/src/lib.rs
Normal file
6
tauri-api/src/lib.rs
Normal file
@ -0,0 +1,6 @@
|
||||
pub mod command;
|
||||
pub mod dir;
|
||||
pub mod file;
|
||||
pub mod rpc;
|
||||
|
||||
pub mod version;
|
0
tauri/src/rpc.rs → tauri-api/src/rpc.rs
Executable file → Normal file
0
tauri/src/rpc.rs → tauri-api/src/rpc.rs
Executable file → Normal file
22
tauri-updater/Cargo.toml
Normal file
22
tauri-updater/Cargo.toml
Normal file
@ -0,0 +1,22 @@
|
||||
[package]
|
||||
name = "tauri-updater"
|
||||
version = "0.2.0"
|
||||
authors = ["Lucas Fernandes Gonçalves Nogueira <lucas@quasar.dev>", "Daniel Thompson-Yvetot <denjell@sfosc.org>", "Tensor Programming <tensordeveloper@gmail.com>"]
|
||||
license = "MIT"
|
||||
homepage = "https://tauri-apps.org"
|
||||
repository = "https://github.com/tauri-apps/tauri"
|
||||
description = "Updater for Tauri"
|
||||
edition = "2018"
|
||||
exclude = ["test/fixture/**"]
|
||||
|
||||
[dependencies]
|
||||
reqwest = "0.9"
|
||||
hyper-old-types = "0.11.0"
|
||||
pbr = "1"
|
||||
serde_json = "1.0.44"
|
||||
serde = "1.0"
|
||||
zip = "0.5.3"
|
||||
sysinfo = "0.10"
|
||||
tempdir = "0.3"
|
||||
|
||||
tauri-api = { version = "0.2", path = "../tauri-api" }
|
7
tauri-updater/src/lib.rs
Normal file
7
tauri-updater/src/lib.rs
Normal file
@ -0,0 +1,7 @@
|
||||
#[macro_use]
|
||||
pub mod macros;
|
||||
|
||||
pub mod http;
|
||||
pub mod platform;
|
||||
pub mod process;
|
||||
pub mod updater;
|
@ -2,8 +2,8 @@ use std::env;
|
||||
use std::fs;
|
||||
use std::path::PathBuf;
|
||||
|
||||
use crate::file::{Extract, Move};
|
||||
use crate::http;
|
||||
use tauri_api::file::{Extract, Move};
|
||||
|
||||
pub mod github;
|
||||
|
||||
@ -129,7 +129,7 @@ impl UpdateBuilder {
|
||||
///
|
||||
/// ```
|
||||
/// # use tauri::updater::Update;
|
||||
/// # fn run() -> Result<(), Box<::std::error::Error>> {
|
||||
/// # fn run() -> Result<(), Box<dyn std::error::Error>> {
|
||||
/// Update::configure()?
|
||||
/// .bin_path_in_archive("bin/myapp")
|
||||
/// # .build()?;
|
@ -1,8 +1,8 @@
|
||||
use crate::file;
|
||||
use crate::http;
|
||||
use crate::version;
|
||||
use reqwest;
|
||||
use std;
|
||||
use tauri_api::file;
|
||||
use tauri_api::version;
|
||||
use zip::result::ZipError;
|
||||
|
||||
#[derive(Debug)]
|
@ -1,7 +1,7 @@
|
||||
[package]
|
||||
name = "tauri"
|
||||
version = "0.2.0"
|
||||
authors = ["Lucas Fernandes Gonçalves Nogueira <lucas@quasar.dev>", "Daniel Thompson-Yvetot <denjell@sfosc.org>"]
|
||||
authors = ["Lucas Fernandes Gonçalves Nogueira <lucas@quasar.dev>", "Daniel Thompson-Yvetot <denjell@sfosc.org>", "Tensor Programming <tensordeveloper@gmail.com>"]
|
||||
license = "MIT"
|
||||
homepage = "https://tauri-apps.org"
|
||||
repository = "https://github.com/tauri-apps/tauri"
|
||||
@ -10,40 +10,26 @@ edition = "2018"
|
||||
exclude = ["test/fixture/**"]
|
||||
|
||||
[dependencies]
|
||||
web-view = "0.5.4"
|
||||
serde_json = "1.0.44"
|
||||
serde = "1.0"
|
||||
serde_derive = "1.0"
|
||||
dirs = "2.0.2"
|
||||
ignore = "0.4.10"
|
||||
phf = "0.8.0"
|
||||
threadpool = "1.7"
|
||||
rand = "0.7"
|
||||
zip = "0.5.3"
|
||||
tempdir = "0.3"
|
||||
semver = "0.9"
|
||||
tempfile = "3"
|
||||
either = "1.5.3"
|
||||
tar = "0.4"
|
||||
flate2 = "1"
|
||||
sysinfo = "0.10"
|
||||
webbrowser = "0.5.2"
|
||||
uuid = { version = "0.8.1", features = ["v4"] }
|
||||
lazy_static = "1.4.0"
|
||||
web-view = "0.5.4"
|
||||
tauri_includedir = "0.5.0"
|
||||
tiny_http = "0.6"
|
||||
phf = "0.8.0"
|
||||
base64 = "0.11.0"
|
||||
|
||||
reqwest = {version = "0.9", optional = true }
|
||||
hyper-old-types = {version = "0.11.0", optional = true }
|
||||
pbr = {version = "1", optional = true }
|
||||
webbrowser = "0.5.2"
|
||||
lazy_static = "1.4.0"
|
||||
tiny_http = "0.6"
|
||||
threadpool = "1.7"
|
||||
uuid = { version = "0.8.1", features = ["v4"] }
|
||||
tauri-api = { version = "0.2", path = "../tauri-api" }
|
||||
|
||||
[build-dependencies]
|
||||
tauri_includedir_codegen = "0.5.1"
|
||||
serde_json = "1.0.44"
|
||||
serde = "1.0"
|
||||
serde_derive = "1.0"
|
||||
rand = "0.7"
|
||||
|
||||
[features]
|
||||
edge = ["web-view/edge"]
|
||||
@ -60,4 +46,3 @@ setTitle = []
|
||||
execute = []
|
||||
open = []
|
||||
emit = []
|
||||
updater = []
|
||||
|
87
tauri/build.rs
Executable file → Normal file
87
tauri/build.rs
Executable file → Normal file
@ -1,90 +1,29 @@
|
||||
#[macro_use]
|
||||
extern crate serde_derive;
|
||||
extern crate serde_json;
|
||||
|
||||
use std::env;
|
||||
use std::io::Write;
|
||||
|
||||
#[path = "src/config.rs"]
|
||||
mod config;
|
||||
#[cfg(not(feature = "dev-server"))]
|
||||
extern crate tauri_includedir_codegen;
|
||||
#[cfg(feature = "embedded-server")]
|
||||
mod tcp;
|
||||
|
||||
fn main() {
|
||||
let out_dir = env::var("OUT_DIR").unwrap();
|
||||
let dest_path = std::path::Path::new(&out_dir).join("tauri_src");
|
||||
let mut file = std::fs::File::create(&dest_path).unwrap();
|
||||
#[cfg(not(feature = "dev-server"))]
|
||||
#[macro_use]
|
||||
extern crate serde_derive;
|
||||
#[cfg(not(feature = "dev-server"))]
|
||||
extern crate serde_json;
|
||||
|
||||
let tauri_src: String;
|
||||
let config = config::get();
|
||||
|
||||
#[cfg(not(any(feature = "embedded-server", feature = "no-server")))]
|
||||
{
|
||||
tauri_src = if config.dev_path.starts_with("http") {
|
||||
config.dev_path
|
||||
} else {
|
||||
let dev_path = std::path::Path::new(&config.dev_path).join("index.tauri.html");
|
||||
println!("{}", format!("cargo:rerun-if-changed={:?}", dev_path));
|
||||
std::fs::read_to_string(dev_path).unwrap()
|
||||
};
|
||||
}
|
||||
#[cfg(not(feature = "dev-server"))]
|
||||
#[path = "src/config.rs"]
|
||||
mod config;
|
||||
|
||||
pub fn main() {
|
||||
#[cfg(not(feature = "dev-server"))]
|
||||
{
|
||||
match env::var("TAURI_DIST_DIR") {
|
||||
match std::env::var("TAURI_DIST_DIR") {
|
||||
Ok(dist_path) => {
|
||||
let config = config::get();
|
||||
// include assets
|
||||
tauri_includedir_codegen::start("ASSETS")
|
||||
.dir(dist_path, tauri_includedir_codegen::Compression::None)
|
||||
.build("data.rs", config.inlined_assets)
|
||||
.unwrap()
|
||||
.expect("failed to build data.rs")
|
||||
}
|
||||
Err(_e) => panic!("Build error: Couldn't find ENV: {}", _e),
|
||||
Err(e) => panic!("Build error: Couldn't find ENV: {}", e),
|
||||
}
|
||||
}
|
||||
#[cfg(feature = "embedded-server")]
|
||||
{
|
||||
// define URL
|
||||
let port;
|
||||
let port_valid;
|
||||
if config.embedded_server.port == "random" {
|
||||
match tcp::get_available_port() {
|
||||
Some(available_port) => {
|
||||
port = available_port.to_string();
|
||||
port_valid = true;
|
||||
}
|
||||
None => {
|
||||
port = "0".to_string();
|
||||
port_valid = false;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
port = config.embedded_server.port;
|
||||
port_valid = crate::tcp::port_is_available(
|
||||
port
|
||||
.parse::<u16>()
|
||||
.expect(&format!("Invalid port {}", port)),
|
||||
);
|
||||
}
|
||||
if port_valid {
|
||||
let mut url = format!("{}:{}", config.embedded_server.host, port);
|
||||
if !url.starts_with("http") {
|
||||
url = format!("http://{}", url);
|
||||
}
|
||||
tauri_src = url.to_string();
|
||||
} else {
|
||||
panic!(format!("Port {} is not valid or not open", port));
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "no-server")]
|
||||
{
|
||||
let index_path = std::path::Path::new(env!("TAURI_DIST_DIR")).join("index.tauri.html");
|
||||
println!("{}", format!("cargo:rerun-if-changed={:?}", index_path));
|
||||
tauri_src = std::fs::read_to_string(index_path).unwrap();
|
||||
}
|
||||
|
||||
file.write_all(tauri_src.as_bytes()).unwrap();
|
||||
}
|
||||
|
@ -1,12 +1,65 @@
|
||||
pub(crate) fn run(application: &mut crate::App) {
|
||||
let debug = cfg!(debug_assertions);
|
||||
let config = crate::config::get();
|
||||
let tauri_src = include_str!(concat!(env!("OUT_DIR"), "/tauri_src"));
|
||||
let content = if tauri_src.starts_with("http://") || tauri_src.starts_with("https://") {
|
||||
web_view::Content::Url(tauri_src)
|
||||
} else {
|
||||
web_view::Content::Html(tauri_src)
|
||||
};
|
||||
|
||||
let content;
|
||||
#[cfg(not(any(feature = "embedded-server", feature = "no-server")))]
|
||||
{
|
||||
content = if config.dev_path.starts_with("http") {
|
||||
web_view::Content::Url(config.dev_path)
|
||||
} else {
|
||||
let dev_path = std::path::Path::new(&config.dev_path).join("index.tauri.html");
|
||||
web_view::Content::Html(
|
||||
std::fs::read_to_string(dev_path).expect("failed to build index.tauri.html"),
|
||||
)
|
||||
};
|
||||
}
|
||||
|
||||
#[cfg(feature = "embedded-server")]
|
||||
let server_url;
|
||||
|
||||
#[cfg(feature = "embedded-server")]
|
||||
{
|
||||
// define URL
|
||||
let port;
|
||||
let port_valid;
|
||||
if config.embedded_server.port == "random" {
|
||||
match crate::tcp::get_available_port() {
|
||||
Some(available_port) => {
|
||||
port = available_port.to_string();
|
||||
port_valid = true;
|
||||
}
|
||||
None => {
|
||||
port = "0".to_string();
|
||||
port_valid = false;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
port = config.embedded_server.port;
|
||||
port_valid = crate::tcp::port_is_available(
|
||||
port
|
||||
.parse::<u16>()
|
||||
.expect(&format!("Invalid port {}", port)),
|
||||
);
|
||||
}
|
||||
if port_valid {
|
||||
let mut url = format!("{}:{}", config.embedded_server.host, port);
|
||||
if !url.starts_with("http") {
|
||||
url = format!("http://{}", url);
|
||||
}
|
||||
server_url = url.clone();
|
||||
content = web_view::Content::Url(url.to_string());
|
||||
} else {
|
||||
panic!(format!("Port {} is not valid or not open", port));
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "no-server")]
|
||||
{
|
||||
let index_path = std::path::Path::new(env!("TAURI_DIST_DIR")).join("index.tauri.html");
|
||||
content =
|
||||
web_view::Content::Html(std::fs::read_to_string(index_path).expect("failed to read string"));
|
||||
}
|
||||
|
||||
#[cfg(feature = "updater")]
|
||||
{
|
||||
@ -29,7 +82,7 @@ pub(crate) fn run(application: &mut crate::App) {
|
||||
.debug(debug)
|
||||
.user_data(())
|
||||
.invoke_handler(|webview, arg| {
|
||||
if !crate::api::handler(webview, arg) {
|
||||
if !crate::endpoints::handle(webview, arg) {
|
||||
application.run_invoke_handler(webview, arg);
|
||||
}
|
||||
// the first command is always the `init`, so we can safely run the setup hook here
|
||||
@ -45,30 +98,30 @@ pub(crate) fn run(application: &mut crate::App) {
|
||||
.unwrap();
|
||||
|
||||
#[cfg(feature = "dev-server")]
|
||||
webview.handle()
|
||||
.dispatch(|_webview| {
|
||||
_webview.eval(include_str!(concat!(env!("TAURI_DIR"), "/tauri.js")))
|
||||
})
|
||||
webview
|
||||
.handle()
|
||||
.dispatch(|_webview| _webview.eval(include_str!(concat!(env!("TAURI_DIR"), "/tauri.js"))))
|
||||
.unwrap();
|
||||
|
||||
#[cfg(feature = "embedded-server")]
|
||||
{
|
||||
std::thread::spawn(move || {
|
||||
let server = tiny_http::Server::http(
|
||||
tauri_src
|
||||
server_url
|
||||
.clone()
|
||||
.replace("http://", "")
|
||||
.replace("https://", ""),
|
||||
)
|
||||
.expect(&format!(
|
||||
"Could not start embedded server with the specified url: {}",
|
||||
tauri_src
|
||||
server_url
|
||||
));
|
||||
for request in server.incoming_requests() {
|
||||
let mut url = request.url().to_string();
|
||||
if url == "/" {
|
||||
url = "/index.tauri.html".to_string();
|
||||
let url = match request.url() {
|
||||
"/" => "/index.tauri.html",
|
||||
url => url,
|
||||
}
|
||||
.to_string();
|
||||
request
|
||||
.respond(crate::server::asset_response(&url))
|
||||
.unwrap();
|
||||
|
1
tauri/src/assets.rs
Normal file
1
tauri/src/assets.rs
Normal file
@ -0,0 +1 @@
|
||||
include!(concat!(env!("OUT_DIR"), "/data.rs"));
|
@ -84,5 +84,6 @@ fn default_dev_path() -> String {
|
||||
}
|
||||
|
||||
pub fn get() -> Config {
|
||||
serde_json::from_str(include_str!(concat!(env!("TAURI_DIR"), "/config.json"))).unwrap()
|
||||
serde_json::from_str(include_str!(concat!(env!("TAURI_DIR"), "/config.json")))
|
||||
.expect("failed to create config.json")
|
||||
}
|
||||
|
@ -2,11 +2,8 @@ mod cmd;
|
||||
|
||||
use web_view::WebView;
|
||||
|
||||
#[cfg(not(any(feature = "dev-server", feature = "embedded-server")))]
|
||||
include!(concat!(env!("OUT_DIR"), "/data.rs"));
|
||||
|
||||
#[allow(unused_variables)]
|
||||
pub fn handler<T: 'static>(webview: &mut WebView<'_, T>, arg: &str) -> bool {
|
||||
pub(crate) fn handle<T: 'static>(webview: &mut WebView<'_, T>, arg: &str) -> bool {
|
||||
use cmd::Cmd::*;
|
||||
match serde_json::from_str(arg) {
|
||||
Err(_) => false,
|
||||
@ -56,7 +53,7 @@ pub fn handler<T: 'static>(webview: &mut WebView<'_, T>, arg: &str) -> bool {
|
||||
callback,
|
||||
error,
|
||||
} => {
|
||||
super::file_system::read_text_file(webview, path, callback, error);
|
||||
crate::file_system::read_text_file(webview, path, callback, error);
|
||||
}
|
||||
#[cfg(any(feature = "all-api", feature = "readBinaryFile"))]
|
||||
ReadBinaryFile {
|
||||
@ -64,7 +61,7 @@ pub fn handler<T: 'static>(webview: &mut WebView<'_, T>, arg: &str) -> bool {
|
||||
callback,
|
||||
error,
|
||||
} => {
|
||||
super::file_system::read_binary_file(webview, path, callback, error);
|
||||
crate::file_system::read_binary_file(webview, path, callback, error);
|
||||
}
|
||||
#[cfg(any(feature = "all-api", feature = "writeFile"))]
|
||||
WriteFile {
|
||||
@ -73,7 +70,7 @@ pub fn handler<T: 'static>(webview: &mut WebView<'_, T>, arg: &str) -> bool {
|
||||
callback,
|
||||
error,
|
||||
} => {
|
||||
super::file_system::write_file(webview, file, contents, callback, error);
|
||||
crate::file_system::write_file(webview, file, contents, callback, error);
|
||||
}
|
||||
#[cfg(any(feature = "all-api", feature = "listDirs"))]
|
||||
ListDirs {
|
||||
@ -81,7 +78,7 @@ pub fn handler<T: 'static>(webview: &mut WebView<'_, T>, arg: &str) -> bool {
|
||||
callback,
|
||||
error,
|
||||
} => {
|
||||
super::file_system::list_dirs(webview, path, callback, error);
|
||||
crate::file_system::list_dirs(webview, path, callback, error);
|
||||
}
|
||||
#[cfg(any(feature = "all-api", feature = "listFiles"))]
|
||||
ListFiles {
|
||||
@ -89,7 +86,7 @@ pub fn handler<T: 'static>(webview: &mut WebView<'_, T>, arg: &str) -> bool {
|
||||
callback,
|
||||
error,
|
||||
} => {
|
||||
super::file_system::list(webview, path, callback, error);
|
||||
crate::file_system::list(webview, path, callback, error);
|
||||
}
|
||||
#[cfg(any(feature = "all-api", feature = "setTitle"))]
|
||||
SetTitle { title } => {
|
||||
@ -102,11 +99,11 @@ pub fn handler<T: 'static>(webview: &mut WebView<'_, T>, arg: &str) -> bool {
|
||||
callback,
|
||||
error,
|
||||
} => {
|
||||
super::command::call(webview, command, args, callback, error);
|
||||
crate::call(webview, command, args, callback, error);
|
||||
}
|
||||
#[cfg(any(feature = "all-api", feature = "open"))]
|
||||
Open { uri } => {
|
||||
super::spawn(move || {
|
||||
crate::spawn(move || {
|
||||
webbrowser::open(&uri).unwrap();
|
||||
});
|
||||
}
|
||||
@ -155,45 +152,53 @@ pub fn handler<T: 'static>(webview: &mut WebView<'_, T>, arg: &str) -> bool {
|
||||
Emit { event, payload } => {
|
||||
crate::event::on_event(event, payload);
|
||||
}
|
||||
#[cfg(not(any(feature = "dev-server", feature = "embedded-server")))]
|
||||
LoadAsset {
|
||||
asset,
|
||||
asset_type,
|
||||
callback,
|
||||
error,
|
||||
} => {
|
||||
#[cfg(not(any(feature = "dev-server", feature = "embedded-server")))]
|
||||
{
|
||||
let handle = webview.handle();
|
||||
crate::execute_promise(
|
||||
webview,
|
||||
move || {
|
||||
let read_asset = ASSETS.get(&format!("{}{}{}", env!("TAURI_DIST_DIR"), if asset.starts_with("/") { "" } else { "/" }, asset));
|
||||
if read_asset.is_err() {
|
||||
return Err(r#""Asset not found""#.to_string());
|
||||
}
|
||||
let handle = webview.handle();
|
||||
crate::execute_promise(
|
||||
webview,
|
||||
move || {
|
||||
let read_asset = crate::assets::ASSETS.get(&format!(
|
||||
"{}{}{}",
|
||||
env!("TAURI_DIST_DIR"),
|
||||
if asset.starts_with("/") { "" } else { "/" },
|
||||
asset
|
||||
));
|
||||
if read_asset.is_err() {
|
||||
return Err(r#""Asset not found""#.to_string());
|
||||
}
|
||||
|
||||
if asset_type == "image" {
|
||||
let ext = if asset.ends_with("gif") {
|
||||
"gif"
|
||||
} else if asset.ends_with("png") {
|
||||
"png"
|
||||
} else {
|
||||
"jpeg"
|
||||
};
|
||||
Ok(format!("`data:image/{};base64,{}`", ext, base64::encode(&read_asset.unwrap().into_owned())))
|
||||
if asset_type == "image" {
|
||||
let ext = if asset.ends_with("gif") {
|
||||
"gif"
|
||||
} else if asset.ends_with("png") {
|
||||
"png"
|
||||
} else {
|
||||
handle
|
||||
.dispatch(move |_webview| {
|
||||
_webview.eval(&std::str::from_utf8(&read_asset.unwrap().into_owned()).unwrap())
|
||||
})
|
||||
.map_err(|err| format!("`{}`", err))
|
||||
.map(|_| r#""Asset loaded successfully""#.to_string())
|
||||
}
|
||||
},
|
||||
callback,
|
||||
error,
|
||||
);
|
||||
}
|
||||
"jpeg"
|
||||
};
|
||||
Ok(format!(
|
||||
"`data:image/{};base64,{}`",
|
||||
ext,
|
||||
base64::encode(&read_asset.unwrap().into_owned())
|
||||
))
|
||||
} else {
|
||||
handle
|
||||
.dispatch(move |_webview| {
|
||||
_webview
|
||||
.eval(&std::str::from_utf8(&read_asset.unwrap().into_owned()).unwrap())
|
||||
})
|
||||
.map_err(|err| format!("`{}`", err))
|
||||
.map(|_| r#""Asset loaded successfully""#.to_string())
|
||||
}
|
||||
},
|
||||
callback,
|
||||
error,
|
||||
);
|
||||
}
|
||||
}
|
||||
true
|
@ -63,6 +63,7 @@ pub enum Cmd {
|
||||
event: String,
|
||||
payload: String,
|
||||
},
|
||||
#[cfg(not(any(feature = "dev-server", feature = "embedded-server")))]
|
||||
LoadAsset {
|
||||
asset: String,
|
||||
asset_type: String,
|
15
tauri/src/file_system.rs
Executable file → Normal file
15
tauri/src/file_system.rs
Executable file → Normal file
@ -1,8 +1,7 @@
|
||||
use web_view::WebView;
|
||||
|
||||
use crate::dir;
|
||||
use crate::execute_promise;
|
||||
use crate::file;
|
||||
use tauri_api::dir;
|
||||
use tauri_api::file;
|
||||
|
||||
use std::fs::File;
|
||||
use std::io::Write;
|
||||
@ -13,7 +12,7 @@ pub fn list<T: 'static>(
|
||||
callback: String,
|
||||
error: String,
|
||||
) {
|
||||
execute_promise(
|
||||
crate::execute_promise(
|
||||
webview,
|
||||
move || {
|
||||
dir::walk_dir(path.to_string())
|
||||
@ -30,7 +29,7 @@ pub fn list_dirs<T: 'static>(
|
||||
callback: String,
|
||||
error: String,
|
||||
) {
|
||||
execute_promise(
|
||||
crate::execute_promise(
|
||||
webview,
|
||||
move || {
|
||||
dir::list_dir_contents(&path)
|
||||
@ -48,7 +47,7 @@ pub fn write_file<T: 'static>(
|
||||
callback: String,
|
||||
error: String,
|
||||
) {
|
||||
execute_promise(
|
||||
crate::execute_promise(
|
||||
webview,
|
||||
move || {
|
||||
File::create(file)
|
||||
@ -70,7 +69,7 @@ pub fn read_text_file<T: 'static>(
|
||||
callback: String,
|
||||
error: String,
|
||||
) {
|
||||
execute_promise(
|
||||
crate::execute_promise(
|
||||
webview,
|
||||
move || {
|
||||
file::read_string(path).and_then(|f| {
|
||||
@ -90,7 +89,7 @@ pub fn read_binary_file<T: 'static>(
|
||||
callback: String,
|
||||
error: String,
|
||||
) {
|
||||
execute_promise(
|
||||
crate::execute_promise(
|
||||
webview,
|
||||
move || {
|
||||
file::read_binary(path).and_then(|f| {
|
||||
|
@ -1,65 +1,81 @@
|
||||
#[macro_use]
|
||||
extern crate serde_derive;
|
||||
|
||||
#[cfg(feature = "updater")]
|
||||
#[macro_use]
|
||||
mod macros;
|
||||
extern crate serde_json;
|
||||
|
||||
#[macro_use]
|
||||
extern crate lazy_static;
|
||||
|
||||
extern crate web_view;
|
||||
|
||||
pub mod api;
|
||||
mod app;
|
||||
pub mod command;
|
||||
mod endpoints;
|
||||
pub mod config;
|
||||
pub mod dir;
|
||||
pub mod event;
|
||||
pub mod file;
|
||||
pub mod file_system;
|
||||
#[cfg(feature = "updater")]
|
||||
pub mod http;
|
||||
#[cfg(feature = "updater")]
|
||||
pub mod platform;
|
||||
pub mod process;
|
||||
pub mod rpc;
|
||||
pub mod salt;
|
||||
|
||||
#[cfg(feature = "embedded-server")]
|
||||
pub mod server;
|
||||
#[cfg(feature = "updater")]
|
||||
pub mod updater;
|
||||
|
||||
pub mod version;
|
||||
pub use app::*;
|
||||
#[allow(dead_code)]
|
||||
mod file_system;
|
||||
#[allow(dead_code)]
|
||||
mod salt;
|
||||
|
||||
use web_view::*;
|
||||
#[cfg(feature = "embedded-server")]
|
||||
mod tcp;
|
||||
|
||||
#[cfg(not(feature = "dev-server"))]
|
||||
pub mod assets;
|
||||
|
||||
mod app;
|
||||
|
||||
use std::process::Stdio;
|
||||
|
||||
use threadpool::ThreadPool;
|
||||
|
||||
pub use app::*;
|
||||
use web_view::*;
|
||||
|
||||
pub use tauri_api as api;
|
||||
|
||||
thread_local!(static POOL: ThreadPool = ThreadPool::new(4));
|
||||
|
||||
pub fn spawn<F: FnOnce() -> () + Send + 'static>(what: F) {
|
||||
pub fn spawn<F: FnOnce() -> () + Send + 'static>(task: F) {
|
||||
POOL.with(|thread| {
|
||||
thread.execute(move || {
|
||||
what();
|
||||
task();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
pub fn execute_promise<T: 'static, F: FnOnce() -> Result<String, String> + Send + 'static>(
|
||||
webview: &mut WebView<'_, T>,
|
||||
what: F,
|
||||
task: F,
|
||||
callback: String,
|
||||
error: String,
|
||||
) {
|
||||
let handle = webview.handle();
|
||||
POOL.with(|thread| {
|
||||
thread.execute(move || {
|
||||
let callback_string = rpc::format_callback_result(what(), callback, error);
|
||||
let callback_string = api::rpc::format_callback_result(task(), callback, error);
|
||||
handle
|
||||
.dispatch(move |_webview| _webview.eval(callback_string.as_str()))
|
||||
.unwrap()
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
pub fn call<T: 'static>(
|
||||
webview: &mut WebView<'_, T>,
|
||||
command: String,
|
||||
args: Vec<String>,
|
||||
callback: String,
|
||||
error: String,
|
||||
) {
|
||||
execute_promise(
|
||||
webview,
|
||||
|| {
|
||||
api::command::get_output(command, args, Stdio::piped())
|
||||
.map_err(|err| format!("`{}`", err))
|
||||
.map(|output| format!("`{}`", output))
|
||||
},
|
||||
callback,
|
||||
error,
|
||||
);
|
||||
}
|
||||
|
@ -1,9 +1,7 @@
|
||||
use tiny_http::{Header, Response};
|
||||
|
||||
include!(concat!(env!("OUT_DIR"), "/data.rs"));
|
||||
|
||||
pub fn asset_response(path: &str) -> Response<std::io::Cursor<Vec<u8>>> {
|
||||
let asset = ASSETS
|
||||
let asset = crate::assets::ASSETS
|
||||
.get(&format!("{}{}", env!("TAURI_DIST_DIR"), path))
|
||||
.unwrap()
|
||||
.into_owned();
|
||||
|
Loading…
Reference in New Issue
Block a user