@@ -118,47 +118,12 @@ impl AutoLaunch {
118118 fs:: create_dir ( & dir) ?;
119119 }
120120
121- let mut args = vec ! [ self . app_path. clone( ) ] ;
122- args. extend_from_slice ( & self . args ) ;
123-
124- let section = args
125- . iter ( )
126- . map ( |x| format ! ( "<string>{}</string>" , x) )
127- . collect :: < String > ( ) ;
128-
129- let identifiers = self
130- . bundle_identifiers
131- . iter ( )
132- . map ( |x| format ! ( "<string>{}</string>" , x) )
133- . collect :: < String > ( ) ;
134-
135- let extra_config = if !self . agent_extra_config . is_empty ( ) {
136- format ! ( "{}\n " , self . agent_extra_config)
137- } else {
138- "" . to_string ( )
139- } ;
140-
141- let data = format ! (
142- "{}\n {}\n \
143- <plist version=\" 1.0\" >\n \
144- <dict>\n \
145- <key>Label</key>\n \
146- <string>{}</string>\n \
147- <key>AssociatedBundleIdentifiers</key>\n \
148- <array>{}</array>\n \
149- <key>ProgramArguments</key>\n \
150- <array>{}</array>\n \
151- <key>RunAtLoad</key>\n \
152- <true/>\n \
153- {}\
154- </dict>\n \
155- </plist>",
156- r#"<?xml version="1.0" encoding="UTF-8"?>"# ,
157- r#"<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">"# ,
158- self . app_name,
159- identifiers,
160- section,
161- extra_config
121+ let data = build_launch_agent_plist (
122+ & self . app_name ,
123+ & self . app_path ,
124+ & self . args ,
125+ & self . bundle_identifiers ,
126+ & self . agent_extra_config ,
162127 ) ;
163128 let _ = fs:: File :: create ( self . get_file ( ) ?) ?. write ( data. as_bytes ( ) ) ?;
164129 Ok ( ( ) )
@@ -293,3 +258,79 @@ fn exec_apple_script(cmd_suffix: &str) -> Result<Output> {
293258 . output ( ) ?;
294259 Ok ( output)
295260}
261+
262+ fn build_launch_agent_plist (
263+ app_name : & str ,
264+ app_path : & str ,
265+ args : & [ String ] ,
266+ bundle_identifiers : & [ String ] ,
267+ agent_extra_config : & str ,
268+ ) -> String {
269+ let mut full_args = vec ! [ app_path. to_string( ) ] ;
270+ full_args. extend_from_slice ( args) ;
271+
272+ let section = full_args
273+ . iter ( )
274+ . map ( |x| format ! ( "<string>{}</string>" , x) )
275+ . collect :: < String > ( ) ;
276+
277+ let identifiers = bundle_identifiers
278+ . iter ( )
279+ . map ( |x| format ! ( "<string>{}</string>" , x) )
280+ . collect :: < String > ( ) ;
281+
282+ let extra_config = if !agent_extra_config. is_empty ( ) {
283+ format ! ( "{}\n " , agent_extra_config)
284+ } else {
285+ String :: new ( )
286+ } ;
287+
288+ format ! (
289+ "{}\n {}\n \
290+ <plist version=\" 1.0\" >\n \
291+ <dict>\n \
292+ <key>Label</key>\n \
293+ <string>{}</string>\n \
294+ <key>AssociatedBundleIdentifiers</key>\n \
295+ <array>{}</array>\n \
296+ <key>ProgramArguments</key>\n \
297+ <array>{}</array>\n \
298+ <key>RunAtLoad</key>\n \
299+ <true/>\n \
300+ {}\
301+ </dict>\n \
302+ </plist>",
303+ r#"<?xml version="1.0" encoding="UTF-8"?>"# ,
304+ r#"<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">"# ,
305+ app_name,
306+ identifiers,
307+ section,
308+ extra_config
309+ )
310+ }
311+
312+ #[ cfg( test) ]
313+ mod tests {
314+ use super :: * ;
315+
316+ #[ test]
317+ fn test_build_launch_agent_plist ( ) {
318+ let data = build_launch_agent_plist (
319+ "TestApp" ,
320+ "/Applications/TestApp.app" ,
321+ & vec ! [ "--flag" . into( ) ] ,
322+ & vec ! [ "com.example.testapp" . into( ) ] ,
323+ "<key>KeepAlive</key><true/>" ,
324+ ) ;
325+
326+ assert ! ( data. contains( "<key>Label</key>" ) ) ;
327+ assert ! ( data. contains( "<string>TestApp</string>" ) ) ;
328+ assert ! ( data. contains( "<key>AssociatedBundleIdentifiers</key>" ) ) ;
329+ assert ! ( data. contains( "<string>com.example.testapp</string>" ) ) ;
330+ assert ! ( data. contains( "<key>ProgramArguments</key>" ) ) ;
331+ assert ! ( data. contains( "<string>/Applications/TestApp.app</string>" ) ) ;
332+ assert ! ( data. contains( "<string>--flag</string>" ) ) ;
333+ assert ! ( data. contains( "<key>RunAtLoad</key>" ) ) ;
334+ assert ! ( data. contains( "<key>KeepAlive</key><true/>" ) ) ;
335+ }
336+ }
0 commit comments