Skip to content

Commit 8ecb418

Browse files
feat(auto-start): introduce a builder (#2569)
* feat(auto-start): introduce a builder * Update examples * Fix missing end --- in change file * Fix missing self. * Update .changes/autostart-builder.md Co-authored-by: Amr Bashir <[email protected]> --------- Co-authored-by: Amr Bashir <[email protected]>
1 parent 286e3c6 commit 8ecb418

File tree

30 files changed

+188
-108
lines changed

30 files changed

+188
-108
lines changed

.changes/autostart-builder.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
autostart: minor
3+
autostart-js: minor
4+
---
5+
6+
Add a `Builder` for more flexible settings

plugins/autostart/README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,9 @@ First you need to register the core plugin with Tauri:
5757
`src-tauri/src/lib.rs`
5858

5959
```rust
60-
use tauri_plugin_autostart::MacosLauncher;
61-
6260
fn main() {
6361
tauri::Builder::default()
64-
.plugin(tauri_plugin_autostart::init(MacosLauncher::LaunchAgent, Some(vec!["--flag1", "--flag2"]) /* arbitrary number of args to pass to your app */))
62+
.plugin(tauri_plugin_autostart::Builder::new().args((["--flag1", "--flag2"])).build()))
6563
.run(tauri::generate_context!())
6664
.expect("error while running tauri application");
6765
}

plugins/autostart/permissions/schemas/schema.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
"minimum": 1.0
5050
},
5151
"description": {
52-
"description": "Human-readable description of what the permission does. Tauri convention is to use <h4> headings in markdown content for Tauri documentation generation purposes.",
52+
"description": "Human-readable description of what the permission does. Tauri convention is to use `<h4>` headings in markdown content for Tauri documentation generation purposes.",
5353
"type": [
5454
"string",
5555
"null"
@@ -111,7 +111,7 @@
111111
"type": "string"
112112
},
113113
"description": {
114-
"description": "Human-readable description of what the permission does. Tauri internal convention is to use <h4> headings in markdown content for Tauri documentation generation purposes.",
114+
"description": "Human-readable description of what the permission does. Tauri internal convention is to use `<h4>` headings in markdown content for Tauri documentation generation purposes.",
115115
"type": [
116116
"string",
117117
"null"

plugins/autostart/src/lib.rs

Lines changed: 127 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,17 @@ use auto_launch::{AutoLaunch, AutoLaunchBuilder};
1414
use serde::{ser::Serializer, Serialize};
1515
use tauri::{
1616
command,
17-
plugin::{Builder, TauriPlugin},
17+
plugin::{Builder as PluginBuilder, TauriPlugin},
1818
Manager, Runtime, State,
1919
};
2020

2121
use std::env::current_exe;
2222

2323
type Result<T> = std::result::Result<T, Error>;
2424

25-
#[derive(Debug, Copy, Clone)]
25+
#[derive(Debug, Default, Copy, Clone)]
2626
pub enum MacosLauncher {
27+
#[default]
2728
LaunchAgent,
2829
AppleScript,
2930
}
@@ -71,10 +72,12 @@ impl AutoLaunchManager {
7172
}
7273

7374
pub trait ManagerExt<R: Runtime> {
75+
/// TODO: Rename these to `autostart` or `auto_start` in v3
7476
fn autolaunch(&self) -> State<'_, AutoLaunchManager>;
7577
}
7678

7779
impl<R: Runtime, T: Manager<R>> ManagerExt<R> for T {
80+
/// TODO: Rename these to `autostart` or `auto_start` in v3
7881
fn autolaunch(&self) -> State<'_, AutoLaunchManager> {
7982
self.state::<AutoLaunchManager>()
8083
}
@@ -95,59 +98,132 @@ async fn is_enabled(manager: State<'_, AutoLaunchManager>) -> Result<bool> {
9598
manager.is_enabled()
9699
}
97100

101+
#[derive(Default)]
102+
pub struct Builder {
103+
#[cfg(target_os = "macos")]
104+
macos_launcher: MacosLauncher,
105+
args: Vec<String>,
106+
}
107+
108+
impl Builder {
109+
/// Create a new auto start builder with default settings
110+
pub fn new() -> Self {
111+
Self::default()
112+
}
113+
114+
/// Adds an argument to pass to your app on startup.
115+
///
116+
/// ## Examples
117+
///
118+
/// ```no_run
119+
/// Builder::new()
120+
/// .arg("--from-autostart")
121+
/// .arg("--hey")
122+
/// .build();
123+
/// ```
124+
pub fn arg<S: Into<String>>(mut self, arg: S) -> Self {
125+
self.args.push(arg.into());
126+
self
127+
}
128+
129+
/// Adds multiple arguments to pass to your app on startup.
130+
///
131+
/// ## Examples
132+
///
133+
/// ```no_run
134+
/// Builder::new()
135+
/// .args(["--from-autostart", "--hey"])
136+
/// .build();
137+
/// ```
138+
pub fn args<I, S>(mut self, args: I) -> Self
139+
where
140+
I: IntoIterator<Item = S>,
141+
S: Into<String>,
142+
{
143+
for arg in args {
144+
self = self.arg(arg);
145+
}
146+
self
147+
}
148+
149+
/// Sets whether to use launch agent or apple script to be used to enable auto start,
150+
/// the builder's default is [`MacosLauncher::LaunchAgent`]
151+
#[cfg(target_os = "macos")]
152+
pub fn macos_launcher(mut self, macos_launcher: MacosLauncher) -> Self {
153+
self.macos_launcher = macos_launcher;
154+
self
155+
}
156+
157+
pub fn build<R: Runtime>(self) -> TauriPlugin<R> {
158+
PluginBuilder::new("autostart")
159+
.invoke_handler(tauri::generate_handler![enable, disable, is_enabled])
160+
.setup(move |app, _api| {
161+
let mut builder = AutoLaunchBuilder::new();
162+
builder.set_app_name(&app.package_info().name);
163+
builder.set_args(&self.args);
164+
165+
let current_exe = current_exe()?;
166+
167+
#[cfg(windows)]
168+
builder.set_app_path(&current_exe.display().to_string());
169+
170+
#[cfg(target_os = "macos")]
171+
{
172+
builder.set_use_launch_agent(matches!(
173+
self.macos_launcher,
174+
MacosLauncher::LaunchAgent
175+
));
176+
// on macOS, current_exe gives path to /Applications/Example.app/MacOS/Example
177+
// but this results in seeing a Unix Executable in macOS login items
178+
// It must be: /Applications/Example.app
179+
// If it didn't find exactly a single occurance of .app, it will default to
180+
// exe path to not break it.
181+
let exe_path = current_exe.canonicalize()?.display().to_string();
182+
let parts: Vec<&str> = exe_path.split(".app/").collect();
183+
let app_path = if parts.len() == 2
184+
&& matches!(self.macos_launcher, MacosLauncher::AppleScript)
185+
{
186+
format!("{}.app", parts.first().unwrap())
187+
} else {
188+
exe_path
189+
};
190+
builder.set_app_path(&app_path);
191+
}
192+
193+
#[cfg(target_os = "linux")]
194+
if let Some(appimage) = app
195+
.env()
196+
.appimage
197+
.and_then(|p| p.to_str().map(|s| s.to_string()))
198+
{
199+
builder.set_app_path(&appimage);
200+
} else {
201+
builder.set_app_path(&current_exe.display().to_string());
202+
}
203+
204+
app.manage(AutoLaunchManager(
205+
builder.build().map_err(|e| e.to_string())?,
206+
));
207+
Ok(())
208+
})
209+
.build()
210+
}
211+
}
212+
98213
/// Initializes the plugin.
99214
///
100215
/// `args` - are passed to your app on startup.
101216
pub fn init<R: Runtime>(
102-
macos_launcher: MacosLauncher,
217+
#[allow(unused)] macos_launcher: MacosLauncher,
103218
args: Option<Vec<&'static str>>,
104219
) -> TauriPlugin<R> {
105-
Builder::new("autostart")
106-
.invoke_handler(tauri::generate_handler![enable, disable, is_enabled])
107-
.setup(move |app, _api| {
108-
let mut builder = AutoLaunchBuilder::new();
109-
builder.set_app_name(&app.package_info().name);
110-
if let Some(args) = args {
111-
builder.set_args(&args);
112-
}
113-
builder.set_use_launch_agent(matches!(macos_launcher, MacosLauncher::LaunchAgent));
114-
115-
let current_exe = current_exe()?;
116-
117-
#[cfg(windows)]
118-
builder.set_app_path(&current_exe.display().to_string());
119-
#[cfg(target_os = "macos")]
120-
{
121-
// on macOS, current_exe gives path to /Applications/Example.app/MacOS/Example
122-
// but this results in seeing a Unix Executable in macOS login items
123-
// It must be: /Applications/Example.app
124-
// If it didn't find exactly a single occurance of .app, it will default to
125-
// exe path to not break it.
126-
let exe_path = current_exe.canonicalize()?.display().to_string();
127-
let parts: Vec<&str> = exe_path.split(".app/").collect();
128-
let app_path =
129-
if parts.len() == 2 && matches!(macos_launcher, MacosLauncher::AppleScript) {
130-
format!("{}.app", parts.first().unwrap())
131-
} else {
132-
exe_path
133-
};
134-
builder.set_app_path(&app_path);
135-
}
136-
#[cfg(target_os = "linux")]
137-
if let Some(appimage) = app
138-
.env()
139-
.appimage
140-
.and_then(|p| p.to_str().map(|s| s.to_string()))
141-
{
142-
builder.set_app_path(&appimage);
143-
} else {
144-
builder.set_app_path(&current_exe.display().to_string());
145-
}
146-
147-
app.manage(AutoLaunchManager(
148-
builder.build().map_err(|e| e.to_string())?,
149-
));
150-
Ok(())
151-
})
152-
.build()
220+
let mut builder = Builder::new();
221+
if let Some(args) = args {
222+
builder = builder.args(args)
223+
}
224+
#[cfg(target_os = "macos")]
225+
{
226+
builder = builder.macos_launcher(macos_launcher);
227+
}
228+
builder.build()
153229
}

plugins/barcode-scanner/permissions/schemas/schema.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
"minimum": 1.0
5050
},
5151
"description": {
52-
"description": "Human-readable description of what the permission does. Tauri convention is to use <h4> headings in markdown content for Tauri documentation generation purposes.",
52+
"description": "Human-readable description of what the permission does. Tauri convention is to use `<h4>` headings in markdown content for Tauri documentation generation purposes.",
5353
"type": [
5454
"string",
5555
"null"
@@ -111,7 +111,7 @@
111111
"type": "string"
112112
},
113113
"description": {
114-
"description": "Human-readable description of what the permission does. Tauri internal convention is to use <h4> headings in markdown content for Tauri documentation generation purposes.",
114+
"description": "Human-readable description of what the permission does. Tauri internal convention is to use `<h4>` headings in markdown content for Tauri documentation generation purposes.",
115115
"type": [
116116
"string",
117117
"null"

plugins/biometric/permissions/schemas/schema.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
"minimum": 1.0
5050
},
5151
"description": {
52-
"description": "Human-readable description of what the permission does. Tauri convention is to use <h4> headings in markdown content for Tauri documentation generation purposes.",
52+
"description": "Human-readable description of what the permission does. Tauri convention is to use `<h4>` headings in markdown content for Tauri documentation generation purposes.",
5353
"type": [
5454
"string",
5555
"null"
@@ -111,7 +111,7 @@
111111
"type": "string"
112112
},
113113
"description": {
114-
"description": "Human-readable description of what the permission does. Tauri internal convention is to use <h4> headings in markdown content for Tauri documentation generation purposes.",
114+
"description": "Human-readable description of what the permission does. Tauri internal convention is to use `<h4>` headings in markdown content for Tauri documentation generation purposes.",
115115
"type": [
116116
"string",
117117
"null"

plugins/cli/permissions/schemas/schema.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
"minimum": 1.0
5050
},
5151
"description": {
52-
"description": "Human-readable description of what the permission does. Tauri convention is to use <h4> headings in markdown content for Tauri documentation generation purposes.",
52+
"description": "Human-readable description of what the permission does. Tauri convention is to use `<h4>` headings in markdown content for Tauri documentation generation purposes.",
5353
"type": [
5454
"string",
5555
"null"
@@ -111,7 +111,7 @@
111111
"type": "string"
112112
},
113113
"description": {
114-
"description": "Human-readable description of what the permission does. Tauri internal convention is to use <h4> headings in markdown content for Tauri documentation generation purposes.",
114+
"description": "Human-readable description of what the permission does. Tauri internal convention is to use `<h4>` headings in markdown content for Tauri documentation generation purposes.",
115115
"type": [
116116
"string",
117117
"null"

plugins/clipboard-manager/permissions/schemas/schema.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
"minimum": 1.0
5050
},
5151
"description": {
52-
"description": "Human-readable description of what the permission does. Tauri convention is to use <h4> headings in markdown content for Tauri documentation generation purposes.",
52+
"description": "Human-readable description of what the permission does. Tauri convention is to use `<h4>` headings in markdown content for Tauri documentation generation purposes.",
5353
"type": [
5454
"string",
5555
"null"
@@ -111,7 +111,7 @@
111111
"type": "string"
112112
},
113113
"description": {
114-
"description": "Human-readable description of what the permission does. Tauri internal convention is to use <h4> headings in markdown content for Tauri documentation generation purposes.",
114+
"description": "Human-readable description of what the permission does. Tauri internal convention is to use `<h4>` headings in markdown content for Tauri documentation generation purposes.",
115115
"type": [
116116
"string",
117117
"null"

plugins/deep-link/permissions/schemas/schema.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
"minimum": 1.0
5050
},
5151
"description": {
52-
"description": "Human-readable description of what the permission does. Tauri convention is to use <h4> headings in markdown content for Tauri documentation generation purposes.",
52+
"description": "Human-readable description of what the permission does. Tauri convention is to use `<h4>` headings in markdown content for Tauri documentation generation purposes.",
5353
"type": [
5454
"string",
5555
"null"
@@ -111,7 +111,7 @@
111111
"type": "string"
112112
},
113113
"description": {
114-
"description": "Human-readable description of what the permission does. Tauri internal convention is to use <h4> headings in markdown content for Tauri documentation generation purposes.",
114+
"description": "Human-readable description of what the permission does. Tauri internal convention is to use `<h4>` headings in markdown content for Tauri documentation generation purposes.",
115115
"type": [
116116
"string",
117117
"null"

plugins/dialog/permissions/schemas/schema.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
"minimum": 1.0
5050
},
5151
"description": {
52-
"description": "Human-readable description of what the permission does. Tauri convention is to use <h4> headings in markdown content for Tauri documentation generation purposes.",
52+
"description": "Human-readable description of what the permission does. Tauri convention is to use `<h4>` headings in markdown content for Tauri documentation generation purposes.",
5353
"type": [
5454
"string",
5555
"null"
@@ -111,7 +111,7 @@
111111
"type": "string"
112112
},
113113
"description": {
114-
"description": "Human-readable description of what the permission does. Tauri internal convention is to use <h4> headings in markdown content for Tauri documentation generation purposes.",
114+
"description": "Human-readable description of what the permission does. Tauri internal convention is to use `<h4>` headings in markdown content for Tauri documentation generation purposes.",
115115
"type": [
116116
"string",
117117
"null"

0 commit comments

Comments
 (0)