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
$mafiaVersions = Get-ChildItem -File "KoLmafia-*.jar" | Where-Object {!($_.Name -ilike "*latest*" )} | Sort-Object Name -Descending
$latestName = ($mafiaVersions[0]).Name
if ($latestName -eq $asset.name) {
    Write-Output "You already have the latest version of KoLmafia! This script will now exit."
    Start-Sleep -Seconds 5
} else {
    $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!

NOTE: Buckets reminded me about PowerShell execution policies. By default, your Windows PC won't run a PowerShell script. This safety measure is a good thing for my mother and other folks who are uncomfortable with technology, but it gets in our way. To allow your scripts to run, you will need to type the following command at a PowerShell prompt with Administrator privileges:
Code:
Set-ExecutionPolicy RemoteSigned
This setting will require any remotely-hosted scripts to be cryptographically signed, but local scripts (like the above) will run just fine. Other settings exist and will work, but I've found "RemoteSigned" to be a fair compromise between security and usability.
 
Last edited:
you can also have it start mafia after updating by appending:

Code:
Start-Process java -WindowStyle Hidden -ArgumentList @(
    "-jar kolmafia-latest.jar"
)

you'll need to give PS permission via Execution Policies, before it'll actually run the jar tho.
 
Last edited:
you can also have it start mafia after updating by adding:

Code:
Start-Process java -WindowStyle Hidden -ArgumentList @(
    "-jar kolmafia-latest.jar"
)

you'll need to give PS permission via Execution Policies, before it'll actually run the jar tho.
Ooh, nice improvement! Thanks for the suggestion.

Thanks also for mentioning the PowerShell execution policy. I tend to forget them because I set mine long ago, but they are certainly a thing. I'll add a note to the original post if I can.
 
if i could make one more suggestion (that i'm not talented enough to implement), it'd be nice if it checked to see if an update is necessary before moving on to launch (right now, it always downloads the newest version regardless of what you already have)

ideally it'd check to see if kolmafia-latest = most recent version #, and only download if current version < newest version so it can be used as a "one-click" launcher/updater

adding this bit of logic is beyond my skills tho :(
 
if i could make one more suggestion (that i'm not talented enough to implement), it'd be nice if it checked to see if an update is necessary before moving on to launch (right now, it always downloads the newest version regardless of what you already have)

ideally it'd check to see if kolmafia-latest = most recent version #, and only download if current version < newest version so it can be used as a "one-click" launcher/updater

adding this bit of logic is beyond my skills tho :(
So that bit of inefficiency always bugged me, but I wasn't sufficiently motivated to fix it. Your public shaming (;)) gave me that missing motivation. The code in the original post now checks to see if you already have the current version and won't waste time downloading a duplicate.

Thanks for the suggestion!
 
Back
Top