From 4a732bf44afd78bfe30128ea1e783ec8a9c4cf94 Mon Sep 17 00:00:00 2001 From: Fredrick Eisele Date: Mon, 26 Aug 2019 16:46:59 -0500 Subject: [PATCH 1/2] rewritten in powershell --- bin/gw.ps1 | 96 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100755 bin/gw.ps1 diff --git a/bin/gw.ps1 b/bin/gw.ps1 new file mode 100755 index 0000000..0082ad9 --- /dev/null +++ b/bin/gw.ps1 @@ -0,0 +1,96 @@ +# DEFAULTS may be overridden by calling environment. +Param( + [string] $gradle = "${GRADLE:-gradle}", + [string] $gradlew = "${GRADLEW:-gradlew}", + [string] $gradle_buildfile = "${GRADLE_BUILDFILE:-build.gradle}", + [string] $gradle_kts_buildfile = "${GRADLE_KTS_BUILDFILE:-build.gradle.kts}" +) + +Function err +{ + Write-Error "$args" +} + +Function lookup($file, $curr_path) +{ + $curr_path = if ( [string]::IsNullOrWhiteSpace($curr_path)) + { + (Get-Location) + } + + # Search recursively upwards for file. + do + { + $isGradle = Join-Path -Path $curr_path $file + if (Test-Path -Path $isGradle) + { + return $isGradle + } + else + { + $curr_path = Split-Path -Path $curr_path -parent + } + } while ( [string]::IsNullOrEmpty($curr_path) ) +} + +Function select_gradle($dir) +{ + # Use project's gradlew if found. + $gradlew = lookup ($gradlew, $dir) + if ( [string]::IsNullOrWhiteSpace($gradlew)) + { + Write-Error "No $GRADLEW set up for this project; consider setting one up:" + Write-Error "http://gradle.org/docs/current/userguide/gradle_wrapper.html\n" + } + else + { + return $gradlew + } + + # Deal with a missing wrapper by defaulting to system gradle. + $gradle = which "$GRADLE" + if ( [string]::IsNullOrWhiteSpace($gradle)) + { + Write-Error "$GRADLE not installed or not available in your PATH:" + Write-Error "$PATH" + } + else + { + return $gradle + } +} + +Function execute_gradle($build_gradle, $build_gradle_kts, $build_args) +{ + $build_gradle = lookup $build_gradle + $build_gradle_kts = lookup $build_gradle_kts + $gradle = select_gradle (Get-Location) + + if (Test-Path -Path $build_gradle or Test-Path -Path $build_gradle_kts) + { + $ktsDirName = Split-Path -Path $build_gradle_kts -parent + $dirName = Split-Path -Path $build_gradle -parent + if ( $ktsDirName.Contains($dirName)) + { + $dirName = $ktsDirName + $build_gradle = $build_gradle_kts + } + # We got a good build file, start gradlew there. + } + else + { + Write-Error "Unable to find a gradle build file named $GRADLE_BUILDFILE or $GRADLE_KTS_BUILDFILE." + } + + # Say what we are gonna do, then do it. + Write-Error "Using gradle at '${gradle}' to run buildfile '${build_gradle}':\n" + if ([string]::IsNullOrEmpty($build_args)) { + Start-Process -FilePath $gradle -WorkingDirectory $dirName -Args $build_args + } else { + $gradle + } +} + +# gw may be sourced from other scripts as a library to select / run gradle(w). +execute_gradle((lookup $gradle_buildfile), (lookup $gradle_kts_buildfile), (select_gradle (Get-Location)), $args) + From 8b34143b5d725b1a4a504c0c7941e0a3f1471255 Mon Sep 17 00:00:00 2001 From: Fred Eisele Date: Wed, 28 Aug 2019 16:44:14 -0500 Subject: [PATCH 2/2] powershell version mostly working --- bin/GradleWrapper.psd1 | Bin 0 -> 8956 bytes bin/GradleWrapper.psm1 | 166 +++++++++++++++++++++++++++++++++++++++++ bin/gw.ps1 | 96 ------------------------ docs/index.md | 41 ++++++++++ install.ps1 | 6 ++ 5 files changed, 213 insertions(+), 96 deletions(-) create mode 100644 bin/GradleWrapper.psd1 create mode 100644 bin/GradleWrapper.psm1 delete mode 100755 bin/gw.ps1 create mode 100644 install.ps1 diff --git a/bin/GradleWrapper.psd1 b/bin/GradleWrapper.psd1 new file mode 100644 index 0000000000000000000000000000000000000000..f97d23c973b46897dba6086fac84749b2725d474 GIT binary patch literal 8956 zcmeI2Yj0CW5Qg`2rTzy?Zj~TGAc%%isS<8(L<$s<1p3u>Y}dthTsy&36@R_$`|NnL zyFTaGIFPm<5Q@)bcW3AN&deVE{;Tf3c3-)=tGVay!kxP-H*g(24PDcXT;Fx|G+wMuSBkP~KKmD6P>%_ISOJBPo(J+(Pq-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E%ltj2%;3)`c^BI@j3Mm5PTNo@(7H`f!S~UhEd>PeqS(K?-B913=UKiwmsU9a4wi)@I=Ao~^4V?H5PAPgcDBsOi6=N<=c90Gbqd__1!qrh zjCVRQ&Kr3$TuW_CACjIn;3v74@I(e@W}s;~JNE8n=Nft3<+_dBnMLCF!thloTyt4g z&9000Q*qhzR%*(-#~wYclDqU8!)isZ4tgf;Ko>gEfmWxw%I%ESh*iE3P?K1dLak4q zI`H;@QcwJF3d$Vo4Rg%|Nps$-JvE)*5dAZu#j&nYI(B7$aA`D`Csch^(x*|K?m+BB zI%i&n@KvdZkm<^5-tc1c)|ohLiNkP$R^*cI_`-#61=X6vzv8@4+nY|z8)%4)#nb*)*Uy1or1S%2}N7p=-`+Uk(8I?ezN-wP8#&TGhFMPe0F*23^rv5%YFp##1#Ppf&NvdDxNf nuQ)Zk)VJ|8ch~ex6hR68b1yDRyDLq{@097vB0hY$uBH9~y+w`| literal 0 HcmV?d00001 diff --git a/bin/GradleWrapper.psm1 b/bin/GradleWrapper.psm1 new file mode 100644 index 0000000..6c50fb9 --- /dev/null +++ b/bin/GradleWrapper.psm1 @@ -0,0 +1,166 @@ + + + +Function LookUpwards($file, $curr_path) +{ + $curr_path = if ( [string]::IsNullOrWhiteSpace($curr_path)) + { + (Get-Location) + } + + # Search recursively upwards for file. + do + { + $isGradle = Join-Path -Path $curr_path $file + if (Test-Path -Path $isGradle) + { + return $isGradle + } + else + { + $curr_path = Split-Path -Path $curr_path -Parent + } + } while ( [string]::IsNullOrEmpty($curr_path) ) +} + +Function select_build($build_gradle, $build_gradle_kts) +{ + $valid_bgk = ![string]::IsNullOrWhiteSpace($build_gradle_kts) + $valid_bg = ![string]::IsNullOrWhiteSpace($build_gradle) + + if (!$valid_bg -and !$valid_bgk) + { + Write-Error "Unable to find a gradle build file named $build_gradle or $build_gradle_kts." + exit + } + elseif ($valid_bg -and !$valid_bgk) + { + return $build_gradle + } + elseif (!$valid_bg -and $valid_bgk) + { + return $build_gradle_kts + } + elseif ((Test-Path -LiteralPath "$build_gradle", "$build_gradle_kts") -contains $true) + { + $ktsDirName = Split-Path -Path "$build_gradle_kts" -Parent + $dirName = Split-Path -Path "$build_gradle" -Parent + if ( $ktsDirName.Contains($dirName)) + { + return $build_gradle_kts + } + else + { + return $build_gradle + } + # We got a good build file, start gradlew in its directory. + } + else + { + Write-Host "Unable to find a gradle build file named $build_gradle or $build_gradle_kts." + exit + } +} + +Function select_gradlew($gradlew) +{ + # Use project's gradlew if found. + $gradlewPath = LookUpwards $gradlew + if ( [string]::IsNullOrWhiteSpace($gradlewPath)) + { + Write-Host "No $gradlew set up for this project; consider setting one up: ", + "http://gradle.org/docs/current/userguide/gradle_wrapper.html" + } + else + { + return $gradlewPath + } +} + +Function select_gradle($gradle) +{ + $gradlePath = (Get-Command -Name $gradle).Path + if ( [string]::IsNullOrWhiteSpace($gradlePath)) + { + Write-Host "$gradle not installed or not available in your PATH: $Env:PATH" + exit + } + else + { + return $gradlePath + } +} + +Function execute_gradle($gradlew, $gradle, $build_gradle_path, $build_args) +{ + # Say what we are gonna do, then do it. + $build_dir = Split-Path -Path $build_gradle_path -Parent + + $valid_gradlew = ![string]::IsNullOrEmpty($gradlew) + $valid_gradle = ![string]::IsNullOrEmpty($gradle) + $valid_args = ![string]::IsNullOrEmpty($build_args) + + Write-Host "`$build_gradle='$build_gradle_path'" + Write-Host "`$working_dir='$build_dir'" + + if ($valid_args) { + Write-Host "`$args='$build_args'" + } + if ($valid_gradlew) { + Write-Host "`$gradle_proc='$gradlew'" + if ($valid_args) + { + Start-Process -FilePath "$gradlew" -Verb 'Open' -WorkingDirectory $build_dir -Args $build_args + } else + { + Start-Process -FilePath "$gradlew" -Verb 'Open' -WorkingDirectory $build_dir + } + } + elseif ($valid_gradle) + { + Write-Host "`$gradle_proc='$gradle'" + if ($valid_args) + { + Start-Process -FilePath $gradle -Verb 'Open' -WorkingDirectory $build_dir -Args $build_args + } + else + { + Start-Process -FilePath $gradle -Verb 'Open' -WorkingDirectory $build_dir + } + } + else { + Write-Host "no gradle process" + } +} + +Function GradleWrapper +{ + Param( + [string] $gradle = "gradle.exe", + [string] $gradlew = "gradlew.bat", + [string] $gradle_buildfile = "build.gradle", + [string] $gradle_kts_buildfile = "build.gradle.kts", + [string] $gradle_task = $Task + ) + + $gradleBuildPath = LookUpwards $gradle_buildfile + $gradleBuildKtsPath = LookUpwards $gradle_kts_buildfile + $gradleBuildSelectedPath = select_build $gradleBuildPath $gradleBuildKtsPath + + $gradlewPath = select_gradlew $gradlew + $gradlePath = select_gradle $gradle + Write-Host "select_gradlew ", $gradlew, " : ", $gradlewPath + + Write-Host "execute" + execute_gradle $gradlewPath $gradlePath $gradleBuildSelectedPath $gradle_task +} + +Function GDub ($task) { + Write-Host "gdub" + GradleWrapper -Task $task +} + +Set-Alias -Name gw -Value GDub + +Export-ModuleMember -Function 'GradleWrapper','GDub','LookUpwards' +Export-ModuleMember -Alias 'gw' diff --git a/bin/gw.ps1 b/bin/gw.ps1 deleted file mode 100755 index 0082ad9..0000000 --- a/bin/gw.ps1 +++ /dev/null @@ -1,96 +0,0 @@ -# DEFAULTS may be overridden by calling environment. -Param( - [string] $gradle = "${GRADLE:-gradle}", - [string] $gradlew = "${GRADLEW:-gradlew}", - [string] $gradle_buildfile = "${GRADLE_BUILDFILE:-build.gradle}", - [string] $gradle_kts_buildfile = "${GRADLE_KTS_BUILDFILE:-build.gradle.kts}" -) - -Function err -{ - Write-Error "$args" -} - -Function lookup($file, $curr_path) -{ - $curr_path = if ( [string]::IsNullOrWhiteSpace($curr_path)) - { - (Get-Location) - } - - # Search recursively upwards for file. - do - { - $isGradle = Join-Path -Path $curr_path $file - if (Test-Path -Path $isGradle) - { - return $isGradle - } - else - { - $curr_path = Split-Path -Path $curr_path -parent - } - } while ( [string]::IsNullOrEmpty($curr_path) ) -} - -Function select_gradle($dir) -{ - # Use project's gradlew if found. - $gradlew = lookup ($gradlew, $dir) - if ( [string]::IsNullOrWhiteSpace($gradlew)) - { - Write-Error "No $GRADLEW set up for this project; consider setting one up:" - Write-Error "http://gradle.org/docs/current/userguide/gradle_wrapper.html\n" - } - else - { - return $gradlew - } - - # Deal with a missing wrapper by defaulting to system gradle. - $gradle = which "$GRADLE" - if ( [string]::IsNullOrWhiteSpace($gradle)) - { - Write-Error "$GRADLE not installed or not available in your PATH:" - Write-Error "$PATH" - } - else - { - return $gradle - } -} - -Function execute_gradle($build_gradle, $build_gradle_kts, $build_args) -{ - $build_gradle = lookup $build_gradle - $build_gradle_kts = lookup $build_gradle_kts - $gradle = select_gradle (Get-Location) - - if (Test-Path -Path $build_gradle or Test-Path -Path $build_gradle_kts) - { - $ktsDirName = Split-Path -Path $build_gradle_kts -parent - $dirName = Split-Path -Path $build_gradle -parent - if ( $ktsDirName.Contains($dirName)) - { - $dirName = $ktsDirName - $build_gradle = $build_gradle_kts - } - # We got a good build file, start gradlew there. - } - else - { - Write-Error "Unable to find a gradle build file named $GRADLE_BUILDFILE or $GRADLE_KTS_BUILDFILE." - } - - # Say what we are gonna do, then do it. - Write-Error "Using gradle at '${gradle}' to run buildfile '${build_gradle}':\n" - if ([string]::IsNullOrEmpty($build_args)) { - Start-Process -FilePath $gradle -WorkingDirectory $dirName -Args $build_args - } else { - $gradle - } -} - -# gw may be sourced from other scripts as a library to select / run gradle(w). -execute_gradle((lookup $gradle_buildfile), (lookup $gradle_kts_buildfile), (select_gradle (Get-Location)), $args) - diff --git a/docs/index.md b/docs/index.md index 7f3b80f..0517e6b 100644 --- a/docs/index.md +++ b/docs/index.md @@ -79,6 +79,47 @@ source ~/.bashrc From now on you can just type `gradle ...` from wherever you are and `gw` takes care of the rest. Happiness ensues! + +## What about Windows? + +The main gdub is a bash script which is great but Windows support for bash is still flaky. +PowerShell is a better option. + +### Install a PowerShell Profile + +```powershell +Test-Path $PROFILE +``` +There are actually six profiles but here we will use the most local. + +If you do not have a profile you will need to create one. +```powershell +Set-ExecutionPolicy RemoteSigned +New-Item -Path $PROFILE -Type File -Force +notepad $PROFILE +``` + +The following profile will get you started. +```powershell +$ProfileRoot = (Split-Path -Parent $MyInvocation.MyCommand.Path) + +$ProfileScripts = "$ProfileRoot\Scripts" +$env:path += ";$ProfileScripts" + +$ProfileModules = "$ProfileRoot\Modules" + +$ProfileDocs = $(Resolve-Path "$Env:userprofile\Documents") +$ProfileDesk = $(Resolve-Path "$Env:userprofile\Desktop") + +Import-Module GradleWrapper +``` + +Copy gdub into the module directory then reload/restart PowerShell. +```powershell +.\install.ps1 +``` + + # Why gdub? ## The problems with `gradle` and `gradlew` diff --git a/install.ps1 b/install.ps1 new file mode 100644 index 0000000..3aa564a --- /dev/null +++ b/install.ps1 @@ -0,0 +1,6 @@ + +$ProfileRoot = (Split-Path -Parent $Profile) +New-Item -Path $ProfileRoot\Modules\GradleWrapper -ItemType directory +Copy-Item -Path .\bin\GradleWrapper.psm1 -Destination $ProfileRoot\Modules\GradleWrapper\GradleWrapper.psm1 -Force +Copy-Item -Path .\bin\GradleWrapper.psd1 -Destination $ProfileRoot\Modules\GradleWrapper\GradleWrapper.psd1 -Force +