1
0
mirror of https://github.com/tw93/Mole.git synced 2026-03-23 03:10:08 +00:00

fix(windows): stabilize install and prerelease versioning

This commit is contained in:
Tw93
2026-03-22 14:39:25 +08:00
parent 05fbc007d5
commit 16c3ea9867
13 changed files with 211 additions and 54 deletions

View File

@@ -16,6 +16,9 @@ $script:MOLE_CORE_DIR = Split-Path -Parent $MyInvocation.MyCommand.Path
$script:MOLE_LIB_DIR = Split-Path -Parent $script:MOLE_CORE_DIR
$script:MOLE_ROOT_DIR = Split-Path -Parent $script:MOLE_LIB_DIR
# Version helpers are standalone and safe to load before the other core modules.
. "$script:MOLE_CORE_DIR\version.ps1"
# ============================================================================
# Load Core Modules
# ============================================================================
@@ -36,7 +39,7 @@ $script:MOLE_ROOT_DIR = Split-Path -Parent $script:MOLE_LIB_DIR
# Version Information
# ============================================================================
$script:MOLE_VERSION = "1.0.0"
$script:MOLE_VERSION = Get-MoleVersionString -RootDir $script:MOLE_ROOT_DIR
$script:MOLE_BUILD_DATE = "2026-01-07"
function Get-MoleVersion {

View File

@@ -9,6 +9,9 @@ if ((Get-Variable -Name 'MOLE_TUI_BINARIES_LOADED' -Scope Script -ErrorAction Si
}
$script:MOLE_TUI_BINARIES_LOADED = $true
$script:MOLE_TUI_CORE_DIR = Split-Path -Parent $MyInvocation.MyCommand.Path
. "$script:MOLE_TUI_CORE_DIR\version.ps1"
$script:MoleGitHubRepo = "tw93/Mole"
$script:MoleGitHubApiRoot = "https://api.github.com/repos/$($script:MoleGitHubRepo)"
$script:MoleGitHubHeaders = @{
@@ -19,17 +22,7 @@ $script:MoleGitHubHeaders = @{
function Get-MoleVersionFromScriptFile {
param([string]$WindowsDir)
$moleScript = Join-Path $WindowsDir "mole.ps1"
if (-not (Test-Path $moleScript)) {
return $null
}
$content = Get-Content $moleScript -Raw
if ($content -match '\$script:MOLE_VER\s*=\s*"([^"]+)"') {
return $Matches[1]
}
return $null
return Get-MoleVersionString -RootDir $WindowsDir
}
function Get-TuiBinaryAssetName {
@@ -136,18 +129,50 @@ function Build-TuiBinary {
Write-Host "Building $Name tool..." -ForegroundColor Cyan
$stdoutPath = Join-Path $env:TEMP "mole-$Name-build.stdout.log"
$stderrPath = Join-Path $env:TEMP "mole-$Name-build.stderr.log"
foreach ($path in @($stdoutPath, $stderrPath)) {
if (Test-Path $path) {
Remove-Item $path -Force -ErrorAction SilentlyContinue
}
}
Push-Location $WindowsDir
try {
$result = & go build -o "$DestinationPath" $SourcePath 2>&1
if ($LASTEXITCODE -ne 0) {
Write-Host "Failed to build $Name tool: $result" -ForegroundColor Red
return $false
}
$process = Start-Process -FilePath "go" `
-ArgumentList @("build", "-o", $DestinationPath, $SourcePath) `
-NoNewWindow `
-Wait `
-PassThru `
-RedirectStandardOutput $stdoutPath `
-RedirectStandardError $stderrPath
}
catch {
Write-Host "Failed to start go build for $Name tool: $_" -ForegroundColor Red
return $false
}
finally {
Pop-Location
}
$buildOutput = @()
foreach ($path in @($stdoutPath, $stderrPath)) {
if (Test-Path $path) {
$content = (Get-Content $path -Raw).Trim()
if ($content) {
$buildOutput += $content
}
Remove-Item $path -Force -ErrorAction SilentlyContinue
}
}
if ($process.ExitCode -ne 0) {
$message = if ($buildOutput.Count -gt 0) { $buildOutput -join [Environment]::NewLine } else { "go build exited with code $($process.ExitCode)" }
Write-Host "Failed to build $Name tool: $message" -ForegroundColor Red
return $false
}
return $true
}
@@ -169,15 +194,24 @@ function Ensure-TuiBinary {
$Version = Get-MoleVersionFromScriptFile -WindowsDir $WindowsDir
}
if (Restore-PrebuiltTuiBinary -Name $Name -WindowsDir $WindowsDir -DestinationPath $DestinationPath -Version $Version) {
return $DestinationPath
try {
if (Restore-PrebuiltTuiBinary -Name $Name -WindowsDir $WindowsDir -DestinationPath $DestinationPath -Version $Version) {
return $DestinationPath
}
}
catch {
Write-Host "Failed to restore prebuilt $Name tool: $_" -ForegroundColor Yellow
}
if (Get-Command go -ErrorAction SilentlyContinue) {
if (Build-TuiBinary -Name $Name -WindowsDir $WindowsDir -DestinationPath $DestinationPath -SourcePath $SourcePath) {
return $DestinationPath
try {
if (Build-TuiBinary -Name $Name -WindowsDir $WindowsDir -DestinationPath $DestinationPath -SourcePath $SourcePath) {
return $DestinationPath
}
}
catch {
Write-Host "Failed to prepare $Name tool: $_" -ForegroundColor Yellow
}
return $null
}
return $null

41
lib/core/version.ps1 Normal file
View File

@@ -0,0 +1,41 @@
# Mole - Version helpers
# Provides a single source of truth for the Windows version string.
#Requires -Version 5.1
Set-StrictMode -Version Latest
if ((Get-Variable -Name 'MOLE_VERSION_HELPERS_LOADED' -Scope Script -ErrorAction SilentlyContinue) -and $script:MOLE_VERSION_HELPERS_LOADED) {
return
}
$script:MOLE_VERSION_HELPERS_LOADED = $true
$script:MoleDefaultVersion = "1.29.0"
function Get-MoleVersionFilePath {
param([string]$RootDir)
if ([string]::IsNullOrWhiteSpace($RootDir)) {
return $null
}
return Join-Path $RootDir "VERSION"
}
function Get-MoleVersionString {
param(
[string]$RootDir,
[string]$DefaultVersion = $script:MoleDefaultVersion
)
$versionFile = Get-MoleVersionFilePath -RootDir $RootDir
if (-not $versionFile -or -not (Test-Path $versionFile)) {
return $DefaultVersion
}
$version = (Get-Content $versionFile -Raw).Trim()
if ([string]::IsNullOrWhiteSpace($version)) {
return $DefaultVersion
}
return $version
}