-
Notifications
You must be signed in to change notification settings - Fork 223
Expand file tree
/
Copy pathBuild-Module.ps1
More file actions
350 lines (272 loc) · 11.6 KB
/
Build-Module.ps1
File metadata and controls
350 lines (272 loc) · 11.6 KB
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
<#
.SYNOPSIS
Build script for Posh-SSH module.
.DESCRIPTION
This script:
1. Compiles the C# binary module (PoshSSH.dll) using MSBuild
2. Copies the netstandard2.0 version to the module directory
3. Creates a clean distribution package (ZIP file) without source code
4. Validates the module can be loaded
.PARAMETER Configuration
Build configuration (Debug or Release). Default: Release
.PARAMETER SkipBuild
Skip the compilation step and only create the package from existing binaries.
.PARAMETER SkipTests
Skip module loading tests.
.PARAMETER OutputPath
Path where the ZIP file will be created. Default: repository root
.EXAMPLE
.\Build-Module.ps1
.EXAMPLE
.\Build-Module.ps1 -Configuration Debug
.EXAMPLE
.\Build-Module.ps1 -SkipBuild -OutputPath "C:\Releases"
#>
[CmdletBinding()]
param(
[Parameter(Mandatory=$false)]
[ValidateSet('Debug', 'Release')]
[string]$Configuration = 'Release',
[Parameter(Mandatory=$false)]
[switch]$SkipBuild,
[Parameter(Mandatory=$false)]
[switch]$SkipTests,
[Parameter(Mandatory=$false)]
[string]$OutputPath
)
$ErrorActionPreference = 'Stop'
#region Helper Functions
function Write-Step {
param([string]$Message)
Write-Host "`n===================================================" -ForegroundColor Cyan
Write-Host $Message -ForegroundColor Cyan
Write-Host "===================================================`n" -ForegroundColor Cyan
}
function Write-Success {
param([string]$Message)
Write-Host "[OK] $Message" -ForegroundColor Green
}
function Write-Info {
param([string]$Message)
Write-Host "[INFO] $Message" -ForegroundColor Yellow
}
function Write-ErrorMessage {
param([string]$Message)
Write-Host "[ERROR] $Message" -ForegroundColor Red
}
function Find-MSBuild {
# Try to find MSBuild in common locations
$msbuildPaths = @(
"C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Current\Bin\MSBuild.exe",
"C:\Program Files\Microsoft Visual Studio\2022\Professional\MSBuild\Current\Bin\MSBuild.exe",
"C:\Program Files\Microsoft Visual Studio\2022\Enterprise\MSBuild\Current\Bin\MSBuild.exe",
"C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\MSBuild.exe",
"C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\MSBuild\Current\Bin\MSBuild.exe",
"C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Current\Bin\MSBuild.exe",
"C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\MSBuild\Current\Bin\MSBuild.exe"
)
foreach ($path in $msbuildPaths) {
if (Test-Path $path) {
return $path
}
}
# Try to find via vswhere
$vswhere = "${env:ProgramFiles(x86)}\Microsoft Visual Studio\Installer\vswhere.exe"
if (Test-Path $vswhere) {
$vsPath = & $vswhere -latest -products * -requires Microsoft.Component.MSBuild -property installationPath
if ($vsPath) {
$msbuildPath = Join-Path $vsPath "MSBuild\Current\Bin\MSBuild.exe"
if (Test-Path $msbuildPath) {
return $msbuildPath
}
}
}
throw "MSBuild not found. Please install Visual Studio or Visual Studio Build Tools."
}
#endregion
#region Main Script
try {
$scriptRoot = $PSScriptRoot
$sourceDir = Join-Path $scriptRoot "Source\PoshSSH"
$moduleDir = Join-Path $scriptRoot "Posh-SSH"
$solutionFile = Join-Path $sourceDir "PoshSSH.sln"
$coreProjectDir = Join-Path $sourceDir "PoshSSH.Core"
$netStandardDll = Join-Path $coreProjectDir "bin\$Configuration\netstandard2.0\PoshSSH.dll"
$targetDll = Join-Path $moduleDir "PoshSSH.dll"
# Set output path
if (-not $OutputPath) {
$OutputPath = $scriptRoot
}
# Read version from manifest
$manifestPath = Join-Path $moduleDir "Posh-SSH.psd1"
$manifest = Import-PowerShellDataFile -Path $manifestPath
$version = $manifest.ModuleVersion
Write-Host "`nPosh-SSH Build Script" -ForegroundColor Magenta
Write-Host "Version: $version" -ForegroundColor Magenta
Write-Host "Configuration: $Configuration" -ForegroundColor Magenta
Write-Host "Repository: $scriptRoot`n" -ForegroundColor Magenta
#region Build Binary Module
if (-not $SkipBuild) {
Write-Step "Step 1: Compiling C# Binary Module"
# Find MSBuild
Write-Info "Locating MSBuild..."
$msbuildPath = Find-MSBuild
Write-Success "Found MSBuild: $msbuildPath"
# Check if solution exists
if (-not (Test-Path $solutionFile)) {
throw "Solution file not found: $solutionFile"
}
# Clean previous builds
Write-Info "Cleaning previous builds..."
& $msbuildPath $solutionFile /t:Clean /p:Configuration=$Configuration /v:minimal /nologo
if ($LASTEXITCODE -ne 0) {
throw "Clean failed with exit code $LASTEXITCODE"
}
# Build solution
Write-Info "Building solution ($Configuration)..."
& $msbuildPath $solutionFile /t:Build /p:Configuration=$Configuration /v:minimal /nologo
if ($LASTEXITCODE -ne 0) {
throw "Build failed with exit code $LASTEXITCODE"
}
# Verify DLL was created
if (-not (Test-Path $netStandardDll)) {
throw "Build succeeded but DLL not found: $netStandardDll"
}
$dllSize = (Get-Item $netStandardDll).Length
Write-Success "Build completed successfully (DLL size: $([math]::Round($dllSize/1KB, 2)) KB)"
# Copy DLL to module directory
Write-Info "Copying netstandard2.0 DLL to module directory..."
Copy-Item -Path $netStandardDll -Destination $targetDll -Force
Write-Success "DLL copied to: $targetDll"
# Copy dependencies to Assembly directory to ensure version compatibility
Write-Info "Copying dependencies to Assembly directory..."
$buildOutputDir = Join-Path $coreProjectDir "bin\$Configuration\netstandard2.0"
$assemblyDir = Join-Path $moduleDir "Assembly"
# Ensure Assembly directory exists
if (-not (Test-Path $assemblyDir)) {
New-Item -ItemType Directory -Path $assemblyDir -Force | Out-Null
}
# Copy dependency DLLs (exclude PoshSSH.dll as it goes to module root)
$dependencyFiles = Get-ChildItem -Path $buildOutputDir -Filter "*.dll" |
Where-Object { $_.Name -ne "PoshSSH.dll" -and $_.Name -ne "System.Management.Automation.dll" }
foreach ($file in $dependencyFiles) {
Copy-Item -Path $file.FullName -Destination $assemblyDir -Force
Write-Info " Copied: $($file.Name)"
}
Write-Success "Dependencies synchronized with build output"
}
else {
Write-Step "Step 1: Skipping Build (using existing binaries)"
if (-not (Test-Path $targetDll)) {
throw "Module DLL not found: $targetDll. Cannot skip build."
}
Write-Success "Using existing DLL: $targetDll"
}
#endregion
#region Test Module Loading
if (-not $SkipTests) {
Write-Step "Step 2: Testing Module Loading"
Write-Info "Removing any loaded Posh-SSH module..."
Remove-Module -Name Posh-SSH -ErrorAction SilentlyContinue
Write-Info "Importing module..."
Import-Module $manifestPath -Force -ErrorAction Stop
$loadedModule = Get-Module -Name Posh-SSH
if (-not $loadedModule) {
throw "Module failed to load"
}
Write-Success "Module loaded: Version $($loadedModule.Version)"
# Verify cmdlets
$cmdlets = Get-Command -Module Posh-SSH -CommandType Cmdlet
Write-Success "Binary cmdlets loaded: $($cmdlets.Count)"
$functions = Get-Command -Module Posh-SSH -CommandType Function
Write-Success "PowerShell functions loaded: $($functions.Count)"
# Verify key cmdlets exist
$keyCmdlets = @('New-SSHSession', 'New-SFTPSession', 'Get-SCPItem', 'Set-SCPItem')
foreach ($cmdlet in $keyCmdlets) {
$cmd = Get-Command -Name $cmdlet -ErrorAction SilentlyContinue
if (-not $cmd) {
throw "Key cmdlet not found: $cmdlet"
}
}
Write-Success "All key cmdlets verified"
# Clean up
Remove-Module -Name Posh-SSH -ErrorAction SilentlyContinue
}
else {
Write-Step "Step 2: Skipping Module Tests"
}
#endregion
#region Create Distribution Package
Write-Step "Step 3: Creating Distribution Package"
# Create temporary directory for clean module
$tempDir = Join-Path $env:TEMP "Posh-SSH-Build-$([guid]::NewGuid().ToString('N').Substring(0,8))"
$tempModuleDir = Join-Path $tempDir "Posh-SSH"
Write-Info "Creating temporary directory: $tempDir"
New-Item -ItemType Directory -Path $tempModuleDir -Force | Out-Null
# Copy module files (excluding source code, tests, etc.)
Write-Info "Copying module files..."
$itemsToCopy = @(
@{Path = "$moduleDir\*.psd1"; Destination = $tempModuleDir},
@{Path = "$moduleDir\*.psm1"; Destination = $tempModuleDir},
@{Path = "$moduleDir\*.ps1"; Destination = $tempModuleDir},
@{Path = "$moduleDir\*.dll"; Destination = $tempModuleDir},
@{Path = "$moduleDir\Assembly"; Destination = "$tempModuleDir\Assembly"},
@{Path = "$moduleDir\en-US"; Destination = "$tempModuleDir\en-US"},
@{Path = "$moduleDir\Format"; Destination = "$tempModuleDir\Format"}
)
foreach ($item in $itemsToCopy) {
if (Test-Path $item.Path) {
Copy-Item -Path $item.Path -Destination $item.Destination -Recurse -Force
Write-Info " Copied: $(Split-Path $item.Path -Leaf)"
}
}
# Copy root documentation files
$docFiles = @("README.md", "License.md", "CHANGELOG.md")
foreach ($doc in $docFiles) {
$docPath = Join-Path $scriptRoot $doc
if (Test-Path $docPath) {
Copy-Item -Path $docPath -Destination $tempDir -Force
Write-Info " Copied: $doc"
}
}
Write-Success "Module files copied"
# Create ZIP file
$zipFileName = "Posh-SSH-$version.zip"
$zipPath = Join-Path $OutputPath $zipFileName
# Remove existing ZIP if it exists
if (Test-Path $zipPath) {
Write-Info "Removing existing ZIP file..."
Remove-Item -Path $zipPath -Force
}
Write-Info "Creating ZIP archive..."
Compress-Archive -Path "$tempDir\*" -DestinationPath $zipPath -CompressionLevel Optimal -Force
$zipSize = (Get-Item $zipPath).Length
Write-Success "ZIP created: $zipPath ($([math]::Round($zipSize/1MB, 2)) MB)"
# Clean up temporary directory
Write-Info "Cleaning up temporary files..."
Remove-Item -Path $tempDir -Recurse -Force
#endregion
#region Summary
Write-Step "Build Complete!"
Write-Host "Summary:" -ForegroundColor White
Write-Host " Version: $version" -ForegroundColor White
Write-Host " Configuration: $Configuration" -ForegroundColor White
Write-Host " Package: $zipPath" -ForegroundColor White
Write-Host " Package Size: $([math]::Round($zipSize/1MB, 2)) MB" -ForegroundColor White
Write-Host ""
# Calculate hash for verification
Write-Info "Calculating package hash..."
$hash = Get-FileHash -Path $zipPath -Algorithm SHA256
Write-Host " SHA256: $($hash.Hash)" -ForegroundColor Gray
Write-Host ""
Write-Success "Build completed successfully!"
#endregion
}
catch {
Write-Host ""
Write-ErrorMessage "Build failed: $_"
Write-Host ""
Write-Host $_.ScriptStackTrace -ForegroundColor Red
exit 1
}
#endregion