Skip to content

[Feature] Auto Update #152

@shiba2046

Description

@shiba2046

Feature description

Would like the app to be able to auto update to the latest version.

Use case / motivation

Have to download the zip and update manually every time. This is annoying.

Area of the app

Other

OS relevance

Windows

Alternatives considered

I've asked AI to write a PowerShell Script which does the job ok. But would be nice to be included in the actual program itself.

# ==========================================
# Mouser Auto-Updater (English Version)
# ==========================================

# Set Working Directory to script location
$currentDir = $PSScriptRoot
if ([string]::IsNullOrEmpty($currentDir)) { $currentDir = Get-Location }

$repo = "TomBadash/Mouser"
$tempZip = Join-Path $env:TEMP "Mouser_Latest.zip"
$tempUnzipFolder = Join-Path $env:TEMP "Mouser_Temp_Extract"
$exeName = "Mouser.exe"
$exePath = Join-Path $currentDir $exeName

try {
    # 1. Terminate running process
    Write-Host "Checking for running Mouser instances..." -ForegroundColor Cyan
    $mouserProcess = Get-Process -Name "Mouser" -ErrorAction SilentlyContinue
    if ($mouserProcess) {
        Write-Host "Closing Mouser.exe..." -ForegroundColor Yellow
        Stop-Process -Name "Mouser" -Force
        Start-Sleep -Seconds 2 # Wait for file handles to release
    }

    # 2. Fetch latest release info from GitHub API
    Write-Host "Fetching latest release info from GitHub..." -ForegroundColor Cyan
    $apiUrl = "https://api.github.com/repos/$repo/releases/latest"
    $releaseInfo = Invoke-RestMethod -Uri $apiUrl -UseBasicParsing
    
    # Filter for Windows zip asset
    $asset = $releaseInfo.assets | Where-Object { $_.name -like "*win*" -and $_.name -like "*.zip" } | Select-Object -First 1
    if ($null -eq $asset) { $asset = $releaseInfo.assets | Where-Object { $_.name -like "*.zip" } | Select-Object -First 1 }
    if ($null -eq $asset) { throw "No valid ZIP asset found in the latest release." }

    # 3. Download the asset
    Write-Host "Downloading: $($asset.name) ..." -ForegroundColor Cyan
    Invoke-WebRequest -Uri $asset.browser_download_url -OutFile $tempZip

    # 4. Clean up old files
    Write-Host "Removing old version files..." -ForegroundColor Yellow
    $targets = @($exeName, "_internal")
    foreach ($target in $targets) {
        $fullPath = Join-Path $currentDir $target
        if (Test-Path $fullPath) {
            Remove-Item -Path $fullPath -Force -Recurse -ErrorAction SilentlyContinue
            Write-Host "Deleted: $target" -ForegroundColor Gray
        }
    }

    # 5. Extract and move files
    if (Test-Path $tempUnzipFolder) { Remove-Item $tempUnzipFolder -Force -Recurse }
    Write-Host "Extracting and deploying files..." -ForegroundColor Cyan
    Expand-Archive -Path $tempZip -DestinationPath $tempUnzipFolder -Force
    
    # Check if files are nested inside a 'Mouser' folder within the zip
    $innerMouserFolder = Join-Path $tempUnzipFolder "Mouser"
    $sourcePath = if (Test-Path $innerMouserFolder) { $innerMouserFolder } else { $tempUnzipFolder }
    
    Get-ChildItem -Path $sourcePath | ForEach-Object {
        Move-Item -Path $_.FullName -Destination $currentDir -Force
    }

    Write-Host "`n[SUCCESS] Update completed successfully!" -ForegroundColor Green

    # 6. Launch the application
    if (Test-Path $exePath) {
        Write-Host "Launching Mouser..." -ForegroundColor Magenta
        Start-Process -FilePath $exePath -WorkingDirectory $currentDir
    } else {
        Write-Host "[WARNING] Could not find $exeName in $currentDir" -ForegroundColor Red
    }

}
catch {
    Write-Host "`n[ERROR] An error occurred: $_" -ForegroundColor Red
}
finally {
    # Final cleanup of temporary files
    if (Test-Path $tempZip) { Remove-Item $tempZip -Force }
    if (Test-Path $tempUnzipFolder) { Remove-Item $tempUnzipFolder -Force -Recurse }
    
    Write-Host "`nPress any key to exit..." -ForegroundColor White
    $null = [Console]::ReadKey($true)
}

To run it with double click, create a Update.bat file.

@echo off
title Mouser Updater
cd /d "%~dp0"
:: Run PowerShell with Bypass policy to avoid execution restrictions
powershell -NoProfile -ExecutionPolicy Bypass -File "%~dp0Update-Mouser.ps1"

Then you can click the bat file to update.

Note that the powershell script will be identified as virus by some anti-virus software. Add it to trusted zone.

Device info JSON (if relevant)

Mockups, screenshots, or references

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions