PowerShell to Auto-Update KoLmafia

BigVinnie

New member
As you may know, the recent move to deprecate Jenkins has broken some auto-updaters. I can't find any Windows auto-updaters that work on my (Windows 11) system. I'm a systems engineer and PowerShell fanboy in "real life," so I decided to explore the possibilities of a PowerShell script. I found some code online, "borrowed" it, tweaked it to my liking, and here we are:

Code:
$repoName = "kolmafia/kolmafia"
$assetPattern = "*.jar"
$extractDirectory = Split-Path -Parent $PSCommandPath

$releasesUri = "https://api.github.com/repos/$repoName/releases/latest"
$asset = (Invoke-WebRequest $releasesUri | ConvertFrom-Json).assets | Where-Object name -like $assetPattern
$downloadUri = $asset.browser_download_url

$extractPath = [System.IO.Path]::Combine($extractDirectory, $asset.name)
Write-Output "Downloading to: $extractPath"
try {
    Invoke-WebRequest -Uri $downloadUri -Out $extractPath
}
catch {
    Write-Warning "Something went terribly wrong!"
    Write-Output "Checking URL: $relasesUri"
    Write-Output "Checking for: $assetPattern"
    Write-Output "Downloading to: $extractDirectory"
    Write-Output "That's all the information I have for you. Go forth and debug!"
}

$i = 0
$latestPath = [System.IO.Path]::Combine($extractDirectory, "KoLmafia-latest.jar")
$mafiaVersions = Get-ChildItem -File "KoLmafia-*.jar" | Sort-Object Name -Descending
ForEach ($mafiaVersion in $mafiaVersions) {
    if ($i -eq 1) {
        $mafiaVersion | Copy-Item -Destination $latestPath -Force
    }
    if ($i -gt 3) {
        $mafiaVersion | Remove-Item -Force
    }
    $i++
}

I'm not claiming this script is perfect. It can be improved, but it does get the job done. When you run it, it'll download the latest daily build to the same directory in which the script is located. It'll copy the build to "KoLmafia-latest.jar" (for convenience). Lastly, it'll keep the last three builds you've downloaded and delete any older ones.

Share and enjoy, and please feel free to share any improvements!
 
Top