feat: 增加跨平台打包脚本
@@ -0,0 +1,16 @@
|
||||
[package]
|
||||
name = "capacity-report-desktop"
|
||||
version = "2.0.2"
|
||||
description = "CapacityReport desktop shell"
|
||||
authors = ["NIXEVOL"]
|
||||
edition = "2021"
|
||||
|
||||
[build-dependencies]
|
||||
tauri-build = { version = "2", features = [] }
|
||||
|
||||
[dependencies]
|
||||
tauri = { version = "2", features = [] }
|
||||
tauri-plugin-shell = "2"
|
||||
|
||||
[features]
|
||||
default = []
|
||||
@@ -0,0 +1,3 @@
|
||||
fn main() {
|
||||
tauri_build::build()
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"$schema": "../gen/schemas/desktop-schema.json",
|
||||
"identifier": "default",
|
||||
"description": "Default desktop permissions",
|
||||
"windows": ["main"],
|
||||
"permissions": ["core:default"]
|
||||
}
|
||||
|
After Width: | Height: | Size: 6.0 KiB |
|
After Width: | Height: | Size: 11 KiB |
|
After Width: | Height: | Size: 1.3 KiB |
|
After Width: | Height: | Size: 2.8 KiB |
|
After Width: | Height: | Size: 5.3 KiB |
|
After Width: | Height: | Size: 6.7 KiB |
|
After Width: | Height: | Size: 6.9 KiB |
|
After Width: | Height: | Size: 12 KiB |
|
After Width: | Height: | Size: 1.3 KiB |
|
After Width: | Height: | Size: 13 KiB |
|
After Width: | Height: | Size: 1.9 KiB |
|
After Width: | Height: | Size: 3.3 KiB |
|
After Width: | Height: | Size: 4.2 KiB |
|
After Width: | Height: | Size: 2.3 KiB |
|
After Width: | Height: | Size: 101 KiB |
|
After Width: | Height: | Size: 23 KiB |
|
After Width: | Height: | Size: 21 KiB |
@@ -0,0 +1,116 @@
|
||||
use std::fs;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::Mutex;
|
||||
use std::error::Error;
|
||||
|
||||
use tauri::{Manager, Runtime};
|
||||
use tauri_plugin_shell::process::CommandChild;
|
||||
use tauri_plugin_shell::ShellExt;
|
||||
|
||||
struct ServerProcess {
|
||||
child: CommandChild,
|
||||
pid: u32,
|
||||
}
|
||||
|
||||
struct ServerState(Mutex<Option<ServerProcess>>);
|
||||
|
||||
fn copy_resource<R: Runtime>(
|
||||
app: &tauri::App<R>,
|
||||
name: &str,
|
||||
data_dir: &Path,
|
||||
) -> Result<(), Box<dyn Error>> {
|
||||
let target = data_dir.join(name);
|
||||
if target.exists() {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
for source in resource_candidates(app, name)? {
|
||||
if source.exists() {
|
||||
fs::copy(source, target)?;
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
|
||||
Err(format!("Bundled resource not found: {name}").into())
|
||||
}
|
||||
|
||||
fn resource_candidates<R: Runtime>(
|
||||
app: &tauri::App<R>,
|
||||
name: &str,
|
||||
) -> Result<Vec<PathBuf>, Box<dyn Error>> {
|
||||
let resource_path = app.path().resolve(name, tauri::path::BaseDirectory::Resource)?;
|
||||
let escaped_parent_path = app
|
||||
.path()
|
||||
.resolve(format!("_up_/{name}"), tauri::path::BaseDirectory::Resource)?;
|
||||
let exe_dir = std::env::current_exe()?
|
||||
.parent()
|
||||
.map(Path::to_path_buf)
|
||||
.ok_or("Unable to resolve executable directory")?;
|
||||
|
||||
Ok(vec![
|
||||
resource_path,
|
||||
escaped_parent_path,
|
||||
exe_dir.join(name),
|
||||
exe_dir.join("_up_").join(name),
|
||||
])
|
||||
}
|
||||
|
||||
fn start_server<R: Runtime>(app: &tauri::App<R>) -> Result<(), Box<dyn Error>> {
|
||||
let data_dir = app.path().app_data_dir()?;
|
||||
fs::create_dir_all(&data_dir)?;
|
||||
fs::create_dir_all(data_dir.join("cache"))?;
|
||||
fs::create_dir_all(data_dir.join("logs"))?;
|
||||
copy_resource(app, "Configure.json", &data_dir)?;
|
||||
copy_resource(app, "ReportScript.sql", &data_dir)?;
|
||||
|
||||
let (mut rx, child) = app
|
||||
.shell()
|
||||
.sidecar("capareport-server")?
|
||||
.env("CAPAREPORT_BASE_DIR", data_dir.to_string_lossy().to_string())
|
||||
.args(["--host", "127.0.0.1", "--port", "19082"])
|
||||
.spawn()?;
|
||||
|
||||
tauri::async_runtime::spawn(async move {
|
||||
while rx.recv().await.is_some() {}
|
||||
});
|
||||
|
||||
let pid = child.pid();
|
||||
let state = app.state::<ServerState>();
|
||||
*state.0.lock().expect("server state lock poisoned") = Some(ServerProcess { child, pid });
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn stop_server<R: Runtime>(app: &tauri::AppHandle<R>) {
|
||||
let state = app.state::<ServerState>();
|
||||
let child = state
|
||||
.0
|
||||
.lock()
|
||||
.expect("server state lock poisoned")
|
||||
.take();
|
||||
if let Some(process) = child {
|
||||
#[cfg(target_os = "windows")]
|
||||
{
|
||||
let _ = std::process::Command::new("taskkill")
|
||||
.args(["/F", "/T", "/PID", &process.pid.to_string()])
|
||||
.status();
|
||||
}
|
||||
let _ = process.child.kill();
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
tauri::Builder::default()
|
||||
.plugin(tauri_plugin_shell::init())
|
||||
.manage(ServerState(Mutex::new(None)))
|
||||
.setup(|app| {
|
||||
start_server(app)?;
|
||||
Ok(())
|
||||
})
|
||||
.on_window_event(|window, event| {
|
||||
if let tauri::WindowEvent::CloseRequested { .. } = event {
|
||||
stop_server(window.app_handle());
|
||||
}
|
||||
})
|
||||
.run(tauri::generate_context!())
|
||||
.expect("error while running tauri application");
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
{
|
||||
"$schema": "https://schema.tauri.app/config/2",
|
||||
"productName": "CapacityReport",
|
||||
"version": "2.0.2",
|
||||
"identifier": "com.nixevol.capacityreport",
|
||||
"build": {
|
||||
"frontendDist": "../frontend/dist"
|
||||
},
|
||||
"app": {
|
||||
"windows": [
|
||||
{
|
||||
"label": "main",
|
||||
"title": "CapacityReport",
|
||||
"width": 1280,
|
||||
"height": 820,
|
||||
"minWidth": 1100,
|
||||
"minHeight": 720
|
||||
}
|
||||
],
|
||||
"security": {
|
||||
"csp": null
|
||||
}
|
||||
},
|
||||
"bundle": {
|
||||
"active": true,
|
||||
"targets": "all",
|
||||
"icon": [
|
||||
"icons/32x32.png",
|
||||
"icons/128x128.png",
|
||||
"icons/128x128@2x.png",
|
||||
"icons/icon.icns",
|
||||
"icons/icon.ico"
|
||||
],
|
||||
"externalBin": [
|
||||
"binaries/capareport-server"
|
||||
],
|
||||
"resources": [
|
||||
"../Configure.json",
|
||||
"../ReportScript.sql"
|
||||
]
|
||||
}
|
||||
}
|
||||