Skip to content

Commit

Permalink
v0.6.3
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeysedoy97 committed Nov 30, 2022
1 parent d27eeff commit cbabbf8
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 76 deletions.
145 changes: 75 additions & 70 deletions get-bin.ps1
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
Set-Variable -Name "ProgressPreference" -Value "SilentlyContinue"
Set-Variable -Name 'ProgressPreference' -Value 'SilentlyContinue'

# ${APP_PATHS} = "HKCU:\Software\Microsoft\Windows\CurrentVersion\App Paths"
${DEBUG} = (Test-Path -PathType "Container" -Path (Join-Path -Path ${PSScriptRoot} -ChildPath ".git"))
${PS1_HOME} = (Join-Path -Path ${HOME} -ChildPath ".get-bin")
${PS1_FILE} = (Join-Path -Path ${PS1_HOME} -ChildPath "get-bin.ps1")
${GITHUB_PATH} = (Join-Path -Path ${PS1_HOME} -ChildPath ".github")
${STORE_PATH} = (Join-Path -Path ${PS1_HOME} -ChildPath ".store")
${7ZIP} = (Join-Path -Path ${ENV:PROGRAMFILES} -ChildPath (Join-Path -Path "7-Zip" -ChildPath "7z.exe"))
${DEBUG} = (Test-Path -PathType 'Container' -Path (Join-Path -Path ${PSScriptRoot} -ChildPath '.git'))
${PS1_HOME} = (Join-Path -Path ${HOME} -ChildPath '.get-bin')
${PS1_FILE} = (Join-Path -Path ${PS1_HOME} -ChildPath 'get-bin.ps1')
${GITHUB_PATH} = (Join-Path -Path ${PS1_HOME} -ChildPath '.github')
${STORE_PATH} = (Join-Path -Path ${PS1_HOME} -ChildPath '.store')
${7ZIP} = (Join-Path -Path ${ENV:PROGRAMFILES} -ChildPath (Join-Path -Path '7-Zip' -ChildPath '7z.exe'))
${PER_PAGE} = 100
${VERSION} = "v0.6.2"
${VERSION} = 'v0.6.3'
${HELP} = @"
Usage:
get-bin self-install - update get-bin to latest version
Expand All @@ -24,7 +24,7 @@ ${VERSION}
# NOTE: common
if (${args}.Count -eq 0) {
Write-Host ${HELP}
if ((${Env:Path} -split ";") -cnotcontains ${PS1_HOME}) {
if ((${Env:Path} -split ';') -cnotcontains ${PS1_HOME}) {
Write-Host @"
---------------------------------------------------------
The script are not found in the current PATH, please run:
Expand All @@ -33,53 +33,53 @@ The script are not found in the current PATH, please run:
"@
}
if (${PSVersionTable}.PSVersion.Major -lt 7) {
Write-Host @"
Write-Host @'
-----------------------------------------------------------------
The PowerShell Core is preferable to use this script, please run:
> winget install Microsoft.PowerShell
-----------------------------------------------------------------
"@
'@
}
if ((Get-ExecutionPolicy -Scope "LocalMachine") -ne "RemoteSigned") {
Write-Host @"
if ((Get-ExecutionPolicy -Scope 'LocalMachine') -ne 'RemoteSigned') {
Write-Host @'
-------------------------------------------------------------------------------
The RemoteSigned execution policy is preferable to use this script, please run:
> Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope LocalMachine
-------------------------------------------------------------------------------
"@
'@
}
exit
}

function GetBinaries {
if (${DEBUG}) {
return (Get-Content -Path (Join-Path -Path ${PSScriptRoot} -ChildPath "binaries.json") | ConvertFrom-Json | Sort-Object -Property "binary")
return (Get-Content -Path (Join-Path -Path ${PSScriptRoot} -ChildPath 'binaries.json') | ConvertFrom-Json | Sort-Object -Property 'binary')
}
${uri} = "https://raw.githubusercontent.com/pwsh-bin/get-bin/main/binaries.json"
${uri} = 'https://raw.githubusercontent.com/pwsh-bin/get-bin/main/binaries.json'
${binaries} = $null
if (${DEBUG}) {
Write-Host "[DEBUG] GET ${uri}"
}
try {
${binaries} = (Invoke-RestMethod -Method "Get" -Uri ${uri})
${binaries} = (Invoke-RestMethod -Method 'Get' -Uri ${uri})
}
catch {
Write-Host "[ERROR] GET ${uri}:"
Write-Host $_
exit
}
return (${binaries} | Sort-Object -Property "binary")
return (${binaries} | Sort-Object -Property 'binary')
}

# NOTE: common
function GetGitHubToken {
if (Test-Path -PathType "Leaf" -Path ${GITHUB_PATH}) {
if (Test-Path -PathType 'Leaf' -Path ${GITHUB_PATH}) {
return (Import-Clixml -Path ${GITHUB_PATH})
}
Write-Host @"
Write-Host @'
Generate GitHub API Token w/o expiration and public_repo scope: https://github.com/settings/tokens/new
Enter GitHub API Token:
"@
'@
${token} = (Read-Host -AsSecureString)
Export-Clixml -InputObject ${token} -Path ${GITHUB_PATH}
return ${token}
Expand All @@ -104,11 +104,11 @@ function GetGitHubTagNamesFromReleases {
try {
# NOTE: compat
${headers} = @{
"Authentication" = ("Bearer " + ${Token})
'Authentication' = ('Bearer ' + ${Token})
}
${releases} = (Invoke-RestMethod -Method "Get" -Uri ${uri} -Headers ${headers} -Body @{
"per_page" = ${PER_PAGE}
"page" = ${page}
${releases} = (Invoke-RestMethod -Method 'Get' -Uri ${uri} -Headers ${headers} -Body @{
'per_page' = ${PER_PAGE}
'page' = ${page}
})
}
catch {
Expand All @@ -119,7 +119,7 @@ function GetGitHubTagNamesFromReleases {
if (${releases}.Length -eq 0) {
return $null
}
${result} = (${releases} | Where-Object -FilterScript { ($_.prerelease -eq ${Prerelease}) -and ($_.tag_name -cmatch ${Pattern}) } | Select-Object -ExpandProperty "tag_name")
${result} = (${releases} | Where-Object -FilterScript { ($_.prerelease -eq ${Prerelease}) -and ($_.tag_name -cmatch ${Pattern}) } | Select-Object -ExpandProperty 'tag_name')
if (${result}.Length -ne 0) {
return ${result}
}
Expand All @@ -144,11 +144,11 @@ function GetGitHubTagNamesFromTags {
try {
# NOTE: compat
${headers} = @{
"Authentication" = ("Bearer " + ${Token})
'Authentication' = ('Bearer ' + ${Token})
}
${tags} = (Invoke-RestMethod -Method "Get" -Uri ${uri} -Headers ${headers} -Body @{
"per_page" = ${PER_PAGE}
"page" = ${page}
${tags} = (Invoke-RestMethod -Method 'Get' -Uri ${uri} -Headers ${headers} -Body @{
'per_page' = ${PER_PAGE}
'page' = ${page}
})
}
catch {
Expand All @@ -159,7 +159,7 @@ function GetGitHubTagNamesFromTags {
if (${tags}.Length -eq 0) {
return $null
}
${result} = (${tags} | Where-Object -Property "name" -cmatch ${Pattern} | Select-Object -ExpandProperty "name")
${result} = (${tags} | Where-Object -Property 'name' -cmatch ${Pattern} | Select-Object -ExpandProperty 'name')
if (${result}.Length -ne 0) {
return ${result}
}
Expand Down Expand Up @@ -200,10 +200,10 @@ function GetVersions {
${versionPrefix} = ${Object}.versionPrefix
${tag_names} = (GetGitHubTagNames -Repository ${Repository} -VersionPrefix ${VersionPrefix} -Version ${Version})
if (${tag_names}.Count -eq 0) {
Write-Host "[ERROR] Maintenance required."
Write-Host '[ERROR] Maintenance required.'
exit
}
return (${tag_names} | ForEach-Object -Process { $_ -creplace ${VersionPrefix}, "" } )
return (${tag_names} | ForEach-Object -Process { $_ -creplace ${VersionPrefix}, '' } )
}

function Install {
Expand All @@ -213,7 +213,7 @@ function Install {
)
${versions} = (GetVersions -Object ${Object} -Version ${Version})
if (${versions}.Count -eq 0) {
Write-Host "[ERROR] Unsupported version argument."
Write-Host '[ERROR] Unsupported version argument.'
exit
}
if (${versions} -is [string]) {
Expand All @@ -222,19 +222,19 @@ function Install {
else {
${version} = ${versions}[0]
}
New-Item -Force -ItemType "Directory" -Path ${STORE_PATH} | Out-Null
${directory} = (Join-Path -Path ${STORE_PATH} -ChildPath ((${Object}.binary -creplace ":", "-") + "@${version}"))
if (-not (Test-Path -PathType "Container" -Path ${directory})) {
New-Item -Force -ItemType "Directory" -Path ${directory} | Out-Null
${uri} = (${Object}.uriTemplate -creplace "%version%", ${version})
${filename} = ${uri}.SubString(${uri}.LastIndexOf("/") + 1)
New-Item -Force -ItemType 'Directory' -Path ${STORE_PATH} | Out-Null
${directory} = (Join-Path -Path ${STORE_PATH} -ChildPath ((${Object}.binary -creplace ':', '-') + "@${version}"))
if (-not (Test-Path -PathType 'Container' -Path ${directory})) {
New-Item -Force -ItemType 'Directory' -Path ${directory} | Out-Null
${uri} = (${Object}.uriTemplate -creplace '%version%', ${version})
${filename} = ${uri}.SubString(${uri}.LastIndexOf('/') + 1)
${outfile} = (Join-Path -Path ${directory} -ChildPath ${filename})
if (${DEBUG}) {
Write-Host "[DEBUG] GET ${uri}"
}
${is_blocked} = $false
try {
Invoke-WebRequest -Method "Get" -Uri ${uri} -OutFile ${outfile} -TimeoutSec 15
Invoke-WebRequest -Method 'Get' -Uri ${uri} -OutFile ${outfile} -TimeoutSec 15
}
catch {
Write-Host "[ERROR] GET ${uri}:"
Expand All @@ -243,47 +243,52 @@ function Install {
}
if (${is_blocked} -eq $true) {
try {
Invoke-WebRequest -Method "Get" -Uri ${uri} -OutFile ${outfile} -TimeoutSec 15 -Proxy "http://proxy-nossl.antizapret.prostovpn.org:29976"
Invoke-WebRequest -Method 'Get' -Uri ${uri} -OutFile ${outfile} -TimeoutSec 15 -Proxy 'http://proxy-nossl.antizapret.prostovpn.org:29976'
}
catch {
Write-Host "[ERROR] GET ${uri}:"
Write-Host $_
Remove-Item -Force -Path ${directory}
Remove-Item -Force -Path ${directory} | Out-Null
exit
}
}
${is_archive} = $false
if (${filename}.EndsWith(".zip")) {
if (${filename}.EndsWith('.zip')) {
${is_archive} = $true
Expand-Archive -Force -Path ${outfile} -DestinationPath ${directory}
}
elseif (${filename}.EndsWith(".tar.gz")) {
elseif (${filename}.EndsWith('.tar.gz')) {
${is_archive} = $true
${command} = "tar --extract --file ${outfile} --directory ${directory}"
Invoke-Expression -Command ${command} | Out-Null
}
elseif (${filename}.EndsWith(".7z")) {
elseif (${filename}.EndsWith('.7z')) {
${is_archive} = $true
${command} = "& '${7ZIP}' x -y -o${directory} ${outfile}"
Invoke-Expression -Command ${command} | Out-Null
}
if (${is_archive} -eq $true) {
Remove-Item -Force -Path ${outfile}
Remove-Item -Force -Path ${outfile} | Out-Null
if (-not (Test-Path -PathType 'Container' -Path ${directory})) {
Remove-Item -Force -Path ${directory} | Out-Null
Write-Host '[ERROR] Extraction failed.'
exit
}
}
}
foreach (${path} in ${Object}.paths) {
${target} = (Join-Path -Path ${directory} -ChildPath (${path}[0] -creplace "%version%", ${version}))
if (-not (Test-Path -PathType "Leaf" -Path ${target})) {
Write-Host ("[WARN] Binary " + ${target} + " does not exists. Will skip it.")
${target} = (Join-Path -Path ${directory} -ChildPath (${path}[0] -creplace '%version%', ${version}))
if (-not (Test-Path -PathType 'Leaf' -Path ${target})) {
Write-Host ('[WARN] Binary ' + ${target} + ' does not exists. Will skip it.')
continue
}
${link} = (Join-Path -Path ${PS1_HOME} -ChildPath ${path}[1])
New-Item -Force -ItemType "HardLink" -Path ${link} -Target ${target} | Out-Null
New-Item -Force -ItemType 'HardLink' -Path ${link} -Target ${target} | Out-Null
# TODO
# New-Item -Force -Path ${APP_PATHS} -Name ${path}[1] -Value ${target} | Out-Null
# New-ItemProperty -Force -Path (Join-Path -Path ${APP_PATHS} -ChildPath ${path}[1]) -Name "Path" -Value (Split-Path -Path ${target}) | Out-Null
if (${path}.Count -eq 3) {
${command} = (${link} + " " + ${path}[2])
${command} = (${link} + ' ' + ${path}[2])
Invoke-Expression -Command ${command}
}
}
Expand All @@ -293,30 +298,30 @@ function GetObject {
param (
${Binary}
)
${objects} = (GetBinaries | Where-Object -Property "binary" -clike "${Binary}*")
${objects} = (GetBinaries | Where-Object -Property 'binary' -clike "${Binary}*")
if (${objects}.Count -eq 0) {
Write-Host "[ERROR] Unsupported binary argument."
Write-Host '[ERROR] Unsupported binary argument.'
exit
}
${object} = ${objects}[0]
if ((${objects}.Count -gt 1) -and (${object}.binary.Length -ne ${Binary}.Length)) {
Write-Host ("[WARN] Found many supported binaries. Will proceed with " + ${object}.binary)
Write-Host ('[WARN] Found many supported binaries. Will proceed with ' + ${object}.binary)
}
elseif (${Binary} -ne ${object}.binary) {
Write-Host ("[WARN] Found supported binary. Will proceed with " + ${object}.binary)
Write-Host ('[WARN] Found supported binary. Will proceed with ' + ${object}.binary)
}
return ${object}
}

switch (${args}[0]) {
{ $_ -in "si", "self-install" } {
${uri} = "https://raw.githubusercontent.com/pwsh-bin/get-bin/main/install.ps1"
{ $_ -in 'si', 'self-install' } {
${uri} = 'https://raw.githubusercontent.com/pwsh-bin/get-bin/main/install.ps1'
${command} = $null
if (${DEBUG}) {
Write-Host "[DEBUG] GET ${uri}"
}
try {
${command} = Invoke-RestMethod -Method "Get" -Uri ${uri}
${command} = Invoke-RestMethod -Method 'Get' -Uri ${uri}
}
catch {
Write-Host "[ERROR] GET ${uri}:"
Expand All @@ -325,30 +330,30 @@ switch (${args}[0]) {
}
Invoke-Expression -Command ${command}
}
{ $_ -in "i", "install" } {
{ $_ -in 'i', 'install' } {
if (${args}.Count -eq 1) {
Write-Host "[ERROR] Missing binary argument."
Write-Host '[ERROR] Missing binary argument.'
exit
}
${binary}, ${version} = (${args}[1] -csplit "@")
${binary}, ${version} = (${args}[1] -csplit '@')
Install -Object (GetObject -Binary ${binary}) -Version ${version}
}
{ $_ -in "ls", "list-supported" } {
{ $_ -in 'ls', 'list-supported' } {
if (${args}.Count -eq 1) {
Write-Host ((GetBinaries | Select-Object -ExpandProperty "binary") -join "`n")
Write-Host ((GetBinaries | Select-Object -ExpandProperty 'binary') -join "`n")
exit
}
${binary}, ${version} = (${args}[1] -csplit "@")
${binary}, ${version} = (${args}[1] -csplit '@')
Write-Host ((GetVersions -Object (GetObject -Binary ${binary}) -Version ${version}) -join "`n")
}
{ $_ -in "init" } {
if ((${Env:Path} -split ";") -cnotcontains ${PS1_HOME}) {
{ $_ -in 'init' } {
if ((${Env:Path} -split ';') -cnotcontains ${PS1_HOME}) {
${Env:Path} += ";${PS1_HOME}"
}
}
# NOTE: common
{ $_ -in "setup" } {
New-Item -Force -ItemType "File" -Path ${PROFILE} | Out-Null
{ $_ -in 'setup' } {
New-Item -Force -ItemType 'File' -Path ${PROFILE} | Out-Null
${value} = "& '${PS1_FILE}' init"
if (((Get-Content -Path ${PROFILE}) -split "`n") -cnotcontains ${value}) {
Add-Content -Path ${PROFILE} -Value ${value}
Expand All @@ -358,6 +363,6 @@ switch (${args}[0]) {
}
}
default {
Write-Host "[ERROR] Unsupported command argument."
Write-Host '[ERROR] Unsupported command argument.'
}
}
10 changes: 5 additions & 5 deletions install.ps1
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Set-Variable -Name "ProgressPreference" -Value "SilentlyContinue"
${PS1_HOME} = Join-Path -Path ${HOME} -ChildPath ".get-bin"
${PS1_FILE} = Join-Path -Path ${PS1_HOME} -ChildPath "get-bin.ps1"
New-Item -Force -ItemType "Directory" -Path ${PS1_HOME} | Out-Null
Invoke-RestMethod -Method "Get" -Uri "https://raw.githubusercontent.com/pwsh-bin/get-bin/main/get-bin.ps1" -OutFile ${PS1_FILE}
Set-Variable -Name 'ProgressPreference' -Value 'SilentlyContinue'
${PS1_HOME} = Join-Path -Path ${HOME} -ChildPath '.get-bin'
${PS1_FILE} = Join-Path -Path ${PS1_HOME} -ChildPath 'get-bin.ps1'
New-Item -Force -ItemType 'Directory' -Path ${PS1_HOME} | Out-Null
Invoke-RestMethod -Method 'Get' -Uri 'https://raw.githubusercontent.com/pwsh-bin/get-bin/main/get-bin.ps1' -OutFile ${PS1_FILE}
Invoke-Expression -Command ${PS1_FILE}
2 changes: 1 addition & 1 deletion test.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
${binaries} = (Get-Content -Path (Join-Path -Path ${PSScriptRoot} -ChildPath "binaries.json") | ConvertFrom-Json)
${binaries} = (Get-Content -Path (Join-Path -Path ${PSScriptRoot} -ChildPath 'binaries.json') | ConvertFrom-Json)
foreach (${binary} in ${binaries}) {
.\get-bin.ps1 ls ${binary}.binary
}
Expand Down

0 comments on commit cbabbf8

Please sign in to comment.