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:
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:
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.
Code:
# Suppress progress UI; Invoke-WebRequest is much slower with it enabled
$ProgressPreference = "SilentlyContinue"
# Set the repo name and asset pattern for later use
$repoName = "kolmafia/kolmafia"
$assetPattern = "*.jar"
# Save everything to the current directory (wherever the script is located)
$extractDirectory = Split-Path -Parent $PSCommandPath
$releasesUri = "https://api.github.com/repos/$repoName/releases/latest"
$asset = (Invoke-RestMethod -Uri $releasesUri).assets | Where-Object name -like $assetPattern
$downloadUri = $asset.browser_download_url
# Look at all the KoLmafia versions that you have already downloaded and sort them with the most recent at the top
$mafiaVersions = Get-ChildItem -File "KoLmafia-*.jar" | Where-Object {!($_.Name -ilike "*latest*" )} | Sort-Object Name -Descending
if (-not($null -eq $mafiaVersions)) {
$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
Exit
} else {
$extractPath = Join-Path $extractDirectory $asset.name
Write-Output "Downloading to: $extractPath"
# Try to download the latest version and throw errors if something goes wrong
try {
Invoke-WebRequest -Uri $downloadUri -Out $extractPath -UseBasicParsing
}
catch {
Write-Warning "Something went terribly wrong!"
Write-Output "Checking URL: $releasesUri"
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!"
}
}
# How many versions of KoLmafia do we have already downloaded, including the new one?
$mafiaVersions = Get-ChildItem -File "KoLmafia-*.jar" | Sort-Object Name -Descending
$latestPath = Join-Path $extractDirectory "KoLmafia-latest.jar"
if ($mafiaVersions.Count -gt 1) {
# If we have multiple versions, keep only the last three versions and copy the newest version to KoLmafia-latest.jar
$i = 0
ForEach ($mafiaVersion in $mafiaVersions) {
if ($i -eq 1) {
Write-Output "Copying $($mafiaVersion.Name) to KoLmafia-latest.jar"
$mafiaVersion | Copy-Item -Destination $latestPath -Force
}
if ($i -gt 3) {
$mafiaVersion | Remove-Item -Force
}
$i++
}
} else {
# If we have only the version we just downloaded, copy it to KoLmafia-latest.jar
Write-Output "Copying $($mafiaVersions.Name) to KoLmafia-latest.jar"
$mafiaVersions | Copy-Item -Destination $latestPath -Force
}
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
Last edited: