File size: 7,687 Bytes
598838a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<#
    build.ps1 - Standalone, from-scratch build script for Logbert.

    Logbert is a .NET Framework 4.8 WinForms desktop application (log viewer for
    log4net / log4j / NLog / syslog). The solution contains three managed
    projects that build with MSBuild:

        - Couchcoding.Logbert.Theme  (class library)
        - Couchcoding.Logbert.Gui    (class library)
        - Logbert                    (WinExe - the GUI application)

    All third-party dependencies (MoonSharp, ScintillaNET, WeifenLuo docking)
    ship as pre-built DLLs checked into src\Logbert\Libraries, so there is no
    NuGet / package restore step. The only build requirements are:

        - MSBuild (Visual Studio Build Tools 2022, MSVC/.NET desktop workload)
        - .NET Framework 4.8 targeting pack

    Both are part of the "Visual Studio Build Tools 2022 with .NET desktop
    workload" that is pre-installed on the build machine. If MSBuild cannot be
    located the script will attempt to install the Build Tools via Chocolatey.

    The script builds Release|x64 and copies the resulting binaries to the
    install directory.
#>

[CmdletBinding()]
param(
    [string]$Configuration = 'Release',
    [string]$Platform      = 'x64'
)

$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

# ---------------------------------------------------------------------------
# Paths
# ---------------------------------------------------------------------------
$RepoRoot    = (Join-Path (Split-Path $PSScriptRoot -Parent) 'repo')
$SrcDir      = Join-Path $RepoRoot 'src'
$ProjectFile = Join-Path $SrcDir 'Logbert\Logbert.csproj'
$InstallDir  = (Join-Path (Split-Path $PSScriptRoot -Parent) 'install')

Write-Output "=== Logbert build starting ($Configuration|$Platform) ==="
Write-Output "Repo root  : $RepoRoot"
Write-Output "Project    : $ProjectFile"
Write-Output "Install to : $InstallDir"

if (-not (Test-Path $ProjectFile)) {
    throw "Project file not found: $ProjectFile"
}

# ---------------------------------------------------------------------------
# Locate MSBuild (via vswhere). Install VS Build Tools if it is missing.
# ---------------------------------------------------------------------------
function Find-MSBuild {
    $vswhere = Join-Path ${env:ProgramFiles(x86)} 'Microsoft Visual Studio\Installer\vswhere.exe'
    if (Test-Path $vswhere) {
        $msb = & $vswhere -latest -products * `
            -requires Microsoft.Component.MSBuild `
            -find 'MSBuild\**\Bin\MSBuild.exe' | Select-Object -First 1
        if ($msb -and (Test-Path $msb)) { return $msb }
    }

    # Fallback: probe well-known install locations.
    $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',
        'C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Current\Bin\MSBuild.exe',
        'C:\Program Files\Microsoft Visual Studio\2022\Enterprise\MSBuild\Current\Bin\MSBuild.exe',
        'C:\Program Files\Microsoft Visual Studio\2022\Professional\MSBuild\Current\Bin\MSBuild.exe'
    )
    foreach ($c in $candidates) {
        if (Test-Path $c) { return $c }
    }
    return $null
}

$msbuild = Find-MSBuild

if (-not $msbuild) {
    Write-Output "MSBuild not found - installing Visual Studio Build Tools 2022 via Chocolatey ..."
    choco install -y visualstudio2022buildtools `
        --package-parameters "--add Microsoft.VisualStudio.Workload.ManagedDesktopBuildTools --add Microsoft.VisualStudio.Workload.NetCoreBuildTools --includeRecommended --quiet --norestart"
    choco install -y netfx-4.8-devpack

    $msbuild = Find-MSBuild
    if (-not $msbuild) {
        throw "MSBuild could not be located even after installing the Build Tools."
    }
}

Write-Output "Using MSBuild: $msbuild"
& $msbuild -version

# ---------------------------------------------------------------------------
# Build (Rebuild = clean + build) the GUI project. Project references pull in
# the Gui and Theme class libraries automatically.
# ---------------------------------------------------------------------------
Write-Output "=== Building ==="
& $msbuild $ProjectFile `
    "/t:Rebuild" `
    "/p:Configuration=$Configuration" `
    "/p:Platform=$Platform" `
    "/m" `
    "/v:minimal" `
    "/nologo"

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

# ---------------------------------------------------------------------------
# Install: copy the build output to the install directory.
# ---------------------------------------------------------------------------
$OutputDir = Join-Path $SrcDir "Logbert\bin\$Platform\$Configuration"
if (-not (Test-Path (Join-Path $OutputDir 'Logbert.exe'))) {
    throw "Build output not found at: $OutputDir\Logbert.exe"
}

Write-Output "=== Installing to $InstallDir ==="
New-Item -ItemType Directory -Force -Path $InstallDir | Out-Null
# Clean any stale contents so the install directory always mirrors the build.
Get-ChildItem -Path $InstallDir -Force -ErrorAction SilentlyContinue | Remove-Item -Recurse -Force -ErrorAction SilentlyContinue
Copy-Item -Path (Join-Path $OutputDir '*') -Destination $InstallDir -Recurse -Force

# ---------------------------------------------------------------------------
# Harden the installed configuration for automated (re)launching.
#
# Logbert enforces a single running instance via a machine-global named pipe
# (see Program.cs / FrmMainAllowOnlyOneInstance). When the app is relaunched
# while a previous instance is still alive, the new process detects the pipe,
# hands off to the existing instance and exits immediately. That breaks a test
# harness that starts the app multiple times. Disable the single-instance
# guard in the installed config so every launch spins up its own GUI.
# Also suppress the "check for update on startup" network call and the welcome
# page so the app comes up clean and self-contained.
# ---------------------------------------------------------------------------
$cfgPath = Join-Path $InstallDir 'Logbert.exe.config'
if (Test-Path $cfgPath) {
    Write-Output "=== Patching $cfgPath (disable single-instance guard) ==="
    [xml]$cfg = Get-Content -LiteralPath $cfgPath
    $overrides = @{
        'FrmMainAllowOnlyOneInstance'    = 'False'
        'FrmMainCheckForUpdateOnStartup' = 'False'
    }
    foreach ($node in $cfg.SelectNodes('//setting')) {
        if ($overrides.ContainsKey($node.name)) {
            $node.value = $overrides[$node.name]
        }
    }
    $cfg.Save($cfgPath)
}

$exePath = Join-Path $InstallDir 'Logbert.exe'
if (-not (Test-Path $exePath)) {
    throw "Install failed - $exePath does not exist."
}

Write-Output "=== Build complete ==="
Write-Output "Executable: $exePath"
Get-ChildItem $InstallDir | Select-Object Name, Length | Format-Table -AutoSize