Files

184 lines
5.9 KiB
PowerShell

[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[string]$InstallRoot,
[Parameter(Mandatory = $true)]
[string]$PythonExecutable,
[Parameter(Mandatory = $true)]
[string]$CompatibilityProfile,
[string]$SettingsRoot,
[switch]$TestMode,
[switch]$WhatIf
)
Set-StrictMode -Version Latest
$ErrorActionPreference = "Stop"
. (Join-Path $PSScriptRoot "PathSafety.ps1")
function Resolve-FullPath {
param([string]$Value)
return [IO.Path]::GetFullPath($Value)
}
function Resolve-ExistingFile {
param([string]$Value, [string]$Name)
$resolved = Resolve-FullPath $Value
Assert-DxNoReparsePath $resolved $Name
if (-not (Test-Path -LiteralPath $resolved -PathType Leaf)) {
throw "$Name does not exist: $resolved"
}
return $resolved
}
$toolHome = Resolve-FullPath $InstallRoot
Assert-DxNoReparsePath $toolHome "installation root"
if (-not (Test-Path -LiteralPath $toolHome -PathType Container)) {
throw "installation root does not exist: $toolHome"
}
$runtimeDirectory = Join-Path $toolHome "integration\dxdesigner795"
Assert-DxNoReparsePath $runtimeDirectory "runtime directory"
$requiredRuntimeFiles = @(
"PathSafety.ps1",
"PartReplacementContract.ps1",
"MenuAdd.vbs",
"MenuRuntime.vbs",
"PartReplaceMenuRuntime.vbs",
"BridgeCommon.vbs",
"PartReplaceProbe.vbs",
"PartReplaceBridge.vbs"
)
$missingRuntimeFiles = @()
$requiredRuntimePaths = @()
foreach ($runtimeName in $requiredRuntimeFiles) {
$runtimePath = Join-Path $runtimeDirectory $runtimeName
Assert-DxNoReparsePath $runtimePath "runtime file"
$requiredRuntimePaths += $runtimePath
if (-not (Test-Path -LiteralPath $runtimePath -PathType Leaf)) {
$missingRuntimeFiles += $runtimePath
}
}
if ($missingRuntimeFiles.Count -ne 0) {
throw "required runtime files are missing (including MenuRuntime.vbs): $($missingRuntimeFiles -join ', ')"
}
$python = Resolve-ExistingFile $PythonExecutable "Python executable"
$profileSource = Resolve-ExistingFile $CompatibilityProfile `
"compatibility profile"
if ($TestMode) {
if ($env:DX_PRODUCTIVITY_TEST_MODE -cne "1") {
throw "TestMode requires DX_PRODUCTIVITY_TEST_MODE=1"
}
if ([string]::IsNullOrWhiteSpace($SettingsRoot)) {
throw "TestMode requires an explicit SettingsRoot"
}
$settingsBase = Resolve-FullPath $SettingsRoot
Assert-DxNoReparsePath $settingsBase "TestMode settings root"
$systemTemp = (Resolve-FullPath ([IO.Path]::GetTempPath())).TrimEnd('\')
$tempPrefix = $systemTemp + "\"
if (-not $toolHome.StartsWith(
$tempPrefix,
[StringComparison]::OrdinalIgnoreCase
)) {
throw "TestMode InstallRoot must be under the system temp directory"
}
if (-not $settingsBase.StartsWith(
$tempPrefix,
[StringComparison]::OrdinalIgnoreCase
)) {
throw "TestMode SettingsRoot must be under the system temp directory"
}
}
else {
if (-not [string]::IsNullOrWhiteSpace($SettingsRoot)) {
throw "SettingsRoot is allowed only with TestMode"
}
$settingsBase = [Environment]::GetFolderPath(
[Environment+SpecialFolder]::LocalApplicationData
)
$settingsBase = Resolve-FullPath $settingsBase
Assert-DxNoReparsePath $settingsBase "settings root"
}
$settingsDirectory = Join-Path $settingsBase "DxProductivity"
$settingsPath = Join-Path $settingsDirectory "settings.json"
$runRoot = Join-Path $toolHome "runs"
$deployedProfile = Join-Path $runRoot "compatibility-profile.json"
Assert-DxNoReparsePath $settingsDirectory "settings directory"
Assert-DxNoReparsePath $settingsPath "settings path"
Assert-DxNoReparsePath $runRoot "run root"
Assert-DxNoReparsePath $deployedProfile "deployed compatibility profile"
Write-Output "InstallRoot=$toolHome"
Write-Output "PythonExecutable=$python"
Write-Output "ProfileSource=$profileSource"
Write-Output "CompatibilityProfile=$deployedProfile"
Write-Output "SettingsPath=$settingsPath"
Write-Output "RunRoot=$runRoot"
Write-Output "DXTOOLS_HOME=$toolHome"
if ($TestMode) {
Write-Output "TEST MODE: user-scope DXTOOLS_HOME is not written"
}
if ($WhatIf) {
exit 0
}
Assert-DxNoReparsePath $toolHome "installation root"
foreach ($runtimePath in $requiredRuntimePaths) {
Assert-DxNoReparsePath $runtimePath "runtime file"
}
Assert-DxNoReparsePath $python "Python executable"
Assert-DxNoReparsePath $profileSource "compatibility profile"
Assert-DxNoReparsePath $runRoot "run root"
Assert-DxNoReparsePath $deployedProfile "deployed compatibility profile"
Assert-DxNoReparsePath $settingsPath "settings path"
$pythonCheck = @(
"-c",
"import sys, dx_productivity; raise SystemExit(0 if sys.version_info[:2] == (3, 12) else 1)"
)
& $python @pythonCheck
if ($LASTEXITCODE -ne 0) {
throw "Python must import dx_productivity and be exactly 3.12: $python"
}
$profileDeploy = @(
"-m",
"dx_productivity",
"deploy-profile",
$profileSource,
$deployedProfile
)
& $python @profileDeploy
if ($LASTEXITCODE -ne 0) {
throw "compatibility profile deployment failed: $profileSource"
}
$utf8 = New-Object Text.UTF8Encoding($false)
$settingsData = [ordered]@{
compatibility_profile = $deployedProfile
dxtools_home = $toolHome
python_executable = $python
run_root = $runRoot
}
$settingsText = ($settingsData | ConvertTo-Json -Compress) + "`n"
Assert-DxNoReparsePath $settingsDirectory "settings directory"
New-Item -ItemType Directory -Force -Path $settingsDirectory | Out-Null
Assert-DxNoReparsePath $toolHome "installation root"
foreach ($runtimePath in $requiredRuntimePaths) {
Assert-DxNoReparsePath $runtimePath "runtime file"
}
Assert-DxNoReparsePath $deployedProfile "deployed compatibility profile"
Assert-DxNoReparsePath $settingsPath "settings path"
Write-DxBytesAtomic $settingsPath $utf8.GetBytes($settingsText)
if (-not $TestMode) {
[Environment]::SetEnvironmentVariable(
"DXTOOLS_HOME",
$toolHome,
[EnvironmentVariableTarget]::User
)
}