1
0
mirror of https://github.com/tw93/Mole.git synced 2026-03-22 19:40:07 +00:00
Files
Mole/scripts/build-exe.ps1

246 lines
8.1 KiB
PowerShell

# Mole Windows - Standalone EXE Builder
# Creates a true standalone executable using PS2EXE
# Requires: PS2EXE module (Install-Module ps2exe)
#Requires -Version 5.1
param(
[Parameter(Mandatory=$false)]
[string]$Version,
[switch]$ShowHelp
)
$ErrorActionPreference = "Stop"
Set-StrictMode -Version Latest
# ============================================================================
# Configuration
# ============================================================================
$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
$projectRoot = Split-Path -Parent $scriptDir
$releaseDir = Join-Path $projectRoot "release"
$versionFile = Join-Path $projectRoot "VERSION"
# Read version from VERSION if not provided
if (-not $Version) {
if (Test-Path $versionFile) {
$Version = (Get-Content $versionFile -Raw).Trim()
}
if (-not $Version) {
Write-Host "Error: Could not detect version from VERSION" -ForegroundColor Red
exit 1
}
}
$exeName = "mole-$Version-x64.exe"
$exePath = Join-Path $releaseDir $exeName
# ============================================================================
# Help
# ============================================================================
function Show-BuildHelp {
Write-Host ""
Write-Host "Mole Windows Standalone EXE Builder" -ForegroundColor Cyan
Write-Host ""
Write-Host "Usage: .\build-exe.ps1 [-Version <version>]"
Write-Host ""
Write-Host "Requirements:"
Write-Host " Install-Module ps2exe -Scope CurrentUser"
Write-Host ""
Write-Host "Options:"
Write-Host " -Version <ver> Specify version (default: auto-detect)"
Write-Host " -ShowHelp Show this help message"
Write-Host ""
}
if ($ShowHelp) {
Show-BuildHelp
exit 0
}
# ============================================================================
# Banner
# ============================================================================
Write-Host ""
Write-Host "========================================" -ForegroundColor Cyan
Write-Host " Mole - Standalone EXE Builder" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""
# ============================================================================
# Check Dependencies
# ============================================================================
Write-Host "[1/4] Checking dependencies..." -ForegroundColor Cyan
# Check if ps2exe is installed
try {
$ps2exeModule = Get-Module -ListAvailable -Name ps2exe
if (-not $ps2exeModule) {
Write-Host " Error: ps2exe module not found" -ForegroundColor Red
Write-Host ""
Write-Host " Install with:" -ForegroundColor Yellow
Write-Host " Install-Module ps2exe -Scope CurrentUser" -ForegroundColor Gray
Write-Host ""
Write-Host " Or use the package as a ZIP archive instead" -ForegroundColor Gray
exit 1
}
Import-Module ps2exe -ErrorAction Stop
Write-Host " ps2exe module: OK" -ForegroundColor Green
} catch {
Write-Host " Error loading ps2exe: $_" -ForegroundColor Red
exit 1
}
# Check release directory exists
if (-not (Test-Path $releaseDir)) {
Write-Host " Error: Release directory not found: $releaseDir" -ForegroundColor Red
Write-Host " Run build-release.ps1 first" -ForegroundColor Gray
exit 1
}
Write-Host " Release directory: OK" -ForegroundColor Green
Write-Host ""
# ============================================================================
# Create Launcher Script
# ============================================================================
Write-Host "[2/4] Creating launcher script..." -ForegroundColor Cyan
$launcherPath = Join-Path $releaseDir "mole-launcher.ps1"
# Create a self-contained launcher that embeds the main script
$launcherContent = @"
#Requires -Version 5.1
# Mole Windows - Standalone Launcher
# Version: $Version
# This is a self-contained executable generated by PS2EXE
param(
[Parameter(Position = 0)]
[string]`$Command,
[Parameter(Position = 1, ValueFromRemainingArguments)]
[string[]]`$CommandArgs
)
`$ErrorActionPreference = "Stop"
# Embedded version info
`$script:MOLE_VER = "$Version"
`$script:MOLE_BUILD = "$(Get-Date -Format 'yyyy-MM-dd')"
# Check if running as compiled EXE
`$isCompiled = `$PSScriptRoot -eq `$null -or `$MyInvocation.MyCommand.Path -like "*.exe"
if (`$isCompiled) {
Write-Host "Error: Standalone EXE mode requires embedded resources" -ForegroundColor Red
Write-Host "Please use the ZIP distribution for full functionality" -ForegroundColor Yellow
Write-Host ""
Write-Host "Download from: https://github.com/bhadraagada/mole/releases" -ForegroundColor Gray
exit 1
}
# If running as script, delegate to main mole.ps1
`$moleScript = Join-Path `$PSScriptRoot "mole.ps1"
if (Test-Path `$moleScript) {
& `$moleScript `$Command `$CommandArgs
} else {
Write-Host "Error: mole.ps1 not found in `$PSScriptRoot" -ForegroundColor Red
exit 1
}
"@
Set-Content -Path $launcherPath -Value $launcherContent -Encoding UTF8
Write-Host " Created launcher script" -ForegroundColor Green
Write-Host ""
# ============================================================================
# Build EXE
# ============================================================================
Write-Host "[3/4] Building standalone EXE..." -ForegroundColor Cyan
Write-Host " This may take a few minutes..." -ForegroundColor Gray
Write-Host ""
try {
# Extract numeric version for ps2exe (requires n.n.n.n format)
# Remove non-numeric suffixes like "-windows"
$numericVersion = $Version -replace '[^0-9.].*$', ''
if ($numericVersion -notmatch '^\d+(\.\d+){0,3}$') {
$numericVersion = "1.0.0.0"
}
# Build parameters (only include non-null values)
$ps2exeParams = @{
inputFile = $launcherPath
outputFile = $exePath
noConsole = $false
title = "Mole - Windows System Maintenance"
description = "Deep clean and optimize your Windows system"
company = "Mole Project"
product = "Mole"
copyright = "MIT License"
version = $numericVersion
requireAdmin = $false
verbose = $true
}
# Note: iconFile omitted (null) - add when icon is available
# Build EXE
Invoke-PS2EXE @ps2exeParams
if (Test-Path $exePath) {
$exeSize = (Get-Item $exePath).Length / 1MB
Write-Host ""
Write-Host " Built: $exeName ($([math]::Round($exeSize, 2)) MB)" -ForegroundColor Green
} else {
Write-Host " Error: EXE was not created" -ForegroundColor Red
exit 1
}
} catch {
Write-Host " Error building EXE: $_" -ForegroundColor Red
exit 1
} finally {
# Cleanup launcher script
if (Test-Path $launcherPath) {
Remove-Item $launcherPath -Force
}
}
Write-Host ""
# ============================================================================
# Update Checksums
# ============================================================================
Write-Host "[4/4] Updating checksums..." -ForegroundColor Cyan
$hashFile = Join-Path $releaseDir "SHA256SUMS.txt"
$exeHash = (Get-FileHash $exePath -Algorithm SHA256).Hash.ToLower()
# Append to existing hash file
$hashLine = "$exeHash $exeName"
Add-Content -Path $hashFile -Value $hashLine -Encoding UTF8
Write-Host " $exeName" -ForegroundColor Gray
Write-Host " SHA256: $exeHash" -ForegroundColor Gray
Write-Host ""
# ============================================================================
# Summary
# ============================================================================
Write-Host "========================================" -ForegroundColor Cyan
Write-Host " Build Complete!" -ForegroundColor Green
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""
Write-Host "Standalone executable created:" -ForegroundColor Yellow
Write-Host " $exePath" -ForegroundColor Gray
Write-Host ""
Write-Host "Note: This is a launcher EXE that requires the full Mole distribution" -ForegroundColor Yellow
Write-Host "For true standalone functionality, use the ZIP archive" -ForegroundColor Yellow
Write-Host ""