@@ -78,11 +78,18 @@ launchctl load ~/Library/LaunchAgents/com.ikiapps.setupXcodeDerivedDataRamDisk.p
7878
7979*/
8080
81+ /// File systems:
82+ enum FileSystemType {
83+ case apfs
84+ case hfsPlus
85+ }
86+
8187/// Constants:
8288let RAMDISK_GB = 4 // Set the number of gigabytes for the RAM disk here!
8389let home = NSHomeDirectory ( )
8490let derivedDataPath = " \( home) /Library/Developer/Xcode/DerivedData "
8591let encoding : String . Encoding = . utf8
92+ let fileSystem = FileSystemType . apfs
8693
8794/// Error cases:
8895enum RamDiskSetupError : Error {
@@ -130,10 +137,30 @@ func createRamDisk(blocks: Int) throws -> Bool
130137
131138func makeFilesystemOn( disk: String ) throws
132139{
133- let drive = " /dev/rdisk \( disk) "
134- let output = try runTask ( launchPath: " /sbin/newfs_hfs " ,
140+ var drive = " /dev/rdisk \( disk) "
141+ var output : String !
142+ switch fileSystem {
143+ case . apfs:
144+ output = try runTask ( launchPath: " /usr/sbin/diskutil " ,
145+ arguments: [ " ap " , " createContainer " , drive] )
146+ print ( output!)
147+ let apfsMsg = " Disk from APFS operation: "
148+ let regex = try NSRegularExpression ( pattern: " \( apfsMsg) [[:alnum:]]+ \n " , options: . caseInsensitive)
149+ var allOutput = NSMakeRange ( 0 , output. count)
150+ var matchRange = regex. firstMatch ( in: output, options: [ ] , range: allOutput) !. range
151+ drive = String ( output [ output. index ( output. startIndex, offsetBy: ( matchRange. location + apfsMsg. count) ) ..<
152+ output. index ( output. startIndex, offsetBy: ( matchRange. location + matchRange. length - 1 ) ) ] )
153+ output = try runTask ( launchPath: " /usr/sbin/diskutil " ,
154+ arguments: [ " ap " , " addVolume " , drive, " APFS " , " DerivedData " , " -nomount " ] )
155+ allOutput = NSMakeRange ( 0 , output. count)
156+ matchRange = regex. firstMatch ( in: output, options: [ ] , range: allOutput) !. range
157+ drive = String ( output [ output. index ( output. startIndex, offsetBy: ( matchRange. location + apfsMsg. count) ) ..<
158+ output. index ( output. startIndex, offsetBy: ( matchRange. location + matchRange. length - 1 ) ) ] )
159+ case . hfsPlus:
160+ output = try runTask ( launchPath: " /sbin/newfs_hfs " ,
135161 arguments: [ " -v " , " DerivedData " , drive] )
136- print ( output)
162+ }
163+ print ( output!)
137164 try mountRamDisk ( drive: drive)
138165}
139166
0 commit comments