diff --git a/README.md b/README.md index a5f9368..db39e6b 100644 --- a/README.md +++ b/README.md @@ -1,20 +1,20 @@ -# java-get +# get-tool ### install ```powershell -irm "https://raw.githubusercontent.com/pwsh-bin/java-get/main/install.ps1" | iex +irm "https://raw.githubusercontent.com/pwsh-bin/get-tool/main/install.ps1" | iex ``` ### usage ```powershell -java-get self-install - update java-get to latest version -java-get install java@openjdk-1.8 - install java openjdk version 1.8 -java-get install maven@3.1 - install meven version 3.1 -java-get list-supported - list all supported binaries -java-get list-supported java - list all supported java versions -java-get list-installed java - list all installed java versions -java-get pick java@temurin-11 - pick installed java version -java-get pick ant@1.9 - pick installed ant version +get-tool self-install - update get-tool to latest version +get-tool install java@openjdk-1.8 - install java openjdk version 1.8 +get-tool install maven@3.1 - install maven version 3.1 +get-tool list-supported - list all supported tools +get-tool list-supported java - list all supported java versions +get-tool list-installed - list all installed tools +get-tool init - add tools to current path +get-tool setup - add init to current profile ``` diff --git a/get-tool.ps1 b/get-tool.ps1 new file mode 100644 index 0000000..a780439 --- /dev/null +++ b/get-tool.ps1 @@ -0,0 +1,545 @@ +Set-Variable -Name "ProgressPreference" -Value "SilentlyContinue" + +${DEBUG} = Test-Path -PathType "Container" -Path (Join-Path -Path ${PSScriptRoot} -ChildPath ".git") +${PS1_HOME} = Join-Path -Path ${HOME} -ChildPath ".get-tool" +${PS1_FILE} = Join-Path -Path ${PS1_HOME} -ChildPath "get-tool.ps1" +${STORE_PATH} = Join-Path -Path ${PS1_HOME} -ChildPath ".store" +${7ZIP} = Join-Path -Path ${ENV:PROGRAMFILES} -ChildPath "7-Zip" -AdditionalChildPath "7z.exe" +${TOOLS} = @( + "java", + "maven", + "ant", + "gradle", + "python", + "node", + "ruby", + "go" +) +${VERSION} = "v0.2.0" +${HELP} = @" +Usage: +get-tool self-install - update get-tool to latest version +get-tool install java@openjdk-1.8 - install java openjdk version 1.8 +get-tool install maven@3.1 - install maven version 3.1 +get-tool list-supported - list all supported tools +get-tool list-supported java - list all supported java versions +get-tool list-installed - list all installed tools +get-tool init - add tools to current path +get-tool setup - add init to current profile + +${VERSION} +"@ + +if (${args}.Count -eq 0) { + Write-Host ${HELP} + return +} + +function GetJDKS { + param ( + ${Version} + ) + ${uri} = "https://download.jetbrains.com/jdk/feed/v1/jdks.json" + ${jdks} = $null + if (${DEBUG}) { + Write-Host "[DEBUG] GET ${uri}" + } + try { + ${jdks} = Invoke-RestMethod -Method "Get" -Uri ${uri} + } + catch { + Write-Host "[ERROR] GET ${uri}:" + Write-Host $_ + exit + } + return (${jdks}.jdks | Where-Object -Property "listed" -eq $null | Select-Object -ExpandProperty "packages" | Where-Object -FilterScript { ($_.os -eq "windows") -and ($_.arch -eq "x86_64") -and ($_.install_folder_name -clike "${Version}*") }) +} + +function GetMaven { + param ( + ${Version} + ) + ${uri} = "https://dlcdn.apache.org/maven/maven-3/" + ${response} = $null + if (${DEBUG}) { + Write-Host "[DEBUG] GET ${uri}" + } + try { + ${response} = Invoke-WebRequest -Method "Get" -Uri ${uri} -Body @{ + "C" = "N" + "O" = "D" + "F" = "0" + "P" = "${Version}*" + } + } + catch { + Write-Host "[ERROR] GET ${uri}:" + Write-Host $_ + exit + } + ${hrefs} = (${response}.Links | Where-Object -Property "href" -ne $null | Select-Object -ExpandProperty "href" -Skip 1) + ${binaries} = @() + foreach (${href} in ${hrefs}) { + ${_version} = ${href}.SubString(0, ${href}.Length - 1) + ${version} = [version]${_version} + ${unpack_prefix_filter} = "apache-maven-${_version}" + ${archive_file_name} = "apache-maven-${_version}-bin.zip" + ${install_folder_name} = "maven-${_version}" + ${binaries} += [pscustomobject]@{ + "url" = "${uri}${_version}/binaries/${archive_file_name}" + "package_type" = "zip" + "unpack_prefix_filter" = ${unpack_prefix_filter} + "archive_file_name" = ${archive_file_name} + "install_folder_name" = ${install_folder_name} + "version" = ${version} + } + } + return (${binaries} | Sort-Object -Property "version") +} + +function GetAnt { + param ( + ${Version} + ) + ${uri} = "https://dlcdn.apache.org/ant/binaries/" + ${response} = $null + if (${DEBUG}) { + Write-Host "[DEBUG] GET ${uri}" + } + try { + ${response} = Invoke-WebRequest -Method "Get" -Uri ${uri} -Body @{ + "C" = "N" + "O" = "D" + "F" = "0" + "P" = "*-${Version}*-bin.zip" + } + } + catch { + Write-Host "[ERROR] GET ${uri}:" + Write-Host $_ + exit + } + ${hrefs} = (${response}.Links | Where-Object -Property "href" -ne $null | Select-Object -ExpandProperty "href" -Skip 1) + ${binaries} = @() + foreach (${href} in ${hrefs}) { + ${unpack_prefix_filter} = ${href}.SubString(0, ${href}.LastIndexOf("-")) + ${install_folder_name} = ${unpack_prefix_filter}.SubString(${unpack_prefix_filter}.IndexOf("-") + 1) + ${version} = [version]${install_folder_name}.SubString(${install_folder_name}.IndexOf("-") + 1) + ${binaries} += [pscustomobject]@{ + "url" = "${uri}${href}" + "package_type" = "zip" + "unpack_prefix_filter" = ${unpack_prefix_filter} + "archive_file_name" = ${href} + "install_folder_name" = ${install_folder_name} + "version" = ${version} + } + } + return (${binaries} | Sort-Object -Property "version") +} + +function GetGradle { + param ( + ${Version} + ) + ${uri} = "https://services.gradle.org/distributions/" + ${response} = $null + if (${DEBUG}) { + Write-Host "[DEBUG] GET ${uri}" + } + try { + ${response} = Invoke-WebRequest -Method "Get" -Uri ${uri} + } + catch { + Write-Host "[ERROR] GET ${uri}:" + Write-Host $_ + exit + } + ${hrefs} = (${response}.Links | Where-Object -Property "href" -ne $null | Select-Object -ExpandProperty "href" -Skip 1 | Where-Object -FilterScript { ($_ -cnotlike "*rc*") -and ($_ -cnotlike "*milestone*") -and ($_ -clike "*-${version}*-bin.zip") }) + ${binaries} = @() + foreach (${href} in ${hrefs}) { + ${archive_file_name} = ${href}.SubString(${href}.LastIndexOf("/") + 1) + ${install_folder_name} = ${archive_file_name}.SubString(0, ${archive_file_name}.LastIndexOf("-")) + ${version} = [version]${install_folder_name}.SubString(${install_folder_name}.IndexOf("-") + 1) + ${binaries} += [pscustomobject]@{ + "url" = "${uri}${archive_file_name}" + "package_type" = "zip" + "unpack_prefix_filter" = ${install_folder_name} + "archive_file_name" = ${archive_file_name} + "install_folder_name" = ${install_folder_name} + "version" = ${version} + } + } + return (${binaries} | Sort-Object -Property "version") +} + +function GetPython { + param ( + ${Version} + ) + ${uri} = "https://www.python.org/ftp/python/" + ${response} = $null + if (${DEBUG}) { + Write-Host "[DEBUG] GET ${uri}" + } + try { + ${response} = Invoke-WebRequest -Method "Get" -Uri ${uri} + } + catch { + Write-Host "[ERROR] GET ${uri}:" + Write-Host $_ + exit + } + ${hrefs} = (${response}.Links | Where-Object -Property "href" -ne $null | Select-Object -ExpandProperty "href" -Skip 1 | Where-Object -FilterScript { ($_ -clike "*.*.*") -and ($_ -cnotlike "*-*") -and ($_ -clike "${Version}*") }) + ${binaries} = @() + ${minimumVersion} = [version]"3.5.0" + foreach (${href} in ${hrefs}) { + ${_version} = ${href}.SubString(0, ${href}.Length - 1) + ${version} = [version]${_version} + if (${version} -lt ${minimumVersion}) { + continue + } + ${archive_file_name} = "python-${_version}-embed-amd64.zip" + ${install_folder_name} = "python-${_version}" + ${binaries} += [pscustomobject]@{ + "url" = "${uri}${href}${archive_file_name}" + "package_type" = "zip" + "unpack_prefix_filter" = "" + "archive_file_name" = ${archive_file_name} + "install_folder_name" = ${install_folder_name} + "version" = ${version} + } + } + ${result} = (${binaries} | Sort-Object -Property "version") + # NOTE: check is last release is not a prerelease + ${uri} = ${result}[-1].url + ${response} = $null + if (${DEBUG}) { + Write-Host "[DEBUG] HEAD ${uri}" + } + try { + ${response} = Invoke-WebRequest -Method "Head" -Uri ${uri} + } + catch { + return ${result}[0..(${result}.Count - 2)] + } + return ${result} +} + +function GetNode { + param ( + ${Version} + ) + ${uri} = "https://nodejs.org/dist/" + ${response} = $null + if (${DEBUG}) { + Write-Host "[DEBUG] GET ${uri}" + } + try { + ${response} = Invoke-WebRequest -Method "Get" -Uri ${uri} + } + catch { + Write-Host "[ERROR] GET ${uri}:" + Write-Host $_ + exit + } + ${hrefs} = (${response}.Links | Where-Object -Property "href" -ne $null | Select-Object -ExpandProperty "href" -Skip 1 | Where-Object -FilterScript { ($_ -clike "v*.*.*") -and ($_ -cnotlike "*-*") -and ($_ -clike "v${Version}*") }) + ${binaries} = @() + ${minimumVersion} = [version]"4.5.0" + foreach (${href} in ${hrefs}) { + ${_version} = ${href}.SubString(1, ${href}.Length - 2) + ${version} = [version]${_version} + if (${version} -lt ${minimumVersion}) { + continue + } + ${unpack_prefix_filter} = "node-v${_version}-win-x64" + ${archive_file_name} = "${unpack_prefix_filter}.zip" + ${install_folder_name} = "node-${_version}" + ${binaries} += [pscustomobject]@{ + "url" = "${uri}${href}${archive_file_name}" + "package_type" = "zip" + "unpack_prefix_filter" = ${unpack_prefix_filter} + "archive_file_name" = ${archive_file_name} + "install_folder_name" = ${install_folder_name} + "version" = ${version} + } + } + return (${binaries} | Sort-Object -Property "version") +} + +function GetRuby { + param ( + ${Version} + ) + ${uri} = "https://rubyinstaller.org/downloads/archives/" + ${response} = $null + if (${DEBUG}) { + Write-Host "[DEBUG] GET ${uri}" + } + try { + ${response} = Invoke-WebRequest -Method "Get" -Uri ${uri} + } + catch { + Write-Host "[ERROR] GET ${uri}:" + Write-Host $_ + exit + } + ${hrefs} = (${response}.Links | Where-Object -Property "href" -ne $null | Select-Object -ExpandProperty "href" | Where-Object -FilterScript { ($_ -clike "*-x64.7z") -and ($_ -clike "*-${Version}*") }) + ${binaries} = @() + foreach (${href} in ${hrefs}) { + ${archive_file_name} = ${href}.SubString(${href}.LastIndexOf("/") + 1) + ${unpack_prefix_filter} = ${archive_file_name}.SubString(0, ${archive_file_name}.Length - 3) # NOTE: drop '.7z' + ${temp} = ${archive_file_name}.SubString(${archive_file_name}.IndexOf("-") + 1) + ${_version} = (${temp}.SubString(0, ${temp}.Length - 7) -creplace '-', '.') # NOTE: drop '-x64.7z' + ${version} = [version]${_version} + ${install_folder_name} = "ruby-${_version}" + ${binaries} += [pscustomobject]@{ + "url" = ${href} + "package_type" = "7z" + "unpack_prefix_filter" = ${unpack_prefix_filter} + "archive_file_name" = ${archive_file_name} + "install_folder_name" = ${install_folder_name} + "version" = ${version} + } + } + return (${binaries} | Sort-Object -Property "version") +} + +function GetGo { + param ( + ${Version} + ) + ${uri} = "https://go.dev/dl/" + ${response} = $null + if (${DEBUG}) { + Write-Host "[DEBUG] GET ${uri}" + } + try { + ${response} = Invoke-WebRequest -Method "Get" -Uri ${uri} + } + catch { + Write-Host "[ERROR] GET ${uri}:" + Write-Host $_ + exit + } + ${hrefs} = (${response}.Links | Where-Object -Property "href" -ne $null | Select-Object -ExpandProperty "href" | Where-Object -FilterScript { ($_ -clike "*.windows-amd64.zip") -and ($_ -cnotlike "*rc*") -and ($_ -cnotlike "*beta*") -and ($_ -clike "/dl/go${Version}*") }) + ${binaries} = @() + foreach (${href} in ${hrefs}) { + ${archive_file_name} = ${href}.SubString(${href}.LastIndexOf("/") + 1) + ${temp} = ${archive_file_name}.SubString(2) # NOTE: drop 'go' + ${_version} = ${temp}.SubString(0, ${temp}.Length - 18) # NOTE: drop '.windows-amd64.zip' + ${version} = [version]${_version} + ${install_folder_name} = "go-${_version}" + ${binaries} += [pscustomobject]@{ + "url" = "${uri}${archive_file_name}" + "package_type" = "zip" + "unpack_prefix_filter" = "go" + "archive_file_name" = ${archive_file_name} + "install_folder_name" = ${install_folder_name} + "version" = ${version} + } + } + return (${binaries} | Sort-Object -Property "version") +} + +function Install { + param ( + ${Tool}, + ${Executable}, + ${Arguments}, + ${Objects} + ) + if (${Objects}.Count -eq 0) { + Write-Host "[ERROR] Unsupported version argument." + exit + } + ${object} = ${Objects}[-1] + ${uri} = ${object}.url + ${package_type} = ${object}.package_type + ${unpack_prefix_filter} = ${object}.unpack_prefix_filter + ${archive_file_name} = ${object}.archive_file_name + ${install_folder_name} = ${object}.install_folder_name + ${outfile} = (Join-Path -Path ${STORE_PATH} -ChildPath ${archive_file_name}) + ${directory} = (Join-Path -Path ${STORE_PATH} -ChildPath ${install_folder_name}) + ${link} = (Join-Path -Path ${PS1_HOME} -ChildPath ${Tool}) + ${target} = (Join-Path -Path ${directory} -ChildPath ${unpack_prefix_filter}) + if (-not (Test-Path -PathType "Container" -Path ${directory})) { + New-Item -Force -ItemType "Directory" -Path ${STORE_PATH} | Out-Null + if (${DEBUG}) { + Write-Host "[DEBUG] GET ${uri}" + } + try { + Invoke-RestMethod -Method "Get" -Uri ${uri} -OutFile ${outfile} + } + catch { + Write-Host "[ERROR] GET ${uri}:" + Write-Host $_ + exit + } + switch (${package_type}) { + "zip" { + Expand-Archive -Force -Path ${outfile} -DestinationPath ${directory} + break + } + "targz" { + ${command} = "tar --extract --file ${outfile} --directory ${directory}" + Invoke-Expression -Command ${command} | Out-Null + break + } + "7z" { + ${command} = "& '${7ZIP}' x -y -o${directory} ${outfile}" + Invoke-Expression -Command ${command} | Out-Null + } + default { + Write-Host "[ERROR] Unsupported file extension." + exit + } + } + Remove-Item -Force -Path ${outfile} | Out-Null + if (-not (Test-Path -PathType "Container" -Path ${directory})) { + Write-Host "[ERROR] Extraction failed." + exit + } + } + if (Test-Path -PathType "Container" -Path ${link}) { + Remove-Item -Force -Path ${link} + } + New-Item -Force -ItemType "Junction" -Path ${link} -Target ${target} | Out-Null + Invoke-Expression -Command ((Join-Path -Path ${link} -ChildPath ${Executable}) + " " + ${Arguments}) +} + +function ListSupported { + param ( + ${Objects} + ) + Write-Host ((${Objects} | Select-Object -ExpandProperty "install_folder_name") -join "`n") +} + +switch (${args}[0]) { + { $_ -in "si", "self-install" } { + ${uri} = "https://raw.githubusercontent.com/pwsh-bin/get-tool/main/install.ps1" + ${command} = $null + if (${DEBUG}) { + Write-Host "[DEBUG] GET ${uri}" + } + try { + ${command} = Invoke-RestMethod -Method "Get" -Uri ${uri} + } + catch { + Write-Host "[ERROR] GET ${uri}:" + Write-Host $_ + exit + } + Invoke-Expression -Command ${command} + } + { $_ -in "i", "install" } { + ${tool}, ${version} = (${args}[1] -csplit "@") + switch (${tool}) { + ${TOOLS}[0] { + if ($null -eq ${version}) { + ${version} = "openjdk" + } + ${objects} = (GetJDKS -Version ${version}) + Install -Tool ${tool} -Executable (Join-Path -Path "bin" -ChildPath "java.exe") -Arguments "-version" -Objects ${objects} + } + ${TOOLS}[1] { + ${objects} = (GetMaven -Version ${version}) + Install -Tool ${tool} -Executable (Join-Path -Path "bin" -ChildPath "mvn.cmd") -Arguments "-version" -Objects ${objects} + } + ${TOOLS}[2] { + ${objects} = (GetAnt -Version ${version}) + Install -Tool ${tool} -Executable (Join-Path -Path "bin" -ChildPath "ant.bat") -Arguments "-version" -Objects ${objects} + } + ${TOOLS}[3] { + ${objects} = (GetGradle -Version ${version}) + Install -Tool ${tool} -Executable (Join-Path -Path "bin" -ChildPath "gradle.bat") -Arguments "--version" -Objects ${objects} + } + ${TOOLS}[4] { + ${objects} = (GetPython -Version ${version}) + Install -Tool ${tool} -Executable "python.exe" -Arguments "--version" -Objects ${objects} + } + ${TOOLS}[5] { + ${objects} = (GetNode -Version ${version}) + Install -Tool ${tool} -Executable "node.exe" -Arguments "--version" -Objects ${objects} + } + ${TOOLS}[6] { + ${objects} = (GetRuby -Version ${version}) + Install -Tool ${tool} -Executable (Join-Path -Path "bin" -ChildPath "ruby.exe") -Arguments "--version" -Objects ${objects} + } + ${TOOLS}[7] { + ${objects} = (GetGo -Version ${version}) + Install -Tool ${tool} -Executable (Join-Path -Path "bin" -ChildPath "go.exe") -Arguments "version" -Objects ${objects} + } + default { + Write-Host "[ERROR] Unsupported or missing tool argument." + } + } + } + { $_ -in "ls", "list-supported" } { + ${tool} = ${args}[1] + switch (${tool}) { + ${TOOLS}[0] { + ${objects} = (GetJDKS) + ListSupported -Objects ${objects} + } + ${TOOLS}[1] { + ${objects} = (GetMaven) + ListSupported -Objects ${objects} + } + ${TOOLS}[2] { + ${objects} = (GetAnt) + ListSupported -Objects ${objects} + } + ${TOOLS}[3] { + ${objects} = (GetGradle) + ListSupported -Objects ${objects} + } + ${TOOLS}[4] { + ${objects} = (GetPython) + ListSupported -Objects ${objects} + } + ${TOOLS}[5] { + ${objects} = (GetNode) + ListSupported -Objects ${objects} + } + ${TOOLS}[6] { + ${objects} = (GetRuby) + ListSupported -Objects ${objects} + } + ${TOOLS}[7] { + ${objects} = (GetGo) + ListSupported -Objects ${objects} + } + default { + Write-Host (${TOOLS} -join "`n") + } + } + } + { $_ -in "li", "list-installed" } { + Write-Host ((Get-ChildItem -Path ${STORE_PATH} -Filter ${Pattern} -Directory -Name) -join "`n") + } + { $_ -in "init" } { + if (${env:PATH} -split ";" -cnotcontains ${PS1_HOME}) { + ${env:PATH} += ";${PS1_HOME}" + ${env:PATH} += (";" + (Join-Path -Path ${PS1_HOME} -ChildPath ${TOOLS}[0] -AdditionalChildPath "bin")) + ${env:PATH} += (";" + (Join-Path -Path ${PS1_HOME} -ChildPath ${TOOLS}[1] -AdditionalChildPath "bin")) + ${env:PATH} += (";" + (Join-Path -Path ${PS1_HOME} -ChildPath ${TOOLS}[2] -AdditionalChildPath "bin")) + ${env:PATH} += (";" + (Join-Path -Path ${PS1_HOME} -ChildPath ${TOOLS}[3] -AdditionalChildPath "bin")) + ${env:PATH} += (";" + (Join-Path -Path ${PS1_HOME} -ChildPath ${TOOLS}[4])) + ${env:PATH} += (";" + (Join-Path -Path ${PS1_HOME} -ChildPath ${TOOLS}[5])) + ${env:PATH} += (";" + (Join-Path -Path ${PS1_HOME} -ChildPath ${TOOLS}[6] -AdditionalChildPath "bin")) + ${env:PATH} += (";" + (Join-Path -Path ${PS1_HOME} -ChildPath ${TOOLS}[7] -AdditionalChildPath "bin")) + } + } + { $_ -in "setup" } { + ${value} = "& '${PS1_FILE}' init" + if (((Get-Content -Path ${PROFILE}) -split "`n") -cnotcontains ${value}) { + Add-Content -Path ${PROFILE} -Value ${value} + if (${DEBUG}) { + Write-Host "[DEBUG] ${PROFILE}" + } + } + } + default { + Write-Host "[ERROR] Unsupported command argument." + } +} diff --git a/install.ps1 b/install.ps1 index b8c6156..36b67c6 100644 --- a/install.ps1 +++ b/install.ps1 @@ -1,17 +1,6 @@ -if (-not ${env:JAVA_GET_HOME}) { - ${env:JAVA_GET_HOME} = "${HOME}\.java-get" - [System.Environment]::SetEnvironmentVariable("JAVA_GET_HOME", ${env:JAVA_GET_HOME}, [System.EnvironmentVariableTarget]::User) -} - -if (${env:PATH} -cnotlike "*${env:JAVA_GET_HOME}*") { - ${env:PATH} = "${env:JAVA_GET_HOME};${env:PATH}" - [System.Environment]::SetEnvironmentVariable("PATH", ${env:PATH}, [System.EnvironmentVariableTarget]::User) -} - -if (-not (Test-Path -Path ${env:JAVA_GET_HOME} -PathType "Container")) { - ${NO_OUTPUT} = (New-Item -Path ${env:JAVA_GET_HOME} -ItemType "Directory") -} - Set-Variable -Name "ProgressPreference" -Value "SilentlyContinue" -Invoke-RestMethod -OutFile "${env:JAVA_GET_HOME}/java-get.ps1" -Uri "https://raw.githubusercontent.com/pwsh-bin/java-get/main/java-get.ps1" -Method "Get" -Invoke-Expression -Command "java-get.ps1" +${PS1_HOME} = Join-Path -Path ${HOME} -ChildPath ".get-tool" +${PS1_FILE} = Join-Path -Path ${PS1_HOME} -ChildPath "get-tool.ps1" +New-Item -Force -ItemType "Directory" -Path ${PS1_HOME} | Out-Null +Invoke-RestMethod -Method "Get" -Uri "https://raw.githubusercontent.com/pwsh-bin/get-tool/main/get-tool.ps1" -OutFile ${PS1_FILE} +Invoke-Expression -Command ${PS1_FILE} diff --git a/java-get.ps1 b/java-get.ps1 deleted file mode 100644 index 8da953d..0000000 --- a/java-get.ps1 +++ /dev/null @@ -1,382 +0,0 @@ -Set-Variable -Name "ProgressPreference" -Value "SilentlyContinue" - -${VERSION} = "v0.1.0" -${HELP} = @" -Usage: -java-get self-install - update java-get to latest version -java-get install java@openjdk-1.8 - install java openjdk version 1.8 -java-get install maven@3.1 - install meven version 3.1 -java-get list-supported - list all supported binaries -java-get list-supported java - list all supported java versions -java-get list-installed java - list all installed java versions -java-get pick java@temurin-11 - pick installed java version -java-get pick ant@1.9 - pick installed ant version - -${VERSION} -"@ - -if (${args}.count -eq 0) { - Write-Host ${HELP} - return -} - -${TEMP_PATH} = "${env:JAVA_GET_HOME}\.temp" -if (-not (Test-Path -Path ${TEMP_PATH} -PathType "Container")) { - ${NO_OUTPUT} = (New-Item -Path ${TEMP_PATH} -ItemType "Directory") -} - -${JDKS_PATH} = "${HOME}\.jdks" -if (-not (Test-Path -Path ${JDKS_PATH} -PathType "Container")) { - ${NO_OUTPUT} = (New-Item -Path ${JDKS_PATH} -ItemType "Directory") -} - -${JAVA_TOOLS_PATH} = "${HOME}\.java-tools" -if (-not (Test-Path -Path ${JAVA_TOOLS_PATH} -PathType "Container")) { - ${NO_OUTPUT} = (New-Item -Path ${JAVA_TOOLS_PATH} -ItemType "Directory") -} - -${BINARIES} = @( - "java", - "maven", - "ant", - "gradle" -) - -function UpdatePath { - param ( - ${Binary} - ) - ${path} = "${env:JAVA_GET_HOME}\${Binary}\bin" - if (${env:PATH} -cnotlike "*${path}*") { - ${env:PATH} = "${path};${env:PATH}" - [System.Environment]::SetEnvironmentVariable("PATH", ${env:PATH}, [System.EnvironmentVariableTarget]::User) - } -} - -function GetJDKS { - ${uri} = "https://download.jetbrains.com/jdk/feed/v1/jdks.json" - return ((Invoke-RestMethod -Method "Get" -Uri ${uri}).jdks | - Where-Object -Property "listed" -eq $null | - Select-Object -ExpandProperty "packages" | - Where-Object -FilterScript { ($_.os -eq "windows") -and ($_.arch -eq "x86_64") } | - Sort-Object -Property "install_folder_name") -} - -function GetMavenBinaries { - param ( - ${Version} - ) - ${prefix} = "apache-maven" - ${postfix} = "bin.zip" - ${path} = "maven/maven-3" - ${subpath} = "/binaries/" - ${uri} = "https://dlcdn.apache.org/${path}" - ${hrefs} = ((Invoke-WebRequest -Method "Get" -Uri ${uri} -Body @{ - "C" = "N" - "O" = "D" - "F" = "0" - "P" = "${Version}*" - }).Links | - Select-Object -ExpandProperty "href" -Skip 1) - ${binaries} = @() - foreach (${href} in ${hrefs}) { - ${version} = (${href} -creplace "/", "") - ${install_folder_name} = "${prefix}-${version}" - ${archive_file_name} = "${install_folder_name}-${postfix}" - ${binaries} += [pscustomobject]@{ - "url" = "${uri}/${version}${subpath}${archive_file_name}" - "package_type" = "zip" - "unpack_prefix_filter" = ${install_folder_name} - "archive_file_name" = ${archive_file_name} - "install_folder_name" = ${install_folder_name} - } - } - return ${binaries} -} - -function GetApacheBinaries { - param ( - ${Path}, - ${Version} - ) - ${prefix} = "*-" - ${postfix} = "*-bin.zip" - ${uri} = "https://dlcdn.apache.org/${Path}/binaries" - ${hrefs} = ((Invoke-WebRequest -Method "Get" -Uri ${uri} -Body @{ - "C" = "N" - "O" = "D" - "F" = "0" - "P" = "${prefix}${Version}${postfix}" - }).Links | - Select-Object -ExpandProperty "href" -Skip 1) - ${binaries} = @() - foreach (${href} in ${hrefs}) { - ${name} = ${href}.SubString(0, ${href}.Length - ${postfix}.Length + 1) - ${binaries} += [pscustomobject]@{ - "url" = "${uri}/${href}" - "package_type" = "zip" - "unpack_prefix_filter" = ${name} - "archive_file_name" = ${href} - "install_folder_name" = ${name} - } - } - return ${binaries} -} - -function GetGradleBinaries { - param ( - ${Version} - ) - ${prefix} = "*-" - ${postfix} = "*-bin.zip" - ${uri} = "https://services.gradle.org/distributions" - ${hrefs} = ((Invoke-WebRequest -Method "Get" -Uri ${uri}).Links | - Select-Object -ExpandProperty "href" -Skip 1 | - Where-Object -FilterScript { ($_ -cnotlike "*rc*") -and ($_ -cnotlike "*milestone*") -and ($_ -clike "${prefix}${version}${postfix}") } | - Sort-Object -Descending) - ${binaries} = @() - foreach (${href} in ${hrefs}) { - ${archive_file_name} = (${href} -csplit "/")[-1] - ${install_folder_name} = ${archive_file_name}.SubString(0, ${archive_file_name}.Length - ${postfix}.Length + 1) - ${binaries} += [pscustomobject]@{ - "url" = "${uri}/${archive_file_name}" - "package_type" = "zip" - "unpack_prefix_filter" = ${install_folder_name} - "archive_file_name" = ${archive_file_name} - "install_folder_name" = ${install_folder_name} - } - } - return ${binaries} -} - -function Unpack { - param ( - ${Binary}, - ${Arguments}, - ${Objects}, - ${Path} - ) - if (${Objects}.count -eq 0) { - Write-Host "[ERROR] Unsupported version argument." - return - } - ${object} = ${Objects}[0] - ${uri} = ${object}.url - ${package_type} = ${object}.package_type - ${unpack_prefix_filter} = ${object}.unpack_prefix_filter - ${archive_file_name} = ${object}.archive_file_name - ${install_folder_name} = ${object}.install_folder_name - ${filepath} = "${TEMP_PATH}\${archive_file_name}" - try { - Invoke-RestMethod -Method "Get" -Uri ${uri} -OutFile ${filepath} - } - catch { - Write-Host $_ - return - } - switch (${package_type}) { - "zip" { - Expand-Archive -Force -Path ${filepath} -DestinationPath ${TEMP_PATH} - Remove-Item -Force -Path ${filepath} - break - } - "targz" { - ${command} = "tar --extract --lzma --file ${filepath} --directory ${TEMP_PATH}" - Invoke-Expression -Command ${command} - break - } - default { - Write-Host "[ERROR] Unsupported file extension." - return - } - } - ${from} = "${TEMP_PATH}\${unpack_prefix_filter}" - ${to} = "${Path}\${install_folder_name}" - ${command} = "${to}\bin\${Binary} ${Arguments}" - if (Test-Path -Path ${to} -PathType "Container") { - Remove-Item -Force -Recurse -Path ${to} - } - Move-Item -Force -Path ${from} -Destination ${to} - Invoke-Expression -Command ${command} -} - -function Pick { - param ( - ${Binary}, - ${Path}, - ${Objects} - ) - if (${Objects}.count -eq 0) { - Write-Host "[ERROR] Unsupported version argument." - return $null - } - ${picked} = ${object}[0].Name - ${source_path} = "${Path}\${picked}" - ${link_path} = "${env:JAVA_GET_HOME}\${Binary}" - ${NO_OUTPUT} = (New-Item -Force -ItemType "SymbolicLink" -Path ${link_path} -Target ${source_path}) - return ${picked} -} - -function ListSupported { - param ( - ${Objects} - ) - Write-Host ((${Objects} | - Select-Object -ExpandProperty "install_folder_name") -join "`n") -} - -function ListInstalledJavaTool { - param ( - ${Pattern} - ) - Write-Host ((Get-ChildItem -Path ${JAVA_TOOLS_PATH} -Filter ${Pattern} -Directory -Name) -join "`n") -} - -switch (${args}[0]) { - { $_ -in "si", "self-install" } { - Invoke-RestMethod -Method "Get" -Uri "https://raw.githubusercontent.com/pwsh-bin/java-get/main/install.ps1" | - Invoke-Expression - return - } - { $_ -in "i", "install" } { - ${binary}, ${version} = (${args}[1] -csplit "@") - switch (${binary}) { - ${BINARIES}[0] { - if ($null -eq ${version}) { - ${version} = "openjdk" - } - ${objects} = (GetJDKS | - Where-Object -Property "install_folder_name" -clike "${version}*") - Unpack -Binary "java.exe" -Arguments "-version" -Objects ${objects} -Path ${JDKS_PATH} - return - } - ${BINARIES}[1] { - ${objects} = (GetMavenBinaries -Version ${version}) - Unpack -Binary "mvn.cmd" -Arguments "-version" -Objects ${objects} -Path ${JAVA_TOOLS_PATH} - return - } - ${BINARIES}[2] { - ${objects} = (GetApacheBinaries -Path "ant" -Version ${version}) - Unpack -Binary "ant.bat" -Arguments "-version" -Objects ${objects} -Path ${JAVA_TOOLS_PATH} - return - } - ${BINARIES}[3] { - ${objects} = (GetGradleBinaries | - Where-Object -Property "install_folder_name" -clike "*-${version}*") - Unpack -Binary "gradle.bat" -Arguments "--version" -Objects ${objects} -Path ${JAVA_TOOLS_PATH} - return - } - default { - Write-Host "[ERROR] Unsupported binary argument." - return - } - } - } - { $_ -in "ls", "list-supported" } { - ${binary} = ${args}[1] - switch (${binary}) { - ${BINARIES}[0] { - ${objects} = (GetJDKS) - return (ListSupported -Objects ${objects}) - } - ${BINARIES}[1] { - ${objects} = (GetMavenBinaries) - return (ListSupported -Objects ${objects}) - } - ${BINARIES}[2] { - ${objects} = (GetApacheBinaries -Path "ant") - return (ListSupported -Objects ${objects}) - } - ${BINARIES}[3] { - ${objects} = (GetGradleBinaries) - return (ListSupported -Objects ${objects}) - } - default { - Write-Host ((${BINARIES} | - Sort-Object) -join "`n") - return - } - } - return - } - { $_ -in "li", "list-installed" } { - ${binary} = ${args}[1] - switch (${binary}) { - ${BINARIES}[0] { - return (Get-ChildItem -Path ${JDKS_PATH} -Directory -Name) - } - ${BINARIES}[1] { - return (ListInstalledJavaTool -Pattern "apache-maven-*") - } - ${BINARIES}[2] { - return (ListInstalledJavaTool -Pattern "apache-ant-*") - } - ${BINARIES}[3] { - return (ListInstalledJavaTool -Pattern "gradle-*") - } - default { - Write-Host "[ERROR] Unsupported binary argument." - return - } - } - return - } - { $_ -in "p", "pick" } { - ${binary}, ${version} = ${args}[1] -csplit "@" - switch (${binary}) { - ${BINARIES}[0] { - UpdatePath -Binary ${binary} - ${objects} = (Get-ChildItem -Path ${JDKS_PATH} -Filter "${version}*" -Directory -Name) - ${picked} = (Pick -Binary ${binary} -Path ${JDKS_PATH} -Objects ${objects}) - if ($null -ne ${picked}) { - ${env:JAVA_HOME} = "${JDKS_PATH}\${picked}" - [System.Environment]::SetEnvironmentVariable("JAVA_HOME", ${env:JAVA_HOME}, [System.EnvironmentVariableTarget]::User) - Write-Host "[DEBUG] JAVA_HOME: ${env:JAVA_HOME}" - } - return - } - ${BINARIES}[1] { - UpdatePath -Binary ${binary} - ${objects} = (Get-ChildItem -Path ${JAVA_TOOLS_PATH} -Filter "apache-maven-${version}*" -Directory -Name) - ${picked} = (Pick -Binary ${binary} -Path ${JAVA_TOOLS_PATH} -Objects ${objects}) - if ($null -ne ${picked}) { - ${env:M2_HOME} = "${JAVA_TOOLS_PATH}\${picked}" - [System.Environment]::SetEnvironmentVariable("M2_HOME", ${env:M2_HOME}, [System.EnvironmentVariableTarget]::User) - Write-Host "[DEBUG] M2_HOME: ${env:M2_HOME}" - } - return - } - ${BINARIES}[2] { - UpdatePath -Binary ${binary} - ${objects} = (Get-ChildItem -Path ${JAVA_TOOLS_PATH} -Filter "apache-ant-${version}*" -Directory -Name) - ${picked} = (Pick -Binary ${binary} -Path ${JAVA_TOOLS_PATH} -Objects ${objects}) - if ($null -ne ${picked}) { - ${env:ANT_HOME} = "${JAVA_TOOLS_PATH}\${picked}" - [System.Environment]::SetEnvironmentVariable("ANT_HOME", ${env:ANT_HOME}, [System.EnvironmentVariableTarget]::User) - Write-Host "[DEBUG] ANT_HOME: ${env:ANT_HOME}" - } - return - } - ${BINARIES}[3] { - UpdatePath -Binary ${binary} - ${objects} = (Get-ChildItem -Path ${JAVA_TOOLS_PATH} -Filter "gradle-${version}*" -Directory -Name) - ${picked} = (Pick -Binary ${binary} -Path ${JAVA_TOOLS_PATH} -Objects ${objects}) - if ($null -ne ${picked}) { - ${env:GRADLE_HOME} = "${JAVA_TOOLS_PATH}\${picked}" - [System.Environment]::SetEnvironmentVariable("GRADLE_HOME", ${env:GRADLE_HOME}, [System.EnvironmentVariableTarget]::User) - Write-Host "[DEBUG] GRADLE_HOME: ${env:GRADLE_HOME}" - } - return - } - default { - Write-Host "[ERROR] Unsupported binary argument." - return - } - } - return - } - default { - Write-Host "[ERROR] Unsupported command argument." - return - } -}