Skip to content

Instantly share code, notes, and snippets.

@arberg arberg/gw-simple.ps1
Last active Mar 4, 2018

Embed
What would you like to do?
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
You can’t perform that action at this time.