forked from PowerShell/PSResourceGet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
doBuild.ps1
149 lines (132 loc) · 5.91 KB
/
doBuild.ps1
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
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
<#
.DESCRIPTION
Implement build and packaging of the package and place the output $OutDirectory/$ModuleName
#>
function DoBuild
{
Write-Verbose -Verbose -Message "Starting DoBuild for $ModuleName with configuration: $BuildConfiguration, framework: $BuildFramework"
# Module build out path
$BuildOutPath = "${OutDirectory}/${ModuleName}"
Write-Verbose -Verbose -Message "Module output file path: '$BuildOutPath'"
# Module build source path
$BuildSrcPath = "bin/${BuildConfiguration}/${BuildFramework}/publish"
Write-Verbose -Verbose -Message "Module build source path: '$BuildSrcPath'"
# Copy module script files
Write-Verbose -Verbose "Copy-Item ${SrcPath}/${ModuleName}.psd1 to $BuildOutPath"
Copy-Item -Path "${SrcPath}/${ModuleName}.psd1" -Dest "$BuildOutPath" -Force
#Copy module format ps1xml file
Write-Verbose -Verbose -Message "Copy-Item ${SrcPath}/${FormatFileName}.ps1xml to $BuildOutPath"
Copy-Item -Path "${SrcPath}/${FormatFileName}.ps1xml" -Dest "$BuildOutPath" -Force
# Create BuildFramework directory for binary location
$BuildOutputBin = Join-Path -Path $BuildOutPath -ChildPath $BuildFramework
if (! (Test-Path -Path $BuildOutputBin)) {
Write-Verbose -Verbose "Creating output path for binaries: $BuildOutputBin"
$null = New-Item -ItemType Directory -Path $BuildOutputBin
}
# Copy help
Write-Verbose -Verbose -Message "Copying help files to '$BuildOutPath'"
Copy-Item -Path "${HelpPath}/${Culture}" -Dest "$BuildOutPath" -Recurse -Force
#
# Copy DSC resources
# TODO: This should not be part of PowerShellGet build/publish and should be moved to its own project
#
Write-Verbose -Verbose -Message "Copying DSC resources to '$BuildOutPath"
Copy-Item -Path "${DSCModulePath}/DscResources" -Dest "$BuildOutPath" -Recurse -Force
#
# Copy Localization and Resources helper modules
# TODO: This should not be part of PowerShellGet build/publish and should be moved to its own project
#
Write-Verbose -Verbose -Message "Copying DSC resources to '$BuildOutPath"
Copy-Item -Path "${DSCModulePath}/Modules" -Dest "$BuildOutPath" -Recurse -Force
# Build and place binaries
if ( Test-Path "${SrcPath}/code" ) {
Write-Verbose -Verbose -Message "Building assembly and copying to '$BuildOutPath'"
# Build code and place it in the staging location
Push-Location "${SrcPath}/code"
try {
# Build source
Write-Verbose -Verbose -Message "Building with configuration: $BuildConfiguration, framework: $BuildFramework"
Write-Verbose -Verbose -Message "Build location: PSScriptRoot: $PSScriptRoot, PWD: $pwd"
dotnet publish --configuration $BuildConfiguration --framework $BuildFramework --output $BuildSrcPath -warnaserror
if ($LASTEXITCODE -ne 0) {
throw "Build failed with exit code: $LASTEXITCODE"
}
# Place build results
if ($BuildFramework -eq "netstandard2.0") {
$assemblyNames = @(
'PowerShellGet'
'Microsoft.Extensions.Logging.Abstractions'
'MoreLinq'
'NuGet.Commands'
'NuGet.Common'
'NuGet.Configuration'
'NuGet.Frameworks'
'NuGet.Packaging'
'NuGet.ProjectModel'
'NuGet.Protocol'
'NuGet.Repositories'
'NuGet.Versioning'
'Newtonsoft.Json'
)
} elseif ($BuildFramework -eq 'net472') {
$assemblyNames = @(
'PowerShellGet'
'Microsoft.Extensions.Logging.Abstractions'
'MoreLinq'
'NuGet.Commands'
'NuGet.Common'
'NuGet.Configuration'
'NuGet.Frameworks'
'NuGet.Packaging'
'NuGet.ProjectModel'
'NuGet.Protocol'
'NuGet.Repositories'
'NuGet.Versioning'
'Newtonsoft.Json'
'System.Security.Principal.Windows'
)
}
$buildSuccess = $true
foreach ($fileName in $assemblyNames)
{
# Copy bin file
$filePath = Join-Path -Path $BuildSrcPath -ChildPath "${fileName}.dll"
if (! (Test-Path -Path $filePath))
{
Write-Error "Expected file $filePath is missing from build output."
$BuildSuccess = $false
continue
}
Copy-Item -Path $filePath -Dest $BuildOutputBin -Verbose -Force
# Copy pdb file if available
$filePathPdb = Join-Path -Path $BuildSrcPath -ChildPath "${fileName}.pdb"
if (Test-Path -Path $filePathPdb)
{
Copy-Item -Path $filePathPdb -Dest $BuildOutputBin -Verbose -Force
}
}
if (! $buildSuccess)
{
throw "Build failed to create expected binaries."
}
if (! (Test-Path -Path "$BuildSrcPath/${ModuleName}.dll"))
{
throw "Expected binary was not created: $BuildSrcPath/${ModuleName}.dll"
}
}
catch {
# Write-Error "dotnet build failed with error: $_"
Write-Verbose -Verbose -Message "dotnet build failed with error: $_"
}
finally {
Pop-Location
}
}
else {
Write-Verbose -Verbose -Message "No code to build in '${SrcPath}/code'"
}
## Add build and packaging here
Write-Verbose -Verbose -Message "Ending DoBuild"
}