Last active
April 9, 2019 11:38
-
-
Save mubbashir/821e9327e48ae828b8658626ed0a69b2 to your computer and use it in GitHub Desktop.
Gradle task to check files/commands in PATH environment variable
This file contains 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
// rest of your build | |
// Verfied with gradle 4.10 | |
task("checkEnv"){ | |
doFirst { | |
def listOfFileToCheckInPath = ['find', 'grep'] | |
listOfFileToCheckInPath.each { file -> | |
if(!isFoundInPath(file)) | |
throw new GradleException("${file} was not found in any of the folder in PATH: ${System.getenv('PATH').split(File.pathSeparator)}") | |
} | |
} | |
} | |
/** | |
* Static function to verify if a file/command exist in PATH environment | |
* @param file | |
* @return true if found, else false | |
*/ | |
def static isFoundInPath( file){ | |
def PATH_ENV = System.getenv('PATH') | |
def fileFound = PATH_ENV.split(File.pathSeparator).find{ folder -> | |
println("Looking for ${file} in ${folder}") | |
if (Paths.get( "${folder}${File.separator}${file}").toFile().exists()){ | |
println("Found ${file} in ${folder}") | |
return true | |
} | |
} | |
return fileFound | |
} | |
// Making test task to depend on checkEnv | |
test.dependsOn checkEnv |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment