File size: 6,732 Bytes
f7e6f7b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<#
    build.ps1 - Self-contained build script for TinyNotepad (MyNotepad.exe)

    TinyNotepad is a .NET Framework WinForms desktop application (C#).
    The project file targets .NET Framework v4.6.1, but this build machine only
    ships the v4.7.2 / v4.8 / v4.8.1 reference-assembly (targeting) packs - the
    v4.6.1 pack contains only XML docs, no reference DLLs. .NET Framework is
    backward compatible, so we build against an available targeting pack
    (preferring v4.8) via the TargetFrameworkVersion MSBuild override. No source
    changes are required.

    The app has no NuGet packages - only standard framework references.

    Output: <task-root>\install\MyNotepad.exe
#>

$ErrorActionPreference = 'Stop'
# RB unified-0827: build_result.json is portable on OSS; materialize its path for this VM run.
$__rbBuildResultPath = Join-Path $PSScriptRoot 'build_result.json'
if (Test-Path $__rbBuildResultPath) {
    $__rbBuildResult = Get-Content $__rbBuildResultPath -Raw -Encoding UTF8 | ConvertFrom-Json
    if ($__rbBuildResult.executable -and -not [System.IO.Path]::IsPathRooted([string]$__rbBuildResult.executable)) {
        $__rbBuildResult.executable = [System.IO.Path]::GetFullPath((Join-Path $PSScriptRoot ([string]$__rbBuildResult.executable)))
        $__rbBuildResult | ConvertTo-Json -Depth 10 | Set-Content $__rbBuildResultPath -Encoding UTF8
    }
}

Set-StrictMode -Version Latest

$RepoRoot    = (Join-Path (Split-Path $PSScriptRoot -Parent) 'repo')
$SolutionDir = Join-Path $RepoRoot 'TinyNotepad'
$Solution    = Join-Path $SolutionDir 'TinyNotepad.sln'
$InstallDir  = (Join-Path (Split-Path $PSScriptRoot -Parent) 'install')

Write-Output "==> TinyNotepad build starting"
Write-Output "    Repo:     $RepoRoot"
Write-Output "    Solution: $Solution"
Write-Output "    Install:  $InstallDir"

# ---------------------------------------------------------------------------
# 1. Locate MSBuild (Visual Studio Build Tools 2022) via vswhere.
# ---------------------------------------------------------------------------
$vswhere = Join-Path ${env:ProgramFiles(x86)} 'Microsoft Visual Studio\Installer\vswhere.exe'
$msbuild = $null
if (Test-Path $vswhere) {
    $msbuild = & $vswhere -latest -products '*' -requires Microsoft.Component.MSBuild `
                          -find 'MSBuild\**\Bin\MSBuild.exe' | Select-Object -First 1
}
if (-not $msbuild -or -not (Test-Path $msbuild)) {
    # Fallback to well-known Build Tools 2022 location.
    $candidates = @(
        'C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\MSBuild\Current\Bin\MSBuild.exe',
        'C:\Program Files\Microsoft Visual Studio\2022\BuildTools\MSBuild\Current\Bin\MSBuild.exe',
        'C:\Program Files (x86)\Microsoft Visual Studio\2022\Community\MSBuild\Current\Bin\MSBuild.exe'
    )
    $msbuild = $candidates | Where-Object { Test-Path $_ } | Select-Object -First 1
}
if (-not $msbuild -or -not (Test-Path $msbuild)) {
    throw "MSBuild.exe could not be located. Ensure Visual Studio Build Tools 2022 is installed."
}
Write-Output "==> Using MSBuild: $msbuild"

# ---------------------------------------------------------------------------
# 2. Pick an available .NET Framework targeting pack (reference assemblies).
#    Prefer v4.8; fall back to any pack that actually contains reference DLLs.
#    If none is found, install the .NET 4.8 developer pack machine-wide via
#    Chocolatey (this is normally already present on the build image).
# ---------------------------------------------------------------------------
$refRoot = Join-Path ${env:ProgramFiles(x86)} 'Reference Assemblies\Microsoft\Framework\.NETFramework'

function Get-AvailableTargetFramework {
    param([string]$Root)
    if (-not (Test-Path $Root)) { return $null }
    # Preference order (highest/most-compatible first).
    $preferred = @('v4.8','v4.8.1','v4.7.2','v4.7.1','v4.7','v4.6.2','v4.6.1')
    foreach ($v in $preferred) {
        $dir = Join-Path $Root $v
        if ((Test-Path $dir) -and (Get-ChildItem -Path $dir -Filter '*.dll' -ErrorAction SilentlyContinue | Select-Object -First 1)) {
            return $v
        }
    }
    # Otherwise take any vX.Y folder that has reference DLLs.
    $any = Get-ChildItem -Path $Root -Directory -ErrorAction SilentlyContinue |
           Where-Object { $_.Name -match '^v4\.' } |
           Where-Object { Get-ChildItem -Path $_.FullName -Filter '*.dll' -ErrorAction SilentlyContinue | Select-Object -First 1 } |
           Sort-Object Name -Descending |
           Select-Object -First 1
    if ($any) { return $any.Name }
    return $null
}

$targetFramework = Get-AvailableTargetFramework -Root $refRoot
if (-not $targetFramework) {
    Write-Output "==> No .NET Framework targeting pack with reference assemblies found; installing netfx-4.8-devpack via Chocolatey (machine-wide)."
    if (Get-Command choco -ErrorAction SilentlyContinue) {
        & choco install -y netfx-4.8-devpack | Out-Host
    } else {
        throw "Chocolatey not available to install the .NET 4.8 developer pack."
    }
    $targetFramework = Get-AvailableTargetFramework -Root $refRoot
    if (-not $targetFramework) {
        throw "Still no .NET Framework targeting pack available after attempting install."
    }
}
Write-Output "==> Building against TargetFrameworkVersion=$targetFramework"

# ---------------------------------------------------------------------------
# 3. Prepare a clean install directory.
# ---------------------------------------------------------------------------
if (Test-Path $InstallDir) {
    Remove-Item -Path (Join-Path $InstallDir '*') -Recurse -Force -ErrorAction SilentlyContinue
} else {
    New-Item -ItemType Directory -Path $InstallDir -Force | Out-Null
}

# ---------------------------------------------------------------------------
# 4. Build in Release mode, emitting directly into the install directory.
#    OutputPath is set to the install dir so the compiled exe lands there.
# ---------------------------------------------------------------------------
& $msbuild $Solution `
    /t:Rebuild `
    /p:Configuration=Release `
    /p:Platform="Any CPU" `
    /p:TargetFrameworkVersion=$targetFramework `
    /p:OutputPath="$InstallDir" `
    /nologo /m /v:minimal

if ($LASTEXITCODE -ne 0) {
    throw "MSBuild failed with exit code $LASTEXITCODE."
}

# ---------------------------------------------------------------------------
# 5. Verify the executable exists.
# ---------------------------------------------------------------------------
$exe = Join-Path $InstallDir 'MyNotepad.exe'
if (-not (Test-Path $exe)) {
    throw "Build reported success but $exe was not produced."
}

Write-Output "==> BUILD SUCCEEDED"
Write-Output "    Executable: $exe"