-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBuild.ps1
More file actions
194 lines (160 loc) · 6.72 KB
/
Build.ps1
File metadata and controls
194 lines (160 loc) · 6.72 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
[CmdletBinding()]
param (
[Parameter()]
[switch]
$Local,
[Parameter()]
[switch]
$Package,
[Parameter()]
[Int16]
$majorVersion = 0,
[Parameter()]
[Int16]
$minorVersion = 26,
[Parameter()]
[Int16]
$patchVersion = 0,
[Parameter()]
[switch]
$Analyze,
[Parameter()]
[string]
$NuGetApiKey = ''
)
Begin {
Write-Debug "DEBUG MODE ENABLED"
# Ensure you have the NuGet module installed if not install it
if (-not (Get-Module -Name NuGet -ListAvailable)) {
Install-Module -Name NuGet -Force -SkipPublisherCheck
}
# Set the repository name for local testing vs. publishing to the PSGallery
if ($NuGetApiKey) {
$repoName = "PSGallery"
}
else {
$repoName = "AzKubeRepo"
$NuGetApiKey = 'PLACEHOLDER'
}
}
Process {
# Navigate to the src/AzKube directory
Push-Location $PSScriptRoot
Push-Location src/AzKube
# Increment the module version to the format yymmdd.hhmm.ss
# if ($Package) {
$moduleVersion = $("$majorVersion.$minorVersion.$patchVersion")
# }
# else {
# $moduleVersion = $(Get-Date).ToString('yyMMdd.HHmm.ss')
# }
# $moduleVersion = $("$majorVersion.$minorVersion.$patchVersion+$((Get-Date).ToString('yyMMdd.HHmm.ss'))")
Write-Host "Setting module version to $moduleVersion"
# Create the module manifest
Update-ModuleManifest -Path ./AzKube.psd1 -RootModule ./AzKube.psm1 `
-Author 'David Mealo' `
-Copyright '(c) David Mealo. All rights reserved.' `
-CompanyName 'dmealo' `
-Description 'PowerShell module to simplify the management and automation of Azure Kubernetes Service (AKS) clusters. Batch creates kubectl connections including optional proxy, tests them, and more. See Project Site at https://github.com/dmealo/AzKube for more details, support, or to contribute.' `
-ModuleVersion $moduleVersion `
-ProjectUri 'https://github.com/dmealo/AzKube' `
-HelpInfoUri 'https://github.com/dmealo/AzKube/README.md'
# -RequiredModules @('Az', 'PSMenu')
# Test the module manifest
Test-ModuleManifest -Path ./AzKube.psd1
if ($Package) {
if ($Local) {
### Set up project /NuGetRepo folder as a NuGet repository
# Create the NuGet repository folder path (ensure it's correct)
$repoPath = Join-Path (Get-Location) "NuGetRepo"
if (-not (Test-Path $repoPath)) {
Write-Debug "Creating NuGet repository directory at $repoPath"
New-Item -Path $repoPath -ItemType Directory | Out-Null
}
# If the module is already installed, uninstall it
Write-Debug "Checking for existing module installation"
if (Get-Module -Name AzKube -ListAvailable) {
# Remove the module
Remove-Module -Name AzKube -Force
# Verify the module is removed
Get-Module -Name AzKube
# Uninstall the module
Write-Debug "Uninstalling module"
Uninstall-Module -Name AzKube -Force -AllVersions
}
# If the NuGet repository exists, remove it
Write-Debug "Checking for existing NuGet repository"
if (Get-PSRepository -Name $repoName -ErrorAction SilentlyContinue) {
Write-Debug "Removing existing NuGet repository to recreate it"
Unregister-PSRepository -Name $repoName -ErrorAction SilentlyContinue
}
# If the module is already in the NuGet repository, remove it
Write-Debug "Checking for existing module in the NuGet repository"
# Delete the module from the NuGet repository file system
$modulePath = Join-Path $repoPath "AzKube.$moduleVersion.nupkg"
if (Test-Path $modulePath) {
Write-Debug "Removing existing module from the NuGet repository"
Remove-Item -Path $modulePath -Force
}
# Remove the module from the PSModulePath
$path = (Resolve-Path '.').Path
if ($env:PSModulePath -like "*$path*") {
$env:PSModulePath = $env:PSModulePath -replace ";$path", ""
}
# Register the NuGet repository with a proper name
if (-not (Get-PSRepository -Name $repoName -ErrorAction SilentlyContinue)) {
Write-Debug "Registering NuGet repository '$repoName' with source location $repoPath"
Register-PSRepository -Name $repoName -SourceLocation $repoPath -InstallationPolicy Trusted | Out-Null
}
}
# Package the module using the module manifest, Publish-Module
Write-Debug "Packaging the module"
Publish-Module -Path (Get-Location) -NuGetApiKey $NuGetApiKey -Repository $repoName -Force -Verbose
# Install the module from the NuGet repository
Write-Debug "Installing the module from the NuGet repository"
Install-Module -Name AzKube -Repository $repoName -Force -ErrorAction Continue
if ($Local) {
# If already imported, remove the module, then import it
Write-Debug "Checking for existing module installation"
if (Get-Module -Name AzKube) {
Write-Debug "Removing existing module installation"
Remove-Module -Name AzKube -Force
# Verify the module is removed
Write-Debug "Verifying the module is removed"
Get-Module -Name AzKube
}
}
# Import the module
Write-Debug "Importing the module"
Import-Module AzKube -Force
# Verify the module is imported
Write-Debug "Verifying the module is imported"
Get-Module -Name AzKube
}
else {
$path = (Resolve-Path '.').Path
if ($env:PSModulePath -notlike "*$path*") {
$env:PSModulePath += ";$path"
}
Write-Host "Module path: $path"
Import-Module "$path\AzKube.psm1" -Force
}
# Verify the module is imported
Get-Module -Name AzKube
# Navigate back to the root directory
Pop-Location
if ($Analyze) {
# Ensure you have the PSScriptAnalyzer module installed if not install it
if (-not (Get-Module -Name PSScriptAnalyzer -ListAvailable)) {
Write-Host "Installing PSScriptAnalyzer module"
Install-Module -Name PSScriptAnalyzer -Force
}
# Analyze the module
$analysis = Invoke-ScriptAnalyzer -Path ./src/AzKube/AzKube.psm1 -Recurse -Severity Warning -Settings PSGallery
$analysis
}
# # Run Pester tests
# Invoke-Pester -Path ./tests/AzKube/private/Aks-Utilities.Tests.ps1
}
End {
}