Last active
September 10, 2023 20:51
-
-
Save ClassicDarkChocolate/4f10ad43e5de4fb103fa2ac5a1727005 to your computer and use it in GitHub Desktop.
Commands: Get-ScoopPackageVersions, Invoke-ScoopPackageCommand, Install-ScoopPackage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Get-ScoopRoot { | |
$scoopdir = & { | |
$env:SCOOP, (scoop config 'rootPath'), "$env:USERPROFILE\scoop" | Where-Object { -not [String]::IsNullOrEmpty($_) } | Select-Object -First 1 | |
} 6>$null | |
$scoopdir | |
} | |
function Get-ScoopPackageVersions { | |
param ( | |
[Parameter(Position = 1, Mandatory = $true)] | |
$Name, | |
[switch] | |
$IncludeDescription | |
) | |
$info = $(scoop info $Name) 6>$null | |
if (-not $?) { | |
Write-Error "Could not find manifest for '$Name'." -ErrorAction Stop | |
} | |
$scoopdir = Get-ScoopRoot | |
$bucket = $info.Bucket | |
Push-Location -Path "$scoopdir\buckets\$bucket\bucket" | |
$rawLogs = $(git grep '\"version\": \?\"' $(git rev-list --all -- "$Name.json") -- "$Name.json") | |
$logs = $rawLogs | ForEach-Object { | |
($_ -match '^(?<hash>[\w\d]+):.+?\"version\": ?\"(?<version>.+?)\"') | Out-Null | |
$obj = [PSCustomObject]@{ | |
CommitHash = $Matches.hash | |
Version = $Matches.version | |
Description = "" | |
} | |
if ($IncludeDescription.IsPresent) { | |
$obj.Description = $(git log --format=%B -n 1 $Matches.hash)[0] | |
} | |
$obj | |
} | |
Pop-Location | |
$logs | |
} | |
function Invoke-ScoopPackageCommand { | |
param ( | |
[ValidateSet( | |
"cat", | |
"depends", | |
"home", | |
"info", | |
"install", | |
"virustotal" | |
)] | |
[Parameter(Position = 1, Mandatory = $true)] | |
$Command, | |
[Parameter(Position = 2, Mandatory = $true)] | |
$PackageName, | |
$CommandArgs, | |
$CommitHash, | |
$Version | |
) | |
$info = $(scoop info $PackageName) 6>$null | |
if (-not $?) { | |
Write-Error "Could not find manifest for '$PackageName'." -ErrorAction Stop | |
} | |
$scoopdir = Get-ScoopRoot | |
$bucket = $info.Bucket | |
Push-Location -Path "$scoopdir\buckets\$bucket\bucket" | |
if ($null -ne $CommitHash) { | |
git restore --source=$CommitHash "$PackageName.json" | |
if ($?) { | |
scoop $Command $CommandArgs $PackageName | |
} | |
} | |
elseif ($null -ne $Version) { | |
$commits = Get-ScoopPackageVersions -Name $PackageName | |
$filteredCommits = $commits | Where-Object Version -EQ $Version | |
if ($null -ne $filteredCommits) { | |
git restore --source=$($filteredCommits[0].CommitHash) "$PackageName.json" | |
if ($?) { | |
scoop $Command $CommandArgs $PackageName | |
} | |
} | |
else { | |
Write-Error "Could not find manifest for $PackageName@$Version" | |
} | |
} | |
else { | |
scoop $Command $CommandArgs $PackageName | |
} | |
git restore "$PackageName.json" | |
Pop-Location | |
} | |
function Install-ScoopPackage { | |
param ( | |
[Parameter(Position = 1, Mandatory = $true)] | |
$Name, | |
$CommandArgs, | |
$CommitHash, | |
$Version | |
) | |
Invoke-ScoopPackageCommand -Command install -CommandArgs $CommandArgs -PackageName $Name -Version $Version -CommitHash $CommitHash | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@jkniiv Thanks!