From e1be9e86a0d446dd9880c676f851ca0ba9fd36aa Mon Sep 17 00:00:00 2001 From: Subhamoy Biswas Date: Sun, 29 Dec 2024 19:29:37 +0530 Subject: [PATCH] (refactor): switched to cli --hidden autostart method from WebSockets and fixed macOS autostart not auto hiding after restart issue --- copyFiles.aarch64-apple-darwin.js | 4 +- copyFiles.js | 3 - copyFiles.x86_64-apple-darwin.js | 3 - src-tauri/Cargo.lock | 8 --- src-tauri/Cargo.toml | 3 +- src-tauri/autostart/Cargo.lock | 7 -- src-tauri/autostart/Cargo.toml | 12 ---- .../pytubepp-helper-autostart.desktop | 4 +- .../autostart/pytubepp-helper-autostart.plist | 7 +- src-tauri/autostart/src/main.rs | 70 ------------------- src-tauri/installer/windows/nsis-template.nsi | 8 +-- .../windows/wix-fragment-registry.wxs | 2 +- src-tauri/src/main.rs | 11 +++ src-tauri/tauri.linux.conf.json | 6 +- src-tauri/tauri.macos.conf.json | 6 +- src-tauri/tauri.windows.conf.json | 10 ++- 16 files changed, 33 insertions(+), 131 deletions(-) delete mode 100644 src-tauri/autostart/Cargo.lock delete mode 100644 src-tauri/autostart/Cargo.toml delete mode 100644 src-tauri/autostart/src/main.rs diff --git a/copyFiles.aarch64-apple-darwin.js b/copyFiles.aarch64-apple-darwin.js index c7ff7e7..37996cc 100644 --- a/copyFiles.aarch64-apple-darwin.js +++ b/copyFiles.aarch64-apple-darwin.js @@ -7,8 +7,6 @@ const __dirname = path.dirname(__filename); const msghostSrc = path.join(__dirname, 'src-tauri', 'target', 'aarch64-apple-darwin', 'release', 'pytubepp-helper-msghost'); const msghostDest = path.join(__dirname, 'src-tauri', 'pytubepp-helper-msghost'); -const autostartSrc = path.join(__dirname, 'src-tauri', 'target', 'aarch64-apple-darwin', 'release', 'pytubepp-helper-autostart'); -const autostartDest = path.join(__dirname, 'src-tauri', 'pytubepp-helper-autostart'); const autostartPlistSrc = path.join(__dirname, 'src-tauri', 'autostart', 'pytubepp-helper-autostart.plist'); const autostartPlistDest = path.join(__dirname, 'src-tauri', 'pytubepp-helper-autostart.plist'); const msghostManifestMacChromeSrc = path.join(__dirname, 'src-tauri', 'msghost-manifest', 'macos', 'chrome', 'com.neosubhamoy.pytubepp.helper.json'); @@ -17,7 +15,7 @@ const msghostManifestMacFirefoxSrc = path.join(__dirname, 'src-tauri', 'msghost- const msghostManifestMacFirefoxDest = path.join(__dirname, 'src-tauri', 'pytubepp-helper-msghost-moz.json'); fs.copyFileSync(msghostSrc, msghostDest); -fs.copyFileSync(autostartSrc, autostartDest); +// fs.copyFileSync(autostartSrc, autostartDest); fs.copyFileSync(autostartPlistSrc, autostartPlistDest); fs.copyFileSync(msghostManifestMacChromeSrc, msghostManifestMacChromeDest); fs.copyFileSync(msghostManifestMacFirefoxSrc, msghostManifestMacFirefoxDest); diff --git a/copyFiles.js b/copyFiles.js index 043235b..4b12e2a 100644 --- a/copyFiles.js +++ b/copyFiles.js @@ -7,15 +7,12 @@ const __dirname = path.dirname(__filename); const msghostSrc = path.join(__dirname, 'src-tauri', 'target', 'release', 'pytubepp-helper-msghost.exe'); const msghostDest = path.join(__dirname, 'src-tauri', 'pytubepp-helper-msghost.exe'); -const autostartSrc = path.join(__dirname, 'src-tauri', 'target', 'release', 'pytubepp-helper-autostart.exe'); -const autostartDest = path.join(__dirname, 'src-tauri', 'pytubepp-helper-autostart.exe'); const msghostManifestWinChromeSrc = path.join(__dirname, 'src-tauri', 'msghost-manifest', 'windows', 'chrome', 'com.neosubhamoy.pytubepp.helper.json'); const msghostManifestWinChromeDest = path.join(__dirname, 'src-tauri', 'pytubepp-helper-msghost.json'); const msghostManifestWinFirefoxSrc = path.join(__dirname, 'src-tauri', 'msghost-manifest', 'windows', 'firefox', 'com.neosubhamoy.pytubepp.helper.json'); const msghostManifestWinFirefoxDest = path.join(__dirname, 'src-tauri', 'pytubepp-helper-msghost-moz.json'); fs.copyFileSync(msghostSrc, msghostDest); -fs.copyFileSync(autostartSrc, autostartDest); fs.copyFileSync(msghostManifestWinChromeSrc, msghostManifestWinChromeDest); fs.copyFileSync(msghostManifestWinFirefoxSrc, msghostManifestWinFirefoxDest); console.log('Files copied successfully'); \ No newline at end of file diff --git a/copyFiles.x86_64-apple-darwin.js b/copyFiles.x86_64-apple-darwin.js index f92acd0..0351e47 100644 --- a/copyFiles.x86_64-apple-darwin.js +++ b/copyFiles.x86_64-apple-darwin.js @@ -7,8 +7,6 @@ const __dirname = path.dirname(__filename); const msghostSrc = path.join(__dirname, 'src-tauri', 'target', 'x86_64-apple-darwin', 'release', 'pytubepp-helper-msghost'); const msghostDest = path.join(__dirname, 'src-tauri', 'pytubepp-helper-msghost'); -const autostartSrc = path.join(__dirname, 'src-tauri', 'target', 'x86_64-apple-darwin', 'release', 'pytubepp-helper-autostart'); -const autostartDest = path.join(__dirname, 'src-tauri', 'pytubepp-helper-autostart'); const autostartPlistSrc = path.join(__dirname, 'src-tauri', 'autostart', 'pytubepp-helper-autostart.plist'); const autostartPlistDest = path.join(__dirname, 'src-tauri', 'pytubepp-helper-autostart.plist'); const msghostManifestMacChromeSrc = path.join(__dirname, 'src-tauri', 'msghost-manifest', 'macos', 'chrome', 'com.neosubhamoy.pytubepp.helper.json'); @@ -17,7 +15,6 @@ const msghostManifestMacFirefoxSrc = path.join(__dirname, 'src-tauri', 'msghost- const msghostManifestMacFirefoxDest = path.join(__dirname, 'src-tauri', 'pytubepp-helper-msghost-moz.json'); fs.copyFileSync(msghostSrc, msghostDest); -fs.copyFileSync(autostartSrc, autostartDest); fs.copyFileSync(autostartPlistSrc, autostartPlistDest); fs.copyFileSync(msghostManifestMacChromeSrc, msghostManifestMacChromeDest); fs.copyFileSync(msghostManifestMacFirefoxSrc, msghostManifestMacFirefoxDest); diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index a174dd9..9d2904e 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -2485,14 +2485,6 @@ dependencies = [ "tokio-tungstenite", ] -[[package]] -name = "pytubepp-helper-autostart" -version = "0.1.0" -dependencies = [ - "serde_json", - "websocket", -] - [[package]] name = "pytubepp-helper-msghost" version = "0.1.0" diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index d9391e5..40a197e 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -26,6 +26,5 @@ custom-protocol = ["tauri/custom-protocol"] [workspace] members = [ ".", - "msghost", - "autostart" + "msghost" ] diff --git a/src-tauri/autostart/Cargo.lock b/src-tauri/autostart/Cargo.lock deleted file mode 100644 index df3ab83..0000000 --- a/src-tauri/autostart/Cargo.lock +++ /dev/null @@ -1,7 +0,0 @@ -# This file is automatically @generated by Cargo. -# It is not intended for manual editing. -version = 3 - -[[package]] -name = "pytubepp-helper-autostart" -version = "0.1.0" \ No newline at end of file diff --git a/src-tauri/autostart/Cargo.toml b/src-tauri/autostart/Cargo.toml deleted file mode 100644 index f32af6d..0000000 --- a/src-tauri/autostart/Cargo.toml +++ /dev/null @@ -1,12 +0,0 @@ -[package] -name = "pytubepp-helper-autostart" -version = "0.1.0" -description = "PytubePP Helper (Autostart)" -authors = ["neosubhamoy "] -edition = "2021" - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[dependencies] -websocket = "0.27.1" -serde_json = "1.0" \ No newline at end of file diff --git a/src-tauri/autostart/pytubepp-helper-autostart.desktop b/src-tauri/autostart/pytubepp-helper-autostart.desktop index a42f147..f51260d 100644 --- a/src-tauri/autostart/pytubepp-helper-autostart.desktop +++ b/src-tauri/autostart/pytubepp-helper-autostart.desktop @@ -2,7 +2,7 @@ Type=Application Name=pytubepp-helper Icon=pytubepp-helper -Comment=pytubepp-helper autostart script -Exec=/usr/bin/pytubepp-helper-autostart +Comment=pytubepp-helper autostart +Exec=/usr/bin/pytubepp-helper --hidden StartupNotify=false Terminal=false \ No newline at end of file diff --git a/src-tauri/autostart/pytubepp-helper-autostart.plist b/src-tauri/autostart/pytubepp-helper-autostart.plist index 0fcc9c1..629de3f 100644 --- a/src-tauri/autostart/pytubepp-helper-autostart.plist +++ b/src-tauri/autostart/pytubepp-helper-autostart.plist @@ -4,8 +4,11 @@ Label com.neosubhamoy.pytubepp.helper - Program - /Applications/pytubepp-helper.app/Contents/MacOS/pytubepp-helper + ProgramArguments + + /Applications/pytubepp-helper.app/Contents/MacOS/pytubepp-helper + --hidden + RunAtLoad diff --git a/src-tauri/autostart/src/main.rs b/src-tauri/autostart/src/main.rs deleted file mode 100644 index a7a2068..0000000 --- a/src-tauri/autostart/src/main.rs +++ /dev/null @@ -1,70 +0,0 @@ -#![windows_subsystem = "windows"] - -use std::process::Command; -use websocket::client::ClientBuilder; -use websocket::OwnedMessage; -use std::thread::sleep; -use std::time::Duration; - -fn connect_with_retry(url: &str, max_attempts: u32) -> Result, Box> { - let mut attempts = 0; - loop { - match ClientBuilder::new(url).unwrap().connect_insecure() { - Ok(client) => { - eprintln!("Successfully connected to Tauri app :)"); - return Ok(client); - } - Err(e) => { - attempts += 1; - if attempts >= max_attempts { - return Err(Box::new(e)); - } - let wait_time = Duration::from_secs(2u64.pow(attempts)); - eprintln!("Connection attempt {} failed. Retrying in {:?}...", attempts, wait_time); - sleep(wait_time); - } - } - } -} - -fn main() -> Result<(), Box> { - #[cfg(target_os = "windows")] - { - let _ = Command::new("pytubepp-helper.exe").spawn(); - } - - #[cfg(target_os = "linux")] - { - let _ = Command::new("pytubepp-helper").spawn(); - } - - #[cfg(target_os = "macos")] - { - let _ = Command::new("/Applications/pytubepp-helper.app/Contents/MacOS/pytubepp-helper").spawn(); - } - - // Launch the main application - // let _ = Command::new("pytubepp-helper") - // .spawn(); - - // Connect with the Tauri app - let websocket_url = "ws://localhost:3030"; - eprintln!("Attempting to connect to {}", websocket_url); - - let mut client = match connect_with_retry(websocket_url, 2) { - Ok(client) => client, - Err(e) => { - eprintln!("Failed to connect after multiple attempts: {:?}", e); - return Err(e); - } - }; - - // Send message to Tauri app - client.send_message(&OwnedMessage::Text(serde_json::json!({ - "url": "", - "command": "autostart", - "argument": "" - }).to_string()))?; - - Ok(()) -} \ No newline at end of file diff --git a/src-tauri/installer/windows/nsis-template.nsi b/src-tauri/installer/windows/nsis-template.nsi index 8b1db81..409a90f 100644 --- a/src-tauri/installer/windows/nsis-template.nsi +++ b/src-tauri/installer/windows/nsis-template.nsi @@ -43,7 +43,7 @@ ${StrLoc} !define MANUPRODUCTKEY "Software\${MANUFACTURER}\${PRODUCTNAME}" !define UNINSTALLERSIGNCOMMAND "" !define ESTIMATEDSIZE "0x002311" -!define AUTOSTART_EXEC "pytubepp-helper-autostart.exe" +!define AUTOSTART_EXEC "pytubepp-helper.exe" !define CHROME_REG_PATH "Software\Google\Chrome\NativeMessagingHosts\com.neosubhamoy.pytubepp.helper" !define FIREFOX_REG_PATH "Software\Mozilla\NativeMessagingHosts\com.neosubhamoy.pytubepp.helper" !define RUN_REG_PATH "Software\Microsoft\Windows\CurrentVersion\Run" @@ -548,7 +548,7 @@ Section Install ; Copy resources CreateDirectory "$INSTDIR\" - File /a "/oname=pytubepp-helper-autostart.exe" "..\..\..\..\pytubepp-helper-autostart.exe" + ; File /a "/oname=pytubepp-helper-autostart.exe" "..\..\..\..\pytubepp-helper-autostart.exe" File /a "/oname=pytubepp-helper-msghost-moz.json" "..\..\..\..\pytubepp-helper-msghost-moz.json" File /a "/oname=pytubepp-helper-msghost.exe" "..\..\..\..\pytubepp-helper-msghost.exe" File /a "/oname=pytubepp-helper-msghost.json" "..\..\..\..\pytubepp-helper-msghost.json" @@ -585,7 +585,7 @@ Section Install WriteRegStr HKCU "${FIREFOX_REG_PATH}" "" "$INSTDIR\pytubepp-helper-msghost-moz.json" ; Add entry for automatic startup with Windows - WriteRegStr HKCU "${RUN_REG_PATH}" "${PRODUCTNAME}" "$\"$INSTDIR\${AUTOSTART_EXEC}$\"" + WriteRegStr HKCU "${RUN_REG_PATH}" "${PRODUCTNAME}" "$\"$INSTDIR\${AUTOSTART_EXEC}$\" --hidden" ; Create start menu shortcut (GUI) !insertmacro MUI_STARTMENU_WRITE_BEGIN Application @@ -670,7 +670,7 @@ Section Uninstall Delete "$INSTDIR\${MAINBINARYNAME}.exe" ; Delete resources - Delete "$INSTDIR\pytubepp-helper-autostart.exe" + ; Delete "$INSTDIR\pytubepp-helper-autostart.exe" Delete "$INSTDIR\pytubepp-helper-msghost-moz.json" Delete "$INSTDIR\pytubepp-helper-msghost.exe" Delete "$INSTDIR\pytubepp-helper-msghost.json" diff --git a/src-tauri/installer/windows/wix-fragment-registry.wxs b/src-tauri/installer/windows/wix-fragment-registry.wxs index f5d40fd..f859467 100644 --- a/src-tauri/installer/windows/wix-fragment-registry.wxs +++ b/src-tauri/installer/windows/wix-fragment-registry.wxs @@ -10,7 +10,7 @@ - + diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index a3404dd..eb27800 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -170,10 +170,15 @@ fn download_stream(url: String, stream: String) { #[tokio::main] async fn main() { let _ = fix_path_env::fix(); + + let args: Vec = env::args().collect(); + let start_hidden = args.contains(&"--hidden".to_string()); + let websocket_state = Arc::new(Mutex::new(WebSocketState { sender: None, response_channel: ResponseChannel { sender: None }, })); + let tray_menu = SystemTrayMenu::new() .add_item(CustomMenuItem::new("show".to_string(), "Show")) .add_item(CustomMenuItem::new("quit".to_string(), "Quit")); @@ -207,6 +212,12 @@ async fn main() { }) .manage(websocket_state.clone()) .setup(move |app| { + let window = app.get_window("main").unwrap(); + + if start_hidden { + window.hide().unwrap(); + } + let app_handle = app.handle(); let ws_state = websocket_state.clone(); tokio::spawn(async move { diff --git a/src-tauri/tauri.linux.conf.json b/src-tauri/tauri.linux.conf.json index 8463294..58c46c8 100644 --- a/src-tauri/tauri.linux.conf.json +++ b/src-tauri/tauri.linux.conf.json @@ -1,7 +1,7 @@ { "build": { - "beforeDevCommand": "npm run dev && cargo build --manifest-path=./src-tauri/msghost/Cargo.toml && cargo build --manifest-path=./src-tauri/autostart/Cargo.toml", - "beforeBuildCommand": "npm run build && cargo build --release --manifest-path=./src-tauri/msghost/Cargo.toml && cargo build --release --manifest-path=./src-tauri/autostart/Cargo.toml", + "beforeDevCommand": "npm run dev && cargo build --manifest-path=./src-tauri/msghost/Cargo.toml", + "beforeBuildCommand": "npm run build && cargo build --release --manifest-path=./src-tauri/msghost/Cargo.toml", "devPath": "http://localhost:1422", "distDir": "../dist" }, @@ -134,7 +134,6 @@ "/etc/chromium/native-messaging-hosts/com.neosubhamoy.pytubepp.helper.json": "./msghost-manifest/linux/chrome/com.neosubhamoy.pytubepp.helper.json", "/usr/lib/mozilla/native-messaging-hosts/com.neosubhamoy.pytubepp.helper.json": "./msghost-manifest/linux/firefox/com.neosubhamoy.pytubepp.helper.json", "/usr/bin/pytubepp-helper-msghost": "./target/release/pytubepp-helper-msghost", - "/usr/bin/pytubepp-helper-autostart": "./target/release/pytubepp-helper-autostart", "/etc/xdg/autostart/pytubepp-helper-autostart.desktop": "./autostart/pytubepp-helper-autostart.desktop" } }, @@ -148,7 +147,6 @@ "/etc/chromium/native-messaging-hosts/com.neosubhamoy.pytubepp.helper.json": "./msghost-manifest/linux/chrome/com.neosubhamoy.pytubepp.helper.json", "/usr/lib/mozilla/native-messaging-hosts/com.neosubhamoy.pytubepp.helper.json": "./msghost-manifest/linux/firefox/com.neosubhamoy.pytubepp.helper.json", "/usr/bin/pytubepp-helper-msghost": "./target/release/pytubepp-helper-msghost", - "/usr/bin/pytubepp-helper-autostart": "./target/release/pytubepp-helper-autostart", "/etc/xdg/autostart/pytubepp-helper-autostart.desktop": "./autostart/pytubepp-helper-autostart.desktop" } } diff --git a/src-tauri/tauri.macos.conf.json b/src-tauri/tauri.macos.conf.json index a4526d6..2a1bac9 100644 --- a/src-tauri/tauri.macos.conf.json +++ b/src-tauri/tauri.macos.conf.json @@ -1,7 +1,7 @@ { "build": { - "beforeDevCommand": "[[ -n \"$TARGET_ARCH\" ]] && ARCH=\"$TARGET_ARCH\" || ARCH=\"$(uname -m | sed 's/^arm64$/aarch64/')-apple-darwin\" && cargo build --target=$ARCH --manifest-path=./src-tauri/msghost/Cargo.toml && cargo build --target=$ARCH --manifest-path=./src-tauri/autostart/Cargo.toml && npm run dev", - "beforeBuildCommand": "[[ -n \"$TARGET_ARCH\" ]] && ARCH=\"$TARGET_ARCH\" || ARCH=\"$(uname -m | sed 's/^arm64$/aarch64/')-apple-darwin\" && cargo build --release --target=$ARCH --manifest-path=./src-tauri/msghost/Cargo.toml && cargo build --release --target=$ARCH --manifest-path=./src-tauri/autostart/Cargo.toml && node copyFiles.${ARCH}.js && npm run build", + "beforeDevCommand": "[[ -n \"$TARGET_ARCH\" ]] && ARCH=\"$TARGET_ARCH\" || ARCH=\"$(uname -m | sed 's/^arm64$/aarch64/')-apple-darwin\" && cargo build --target=$ARCH --manifest-path=./src-tauri/msghost/Cargo.toml && npm run dev", + "beforeBuildCommand": "[[ -n \"$TARGET_ARCH\" ]] && ARCH=\"$TARGET_ARCH\" || ARCH=\"$(uname -m | sed 's/^arm64$/aarch64/')-apple-darwin\" && cargo build --release --target=$ARCH --manifest-path=./src-tauri/msghost/Cargo.toml && node copyFiles.${ARCH}.js && npm run build", "devPath": "http://localhost:1422", "distDir": "../dist" }, @@ -102,7 +102,6 @@ "$RESOURCE/pytubepp-helper-msghost.json", "$RESOURCE/pytubepp-helper-msghost-moz.json", "$RESOURCE/pytubepp-helper-msghost", - "$RESOURCE/pytubepp-helper-autostart", "$RESOURCE/pytubepp-helper-autostart.plist" ] }, @@ -156,7 +155,6 @@ "pytubepp-helper-msghost.json", "pytubepp-helper-msghost-moz.json", "pytubepp-helper-msghost", - "pytubepp-helper-autostart", "pytubepp-helper-autostart.plist" ] }, diff --git a/src-tauri/tauri.windows.conf.json b/src-tauri/tauri.windows.conf.json index b378b73..f0b2369 100644 --- a/src-tauri/tauri.windows.conf.json +++ b/src-tauri/tauri.windows.conf.json @@ -1,7 +1,7 @@ { "build": { - "beforeDevCommand": "cargo build --manifest-path=./src-tauri/msghost/Cargo.toml && cargo build --manifest-path=./src-tauri/autostart/Cargo.toml && npm run dev", - "beforeBuildCommand": "cargo build --release --manifest-path=./src-tauri/msghost/Cargo.toml && cargo build --release --manifest-path=./src-tauri/autostart/Cargo.toml && node copyFiles.js && npm run build", + "beforeDevCommand": "cargo build --manifest-path=./src-tauri/msghost/Cargo.toml && npm run dev", + "beforeBuildCommand": "cargo build --release --manifest-path=./src-tauri/msghost/Cargo.toml && node copyFiles.js && npm run build", "devPath": "http://localhost:1422", "distDir": "../dist" }, @@ -93,8 +93,7 @@ "scope": [ "$RESOURCE/pytubepp-helper-msghost.json", "$RESOURCE/pytubepp-helper-msghost-moz.json", - "$RESOURCE/pytubepp-helper-msghost.exe", - "$RESOURCE/pytubepp-helper-autostart.exe" + "$RESOURCE/pytubepp-helper-msghost.exe" ] }, "window": { @@ -148,8 +147,7 @@ "resources": [ "pytubepp-helper-msghost.json", "pytubepp-helper-msghost-moz.json", - "pytubepp-helper-msghost.exe", - "pytubepp-helper-autostart.exe" + "pytubepp-helper-msghost.exe" ] }, "systemTray": {