//
// H a n d l e R S w i t c h . s w i f t
// R S w i t c h
//
// C r e a t e d b y h r b r m s t r o n 8 / 2 4 / 1 9 .
// C o p y r i g h t © 2 0 1 9 B o b R u d i s . A l l r i g h t s r e s e r v e d .
//
import Foundation
import Cocoa
extension AppDelegate {
// T h e c o r e w o r k e r f u n c t i o n . R e c e i v e s t h e b a s e n a m e o f t h e s e l e c t e d d i r e c t o r y
// t h e n r e m o v e s t h e c u r r e n t a l i a s a n d c r e a t e s t h e n e w o n e .
@objc func handleRSwitch ( _ sender : NSMenuItem ? ) {
let item = sender ? . representedObject as ! String
let fm = FileManager . default
let title = sender ? . title
let rm_link = ( RVersions . macos_r_framework as NSString ) . appendingPathComponent ( " Current " )
let title_link = ( RVersions . macos_r_framework as NSString ) . appendingPathComponent ( item )
do {
try fm . removeItem ( atPath : rm_link )
} catch {
self . notifyUser ( title : " Action failed " , text : " Failed to remove 'Current' alias " + rm_link )
}
do {
try fm . createSymbolicLink (
at : NSURL ( fileURLWithPath : rm_link ) as URL ,
withDestinationURL : NSURL ( fileURLWithPath : title_link ) as URL
)
self . notifyUser ( title : " Success " , text : " Current R version switched to " + title ! )
// s e l f . n o t i f y U s e r W i t h D L ( t i t l e : " S u c c e s s " , s u b t i t l e : " S u b t i t l e " , t e x t : " C u r r e n t R v e r s i o n s w i t c h e d t o " + t i t l e ! )
} catch {
self . notifyUser ( title : " Action failed " , text : " Failed to create alias for " + title_link + " ( \( error ) ) " )
}
}
}