Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion .github/workflows/base.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ jobs:
- run: |
echo "deb http://gb.archive.ubuntu.com/ubuntu jammy main" | sudo tee -a /etc/apt/sources.list
sudo apt update
sudo apt install -y libwebkit2gtk-4.0-dev \
sudo apt install -y libwebkit2gtk-4.1-dev \
build-essential \
curl \
wget \
Expand Down
6 changes: 4 additions & 2 deletions lunar/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
"@headlessui/tailwindcss": "^0.2.1",
"@heroicons/react": "^2.1.3",
"@tailwindcss/forms": "^0.5.7",
"@tauri-apps/api": "^1.6.0",
"@tauri-apps/api": "^2.0.1",
"@tauri-apps/plugin-fs": "^2.0.0",
"@tauri-apps/plugin-shell": "^2.0.0",
"clsx": "^2.1.1",
"date-fns": "^3.6.0",
"framer-motion": "^11.3.2",
Expand All @@ -29,7 +31,7 @@
"swr": "^2.2.5"
},
"devDependencies": {
"@tauri-apps/cli": "^1.6.0",
"@tauri-apps/cli": "^2.0.0",
"@types/node": "^22",
"@types/react": "^18.3.3",
"@types/react-dom": "^18.3.0",
Expand Down
8 changes: 4 additions & 4 deletions lunar/src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ rust-version = "1.60"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[build-dependencies]
tauri-build = { version = "1.5.3", features = [] }
tauri-build = { version = "2", features = [] }

[dependencies]
serde_json = { workspace = true }
serde = { workspace = true, features = ["derive"] }
tauri = { version = "1.7.1", features = [
"shell-sidecar",
"process-command-api",
tauri = { version = "2", features = [
] }
tokio = { workspace = true }
home = { workspace = true }
tauri-plugin-fs = "2"
tauri-plugin-shell = "2"

[dev-dependencies]
tokio = { workspace = true, features = ["macros"] }
Expand Down
36 changes: 36 additions & 0 deletions lunar/src-tauri/capabilities/migrated.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"identifier": "migrated",
"description": "permissions that were migrated from v1",
"local": true,
"windows": [
"main"
],
"permissions": [
"core:default",
{
"identifier": "fs:scope",
"allow": [
"$RESOURCE/*"
]
},
{
"identifier": "shell:allow-execute",
"allow": [
{
"args": false,
"cmd": "",
"name": "binaries/mega",
"sidecar": true
},
{
"args": false,
"cmd": "",
"name": "binaries/libra",
"sidecar": true
}
]
},
"fs:default",
"shell:default"
]
}
1 change: 1 addition & 0 deletions lunar/src-tauri/gen/schemas/acl-manifests.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions lunar/src-tauri/gen/schemas/capabilities.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"migrated":{"identifier":"migrated","description":"permissions that were migrated from v1","local":true,"windows":["main"],"permissions":["core:default",{"identifier":"fs:scope","allow":["$RESOURCE/*"]},{"identifier":"shell:allow-execute","allow":[{"args":false,"cmd":"","name":"binaries/mega","sidecar":true},{"args":false,"cmd":"","name":"binaries/libra","sidecar":true}]},"fs:default","shell:default"]}}
4,986 changes: 4,986 additions & 0 deletions lunar/src-tauri/gen/schemas/desktop-schema.json

Large diffs are not rendered by default.

4,986 changes: 4,986 additions & 0 deletions lunar/src-tauri/gen/schemas/linux-schema.json

Large diffs are not rendered by default.

4,986 changes: 4,986 additions & 0 deletions lunar/src-tauri/gen/schemas/macOS-schema.json

Large diffs are not rendered by default.

141 changes: 85 additions & 56 deletions lunar/src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ use std::sync::{Arc, Mutex};
use std::{env, fs, thread, time};

use serde::Deserialize;
use tauri::api::process::{Command, CommandChild, CommandEvent};
use tauri::{Manager, State};

use tauri::Manager;
use tauri::State;
use tauri_plugin_shell::process::{CommandChild, CommandEvent};
use tauri_plugin_shell::ShellExt;
#[derive(Default)]
struct ServiceState {
child: Option<CommandChild>,
with_relay: bool,
}

impl Drop for ServiceState {
fn drop(&mut self) {
if let Some(child_process) = self.child.take() {
Expand All @@ -31,10 +31,7 @@ struct MegaStartParams {
}

fn set_up_lib(handle: tauri::AppHandle) {
let resource_path = handle
.path_resolver()
.resource_dir()
.expect("failed to resolve resource");
let resource_path = handle.path().resource_dir().expect("home dir not found");
let libs_dir = resource_path.join("libs");

#[cfg(target_os = "macos")]
Expand All @@ -56,6 +53,7 @@ fn set_up_lib(handle: tauri::AppHandle) {

#[tauri::command]
fn start_mega_service(
app: tauri::AppHandle,
state: State<'_, Arc<Mutex<ServiceState>>>,
params: MegaStartParams,
) -> Result<(), String> {
Expand All @@ -71,8 +69,13 @@ fn start_mega_service(
service_state.with_relay = false;
vec!["service", "http"]
};
let (mut rx, child) = Command::new_sidecar("mega")
.expect("Failed to create `mega` binary command")

let sidecar_command = app
.shell()
.sidecar("mega")
.expect("Failed to create `mega` binary command");

let (mut rx, child) = sidecar_command
.args(args)
.spawn()
.expect("Failed to spawn `Mega service`");
Expand All @@ -84,10 +87,12 @@ fn start_mega_service(
while let Some(event) = rx.recv().await {
match event {
CommandEvent::Stdout(line) => {
print!("{}", line);
// line to string
// print!("{}", line);
println!("Sidecar stdout: {}", String::from_utf8_lossy(&line));
}
CommandEvent::Stderr(line) => {
eprint!("Sidecar stderr: {}", line);
eprint!("Sidecar stderr: {}", String::from_utf8_lossy(&line));
}
CommandEvent::Terminated(payload) => {
if let Some(code) = payload.code {
Expand Down Expand Up @@ -124,13 +129,14 @@ fn stop_mega_service(state: State<'_, Arc<Mutex<ServiceState>>>) -> Result<(), S

#[tauri::command]
fn restart_mega_service(
app: tauri::AppHandle,
state: State<'_, Arc<Mutex<ServiceState>>>,
params: MegaStartParams,
) -> Result<(), String> {
stop_mega_service(state.clone())?;
// wait for process exit
thread::sleep(time::Duration::from_millis(1000));
start_mega_service(state, params)?;
start_mega_service(app, state, params)?;
Ok(())
}

Expand All @@ -141,7 +147,7 @@ fn mega_service_status(state: State<'_, Arc<Mutex<ServiceState>>>) -> Result<(bo
}

#[tauri::command]
fn clone_repository(repo_url: String, name: String) -> Result<(), String> {
fn clone_repository(app: tauri::AppHandle, repo_url: String, name: String) -> Result<(), String> {
let home = match home::home_dir() {
Some(path) if !path.as_os_str().is_empty() => path,
_ => {
Expand All @@ -154,70 +160,93 @@ fn clone_repository(repo_url: String, name: String) -> Result<(), String> {
if target_dir.exists() {
fs::remove_dir_all(&target_dir).unwrap();
}

let output = Command::new_sidecar("libra")
.expect("Failed to create `libra` binary command")
.args(["clone", &repo_url, (target_dir.to_str().unwrap())])
.output()
.map_err(|e| format!("Failed to execute process: {}", e))?;
let app_clone = app.clone();
let output = tauri::async_runtime::block_on(async {
app_clone
.shell()
.sidecar("libra")
.expect("Failed to create `libra` binary command")
.args(["clone", &repo_url, target_dir.to_str().unwrap()])
.output()
.await
})
.map_err(|e| format!("Failed to execute process: {}", e))?;

if output.status.success() {
println!("{}", output.stdout);
println!("{}", String::from_utf8_lossy(&output.stdout));
} else {
eprintln!("{}", output.stderr);
eprintln!("{}", String::from_utf8_lossy(&output.stderr));
}
change_remote_url(target_dir.clone(), name)?;
push_to_new_remote(target_dir)?;
change_remote_url(app.to_owned(), target_dir.clone(), name)?;
push_to_new_remote(app, target_dir)?;
Ok(())
}

fn change_remote_url(repo_path: PathBuf, name: String) -> Result<(), String> {
Command::new_sidecar("libra")
.expect("Failed to create `libra` binary command")
.args(["remote", "remove", "origin"])
.current_dir(repo_path.clone())
.output()
.map_err(|e| format!("Failed to execute process: {}", e))?;

let output = Command::new_sidecar("libra")
.expect("Failed to create `libra` binary command")
.args([
"remote",
"add",
"origin",
&format!("http://localhost:8000/third-part/{}", name),
])
.current_dir(repo_path.clone())
.output()
.map_err(|e| format!("Failed to execute process: {}", e))?;
fn change_remote_url(
app: tauri::AppHandle,
repo_path: PathBuf,
name: String,
) -> Result<(), String> {
tauri::async_runtime::block_on(async {
app.shell()
.sidecar("libra")
.expect("Failed to create `libra` binary command")
.args(["remote", "remove", "origin"])
.current_dir(repo_path.clone())
.output()
.await
})
.map_err(|e| format!("Failed to execute process: {}", e))?;

let output = tauri::async_runtime::block_on(async {
app.shell()
.sidecar("libra")
.expect("Failed to create `libra` binary command")
.args([
"remote",
"add",
"origin",
&format!("http://localhost:8000/third-part/{}", name),
])
.current_dir(repo_path.clone())
.output()
.await
})
.map_err(|e| format!("Failed to execute process: {}", e))?;

if output.status.success() {
println!("{}", output.stdout);
println!("{}", String::from_utf8_lossy(&output.stdout));
} else {
eprintln!("{}", output.stderr);
eprintln!("{}", String::from_utf8_lossy(&output.stderr));
}
Ok(())
}

fn push_to_new_remote(repo_path: PathBuf) -> Result<(), String> {
let output = Command::new_sidecar("libra")
.expect("Failed to create `libra` binary command")
.args(["push", "origin", "master"])
.current_dir(repo_path)
.output()
.map_err(|e| format!("Failed to execute process: {}", e))?;
fn push_to_new_remote(app: tauri::AppHandle, repo_path: PathBuf) -> Result<(), String> {
let output = tauri::async_runtime::block_on(async {
app.shell()
.sidecar("libra")
.expect("Failed to create `libra` binary command")
.args(["push", "origin", "master"])
.current_dir(repo_path)
.output()
.await
})
.map_err(|e| format!("Failed to execute process: {}", e))?;

if output.status.success() {
println!("{}", output.stdout);
println!("{}", String::from_utf8_lossy(&output.stdout));
} else {
eprintln!("{}", output.stderr);
eprintln!("{}", String::from_utf8_lossy(&output.stderr));
}
Ok(())
}

fn main() {
let params = MegaStartParams::default();
tauri::Builder::default()
.plugin(tauri_plugin_shell::init())
.plugin(tauri_plugin_fs::init())
.manage(Arc::new(Mutex::new(ServiceState::default())))
.invoke_handler(tauri::generate_handler![
start_mega_service,
Expand All @@ -228,9 +257,9 @@ fn main() {
])
.setup(|app| {
let app_handle = app.handle().clone();
set_up_lib(app_handle);
set_up_lib(app_handle.to_owned());
let state = app.state::<Arc<Mutex<ServiceState>>>().clone();
if let Err(e) = start_mega_service(state, params) {
if let Err(e) = start_mega_service(app_handle, state, params) {
eprintln!("Failed to restart rust_service: {}", e);
} else {
println!("Rust service restarted successfully");
Expand Down
Loading