Skip to content

Commit 656edb7

Browse files
committed
Cleaned up build scripts a bit
1 parent ecfd169 commit 656edb7

2 files changed

Lines changed: 78 additions & 102 deletions

File tree

buildtools.psm1

Lines changed: 38 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,15 @@ function Get-BuildConfiguration {
1515

1616
if (Test-Path $resolvedPath -PathType Container) {
1717
$fileNamePath = Join-Path -Path $resolvedPath -ChildPath $ConfigurationFileName
18-
}
19-
else {
18+
} else {
2019
$fileName = Split-Path -Path $resolvedPath -Leaf
2120
if ($fileName -ne $ConfigurationFileName) {
2221
throw "$ConfigurationFileName not found in provided pathname: $resolvedPath"
2322
}
2423
$fileNamePath = $resolvedPath
2524
}
2625

27-
if (! (Test-Path -Path $fileNamePath -PathType Leaf)) {
26+
if (-not (Test-Path -Path $fileNamePath -PathType Leaf)) {
2827
throw "$ConfigurationFileName not found at path: $resolvedPath"
2928
}
3029

@@ -37,8 +36,7 @@ function Get-BuildConfiguration {
3736
$configObj.BuildOutputPath = Join-Path $projectRoot -ChildPath $configObj.BuildOutputPath
3837
if ($configObj.SignedOutputPath) {
3938
$configObj.SignedOutputPath = Join-Path $projectRoot -ChildPath $configObj.SignedOutputPath
40-
}
41-
else {
39+
} else {
4240
$configObj | Add-Member -MemberType NoteProperty -Name SignedOutputPath -Value (Join-Path $projectRoot -ChildPath 'signed')
4341
}
4442

@@ -48,26 +46,25 @@ function Get-BuildConfiguration {
4846
function Invoke-ModuleBuild {
4947
[CmdletBinding()]
5048
param (
51-
[Parameter(Mandatory=$true)]
49+
[Parameter(Mandatory = $true)]
5250
[ScriptBlock] $BuildScript
5351
)
5452

55-
Write-Verbose -Verbose -Message "Invoking build script"
53+
Write-Verbose -Verbose -Message 'Invoking build script'
5654

5755
$BuildScript.Invoke()
5856

59-
Write-Verbose -Verbose -Message "Finished invoking build script"
57+
Write-Verbose -Verbose -Message 'Finished invoking build script'
6058
}
6159

62-
function Publish-ModulePackage
63-
{
60+
function Publish-ModulePackage {
6461
[CmdletBinding()]
6562
param (
6663
[Parameter()]
6764
[Switch] $Signed
6865
)
6966

70-
Write-Verbose -Verbose -Message "Creating new local package repo"
67+
Write-Verbose -Verbose -Message 'Creating new local package repo'
7168
$localRepoName = 'packagebuild-local-repo'
7269
$localRepoLocation = Join-Path -Path ([System.io.path]::GetTempPath()) -ChildPath $localRepoName
7370
if (Test-Path -Path $localRepoLocation) {
@@ -80,16 +77,16 @@ function Publish-ModulePackage
8077

8178
Write-Verbose -Verbose -Message "Publishing package to local repo: $localRepoName"
8279
$config = Get-BuildConfiguration
83-
if (! $Signed.IsPresent) {
80+
if (-not $Signed.IsPresent) {
8481
$modulePath = Join-Path -Path $config.BuildOutputPath -ChildPath $config.ModuleName
8582
} else {
8683
$modulePath = Join-Path -Path $config.SignedOutputPath -ChildPath $config.ModuleName
8784
}
8885
Publish-PSResource -Path $modulePath -Repository $localRepoName -SkipDependenciesCheck -Confirm:$false -Verbose
8986

9087
if ($env:TF_BUILD) {
91-
Write-Verbose -Verbose -Message "Uploading module nuget package artifact to AzDevOps"
92-
$artifactName = "nupkg"
88+
Write-Verbose -Verbose -Message 'Uploading module nuget package artifact to AzDevOps'
89+
$artifactName = 'nupkg'
9390
$artifactPath = (Get-ChildItem -Path $localRepoLocation -Filter "$($config.ModuleName)*.nupkg").FullName
9491
$artifactPath = Resolve-Path -Path $artifactPath
9592
Write-Host "##vso[artifact.upload containerfolder=$artifactName;artifactname=$artifactName;]$artifactPath"
@@ -102,36 +99,36 @@ function Publish-ModulePackage
10299
function Install-ModulePackageForTest {
103100
[CmdletBinding()]
104101
param (
105-
[Parameter(Mandatory=$true)]
102+
[Parameter(Mandatory = $true)]
106103
[string] $PackagePath
107104
)
108105

109106
$config = Get-BuildConfiguration
110107

111108
$localRepoName = 'packagebuild-local-repo'
112-
$packagePathWithNupkg = Join-Path -Path $PackagePath -ChildPath "nupkg"
109+
$packagePathWithNupkg = Join-Path -Path $PackagePath -ChildPath 'nupkg'
113110
Write-Verbose -Verbose -Message "Registering local package repo: $localRepoName to path: $packagePathWithNupkg"
114111
Register-PSResourceRepository -Name $localRepoName -Uri $packagePathWithNupkg -Trusted -Force
115112

116113
$installationPath = $config.BuildOutputPath
117-
if ( !(Test-Path $installationPath)) {
114+
if (-not (Test-Path -Path $installationPath)) {
118115
Write-Verbose -Verbose -Message "Creating module directory location for tests: $installationPath"
119116
$null = New-Item -Path $installationPath -ItemType Directory -Verbose
120117
}
121118

122119
Write-Verbose -Verbose -Message "Installing module $($config.ModuleName) to build output path $installationPath"
123-
$psgetModuleBase = (get-command save-psresource).Module.ModuleBase
124-
$psgetVersion = (get-command save-psresource).Module.Version.ToString()
125-
$psgetPrerelease = (get-command find-psresource).module.PrivateData.PSData.Prerelease
120+
$psgetModuleBase = (Get-Command save-psresource).Module.ModuleBase
121+
$psgetVersion = (Get-Command save-psresource).Module.Version.ToString()
122+
$psgetPrerelease = (Get-Command find-psresource).module.PrivateData.PSData.Prerelease
126123
Write-Verbose -Verbose -Message "PSResourceGet module base imported: $psgetModuleBase"
127124
Write-Verbose -Verbose -Message "PSResourceGet version base imported: $psgetVersion"
128125
Write-Verbose -Verbose -Message "PSResourceGet prerelease base imported: $psgetPrerelease"
129126
#Save-PSResource -Name $config.ModuleName -Repository $localRepoName -Path $installationPath -SkipDependencyCheck -Prerelease -Confirm:$false -TrustRepository
130127

131128
Register-PSRepository -Name $localRepoName -SourceLocation $packagePathWithNupkg -InstallationPolicy Trusted -Verbose
132-
$psgetv2ModuleBase = (get-command save-module).Module.ModuleBase
133-
$psgetv2Version = (get-command save-module).Module.Version.ToString()
134-
$psgetv2Prerelease = (get-command save-module).module.PrivateData.PSData.Prerelease
129+
$psgetv2ModuleBase = (Get-Command save-module).Module.ModuleBase
130+
$psgetv2Version = (Get-Command save-module).Module.Version.ToString()
131+
$psgetv2Prerelease = (Get-Command save-module).module.PrivateData.PSData.Prerelease
135132
Write-Verbose -Verbose -Message "PowerShellGet module base imported: $psgetv2ModuleBase"
136133
Write-Verbose -Verbose -Message "PowerShellGet version base imported: $psgetv2Version"
137134
Write-Verbose -Verbose -Message "PowerShellGet prerelease base imported: $psgetv2Prerelease"
@@ -145,14 +142,14 @@ function Install-ModulePackageForTest {
145142
function Invoke-ModuleTestsACR {
146143
[CmdletBinding()]
147144
param (
148-
[ValidateSet("Functional", "StaticAnalysis")]
149-
[string[]] $Type = "Functional"
145+
[ValidateSet('Functional', 'StaticAnalysis')]
146+
[string[]] $Type = 'Functional'
150147
)
151148

152149
$acrTestFiles = @(
153-
"FindPSResourceTests/FindPSResourceContainerRegistryServer.Tests.ps1",
154-
"InstallPSResourceTests/InstallPSResourceContainerRegistryServer.Tests.ps1",
155-
"PublishPSResourceTests/PublishPSResourceContainerRegistryServer.Tests.ps1"
150+
'FindPSResourceTests/FindPSResourceContainerRegistryServer.Tests.ps1',
151+
'InstallPSResourceTests/InstallPSResourceContainerRegistryServer.Tests.ps1',
152+
'PublishPSResourceTests/PublishPSResourceContainerRegistryServer.Tests.ps1'
156153
)
157154

158155
Invoke-ModuleTests -Type $Type -TestFilePath $acrTestFiles
@@ -162,12 +159,12 @@ function Invoke-ModuleTestsACR {
162159
function Invoke-ModuleTests {
163160
[CmdletBinding()]
164161
param (
165-
[ValidateSet("Functional", "StaticAnalysis")]
166-
[string[]] $Type = "Functional",
167-
[string[]] $TestFilePath = "."
162+
[ValidateSet('Functional', 'StaticAnalysis')]
163+
[string[]] $Type = 'Functional',
164+
[string[]] $TestFilePath = '.'
168165
)
169166

170-
Write-Verbose -Verbose -Message "Starting module Pester tests..."
167+
Write-Verbose -Verbose -Message 'Starting module Pester tests...'
171168

172169
# Run module Pester tests.
173170
$config = Get-BuildConfiguration
@@ -176,12 +173,11 @@ function Invoke-ModuleTests {
176173
$testResultFileName = 'result.pester.xml'
177174
$testPath = $config.TestPath
178175
Write-Verbose -Verbose $config.ModuleName
179-
$moduleToTest = Join-Path -Path $config.BuildOutputPath -ChildPath "Microsoft.PowerShell.PSResourceGet"
176+
$moduleToTest = Join-Path -Path $config.BuildOutputPath -ChildPath 'Microsoft.PowerShell.PSResourceGet'
180177

181178
if ($TestFilePath.Count -gt 1) {
182179
$TestFilePathJoined = $TestFilePath -join ','
183-
}
184-
else {
180+
} else {
185181
$TestFilePathJoined = $TestFilePath
186182
}
187183

@@ -192,9 +188,8 @@ function Invoke-ModuleTests {
192188

193189
try {
194190
& $pwshExePath -NoProfile -NoLogo -Command $command
195-
}
196-
catch {
197-
Write-Error -Message "Error invoking module Pester tests."
191+
} catch {
192+
Write-Error -Message 'Error invoking module Pester tests.'
198193
}
199194

200195
$testResultsFilePath = Join-Path -Path $testPath -ChildPath $testResultFileName
@@ -203,7 +198,7 @@ function Invoke-ModuleTests {
203198
# and class it references to do so can't be found. This will be fixed later.
204199

205200
# # Examine Pester test results.
206-
# if (! (Test-Path -Path $testResultsFilePath)) {
201+
# if (-not (Test-Path -Path $testResultsFilePath)) {
207202
# throw "Module test result file not found: '$testResultsFilePath'"
208203
# }
209204
# $xmlDoc = [xml] (Get-Content -Path $testResultsFilePath -Raw)
@@ -219,7 +214,7 @@ function Invoke-ModuleTests {
219214

220215
# Publish test results to AzDevOps
221216
if ($env:TF_BUILD) {
222-
Write-Verbose -Verbose -Message "Uploading test results to AzDevOps"
217+
Write-Verbose -Verbose -Message 'Uploading test results to AzDevOps'
223218
$powerShellName = if ($PSVersionTable.PSEdition -eq 'Core') { 'PowerShell Core' } else { 'Windows PowerShell' }
224219
$TestType = 'NUnit'
225220
$Title = "Functional Tests - $env:AGENT_OS - $powershellName Results"
@@ -229,7 +224,7 @@ function Invoke-ModuleTests {
229224
Write-Host "##$message"
230225
}
231226

232-
Write-Verbose -Verbose -Message "Module Pester tests complete."
227+
Write-Verbose -Verbose -Message 'Module Pester tests complete.'
233228
}
234229

235230
function Show-PSPesterError {
@@ -242,9 +237,9 @@ function Show-PSPesterError {
242237
$description = $testFailure.description
243238
$name = $testFailure.name
244239
$message = $testFailure.failure.message
245-
$stackTrace = $testFailure.failure."stack-trace"
240+
$stackTrace = $testFailure.failure.'stack-trace'
246241

247-
$fullMsg = "`n{0}`n{1}`n{2}`n{3}`{4}" -f ("Description: " + $description), ("Name: " + $name), "message:", $message, "stack-trace:", $stackTrace
242+
$fullMsg = "`n{0}`n{1}`n{2}`n{3}`{4}" -f ('Description: ' + $description), ('Name: ' + $name), 'message:', $message, 'stack-trace:', $stackTrace
248243

249244
Write-Error $fullMsg
250245
}

doBuild.ps1

Lines changed: 40 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -5,50 +5,41 @@
55
.DESCRIPTION
66
Implement build and packaging of the package and place the output $OutDirectory/$ModuleName
77
#>
8-
function DoBuild
9-
{
8+
function DoBuild {
109
Write-Verbose -Verbose -Message "Starting DoBuild for $ModuleName with configuration: $BuildConfiguration, framework: $BuildFramework"
1110

1211
# Module build out path
13-
$BuildOutPath = "${OutDirectory}/${ModuleName}"
12+
$BuildOutPath = [System.IO.Path]::Combine($OutDirectory, $ModuleName)
1413
Write-Verbose -Verbose -Message "Module output file path: '$BuildOutPath'"
1514

1615
# Module build source path
17-
$BuildSrcPath = "bin/${BuildConfiguration}/${BuildFramework}/publish"
16+
$BuildSrcPath = [System.IO.Path]::Combine($SrcPath, 'code', 'bin', $BuildConfiguration, $BuildFramework, 'publish')
1817
Write-Verbose -Verbose -Message "Module build source path: '$BuildSrcPath'"
1918

20-
# Copy module .psd1 file
21-
Write-Verbose -Verbose "Copy-Item ${SrcPath}/${ModuleName}.psd1 to $BuildOutPath"
22-
Copy-Item -Path "${SrcPath}/${ModuleName}.psd1" -Dest "$BuildOutPath" -Force
23-
24-
# Copy module .psm1 file
25-
Write-Verbose -Verbose "Copy-Item ${SrcPath}/${ModuleName}.psm1 to $BuildOutPath"
26-
Copy-Item -Path "${SrcPath}/${ModuleName}.psm1" -Dest "$BuildOutPath" -Force
27-
28-
#Copy module format ps1xml file
29-
Write-Verbose -Verbose -Message "Copy-Item ${SrcPath}/${FormatFileName}.ps1xml to $BuildOutPath"
30-
Copy-Item -Path "${SrcPath}/${FormatFileName}.ps1xml" -Dest "$BuildOutPath" -Force
31-
32-
# Copy license
33-
Write-Verbose -Verbose -Message "Copying LICENSE file to '$BuildOutPath'"
34-
Copy-Item -Path "./LICENSE" -Dest "$BuildOutPath"
35-
36-
# Copy notice
37-
Write-Verbose -Verbose -Message "Copying Notice.txt to '$BuildOutPath'"
38-
Copy-Item -Path "./Notice.txt" -Dest "$BuildOutPath"
39-
40-
# Copy Group Policy files
41-
Write-Verbose -Verbose -Message "Copying InstallPSResourceGetPolicyDefinitions.ps1 to '$BuildOutPath'"
42-
Copy-Item -Path "${SrcPath}/InstallPSResourceGetPolicyDefinitions.ps1" -Dest "$BuildOutPath" -Force
43-
44-
Write-Verbose -Verbose -Message "Copying PSResourceRepository.adml to '$BuildOutPath'"
45-
Copy-Item -Path "${SrcPath}/PSResourceRepository.adml" -Dest "$BuildOutPath" -Force
46-
47-
Write-Verbose -Verbose -Message "Copying PSResourceRepository.admx to '$BuildOutPath'"
48-
Copy-Item -Path "${SrcPath}/PSResourceRepository.admx" -Dest "$BuildOutPath" -Force
19+
# Copy files
20+
$FilesToCopy = [string[]](
21+
# Module .psd1 file
22+
[System.IO.Path]::Combine($SrcPath, $ModuleName + '.psd1'),
23+
# Module .psm1 file
24+
[System.IO.Path]::Combine($SrcPath, $ModuleName + '.psm1'),
25+
# Module format ps1xml file
26+
[System.IO.Path]::Combine($SrcPath, $ModuleName + '.ps1xml'),
27+
# License
28+
[System.IO.Path]::Combine('.', 'LICENSE'),
29+
# Notice
30+
[System.IO.Path]::Combine('.', 'Notice.txt'),
31+
# Group Policy files
32+
[System.IO.Path]::Combine($SrcPath, 'InstallPSResourceGetPolicyDefinitions.ps1'),
33+
[System.IO.Path]::Combine($SrcPath, 'PSResourceRepository.adml'),
34+
[System.IO.Path]::Combine($SrcPath, 'PSResourceRepository.admx')
35+
)
36+
foreach ($File in $FilesToCopy) {
37+
Write-Verbose -Message ('Copying "{0}" to "{1}"' -f $File, $BuildOutPath)
38+
Copy-Item -Path $File -Destination $BuildOutPath -Force
39+
}
4940

5041
# Build and place binaries
51-
if ( Test-Path "${SrcPath}/code" ) {
42+
if (Test-Path -Path "${SrcPath}/code") {
5243
Write-Verbose -Verbose -Message "Building assembly and copying to '$BuildOutPath'"
5344
# Build code and place it in the staging location
5445
Push-Location "${SrcPath}/code"
@@ -58,8 +49,8 @@ function DoBuild
5849

5950
# Check for dotnet for Windows (we only build on Windows platforms).
6051
if ($null -eq $dotnetCommand) {
61-
Write-Verbose -Verbose -Message "dotnet.exe cannot be found in current path. Looking in ProgramFiles path."
62-
$dotnetCommandPath = Join-Path -Path $env:ProgramFiles -ChildPath "dotnet\dotnet.exe"
52+
Write-Verbose -Verbose -Message 'dotnet.exe cannot be found in current path. Looking in ProgramFiles path.'
53+
$dotnetCommandPath = [System.IO.Path]::Combine($env:ProgramFiles, 'dotnet', 'dotnet.exe')
6354
$dotnetCommand = Get-Command -Name $dotnetCommandPath -ErrorAction Ignore
6455
if ($null -eq $dotnetCommand) {
6556
throw "Dotnet.exe cannot be found: $dotnetCommandPath is unavailable for build."
@@ -126,12 +117,10 @@ function DoBuild
126117
$buildSuccess = $true
127118

128119
# Copy module binaries
129-
foreach ($fileName in $assemblyNames)
130-
{
120+
foreach ($fileName in $assemblyNames) {
131121
# Copy bin file
132122
$filePath = Join-Path -Path $BuildSrcPath -ChildPath "${fileName}.dll"
133-
if (! (Test-Path -Path $filePath))
134-
{
123+
if (-not (Test-Path -Path $filePath)) {
135124
Write-Error "Expected file $filePath is missing from build output."
136125
$BuildSuccess = $false
137126
continue
@@ -141,26 +130,23 @@ function DoBuild
141130

142131
# Copy pdb file if available
143132
$filePathPdb = Join-Path -Path $BuildSrcPath -ChildPath "${fileName}.pdb"
144-
if (Test-Path -Path $filePathPdb)
145-
{
133+
if (Test-Path -Path $filePathPdb) {
146134
Copy-Item -Path $filePathPdb -Dest $BuildOutPath -Verbose -Force
147135
}
148136
}
149137

150-
$depsOutputBinPath = Join-Path -Path $BuildOutPath -ChildPath "dependencies"
138+
$depsOutputBinPath = Join-Path -Path $BuildOutPath -ChildPath 'dependencies'
151139

152140
if (-not (Test-Path $depsOutputBinPath)) {
153141
Write-Verbose -Verbose -Message "Creating output path for dependencies: $depsOutputBinPath"
154142
$null = New-Item -ItemType Directory -Path $depsOutputBinPath
155143
}
156144

157145
# Copy dependencies
158-
foreach ($fileName in $depAssemblyNames)
159-
{
146+
foreach ($fileName in $depAssemblyNames) {
160147
# Copy bin file
161148
$filePath = Join-Path -Path $BuildSrcPath -ChildPath "${fileName}.dll"
162-
if (! (Test-Path -Path $filePath))
163-
{
149+
if (-not (Test-Path -Path $filePath)) {
164150
Write-Error "Expected file $filePath is missing from build output."
165151
$BuildSuccess = $false
166152
continue
@@ -169,28 +155,23 @@ function DoBuild
169155
Copy-Item -Path $filePath -Dest $depsOutputBinPath -Verbose -Force
170156
}
171157

172-
if (! $buildSuccess)
173-
{
174-
throw "Build failed to create expected binaries."
158+
if (-not $buildSuccess) {
159+
throw 'Build failed to create expected binaries.'
175160
}
176161

177-
if (! (Test-Path -Path "$BuildSrcPath/${ModuleName}.dll"))
178-
{
162+
if (-not (Test-Path -Path "$BuildSrcPath/${ModuleName}.dll")) {
179163
throw "Expected binary was not created: $BuildSrcPath/${ModuleName}.dll"
180164
}
181-
}
182-
catch {
165+
} catch {
183166
Write-Verbose -Verbose -Message "dotnet build failed with error: $_"
184167
Write-Error "dotnet build failed with error: $_"
185-
}
186-
finally {
168+
} finally {
187169
Pop-Location
188170
}
189-
}
190-
else {
171+
} else {
191172
Write-Verbose -Verbose -Message "No code to build in '${SrcPath}/code'"
192173
}
193174

194175
## Add build and packaging here
195-
Write-Verbose -Verbose -Message "Ending DoBuild"
176+
Write-Verbose -Verbose -Message 'Ending DoBuild'
196177
}

0 commit comments

Comments
 (0)