Files
LinkPort/scripts/start-dev.ps1
T

84 lines
4.3 KiB
PowerShell

param([switch]$FullCheck, [switch]$RebuildWeb, [switch]$BuildOnly)
$ErrorActionPreference = 'Stop'
$projectRoot = Split-Path -Parent $PSScriptRoot
Set-Location -LiteralPath $projectRoot
$buildScript = Join-Path $PSScriptRoot 'build.ps1'
$binary = Join-Path $projectRoot 'serviceport.exe'
$sourceMode = Test-Path -LiteralPath $buildScript
if ($sourceMode) {
$binary = Join-Path $projectRoot 'bin\serviceport.exe'
$candidate = Join-Path $projectRoot 'bin\dev-build\serviceport.exe'
Write-Host 'Preparing latest serviceport (cached frontend and Go compilation)...'
$buildArguments = @('-NoProfile', '-ExecutionPolicy', 'Bypass', '-File', $buildScript, '-OutputPath', $candidate)
if (!$FullCheck) { $buildArguments += '-SkipTests' }
if ($RebuildWeb) { $buildArguments += '-RebuildWeb' }
& powershell.exe @buildArguments
if ($LASTEXITCODE -ne 0 -or -not (Test-Path -LiteralPath $candidate)) {
throw 'Build failed. The existing service and data have not been changed.'
}
if ($BuildOnly) {
Write-Host "Build-only complete: $candidate"
exit 0
}
} elseif ($BuildOnly -or $FullCheck -or $RebuildWeb) {
throw 'Build options require the source checkout, not an extracted release package.'
} elseif (-not (Test-Path -LiteralPath $binary)) {
throw 'serviceport.exe is missing. Extract the complete release ZIP again.'
}
$pathResolver = if ($sourceMode) { $candidate } else { $binary }
$dataPath = & $pathResolver data-path
if ($LASTEXITCODE -ne 0 -or $dataPath -isnot [string] -or ![IO.Path]::IsPathRooted($dataPath)) {
throw 'Cannot resolve the database path. The existing service and data have not been changed.'
}
$dataDirectory = Split-Path -Parent ([IO.Path]::GetFullPath($dataPath))
$backup = Join-Path $projectRoot ('bin\backups\' + (Get-Date -Format 'yyyyMMdd-HHmmss-fff'))
$dataPrefix = $dataDirectory.TrimEnd('\', '/') + [IO.Path]::DirectorySeparatorChar
if ([IO.Path]::GetFullPath($backup).StartsWith($dataPrefix, [StringComparison]::OrdinalIgnoreCase)) {
throw 'The data directory contains the backup destination. Use a separate data directory to avoid recursive backups.'
}
Write-Host 'Stopping previous serviceport service...'
$ownedPaths = @((Join-Path $projectRoot 'serviceport.exe'), (Join-Path $projectRoot 'bin\serviceport.exe'))
Get-CimInstance Win32_Process -Filter "Name = 'serviceport.exe'" | Where-Object { $_.ExecutablePath -in $ownedPaths } | ForEach-Object {
$previousProcess = Get-Process -Id $_.ProcessId -ErrorAction SilentlyContinue
if ($previousProcess) {
$previousProcess.Kill()
if (-not $previousProcess.WaitForExit(10000)) { throw 'Previous serviceport process did not exit.' }
}
}
if (Test-Path -LiteralPath $dataDirectory) {
$hashProvider = [Security.Cryptography.SHA256]::Create()
try {
$lockHash = ([BitConverter]::ToString($hashProvider.ComputeHash([Text.Encoding]::UTF8.GetBytes(([IO.Path]::GetFullPath($dataPath)).ToLowerInvariant())))).Replace('-', '').ToLowerInvariant()
} finally { $hashProvider.Dispose() }
$createdMutex = $false
$dataMutex = New-Object Threading.Mutex($false, "Local\serviceport-$lockHash", [ref]$createdMutex)
$dataLock = $null
try {
if (!$createdMutex) { throw 'The selected database is in use by another serviceport instance.' }
$legacyLock = Join-Path $dataDirectory 'desktop.lock'
if (Test-Path -LiteralPath $legacyLock) {
$dataLock = [IO.File]::Open($legacyLock, [IO.FileMode]::Open, [IO.FileAccess]::Read, [IO.FileShare]::Read)
}
New-Item -ItemType Directory -Path $backup -Force | Out-Null
Copy-Item -LiteralPath $dataDirectory -Destination $backup -Recurse
Write-Host "Data backup: $backup"
} finally {
if ($dataLock) { $dataLock.Dispose() }
$dataMutex.Dispose()
}
}
if ($sourceMode) {
New-Item -ItemType Directory -Force (Split-Path $binary -Parent) | Out-Null
Copy-Item -LiteralPath $candidate -Destination $binary -Force
}
$env:SERVICEPORT_AUTH_ENABLED = 'false'
$env:SERVICEPORT_ADMIN_USER = ''
$env:SERVICEPORT_ADMIN_PASSWORD = ''
if (!$env:SERVICEPORT_HTTP_ADDR) { $env:SERVICEPORT_HTTP_ADDR = '127.0.0.1:0' }
$env:SERVICEPORT_SECURE_COOKIE = 'false'
$env:SERVICEPORT_TLS_CERT = ''
$env:SERVICEPORT_TLS_KEY = ''
Write-Host "SQLite: $dataPath"
& $binary
exit $LASTEXITCODE