Created
October 9, 2020 12:26
-
-
Save CAMOBAP/0fbd38f427064c5d951ac0cc80648c1b to your computer and use it in GitHub Desktop.
Gradle RAMdisk ~/.gradle/init.gradle
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.gradle.internal.os.OperatingSystem | |
println "Loaded personal ~/.gradle/init.gradle" | |
final String RAMDiskName = "GradleRamDisk" | |
final long RAMDiskBytes = 4L * 1024L * 1024L * 1024L // 4Gb | |
interface RAMDiskManager { | |
boolean isReady(); | |
String getPath(); | |
void create(); | |
void drop(); | |
} | |
RAMDiskManager RAMDiskManagerInst = null | |
switch (OperatingSystem.current()) { | |
case OperatingSystem.MAC_OS: | |
// inspired by https://stackoverflow.com/questions/29116000/gradle-make-use-of-ramdisk | |
RAMDiskManagerInst = new RAMDiskManager() { | |
String RAMDiskFSPath = "/Volumes/${RAMDiskName}" | |
String RAMDiskDevPath | |
boolean isReady() { return new File(RAMDiskFSPath).exists() } | |
String getPath() { return RAMDiskFSPath } | |
void create() { | |
long RAMDiskSectors = RAMDiskBytes / 512L // The sector size is 512 bytes | |
RAMDiskDevPath = new ByteArrayOutputStream().withStream { os -> | |
def result = exec { | |
commandLine 'hdiutil', 'attach', '-nomount', "ram://${RAMDiskSectors}" | |
standardOutput os | |
} | |
return os.toString().trim() | |
} | |
exec { | |
commandLine 'diskutil', 'erasevolume', 'HFS+', RAMDiskName, RAMDiskDevPath | |
} | |
} | |
void drop() { | |
exec { | |
commandLine 'diskutil', 'unmountDisk', RAMDiskDevPath | |
} | |
} | |
} | |
break | |
case OperationSystem.LINUX: | |
case OperationSystem.WINDOWS: | |
// not implemented yet | |
break | |
} | |
if (RAMDiskManagerInst != null) { | |
gradle.projectsLoaded { | |
if (!RAMDiskManagerInst.isReady()) { | |
RAMDiskManagerInst.create() | |
} | |
rootProject.allprojects { | |
buildDir = "${RAMDiskManagerInst.getPath()}/gradle-build/${rootProject.name}/${project.name}" | |
println "BUILDING TO RAMDISK: buildDir=$buildDir" | |
} | |
} | |
} else { | |
switch (OperatingSystem.current()) { | |
case OperationSystem.LINUX: | |
println "RAMDisk optimization not implemented. Check: https://stackoverflow.com/questions/29116000/gradle-make-use-of-ramdisk" | |
break | |
case OperationSystem.WINDOWS: | |
println "RAMDisk optimization not implemented. Check: https://en.wikipedia.org/wiki/List_of_RAM_drive_software#Microsoft_Windows" | |
break | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment