@@ -25,7 +25,7 @@ import Foundation
2525/**
2626Create a Derived Data RAM disk for use by Xcode.
2727The regex matching is designed for English language systems.
28- Tested with OS X 10.11.5 (El Capitan) and Swift 2.2 .
28+ Tested with OS X 10.11.6 (El Capitan) and Swift 3.0 .
2929
3030The disk is mounted into the default path for Xcode's Derived Data path and will be used automatically
3131by Xcode if the path is correct for the one set in Xcode's preferences.
@@ -81,14 +81,16 @@ let derivedDataPath = "\(home)/Library/Developer/Xcode/DerivedData"
8181 */
8282func ramDiskExists( ) -> Bool
8383{
84- let output = runTask ( " /sbin/mount " , arguments: [ ] )
85-
84+ let output = runTask ( launchPath: " /sbin/mount " , arguments: [ ] )
8685 let regex : NSRegularExpression ?
86+
8787 do {
8888 regex = try NSRegularExpression ( pattern: " /dev/disk.*Library/Developer/Xcode/DerivedData.*mounted " ,
89- options: NSRegularExpressionOptions . CaseInsensitive )
89+ options: NSRegularExpression . Options . caseInsensitive )
9090
91- let numberOfMatches = regex!. numberOfMatchesInString ( output, options: [ ] , range: NSMakeRange ( 0 , output. characters. count) )
91+ let numberOfMatches = regex!. numberOfMatches ( in: output,
92+ options: [ ] ,
93+ range: NSMakeRange ( 0 , output. characters. count) )
9294
9395 if numberOfMatches == 1 {
9496 print ( " RAM disk is already mounted. \n " )
@@ -110,23 +112,24 @@ func ramDiskExists() -> Bool
110112 */
111113func createRamDisk( blocks: Int ) -> Bool
112114{
113- let output = runTask ( " /usr/bin/hdid " , arguments: [ " -nomount " , " ram:// \( blocks) " ] )
115+ let output = runTask ( launchPath : " /usr/bin/hdid " , arguments: [ " -nomount " , " ram:// \( blocks) " ] )
114116 let allOutput = NSMakeRange ( 0 , output. characters. count)
115-
116117 let regex : NSRegularExpression ?
118+
117119 do {
118- regex = try NSRegularExpression ( pattern: " /dev/disk( \\ d+) " , options: NSRegularExpressionOptions . CaseInsensitive )
119- let numberOfMatches = regex!. numberOfMatchesInString ( output, options: [ ] , range: allOutput)
120+ regex = try NSRegularExpression ( pattern: " /dev/disk( \\ d+) " , options: NSRegularExpression . Options . caseInsensitive )
121+ let numberOfMatches = regex!. numberOfMatches ( in : output, options: [ ] , range: allOutput)
120122
121123 print ( " output \( output) " )
122124
123125 if numberOfMatches == 1 {
124- let matches = regex? . matchesInString ( output, options: [ ] , range: allOutput)
126+ let matches = regex? . matches ( in : output, options: [ ] , range: allOutput)
125127
126128 for match in matches! {
127- let matchRange : NSRange = match. rangeAtIndex ( 1 )
128- let disk = output. substringWithRange ( output. startIndex. advancedBy ( matchRange. location) ..< output. startIndex. advancedBy ( matchRange. location + matchRange. length) )
129- makeFilesystemForDisk ( disk)
129+ let matchRange : NSRange = match. rangeAt ( 1 )
130+ let disk = output. substring ( with: output. index ( output. startIndex, offsetBy: matchRange. location) ..<
131+ output. index ( output. startIndex, offsetBy: ( matchRange. location + matchRange. length) ) )
132+ makeFilesystemOn ( disk: disk)
130133 addRamDiskToSpotlight ( )
131134 }
132135 } else {
@@ -140,49 +143,58 @@ func createRamDisk(blocks: Int) -> Bool
140143 return true
141144}
142145
143- func makeFilesystemForDisk ( disk: String )
146+ func makeFilesystemOn ( disk: String )
144147{
145148 let drive = " /dev/rdisk \( disk) "
146- let output = runTask ( " /sbin/newfs_hfs " , arguments: [ " -v " , " DerivedData " , drive] )
149+ let output = runTask ( launchPath: " /sbin/newfs_hfs " ,
150+ arguments: [ " -v " , " DerivedData " , drive] )
147151
148152 print ( output)
149153
150- mountRamDisk ( drive)
154+ mountRamDisk ( drive: drive )
151155}
152156
153157func mountRamDisk( drive: String )
154158{
155- let output = runTask ( " /usr/sbin/diskutil " , arguments: [ " mount " , " -mountPoint " , derivedDataPath, drive] )
159+ let output = runTask ( launchPath: " /usr/sbin/diskutil " ,
160+ arguments: [ " mount " , " -mountPoint " , derivedDataPath, drive] )
156161
157162 print ( output)
158163}
159164
160165/// Add to Spotlight so that Instruments can find symbols.
161- func addRamDiskToSpotlight( ) {
162- let output = runTask ( " /usr/bin/mdutil " , arguments: [ derivedDataPath, " -i " , " on " ] )
166+ func addRamDiskToSpotlight( )
167+ {
168+ let output = runTask ( launchPath: " /usr/bin/mdutil " ,
169+ arguments: [ derivedDataPath, " -i " , " on " ] )
163170
164171 print ( output)
165172}
166173
167174func runTask( launchPath: String , arguments: [ String ] ) -> String
168175{
169- let task = NSTask ( )
176+ let task = Process ( )
170177 task. launchPath = launchPath
171178 task. arguments = arguments
172179
173- let pipe = NSPipe ( )
180+ let pipe = Pipe ( )
174181 task. standardOutput = pipe
175182 task. launch ( )
176183
177184 let data = pipe. fileHandleForReading. readDataToEndOfFile ( )
178185
179- return NSString ( data: data, encoding: NSUTF8StringEncoding ) as! String
186+ return NSString ( data: data, encoding: String . Encoding . utf8 . rawValue ) as! String
180187}
181188
182189print ( " Setting up RAM disk for Xcode. \n " )
183190
184191if !ramDiskExists( ) {
185- createRamDisk ( RAMDISK_GB * 1024 * 2048 )
192+ let result = createRamDisk ( blocks: RAMDISK_GB * 1024 * 2048 )
193+ if result {
194+ print ( " Created RAM disk. " )
195+ } else {
196+ print ( " Unable to create RAM disk. " )
197+ }
186198} else {
187199 print ( " RAM disk for Derived Data already exists. " )
188200}
0 commit comments