-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPublish-LogicMonitorModule.ps1
More file actions
146 lines (113 loc) · 5.13 KB
/
Publish-LogicMonitorModule.ps1
File metadata and controls
146 lines (113 loc) · 5.13 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
# Publish-LogicMonitorModule.ps1
# Script to prepare and publish the LogicMonitor PowerShell module
param(
[Parameter(Mandatory = $true)]
[string]$Version,
[string]$Repository = "PSGallery",
[string]$ApiKey,
[switch]$WhatIf,
[switch]$SkipBuild,
[switch]$LocalOnly
)
$ErrorActionPreference = "Stop"
Write-Information "Preparing LogicMonitor PowerShell Module v$Version for publishing..." -ForegroundColor Green
# Paths
$rootPath = $PSScriptRoot
$projectPath = Join-Path $rootPath "LogicMonitor.PowerShell"
$manifestPath = Join-Path $projectPath "LogicMonitor.psd1"
$publishPath = Join-Path $rootPath "publish"
$modulePublishPath = Join-Path $publishPath "LogicMonitor"
try {
# Step 1: Build the project
if (-not $SkipBuild) {
Write-Information "Building LogicMonitor.PowerShell project..." -ForegroundColor Yellow
Push-Location $rootPath
$buildResult = dotnet build "LogicMonitor.PowerShell/LogicMonitor.PowerShell.csproj" -c Release
Pop-Location
if ($LASTEXITCODE -ne 0) {
throw "Build failed with exit code $LASTEXITCODE"
}
Write-Information "Build completed successfully." -ForegroundColor Green
}
# Step 2: Update module version
Write-Information "Updating module manifest version to $Version..." -ForegroundColor Yellow
$manifestContent = Get-Content $manifestPath -Raw
$manifestContent = $manifestContent -replace "ModuleVersion = '[^']*'", "ModuleVersion = '$Version'"
Set-Content $manifestPath -Value $manifestContent -Encoding UTF8
# Step 3: Test module manifest
Write-Information "Testing module manifest..." -ForegroundColor Yellow
$testResult = Test-ModuleManifest -Path $manifestPath
if (-not $testResult) {
throw "Module manifest test failed"
}
Write-Information "Module manifest is valid." -ForegroundColor Green
# Step 4: Prepare publishing directory
Write-Information "Preparing module for publishing..." -ForegroundColor Yellow
if (Test-Path $publishPath) {
Remove-Item $publishPath -Recurse -Force
}
New-Item -ItemType Directory -Path $modulePublishPath -Force | Out-Null
# Copy built assemblies
$binPath = Join-Path $projectPath "bin\Release\net9.0"
if (-not (Test-Path $binPath)) {
throw "Build output not found at $binPath. Please build the project first."
}
Copy-Item (Join-Path $binPath "*") $modulePublishPath -Recurse -Force
# Copy module files
Copy-Item $manifestPath $modulePublishPath -Force
Copy-Item (Join-Path $projectPath "LogicMonitor.psm1") $modulePublishPath -Force
# Copy documentation if exists
$readmePath = Join-Path $projectPath "README.md"
if (Test-Path $readmePath) {
Copy-Item $readmePath $modulePublishPath -Force
}
# Step 5: Test the prepared module
Write-Information "Testing prepared module..." -ForegroundColor Yellow
$preparedManifestPath = Join-Path $modulePublishPath "LogicMonitor.psd1"
Test-ModuleManifest -Path $preparedManifestPath | Out-Null
# Import and test commands
Import-Module $preparedManifestPath -Force
$commands = Get-Command -Module LogicMonitor
Write-Information "Module loaded successfully with $($commands.Count) commands." -ForegroundColor Green
# Step 6: Create local package
$packagePath = Join-Path $publishPath "LogicMonitor-PowerShell-v$Version.zip"
Compress-Archive -Path (Join-Path $modulePublishPath "*") -DestinationPath $packagePath -Force
Write-Information "Created local package: $packagePath" -ForegroundColor Green
if ($LocalOnly) {
Write-Information "Local package created. Skipping repository publish as requested." -ForegroundColor Yellow
return
}
# Step 7: Publish to repository
if ($WhatIf) {
Write-Information "WhatIf: Would publish to repository '$Repository'" -ForegroundColor Cyan
Write-Information "Module Path: $modulePublishPath" -ForegroundColor Cyan
Write-Information "Version: $Version" -ForegroundColor Cyan
} else {
if ([string]::IsNullOrEmpty($ApiKey)) {
if ($Repository -eq "PSGallery") {
throw "API key is required for PowerShell Gallery. Get one from https://www.powershellgallery.com/account/apikeys"
} else {
Write-Warning "No API key provided. Attempting to publish without explicit key..."
}
}
Write-Information "Publishing module to repository '$Repository'..." -ForegroundColor Yellow
$publishParams = @{
Path = $modulePublishPath
Repository = $Repository
Verbose = $true
}
if (-not [string]::IsNullOrEmpty($ApiKey)) {
$publishParams.NuGetApiKey = $ApiKey
}
Publish-Module @publishParams
Write-Information "Module published successfully!" -ForegroundColor Green
}
} catch {
Write-Error "Publishing failed: $($_.Exception.Message)"
Write-Error $_.ScriptStackTrace
exit 1
} finally {
# Cleanup: Remove module from session
Remove-Module LogicMonitor -ErrorAction SilentlyContinue
}
Write-Information "Publishing process completed!" -ForegroundColor Green