From c7003326dbcb37194c00afcce2225a028153f619 Mon Sep 17 00:00:00 2001 From: Huakun Shen Date: Tue, 12 Nov 2024 12:25:24 -0500 Subject: [PATCH] fix: implemented file server for custom UI commands on Windows (#24) --- apps/desktop/src-tauri/src/lib.rs | 8 +- apps/desktop/src/lib/cmds/ext.ts | 31 ++++- apps/desktop/src/routes/+page.svelte | 7 +- .../routes/extension/ui-iframe/+page.svelte | 8 +- apps/desktop/src/routes/settings/+page.svelte | 4 +- packages/api/src/commands/extension.ts | 6 + packages/tauri-plugins/jarvis/build.rs | 1 + .../tauri-plugins/jarvis/permissions/all.toml | 1 + .../commands/spawn_extension_file_server.toml | 13 ++ .../permissions/autogenerated/reference.md | 26 ++++ .../jarvis/permissions/schemas/schema.json | 10 ++ .../jarvis/src/commands/extension.rs | 128 ++++++++++++++---- packages/tauri-plugins/jarvis/src/lib.rs | 1 + .../jarvis/src/model/extension.rs | 15 +- 14 files changed, 215 insertions(+), 44 deletions(-) create mode 100644 packages/tauri-plugins/jarvis/permissions/autogenerated/commands/spawn_extension_file_server.toml diff --git a/apps/desktop/src-tauri/src/lib.rs b/apps/desktop/src-tauri/src/lib.rs index 05ed166..eb57386 100644 --- a/apps/desktop/src-tauri/src/lib.rs +++ b/apps/desktop/src-tauri/src/lib.rs @@ -113,8 +113,12 @@ pub fn run() { Some(ext) => { // let app_state = app_handle.state::(); // let extension_path = app_state.extension_path.lock().unwrap().clone(); - // tauri_file_server(app_handle, request, extension_path) - tauri_file_server(app_handle, request, ext.path.clone(), ext.dist.clone()) + tauri_file_server( + app_handle, + request, + ext.info.path.clone(), + ext.info.dist.clone(), + ) } None => tauri::http::Response::builder() .status(tauri::http::StatusCode::NOT_FOUND) diff --git a/apps/desktop/src/lib/cmds/ext.ts b/apps/desktop/src/lib/cmds/ext.ts index 1aede5c..b29abe9 100644 --- a/apps/desktop/src/lib/cmds/ext.ts +++ b/apps/desktop/src/lib/cmds/ext.ts @@ -2,10 +2,12 @@ import { appState } from "@/stores" import { winExtMap } from "@/stores/winExtMap" import { trimSlash } from "@/utils/url" import { constructExtensionSupportDir } from "@kksh/api" +import { spawnExtensionFileServer } from "@kksh/api/commands" import { CustomUiCmd, ExtPackageJsonExtra, TemplateUiCmd } from "@kksh/api/models" import { launchNewExtWindow } from "@kksh/extension" import { convertFileSrc } from "@tauri-apps/api/core" import * as fs from "@tauri-apps/plugin-fs" +import { platform } from "@tauri-apps/plugin-os" import { goto } from "$app/navigation" export async function createExtSupportDir(extPath: string) { @@ -44,28 +46,43 @@ export async function onCustomUiCmdSelect( // console.log("onCustomUiCmdSelect", ext, cmd, isDev, hmr) await createExtSupportDir(ext.extPath) let url = cmd.main - - if (hmr && isDev && cmd.devMain) { + const useDevMain = hmr && isDev && cmd.devMain + if (useDevMain) { url = cmd.devMain } else { url = decodeURIComponent(convertFileSrc(`${trimSlash(cmd.main)}`, "ext")) } - const url2 = `/extension/ui-iframe?url=${encodeURIComponent(url)}&extPath=${encodeURIComponent(ext.extPath)}` + let url2 = `/extension/ui-iframe?url=${encodeURIComponent(url)}&extPath=${encodeURIComponent(ext.extPath)}` if (cmd.window) { const winLabel = await winExtMap.registerExtensionWithWindow({ extPath: ext.extPath, dist: cmd.dist }) - console.log("Launch new window, ", winLabel) + if (platform() === "windows" && !useDevMain) { + const addr = await spawnExtensionFileServer(winLabel) + const newUrl = `http://${addr}` + url2 = `/extension/ui-iframe?url=${encodeURIComponent(newUrl)}&extPath=${encodeURIComponent(ext.extPath)}` + } + console.log("URL 2", url2) const window = launchNewExtWindow(winLabel, url2, cmd.window) window.onCloseRequested(async (event) => { await winExtMap.unregisterExtensionFromWindow(winLabel) }) } else { console.log("Launch main window") - return winExtMap - .registerExtensionWithWindow({ windowLabel: "main", extPath: ext.extPath, dist: cmd.dist }) - .then(() => goto(url2)) + const winLabel = await winExtMap.registerExtensionWithWindow({ + windowLabel: "main", + extPath: ext.extPath, + dist: cmd.dist + }) + if (platform() === "windows" && !useDevMain) { + const addr = await spawnExtensionFileServer(winLabel) // addr has format "127.0.0.1:" + console.log("Extension file server address: ", addr) + const newUrl = `http://${addr}` + url2 = `/extension/ui-iframe?url=${encodeURIComponent(newUrl)}&extPath=${encodeURIComponent(ext.extPath)}` + } + console.log("URL 2", url2) + goto(url2) } appState.clearSearchTerm() } diff --git a/apps/desktop/src/routes/+page.svelte b/apps/desktop/src/routes/+page.svelte index ec0a512..d64616a 100644 --- a/apps/desktop/src/routes/+page.svelte +++ b/apps/desktop/src/routes/+page.svelte @@ -24,7 +24,7 @@ import { cn, commandScore } from "@kksh/ui/utils" import { getCurrentWebviewWindow } from "@tauri-apps/api/webviewWindow" import { exit } from "@tauri-apps/plugin-process" - import { CircleXIcon, EllipsisVerticalIcon, RefreshCcwIcon } from "lucide-svelte" + import { ArrowBigUpIcon, CircleXIcon, EllipsisVerticalIcon, RefreshCcwIcon } from "lucide-svelte" let inputEle: HTMLInputElement | null = null function onKeyDown(event: KeyboardEvent) { @@ -100,7 +100,6 @@ - exit()}> Quit @@ -116,12 +115,12 @@ Toggle Devtools - ⌃+Shift+I + ⌃++I location.reload()}> Reload Window - ⌃+Shift+R + ⌃++R { diff --git a/apps/desktop/src/routes/extension/ui-iframe/+page.svelte b/apps/desktop/src/routes/extension/ui-iframe/+page.svelte index 071f3bd..d0665bf 100644 --- a/apps/desktop/src/routes/extension/ui-iframe/+page.svelte +++ b/apps/desktop/src/routes/extension/ui-iframe/+page.svelte @@ -68,6 +68,7 @@ // navigateTo(localePath("/")) // }, goBack: async () => { + console.log("goBack iframe ui API called") if (isInMainWindow()) { goto("/") } else { @@ -132,6 +133,7 @@ } satisfies IApp function onBackBtnClicked() { + console.log("onBackBtnClicked") if (isInMainWindow()) { goHome() } else { @@ -173,13 +175,12 @@ class={cn("absolute", positionToTailwindClasses(uiControl.backBtnPosition))} size="icon" variant="outline" - data-tauri-drag-region onclick={onBackBtnClicked} > {#if appWin.label === "main"} - + {:else} - + {/if} {/if} @@ -198,7 +199,6 @@ class={cn("absolute", positionToTailwindClasses(uiControl.refreshBtnPosition))} size="icon" variant="outline" - data-tauri-drag-region onclick={iframeUiAPI.reloadPage} > diff --git a/apps/desktop/src/routes/settings/+page.svelte b/apps/desktop/src/routes/settings/+page.svelte index cf5544f..9032f1e 100644 --- a/apps/desktop/src/routes/settings/+page.svelte +++ b/apps/desktop/src/routes/settings/+page.svelte @@ -47,9 +47,9 @@ --> - {#if dev} +