@@ -255,11 +255,16 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSMenuItemValidation {
255
255
}
256
256
}
257
257
258
+ public lazy var CLIURL : URL = {
259
+ return URL ( fileURLWithPath: utsname. isAppleSilicon ? " /opt/sbarex/syntax_highlight_cli " : " /usr/local/bin/syntax_highlight_cli " ) ;
260
+ } ( )
261
+
258
262
@IBAction func installCLITool( _ sender: Any ) {
259
263
guard let srcApp = Bundle . main. url ( forResource: " syntax_highlight_cli " , withExtension: nil ) else {
260
264
return
261
265
}
262
- let dstApp = URL ( fileURLWithPath: " /usr/local/bin/syntax_highlight_cli " )
266
+
267
+ let dstApp = CLIURL;
263
268
264
269
let alert1 = NSAlert ( )
265
270
alert1. messageText = " The tool will be installed in \( dstApp. path) \n Do you want to continue? "
@@ -270,6 +275,24 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSMenuItemValidation {
270
275
guard alert1. runModal ( ) == . alertFirstButtonReturn else {
271
276
return
272
277
}
278
+
279
+ /*if !FileManager.default.fileExists(atPath: dstApp.deletingLastPathComponent().path) {
280
+ do {
281
+ var error: NSDictionary?
282
+ NSAppleScript(source: "do shell script \"sudo mkdir -p \(dstApp.deletingLastPathComponent().path)\" with administrator " + "privileges")!.executeAndReturnError(&error)
283
+ guard error == nil else {
284
+ throw MyShellError.runtimeError(error!.description)
285
+ }
286
+ } catch {
287
+ let alert = NSAlert()
288
+ alert.messageText = "Unable to install the command line tool"
289
+ alert.informativeText = "(\(error.localizedDescription))\n\nYou can manually install the tool from a Terminal shell with this command: \nln -sfv \"\(srcApp.path)\" \"\(dstApp.path)\""
290
+ alert.alertStyle = .critical
291
+ alert.runModal()
292
+ return
293
+ }
294
+ }*/
295
+
273
296
guard access ( dstApp. deletingLastPathComponent ( ) . path, W_OK) == 0 else {
274
297
let alert = NSAlert ( )
275
298
alert. messageText = " Unable to install the tool: \( dstApp. deletingLastPathComponent ( ) . path) is not writable "
@@ -282,6 +305,13 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSMenuItemValidation {
282
305
283
306
let alert = NSAlert ( )
284
307
do {
308
+ if FileManager . default. fileExists ( atPath: dstApp. deletingLastPathComponent ( ) . path) {
309
+ var error : NSDictionary ?
310
+ NSAppleScript ( source: " do shell script \" sudo mkdir1 -p \( dstApp. deletingLastPathComponent ( ) . path) \" with administrator privileges " ) !. executeAndReturnError ( & error)
311
+ guard error == nil else {
312
+ throw MyShellError . runtimeError ( error!. description)
313
+ }
314
+ }
285
315
try FileManager . default. createSymbolicLink ( at: dstApp, withDestinationURL: srcApp)
286
316
alert. messageText = " Command line tool installed "
287
317
alert. informativeText = " You can call it from this path: \( dstApp. path) "
@@ -295,7 +325,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSMenuItemValidation {
295
325
}
296
326
297
327
@IBAction func revealCLITool( _ sender: Any ) {
298
- let u = URL ( fileURLWithPath : " /usr/local/bin/syntax_highlight_cli " )
328
+ let u = CLIURL
299
329
if FileManager . default. fileExists ( atPath: u. path) {
300
330
// Open the Finder to the settings file.
301
331
NSWorkspace . shared. activateFileViewerSelecting ( [ u] )
@@ -314,3 +344,21 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSMenuItemValidation {
314
344
}
315
345
}
316
346
347
+ extension utsname {
348
+ static var sMachine : String {
349
+ var utsname = utsname ( )
350
+ uname ( & utsname)
351
+ return withUnsafePointer ( to: & utsname. machine) {
352
+ $0. withMemoryRebound ( to: CChar . self, capacity: Int ( _SYS_NAMELEN) ) {
353
+ String ( cString: $0)
354
+ }
355
+ }
356
+ }
357
+ static var isAppleSilicon : Bool {
358
+ sMachine == " arm64 "
359
+ }
360
+ }
361
+
362
+ fileprivate enum MyShellError : Error {
363
+ case runtimeError( String )
364
+ }
0 commit comments