diff --git a/.cursor/commands/manage-cli.md b/.cursor/commands/manage-cli.md new file mode 100644 index 0000000000..88ad368e5c --- /dev/null +++ b/.cursor/commands/manage-cli.md @@ -0,0 +1,5 @@ +Based on `plugins/cli`, you might need to update: +- `apps/web/content/docs/cli.mdx` +- `apps/desktop/src-tauri/tauri.conf.json` + +Ref: https://v2.tauri.app/plugin/cli diff --git a/Cargo.lock b/Cargo.lock index 6355cc2434..ab1bece904 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3934,6 +3934,7 @@ dependencies = [ "tauri-plugin-auth", "tauri-plugin-autostart", "tauri-plugin-cli", + "tauri-plugin-cli2", "tauri-plugin-clipboard-manager", "tauri-plugin-db2", "tauri-plugin-deep-link", @@ -14751,6 +14752,22 @@ dependencies = [ "thiserror 2.0.17", ] +[[package]] +name = "tauri-plugin-cli2" +version = "0.1.0" +dependencies = [ + "open", + "serde", + "specta", + "specta-typescript", + "tauri", + "tauri-plugin", + "tauri-plugin-cli", + "tauri-specta", + "thiserror 2.0.17", + "tracing", +] + [[package]] name = "tauri-plugin-clipboard-manager" version = "2.3.2" diff --git a/Cargo.toml b/Cargo.toml index 551f94ae33..19afc9a46e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -111,6 +111,7 @@ tauri-plugin-updater = "2.9" tauri-plugin-analytics = { path = "plugins/analytics" } tauri-plugin-apple-calendar = { path = "plugins/apple-calendar" } tauri-plugin-auth = { path = "plugins/auth" } +tauri-plugin-cli2 = { path = "plugins/cli2" } tauri-plugin-db = { path = "plugins/db" } tauri-plugin-db2 = { path = "plugins/db2" } tauri-plugin-detect = { path = "plugins/detect" } @@ -157,6 +158,7 @@ indoc = "2" itertools = "0.14.0" lazy_static = "1.5.0" once_cell = "1.20.3" +open = "5" regex = "1.11.1" schemars = "0.8.21" serde = "1" diff --git a/apps/desktop/package.json b/apps/desktop/package.json index ab05521f74..4f54bf170d 100644 --- a/apps/desktop/package.json +++ b/apps/desktop/package.json @@ -30,6 +30,7 @@ "@hypr/codemirror": "workspace:^", "@hypr/db": "workspace:*", "@hypr/plugin-analytics": "workspace:*", + "@hypr/plugin-cli2": "workspace:*", "@hypr/plugin-db2": "workspace:*", "@hypr/plugin-detect": "workspace:*", "@hypr/plugin-hooks": "workspace:*", diff --git a/apps/desktop/src-tauri/Cargo.toml b/apps/desktop/src-tauri/Cargo.toml index 08ff672215..d5b736110b 100644 --- a/apps/desktop/src-tauri/Cargo.toml +++ b/apps/desktop/src-tauri/Cargo.toml @@ -24,6 +24,7 @@ tauri-plugin-analytics = { workspace = true } tauri-plugin-auth = { workspace = true } tauri-plugin-autostart = { workspace = true } tauri-plugin-cli = { workspace = true } +tauri-plugin-cli2 = { workspace = true } tauri-plugin-clipboard-manager = { workspace = true } tauri-plugin-db2 = { workspace = true } tauri-plugin-deep-link = { workspace = true } diff --git a/apps/desktop/src-tauri/capabilities/default.json b/apps/desktop/src-tauri/capabilities/default.json index 8463563909..8d69e94915 100644 --- a/apps/desktop/src-tauri/capabilities/default.json +++ b/apps/desktop/src-tauri/capabilities/default.json @@ -75,6 +75,7 @@ "tray:default", "auth:default", "analytics:default", + "cli2:default", "updater:default", "deep-link:default", "store:default", diff --git a/apps/desktop/src-tauri/src/lib.rs b/apps/desktop/src-tauri/src/lib.rs index 14c39d6a18..a6f75adbe1 100644 --- a/apps/desktop/src-tauri/src/lib.rs +++ b/apps/desktop/src-tauri/src/lib.rs @@ -5,7 +5,6 @@ mod store; use ext::*; use store::*; -use tauri_plugin_cli::CliExt; use tauri_plugin_windows::{AppWindow, WindowsPluginExt}; #[tokio::main] @@ -46,6 +45,7 @@ pub async fn main() { builder = builder .plugin(tauri_plugin_cli::init()) + .plugin(tauri_plugin_cli2::init()) .plugin(tauri_plugin_opener::init()) .plugin(tauri_plugin_dialog::init()) .plugin(tauri_plugin_auth::init()) @@ -90,15 +90,6 @@ pub async fn main() { .invoke_handler(specta_builder.invoke_handler()) .on_window_event(tauri_plugin_windows::on_window_event) .setup(move |app| { - match app.cli().matches() { - Ok(matches) => { - println!("{matches:?}"); - } - Err(error) => { - println!("failed to read CLI matches: {error}"); - } - } - let app_handle = app.handle().clone(); let app_clone = app_handle.clone(); diff --git a/apps/desktop/src-tauri/tauri.conf.json b/apps/desktop/src-tauri/tauri.conf.json index 8d5b34d6fc..f301ca3f21 100644 --- a/apps/desktop/src-tauri/tauri.conf.json +++ b/apps/desktop/src-tauri/tauri.conf.json @@ -52,6 +52,15 @@ } }, "plugins": { + "cli": { + "description": "Hyprnote", + "args": [], + "subcommands": { + "hello": { + "description": "Open https://hyprnote.com" + } + } + }, "deep-link": { "desktop": { "schemes": [ diff --git a/apps/web/content/docs/cli.mdx b/apps/web/content/docs/cli.mdx new file mode 100644 index 0000000000..886c0920d3 --- /dev/null +++ b/apps/web/content/docs/cli.mdx @@ -0,0 +1,4 @@ +--- +title: CLI +description: Learn how to use CLI in Hyprnote +--- diff --git a/plugins/cli2/.gitignore b/plugins/cli2/.gitignore new file mode 100644 index 0000000000..50d8e32e89 --- /dev/null +++ b/plugins/cli2/.gitignore @@ -0,0 +1,17 @@ +/.vs +.DS_Store +.Thumbs.db +*.sublime* +.idea/ +debug.log +package-lock.json +.vscode/settings.json +yarn.lock + +/.tauri +/target +Cargo.lock +node_modules/ + +dist-js +dist diff --git a/plugins/cli2/Cargo.toml b/plugins/cli2/Cargo.toml new file mode 100644 index 0000000000..697e309e1b --- /dev/null +++ b/plugins/cli2/Cargo.toml @@ -0,0 +1,26 @@ +[package] +name = "tauri-plugin-cli2" +version = "0.1.0" +authors = ["You"] +edition = "2021" +exclude = ["/js", "/node_modules"] +links = "tauri-plugin-cli2" +description = "" + +[build-dependencies] +tauri-plugin = { workspace = true, features = ["build"] } + +[dev-dependencies] +specta-typescript = { workspace = true } + +[dependencies] +tauri = { workspace = true, features = ["test"] } +tauri-plugin-cli = { workspace = true } +tauri-specta = { workspace = true, features = ["derive", "typescript"] } + +serde = { workspace = true } +specta = { workspace = true } + +open = { workspace = true } +thiserror = { workspace = true } +tracing = { workspace = true } diff --git a/plugins/cli2/build.rs b/plugins/cli2/build.rs new file mode 100644 index 0000000000..83526150ba --- /dev/null +++ b/plugins/cli2/build.rs @@ -0,0 +1,5 @@ +const COMMANDS: &[&str] = &["install_cli", "uninstall_cli", "check_cli_status"]; + +fn main() { + tauri_plugin::Builder::new(COMMANDS).build(); +} diff --git a/plugins/cli2/js/bindings.gen.ts b/plugins/cli2/js/bindings.gen.ts new file mode 100644 index 0000000000..8c22a1c44e --- /dev/null +++ b/plugins/cli2/js/bindings.gen.ts @@ -0,0 +1,106 @@ +// @ts-nocheck + + +// This file was generated by [tauri-specta](https://github.com/oscartbeaumont/tauri-specta). Do not edit this file manually. + +/** user-defined commands **/ + + +export const commands = { +async installCli() : Promise> { + try { + return { status: "ok", data: await TAURI_INVOKE("plugin:cli2|install_cli") }; +} catch (e) { + if(e instanceof Error) throw e; + else return { status: "error", error: e as any }; +} +}, +async uninstallCli() : Promise> { + try { + return { status: "ok", data: await TAURI_INVOKE("plugin:cli2|uninstall_cli") }; +} catch (e) { + if(e instanceof Error) throw e; + else return { status: "error", error: e as any }; +} +}, +async checkCliStatus() : Promise> { + try { + return { status: "ok", data: await TAURI_INVOKE("plugin:cli2|check_cli_status") }; +} catch (e) { + if(e instanceof Error) throw e; + else return { status: "error", error: e as any }; +} +} +} + +/** user-defined events **/ + + + +/** user-defined constants **/ + + + +/** user-defined types **/ + +export type CliStatus = { isInstalled: boolean; symlinkPath: string | null; targetPath: string | null } + +/** tauri-specta globals **/ + +import { + invoke as TAURI_INVOKE, + Channel as TAURI_CHANNEL, +} from "@tauri-apps/api/core"; +import * as TAURI_API_EVENT from "@tauri-apps/api/event"; +import { type WebviewWindow as __WebviewWindow__ } from "@tauri-apps/api/webviewWindow"; + +type __EventObj__ = { + listen: ( + cb: TAURI_API_EVENT.EventCallback, + ) => ReturnType>; + once: ( + cb: TAURI_API_EVENT.EventCallback, + ) => ReturnType>; + emit: null extends T + ? (payload?: T) => ReturnType + : (payload: T) => ReturnType; +}; + +export type Result = + | { status: "ok"; data: T } + | { status: "error"; error: E }; + +function __makeEvents__>( + mappings: Record, +) { + return new Proxy( + {} as unknown as { + [K in keyof T]: __EventObj__ & { + (handle: __WebviewWindow__): __EventObj__; + }; + }, + { + get: (_, event) => { + const name = mappings[event as keyof T]; + + return new Proxy((() => {}) as any, { + apply: (_, __, [window]: [__WebviewWindow__]) => ({ + listen: (arg: any) => window.listen(name, arg), + once: (arg: any) => window.once(name, arg), + emit: (arg: any) => window.emit(name, arg), + }), + get: (_, command: keyof __EventObj__) => { + switch (command) { + case "listen": + return (arg: any) => TAURI_API_EVENT.listen(name, arg); + case "once": + return (arg: any) => TAURI_API_EVENT.once(name, arg); + case "emit": + return (arg: any) => TAURI_API_EVENT.emit(name, arg); + } + }, + }); + }, + }, + ); +} diff --git a/plugins/cli2/js/index.ts b/plugins/cli2/js/index.ts new file mode 100644 index 0000000000..a96e122f03 --- /dev/null +++ b/plugins/cli2/js/index.ts @@ -0,0 +1 @@ +export * from "./bindings.gen"; diff --git a/plugins/cli2/package.json b/plugins/cli2/package.json new file mode 100644 index 0000000000..322aeb3ca4 --- /dev/null +++ b/plugins/cli2/package.json @@ -0,0 +1,11 @@ +{ + "name": "@hypr/plugin-cli2", + "private": true, + "main": "./js/index.ts", + "scripts": { + "codegen": "cargo test -p tauri-plugin-cli2" + }, + "dependencies": { + "@tauri-apps/api": "^2.9.0" + } +} diff --git a/plugins/cli2/permissions/autogenerated/commands/check_cli_status.toml b/plugins/cli2/permissions/autogenerated/commands/check_cli_status.toml new file mode 100644 index 0000000000..4a3c962add --- /dev/null +++ b/plugins/cli2/permissions/autogenerated/commands/check_cli_status.toml @@ -0,0 +1,13 @@ +# Automatically generated - DO NOT EDIT! + +"$schema" = "../../schemas/schema.json" + +[[permission]] +identifier = "allow-check-cli-status" +description = "Enables the check_cli_status command without any pre-configured scope." +commands.allow = ["check_cli_status"] + +[[permission]] +identifier = "deny-check-cli-status" +description = "Denies the check_cli_status command without any pre-configured scope." +commands.deny = ["check_cli_status"] diff --git a/plugins/cli2/permissions/autogenerated/commands/install_cli.toml b/plugins/cli2/permissions/autogenerated/commands/install_cli.toml new file mode 100644 index 0000000000..f2a5ddf783 --- /dev/null +++ b/plugins/cli2/permissions/autogenerated/commands/install_cli.toml @@ -0,0 +1,13 @@ +# Automatically generated - DO NOT EDIT! + +"$schema" = "../../schemas/schema.json" + +[[permission]] +identifier = "allow-install-cli" +description = "Enables the install_cli command without any pre-configured scope." +commands.allow = ["install_cli"] + +[[permission]] +identifier = "deny-install-cli" +description = "Denies the install_cli command without any pre-configured scope." +commands.deny = ["install_cli"] diff --git a/plugins/cli2/permissions/autogenerated/commands/uninstall_cli.toml b/plugins/cli2/permissions/autogenerated/commands/uninstall_cli.toml new file mode 100644 index 0000000000..5dbf9d6452 --- /dev/null +++ b/plugins/cli2/permissions/autogenerated/commands/uninstall_cli.toml @@ -0,0 +1,13 @@ +# Automatically generated - DO NOT EDIT! + +"$schema" = "../../schemas/schema.json" + +[[permission]] +identifier = "allow-uninstall-cli" +description = "Enables the uninstall_cli command without any pre-configured scope." +commands.allow = ["uninstall_cli"] + +[[permission]] +identifier = "deny-uninstall-cli" +description = "Denies the uninstall_cli command without any pre-configured scope." +commands.deny = ["uninstall_cli"] diff --git a/plugins/cli2/permissions/autogenerated/reference.md b/plugins/cli2/permissions/autogenerated/reference.md new file mode 100644 index 0000000000..4aaa9834f7 --- /dev/null +++ b/plugins/cli2/permissions/autogenerated/reference.md @@ -0,0 +1,97 @@ +## Default Permission + +Default permissions for the plugin + +#### This default permission set includes the following: + +- `allow-install-cli` +- `allow-uninstall-cli` +- `allow-check-cli-status` + +## Permission Table + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
IdentifierDescription
+ +`cli2:allow-check-cli-status` + + + +Enables the check_cli_status command without any pre-configured scope. + +
+ +`cli2:deny-check-cli-status` + + + +Denies the check_cli_status command without any pre-configured scope. + +
+ +`cli2:allow-install-cli` + + + +Enables the install_cli command without any pre-configured scope. + +
+ +`cli2:deny-install-cli` + + + +Denies the install_cli command without any pre-configured scope. + +
+ +`cli2:allow-uninstall-cli` + + + +Enables the uninstall_cli command without any pre-configured scope. + +
+ +`cli2:deny-uninstall-cli` + + + +Denies the uninstall_cli command without any pre-configured scope. + +
diff --git a/plugins/cli2/permissions/default.toml b/plugins/cli2/permissions/default.toml new file mode 100644 index 0000000000..0839ab62ff --- /dev/null +++ b/plugins/cli2/permissions/default.toml @@ -0,0 +1,3 @@ +[default] +description = "Default permissions for the plugin" +permissions = ["allow-install-cli", "allow-uninstall-cli", "allow-check-cli-status"] diff --git a/plugins/cli2/permissions/schemas/schema.json b/plugins/cli2/permissions/schemas/schema.json new file mode 100644 index 0000000000..f58d53aae8 --- /dev/null +++ b/plugins/cli2/permissions/schemas/schema.json @@ -0,0 +1,342 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "title": "PermissionFile", + "description": "Permission file that can define a default permission, a set of permissions or a list of inlined permissions.", + "type": "object", + "properties": { + "default": { + "description": "The default permission set for the plugin", + "anyOf": [ + { + "$ref": "#/definitions/DefaultPermission" + }, + { + "type": "null" + } + ] + }, + "set": { + "description": "A list of permissions sets defined", + "type": "array", + "items": { + "$ref": "#/definitions/PermissionSet" + } + }, + "permission": { + "description": "A list of inlined permissions", + "default": [], + "type": "array", + "items": { + "$ref": "#/definitions/Permission" + } + } + }, + "definitions": { + "DefaultPermission": { + "description": "The default permission set of the plugin.\n\nWorks similarly to a permission with the \"default\" identifier.", + "type": "object", + "required": [ + "permissions" + ], + "properties": { + "version": { + "description": "The version of the permission.", + "type": [ + "integer", + "null" + ], + "format": "uint64", + "minimum": 1.0 + }, + "description": { + "description": "Human-readable description of what the permission does. Tauri convention is to use `

` headings in markdown content for Tauri documentation generation purposes.", + "type": [ + "string", + "null" + ] + }, + "permissions": { + "description": "All permissions this set contains.", + "type": "array", + "items": { + "type": "string" + } + } + } + }, + "PermissionSet": { + "description": "A set of direct permissions grouped together under a new name.", + "type": "object", + "required": [ + "description", + "identifier", + "permissions" + ], + "properties": { + "identifier": { + "description": "A unique identifier for the permission.", + "type": "string" + }, + "description": { + "description": "Human-readable description of what the permission does.", + "type": "string" + }, + "permissions": { + "description": "All permissions this set contains.", + "type": "array", + "items": { + "$ref": "#/definitions/PermissionKind" + } + } + } + }, + "Permission": { + "description": "Descriptions of explicit privileges of commands.\n\nIt can enable commands to be accessible in the frontend of the application.\n\nIf the scope is defined it can be used to fine grain control the access of individual or multiple commands.", + "type": "object", + "required": [ + "identifier" + ], + "properties": { + "version": { + "description": "The version of the permission.", + "type": [ + "integer", + "null" + ], + "format": "uint64", + "minimum": 1.0 + }, + "identifier": { + "description": "A unique identifier for the permission.", + "type": "string" + }, + "description": { + "description": "Human-readable description of what the permission does. Tauri internal convention is to use `

` headings in markdown content for Tauri documentation generation purposes.", + "type": [ + "string", + "null" + ] + }, + "commands": { + "description": "Allowed or denied commands when using this permission.", + "default": { + "allow": [], + "deny": [] + }, + "allOf": [ + { + "$ref": "#/definitions/Commands" + } + ] + }, + "scope": { + "description": "Allowed or denied scoped when using this permission.", + "allOf": [ + { + "$ref": "#/definitions/Scopes" + } + ] + }, + "platforms": { + "description": "Target platforms this permission applies. By default all platforms are affected by this permission.", + "type": [ + "array", + "null" + ], + "items": { + "$ref": "#/definitions/Target" + } + } + } + }, + "Commands": { + "description": "Allowed and denied commands inside a permission.\n\nIf two commands clash inside of `allow` and `deny`, it should be denied by default.", + "type": "object", + "properties": { + "allow": { + "description": "Allowed command.", + "default": [], + "type": "array", + "items": { + "type": "string" + } + }, + "deny": { + "description": "Denied command, which takes priority.", + "default": [], + "type": "array", + "items": { + "type": "string" + } + } + } + }, + "Scopes": { + "description": "An argument for fine grained behavior control of Tauri commands.\n\nIt can be of any serde serializable type and is used to allow or prevent certain actions inside a Tauri command. The configured scope is passed to the command and will be enforced by the command implementation.\n\n## Example\n\n```json { \"allow\": [{ \"path\": \"$HOME/**\" }], \"deny\": [{ \"path\": \"$HOME/secret.txt\" }] } ```", + "type": "object", + "properties": { + "allow": { + "description": "Data that defines what is allowed by the scope.", + "type": [ + "array", + "null" + ], + "items": { + "$ref": "#/definitions/Value" + } + }, + "deny": { + "description": "Data that defines what is denied by the scope. This should be prioritized by validation logic.", + "type": [ + "array", + "null" + ], + "items": { + "$ref": "#/definitions/Value" + } + } + } + }, + "Value": { + "description": "All supported ACL values.", + "anyOf": [ + { + "description": "Represents a null JSON value.", + "type": "null" + }, + { + "description": "Represents a [`bool`].", + "type": "boolean" + }, + { + "description": "Represents a valid ACL [`Number`].", + "allOf": [ + { + "$ref": "#/definitions/Number" + } + ] + }, + { + "description": "Represents a [`String`].", + "type": "string" + }, + { + "description": "Represents a list of other [`Value`]s.", + "type": "array", + "items": { + "$ref": "#/definitions/Value" + } + }, + { + "description": "Represents a map of [`String`] keys to [`Value`]s.", + "type": "object", + "additionalProperties": { + "$ref": "#/definitions/Value" + } + } + ] + }, + "Number": { + "description": "A valid ACL number.", + "anyOf": [ + { + "description": "Represents an [`i64`].", + "type": "integer", + "format": "int64" + }, + { + "description": "Represents a [`f64`].", + "type": "number", + "format": "double" + } + ] + }, + "Target": { + "description": "Platform target.", + "oneOf": [ + { + "description": "MacOS.", + "type": "string", + "enum": [ + "macOS" + ] + }, + { + "description": "Windows.", + "type": "string", + "enum": [ + "windows" + ] + }, + { + "description": "Linux.", + "type": "string", + "enum": [ + "linux" + ] + }, + { + "description": "Android.", + "type": "string", + "enum": [ + "android" + ] + }, + { + "description": "iOS.", + "type": "string", + "enum": [ + "iOS" + ] + } + ] + }, + "PermissionKind": { + "type": "string", + "oneOf": [ + { + "description": "Enables the check_cli_status command without any pre-configured scope.", + "type": "string", + "const": "allow-check-cli-status", + "markdownDescription": "Enables the check_cli_status command without any pre-configured scope." + }, + { + "description": "Denies the check_cli_status command without any pre-configured scope.", + "type": "string", + "const": "deny-check-cli-status", + "markdownDescription": "Denies the check_cli_status command without any pre-configured scope." + }, + { + "description": "Enables the install_cli command without any pre-configured scope.", + "type": "string", + "const": "allow-install-cli", + "markdownDescription": "Enables the install_cli command without any pre-configured scope." + }, + { + "description": "Denies the install_cli command without any pre-configured scope.", + "type": "string", + "const": "deny-install-cli", + "markdownDescription": "Denies the install_cli command without any pre-configured scope." + }, + { + "description": "Enables the uninstall_cli command without any pre-configured scope.", + "type": "string", + "const": "allow-uninstall-cli", + "markdownDescription": "Enables the uninstall_cli command without any pre-configured scope." + }, + { + "description": "Denies the uninstall_cli command without any pre-configured scope.", + "type": "string", + "const": "deny-uninstall-cli", + "markdownDescription": "Denies the uninstall_cli command without any pre-configured scope." + }, + { + "description": "Default permissions for the plugin\n#### This default permission set includes:\n\n- `allow-install-cli`\n- `allow-uninstall-cli`\n- `allow-check-cli-status`", + "type": "string", + "const": "default", + "markdownDescription": "Default permissions for the plugin\n#### This default permission set includes:\n\n- `allow-install-cli`\n- `allow-uninstall-cli`\n- `allow-check-cli-status`" + } + ] + } + } +} \ No newline at end of file diff --git a/plugins/cli2/src/commands.rs b/plugins/cli2/src/commands.rs new file mode 100644 index 0000000000..8cd5eb105f --- /dev/null +++ b/plugins/cli2/src/commands.rs @@ -0,0 +1,24 @@ +#[tauri::command] +#[specta::specta] +pub(crate) async fn install_cli(app: tauri::AppHandle) -> Result<(), String> { + use crate::CliPluginExt; + app.install_cli_to_path().map_err(|e| e.to_string()) +} + +#[tauri::command] +#[specta::specta] +pub(crate) async fn uninstall_cli( + app: tauri::AppHandle, +) -> Result<(), String> { + use crate::CliPluginExt; + app.uninstall_cli_from_path().map_err(|e| e.to_string()) +} + +#[tauri::command] +#[specta::specta] +pub(crate) async fn check_cli_status( + app: tauri::AppHandle, +) -> Result { + use crate::CliPluginExt; + app.check_cli_status().map_err(|e| e.to_string()) +} diff --git a/plugins/cli2/src/error.rs b/plugins/cli2/src/error.rs new file mode 100644 index 0000000000..5e73a47b1a --- /dev/null +++ b/plugins/cli2/src/error.rs @@ -0,0 +1,27 @@ +use serde::Serialize; + +#[derive(Debug, thiserror::Error)] +pub enum Error { + #[error(transparent)] + Io(#[from] std::io::Error), + + #[error("Unsupported platform for CLI installation")] + UnsupportedPlatform, + + #[error("Could not determine home directory")] + NoHomeDirectory, + + #[error("Refusing to remove non-symlink CLI path: {0}")] + NonSymlinkCliPath(String), +} + +impl Serialize for Error { + fn serialize(&self, serializer: S) -> std::result::Result + where + S: serde::Serializer, + { + serializer.serialize_str(self.to_string().as_ref()) + } +} + +pub type Result = std::result::Result; diff --git a/plugins/cli2/src/ext.rs b/plugins/cli2/src/ext.rs new file mode 100644 index 0000000000..bbda9c4a6e --- /dev/null +++ b/plugins/cli2/src/ext.rs @@ -0,0 +1,146 @@ +use std::path::PathBuf; + +pub trait CliPluginExt { + fn handle_cli_matches(&self) -> Result<(), crate::Error>; + fn get_cli_symlink_path(&self) -> PathBuf; + fn get_cli_executable_path(&self) -> Result; + fn install_cli_to_path(&self) -> Result<(), crate::Error>; + fn uninstall_cli_from_path(&self) -> Result<(), crate::Error>; + fn check_cli_status(&self) -> Result; +} + +impl> crate::CliPluginExt for T { + fn handle_cli_matches(&self) -> Result<(), crate::Error> { + use tauri_plugin_cli::CliExt; + + match self.cli().matches() { + Ok(matches) => { + if matches.args.contains_key("help") || matches.args.contains_key("version") { + std::process::exit(0); + } + } + Err(error) => { + eprintln!("failed to read CLI matches: {error}"); + std::process::exit(1); + } + } + + Ok(()) + } + + fn get_cli_symlink_path(&self) -> PathBuf { + #[cfg(unix)] + { + if let Some(home) = std::env::var_os("HOME") { + return PathBuf::from(home).join(".local/bin/hyprnote"); + } + PathBuf::from("/usr/local/bin/hyprnote") + } + + #[cfg(windows)] + { + if let Some(home) = std::env::var_os("USERPROFILE") { + return PathBuf::from(home).join(".local\\bin\\hyprnote.exe"); + } + PathBuf::from("C:\\Program Files\\hyprnote\\hyprnote.exe") + } + + #[cfg(not(any(unix, windows)))] + { + PathBuf::from("hyprnote") + } + } + + fn get_cli_executable_path(&self) -> Result { + std::env::current_exe().map_err(|e| e.into()) + } + + fn install_cli_to_path(&self) -> Result<(), crate::Error> { + let exe_path = self.get_cli_executable_path()?; + let symlink_path = self.get_cli_symlink_path(); + + if let Some(parent) = symlink_path.parent() { + if !parent.exists() { + std::fs::create_dir_all(parent)?; + } + } + + #[cfg(unix)] + { + if symlink_path.exists() { + std::fs::remove_file(&symlink_path)?; + } + + std::os::unix::fs::symlink(&exe_path, &symlink_path)?; + } + + #[cfg(windows)] + { + if symlink_path.exists() { + std::fs::remove_file(&symlink_path)?; + } + + std::os::windows::fs::symlink_file(&exe_path, &symlink_path)?; + } + + #[cfg(not(any(unix, windows)))] + { + return Err(crate::Error::UnsupportedPlatform); + } + + Ok(()) + } + + fn uninstall_cli_from_path(&self) -> Result<(), crate::Error> { + #[cfg(not(any(unix, windows)))] + { + return Err(crate::Error::UnsupportedPlatform); + } + + let symlink_path = self.get_cli_symlink_path(); + + let metadata = match std::fs::symlink_metadata(&symlink_path) { + Ok(metadata) => metadata, + Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(()), + Err(error) => return Err(error.into()), + }; + + if metadata.file_type().is_symlink() { + std::fs::remove_file(&symlink_path)?; + } else { + return Err(crate::Error::NonSymlinkCliPath( + symlink_path.to_string_lossy().into_owned(), + )); + } + + Ok(()) + } + + fn check_cli_status(&self) -> Result { + let symlink_path = self.get_cli_symlink_path(); + + if !symlink_path.exists() { + return Ok(CliStatus { + is_installed: false, + symlink_path: None, + target_path: None, + }); + } + + let target = std::fs::read_link(&symlink_path).ok(); + + Ok(CliStatus { + is_installed: true, + symlink_path: Some(symlink_path.to_string_lossy().to_string()), + target_path: target.map(|p| p.to_string_lossy().to_string()), + }) + } +} + +#[derive(serde::Serialize, serde::Deserialize, specta::Type)] +#[serde(rename_all = "camelCase")] +pub struct CliStatus { + pub is_installed: bool, + pub symlink_path: Option, + pub target_path: Option, +} diff --git a/plugins/cli2/src/handler.rs b/plugins/cli2/src/handler.rs new file mode 100644 index 0000000000..211fb88b5f --- /dev/null +++ b/plugins/cli2/src/handler.rs @@ -0,0 +1,32 @@ +use tauri::AppHandle; +use tauri_plugin_cli::Matches; + +pub fn entrypoint(app: &AppHandle, matches: Matches) { + if matches.args.contains_key("help") { + std::process::exit(0); + } + + if matches.args.contains_key("version") { + std::process::exit(0); + } + + if let Some(subcommand_matches) = matches.subcommand { + match subcommand_matches.name.as_str() { + "hello" => hello(app), + _ => { + tracing::warn!("unknown_subcommand: {}", subcommand_matches.name); + std::process::exit(1); + } + } + } +} + +fn hello(_app: &AppHandle) { + match open::that("https://hyprnote.com") { + Ok(_) => std::process::exit(0), + Err(e) => { + tracing::error!("open_url_error: {e}"); + std::process::exit(1); + } + } +} diff --git a/plugins/cli2/src/lib.rs b/plugins/cli2/src/lib.rs new file mode 100644 index 0000000000..3163f0ad0a --- /dev/null +++ b/plugins/cli2/src/lib.rs @@ -0,0 +1,59 @@ +mod commands; +mod error; +mod ext; +mod handler; + +pub use error::{Error, Result}; +pub use ext::*; + +const PLUGIN_NAME: &str = "cli2"; + +fn make_specta_builder() -> tauri_specta::Builder { + tauri_specta::Builder::::new() + .plugin_name(PLUGIN_NAME) + .commands(tauri_specta::collect_commands![ + commands::install_cli::, + commands::uninstall_cli::, + commands::check_cli_status::, + ]) + .error_handling(tauri_specta::ErrorHandlingMode::Result) +} + +pub fn init() -> tauri::plugin::TauriPlugin { + let specta_builder = make_specta_builder(); + + tauri::plugin::Builder::new(PLUGIN_NAME) + .invoke_handler(specta_builder.invoke_handler()) + .setup(|app, _api| { + let matches = { + use tauri_plugin_cli::CliExt; + app.cli().matches() + }; + + match matches { + Ok(matches) => handler::entrypoint(app, matches), + Err(error) => tracing::error!("cli_matches_error: {error}"), + } + + Ok(()) + }) + .build() +} + +#[cfg(test)] +mod test { + use super::*; + + #[test] + fn export_types() { + make_specta_builder::() + .export( + specta_typescript::Typescript::default() + .header("// @ts-nocheck\n\n") + .formatter(specta_typescript::formatter::prettier) + .bigint(specta_typescript::BigIntExportBehavior::Number), + "./js/bindings.gen.ts", + ) + .unwrap() + } +} diff --git a/plugins/cli2/tsconfig.json b/plugins/cli2/tsconfig.json new file mode 100644 index 0000000000..13b985325d --- /dev/null +++ b/plugins/cli2/tsconfig.json @@ -0,0 +1,5 @@ +{ + "extends": "../tsconfig.base.json", + "include": ["./js/*.ts"], + "exclude": ["node_modules"] +} diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index e6d4360b34..541383fab2 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -105,6 +105,9 @@ importers: '@hypr/plugin-analytics': specifier: workspace:* version: link:../../plugins/analytics + '@hypr/plugin-cli2': + specifier: workspace:* + version: link:../../plugins/cli2 '@hypr/plugin-db2': specifier: workspace:* version: link:../../plugins/db2 @@ -1036,6 +1039,12 @@ importers: specifier: ^2.9.0 version: 2.9.0 + plugins/cli2: + dependencies: + '@tauri-apps/api': + specifier: ^2.9.0 + version: 2.9.0 + plugins/db: dependencies: '@tauri-apps/api':