Skip to content

Commit 7466e15

Browse files
feat: allow customize macos launch agent config (#24)
* feat: allow customize macos launch agent config * feat: explicit add bundle identifiers param * chore: add comment * chore: sort properties * chore: sort
1 parent 46aa92c commit 7466e15

File tree

3 files changed

+81
-9
lines changed

3 files changed

+81
-9
lines changed

src/lib.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,14 @@ pub struct AutoLaunch {
202202
/// Whether use Launch Agent for implement or use AppleScript
203203
pub(crate) use_launch_agent: bool,
204204

205+
#[cfg(target_os = "macos")]
206+
/// Bundle identifiers
207+
pub(crate) bundle_identifiers: Vec<String>,
208+
209+
#[cfg(target_os = "macos")]
210+
/// Extra config in plist file for Launch Agent
211+
pub(crate) agent_extra_config: String,
212+
205213
#[cfg(windows)]
206214
pub(crate) enable_mode: WindowsEnableMode,
207215
}
@@ -276,6 +284,10 @@ pub struct AutoLaunchBuilder {
276284

277285
pub use_launch_agent: bool,
278286

287+
pub bundle_identifiers: Option<Vec<String>>,
288+
289+
pub agent_extra_config: Option<String>,
290+
279291
pub windows_enable_mode: WindowsEnableMode,
280292

281293
pub args: Option<Vec<String>>,
@@ -323,6 +335,25 @@ impl AutoLaunchBuilder {
323335
self
324336
}
325337

338+
/// Set the `bundle_identifiers`
339+
/// This setting only works on macOS
340+
pub fn set_bundle_identifiers(&mut self, bundle_identifiers: &[impl AsRef<str>]) -> &mut Self {
341+
self.bundle_identifiers = Some(
342+
bundle_identifiers
343+
.iter()
344+
.map(|s| s.as_ref().to_string())
345+
.collect(),
346+
);
347+
self
348+
}
349+
350+
/// Set the `agent_extra_config`
351+
/// This setting only works on macOS
352+
pub fn set_agent_extra_config(&mut self, config: &str) -> &mut Self {
353+
self.agent_extra_config = Some(config.into());
354+
self
355+
}
356+
326357
/// Set the [`WindowsEnableMode`].
327358
/// This setting only works on Windows
328359
pub fn set_windows_enable_mode(&mut self, mode: WindowsEnableMode) -> &mut Self {
@@ -347,6 +378,8 @@ impl AutoLaunchBuilder {
347378
let app_name = self.app_name.as_ref().ok_or(Error::AppNameNotSpecified)?;
348379
let app_path = self.app_path.as_ref().ok_or(Error::AppPathNotSpecified)?;
349380
let args = self.args.clone().unwrap_or_default();
381+
let bundle_identifiers = self.bundle_identifiers.clone().unwrap_or_default();
382+
let agent_extra_config = self.agent_extra_config.as_ref().map_or("", |v| v);
350383

351384
#[cfg(target_os = "linux")]
352385
return Ok(AutoLaunch::new(app_name, app_path, &args));
@@ -356,6 +389,8 @@ impl AutoLaunchBuilder {
356389
app_path,
357390
self.use_launch_agent,
358391
&args,
392+
&bundle_identifiers,
393+
agent_extra_config,
359394
));
360395
#[cfg(target_os = "windows")]
361396
return Ok(AutoLaunch::new(

src/macos.rs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ impl AutoLaunch {
1111
/// - `app_path`: application path
1212
/// - `use_launch_agent`: whether use Launch Agent or AppleScript
1313
/// - `args`: startup args passed to the binary
14+
/// - `bundle_identifiers`: bundle identifiers
15+
/// - `agent_extra_config`: extra config for Launch Agent
1416
///
1517
/// ## Notes
1618
///
@@ -29,6 +31,8 @@ impl AutoLaunch {
2931
app_path: &str,
3032
use_launch_agent: bool,
3133
args: &[impl AsRef<str>],
34+
bundle_identifiers: &[impl AsRef<str>],
35+
agent_extra_config: &str,
3236
) -> AutoLaunch {
3337
let mut name = app_name;
3438
if !use_launch_agent {
@@ -48,6 +52,11 @@ impl AutoLaunch {
4852
app_path: app_path.into(),
4953
use_launch_agent,
5054
args: args.iter().map(|s| s.as_ref().to_string()).collect(),
55+
bundle_identifiers: bundle_identifiers
56+
.iter()
57+
.map(|s| s.as_ref().to_string())
58+
.collect(),
59+
agent_extra_config: agent_extra_config.into(),
5160
}
5261
}
5362

@@ -92,22 +101,39 @@ impl AutoLaunch {
92101
.map(|x| format!("<string>{}</string>", x))
93102
.collect::<String>();
94103

104+
let identifiers = self
105+
.bundle_identifiers
106+
.iter()
107+
.map(|x| format!("<string>{}</string>", x))
108+
.collect::<String>();
109+
110+
let extra_config = if self.agent_extra_config.len() > 0 {
111+
format!("{}\n ", self.agent_extra_config)
112+
} else {
113+
"".to_string()
114+
};
115+
95116
let data = format!(
96117
"{}\n{}\n\
97118
<plist version=\"1.0\">\n \
98119
<dict>\n \
99120
<key>Label</key>\n \
100121
<string>{}</string>\n \
122+
<key>AssociatedBundleIdentifiers</key>\n \
123+
<array>{}</array>\n \
101124
<key>ProgramArguments</key>\n \
102125
<array>{}</array>\n \
103126
<key>RunAtLoad</key>\n \
104127
<true/>\n \
128+
{}\
105129
</dict>\n\
106130
</plist>",
107131
r#"<?xml version="1.0" encoding="UTF-8"?>"#,
108132
r#"<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">"#,
109133
self.app_name,
110-
section
134+
identifiers,
135+
section,
136+
extra_config
111137
);
112138
fs::File::create(self.get_file())?.write(data.as_bytes())?;
113139
} else {

tests/test.rs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -191,16 +191,17 @@ mod macos_unit_test {
191191
let name_1 = "AutoLaunchTest"; // different name
192192
let name_2 = "auto-launch-test"; // same name
193193

194+
let bundle_identifiers = &["com.github.auto-launch-test"];
194195
let args = &["--minimized", "--hidden"];
195196
let app_path = get_test_bin("auto-launch-test");
196197
let app_path = app_path.as_str();
197198

198199
// applescript
199-
let auto1 = AutoLaunch::new(name_1, app_path, false, args);
200-
let auto2 = AutoLaunch::new(name_2, app_path, false, args);
200+
let auto1 = AutoLaunch::new(name_1, app_path, false, args, bundle_identifiers, "");
201+
let auto2 = AutoLaunch::new(name_2, app_path, false, args, bundle_identifiers, "");
201202
// launch agent
202-
let auto3 = AutoLaunch::new(name_1, app_path, true, args);
203-
let auto4 = AutoLaunch::new(name_2, app_path, true, args);
203+
let auto3 = AutoLaunch::new(name_1, app_path, true, args, bundle_identifiers, "");
204+
let auto4 = AutoLaunch::new(name_2, app_path, true, args, bundle_identifiers, "");
204205

205206
// app_name will be revised
206207
assert_eq!(auto1.get_app_name(), name_2);
@@ -214,6 +215,7 @@ mod macos_unit_test {
214215
fn test_macos_main() {
215216
let app_name = "auto-launch-test";
216217
let app_path = get_test_bin("auto-launch-test");
218+
let bundle_identifiers = &["com.github.auto-launch-test"];
217219
let args = &["--minimized", "--hidden"];
218220
let app_path = app_path.as_str();
219221

@@ -222,27 +224,34 @@ mod macos_unit_test {
222224
let app_path_not = "/Applications/Calculator1.app";
223225

224226
// use applescript
225-
let auto1 = AutoLaunch::new(app_name, app_path, false, args);
227+
let auto1 = AutoLaunch::new(app_name, app_path, false, args, bundle_identifiers, "");
226228
assert_eq!(auto1.get_app_name(), app_name);
227229
auto1.enable().unwrap();
228230
assert!(auto1.is_enabled().unwrap());
229231
auto1.disable().unwrap();
230232
assert!(!auto1.is_enabled().unwrap());
231233

232-
let auto2 = AutoLaunch::new(app_name_not, app_path_not, false, args);
234+
let auto2 = AutoLaunch::new(
235+
app_name_not,
236+
app_path_not,
237+
false,
238+
args,
239+
bundle_identifiers,
240+
"",
241+
);
233242
assert_eq!(auto2.get_app_name(), app_name_not);
234243
assert!(auto2.enable().is_err());
235244
assert!(!auto2.is_enabled().unwrap());
236245

237246
// use launch agent
238-
let auto1 = AutoLaunch::new(app_name, app_path, true, args);
247+
let auto1 = AutoLaunch::new(app_name, app_path, true, args, bundle_identifiers, "");
239248
assert_eq!(auto1.get_app_name(), app_name);
240249
auto1.enable().unwrap();
241250
assert!(auto1.is_enabled().unwrap());
242251
auto1.disable().unwrap();
243252
assert!(!auto1.is_enabled().unwrap());
244253

245-
let auto2 = AutoLaunch::new(app_name, app_path_not, true, args);
254+
let auto2 = AutoLaunch::new(app_name, app_path_not, true, args, bundle_identifiers, "");
246255
assert_eq!(auto2.get_app_name(), app_name); // will not change the name
247256
assert!(auto2.enable().is_err());
248257
assert!(!auto2.is_enabled().unwrap());
@@ -269,6 +278,8 @@ mod macos_unit_test {
269278
.set_app_path(app_path)
270279
.set_use_launch_agent(true)
271280
.set_args(args)
281+
.set_bundle_identifiers(bundle_identifiers)
282+
.set_agent_extra_config("")
272283
.build()
273284
.unwrap();
274285

0 commit comments

Comments
 (0)