diff --git a/src/content/docs/features/shell.mdx b/src/content/docs/features/shell.mdx index a5e1debae2..89fe61852b 100644 --- a/src/content/docs/features/shell.mdx +++ b/src/content/docs/features/shell.mdx @@ -3,11 +3,154 @@ title: Shell description: Access the system shell to manage files and URLs using their default application and to spawn child processes. --- -import Stub from '@components/Stub.astro'; import PluginLinks from '@components/PluginLinks.astro'; +import { Steps, Tabs, TabItem } from '@astrojs/starlight/components'; +import CommandTabs from '@components/CommandTabs.astro'; - - Based on https://github.com/tauri-apps/plugins-workspace/tree/v2/plugins/shell - +Access the system shell. Allows you to spawn child processes and manage files and URLs using their default application. + +## Supported Platforms + +- Windows +- Linux +- macOS + +## Setup + +_This plugin requires a Rust version of at least **1.75**_ + +Install the shell plugin to get started. + + + + Use your project's package manager to add the dependency: + + { ' ' } + + + + + + 1. Install the shell plugin by adding the following to your `Cargo.toml` file: + + ```toml title="src-tauri/Cargo.toml" + [dependencies] + tauri-plugin-shell = "2.0.0-beta" + # alternatively with Git: + tauri-plugin-shell = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "v2" } + ``` + + 2. Modify `lib.rs` to initialize the plugin: + + ```rust title="src-tauri/src/lib.rs" ins={3} + fn run() { + tauri::Builder::default() + .plugin(tauri_plugin_shell::init()) + .run(tauri::generate_context!()) + .expect("error while running tauri application"); + } + ``` + + 3. Install the JavaScript Guest bindings using your preferred JavaScript package manager: + + + + + + +## Usage + +The shell plugin is available in both JavaScript and Rust. + + + + +```js +import { Command } from "@tauri-apps/plugin-shell"; + +let result = await Command.create('exec-sh', [ + '-c', + "echo 'Hello World!'", +]).execute(); +console.log(result); +``` + + + + +```rust +use tauri_plugin_shell::ShellExt; + +let shell = app_handle.shell(); +let output = tauri::async_runtime::block_on(async move { + shell + .command("echo") + .args(["Hello from Rust!"]) + .output() + .await + .unwrap() +}); +if output.status.success() { + println!("Result: {:?}", String::from_utf8(output.stdout)); +} else { + println!("Exit with code: {}", output.status.code().unwrap()); +} +``` + + + + +## Permissions + +By default all plugin commands are blocked and cannot be accessed. You must define a list of permissions in your `capabilities` configuration. + +See [Access Control List](/references/v2/acl) for more information. + +```json title="src-tauri/capabilities/main.json" ins={6-23} +{ + "$schema": "../gen/schemas/desktop-schema.json", + "identifier": "main-capability", + "description": "Capability for the main window", + "windows": ["main"], + "permissions": [ + { + "identifier": "shell:allow-execute", + "allow": [ + { + "name": "exec-sh", + "cmd": "sh", + "args": [ + "-c", + { + "validator": "\\S+" + } + ], + "sidecar": false + } + ] + } + ] +} +``` + +| Permission | Description | +| ------------------------- | ----------------------------------------------------------------- | +| `shell:allow-execute` | Enables the execute command without any pre-configured scope. | +| `shell:deny-execute` | Denies the execute command without any pre-configured scope. | +| `shell:allow-kill` | Enables the kill command without any pre-configured scope. | +| `shell:deny-kill` | Denies the kill command without any pre-configured scope. | +| `shell:allow-open` | Enables the open command without any pre-configured scope. | +| `shell:deny-open` | Denies the open command without any pre-configured scope. | +| `shell:allow-stdin-write` | Enables the stdin_write command without any pre-configured scope. | +| `shell:deny-stdin-write` | Denies the stdin_write command without any pre-configured scope. |