Skip to content

Instantly share code, notes, and snippets.

@arberg
Last active May 7, 2022 02:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save arberg/309c0d29ddce82bfb146487706e91a24 to your computer and use it in GitHub Desktop.
Save arberg/309c0d29ddce82bfb146487706e91a24 to your computer and use it in GitHub Desktop.
Wrapping the gradle wrapper with Powershell
#Finds the first gradle in the list below
#Runs gradle with the arguments to this script in current dir
$gradlePaths = @("./gradlew","../gradlew", "gradle")
$currentGradlePaths = ( $gradlePaths | ?{ gcm $_ -erroraction 'silentlycontinue' } )
if ($currentGradlePaths.Length -gt 0) {
if ($currentGradlePaths -is [array]) {
$gradle = $currentGradlePaths[0]
} else {
$gradle = $currentGradlePaths
}
Write-Host "Using $(gcm $gradle | Select-Object -ExpandProperty Source)" -ForegroundColor yellow
& $gradle $args
} else {
Write-Host "No Gradle found among options: `n" ($gradlePaths -join "`n")
}
#Finds the first gradlew in the first parent directory, or uses gradle
#Runs gradle from first parent with a settings.gradle file
#Runs gradle with the arguments to this script
function searchParents([string]$nameToFind) {
$searchDir=(Get-Item .)
do {
$current = "$($searchDir.FullName)\$nameToFind"
$searchDir = $searchDir.parent
} while ($searchDir -And -Not (Test-Path $current))
if (Test-Path $current) {
$current
} else {
$null
}
}
$gradle = searchParents "gradlew.bat"
$settingsGradle = searchParents "settings.gradle"
if (-Not $gradle) {
$gradle = "gradle"
}
$isGradleValid = $(gcm $gradle -erroraction 'silentlycontinue')
if ($isGradleValid) {
Write-Host "Using $(gcm $gradle | Select-Object -ExpandProperty Source)" -ForegroundColor yellow
$workingDir = $(Get-Item $settingsGradle).Directory.FullName
#Push-Location $(Get-Item $gradle).Directory.FullName
#$workingDirRelativeToGradlew = $(Resolve-Path -Relative $workingDir)
#Pop-Location
if ($(Get-Item $settingsGradle).Directory.FullName -ne $($(Get-Item $gradle).Directory.FullName)) {
#Write-Host " in $workingDirRelative" -ForegroundColor yellow
Write-Host " in $workingDir" -ForegroundColor yellow
}
Push-Location $workingDir
& $gradle $args
Pop-Location
} else {
Write-Host "No Gradle found among options: `n" ($gradlePaths -join "`n")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment