-
Notifications
You must be signed in to change notification settings - Fork 28
/
Build.ps1
147 lines (124 loc) · 6.47 KB
/
Build.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
#requires -Module Configuration
[CmdletBinding()]
param(
[Alias("PSPath")]
[string]$Path = $PSScriptRoot,
[string]$ModuleName = $(Split-Path $Path -Leaf),
# The target framework for .net (for packages), with fallback versions
# The default supports PS3: "net40","net35","net20","net45"
# To only support PS4, use: "net45","net40","net35","net20"
# To support PS2, you use: "net35","net20"
[string[]]$TargetFramework = @("net40","net35","net20","net45"),
[switch]$Monitor,
[Nullable[int]]$RevisionNumber = ${Env:APPVEYOR_BUILD_NUMBER}
)
$ErrorActionPreference = "Stop"
Set-StrictMode -Version Latest
Write-Host "BUILDING: $ModuleName from $Path"
# The output path is just a temporary build location
$OutputPath = Join-Path $Path output
$null = mkdir $OutputPath -Force
# We expect the source for the module in a subdirectory called one of three things:
$SourcePath = "src", "source", ${ModuleName} | ForEach { Join-Path $Path $_ -Resolve -ErrorAction SilentlyContinue } | Select -First 1
if(!$SourcePath) {
Write-Warning "This Build script expects a 'Source' or '$ModuleName' folder to be alongside it."
throw "Can't find module source folder."
}
$ManifestPath = Join-Path $SourcePath "${ModuleName}.psd1" -Resolve -ErrorAction SilentlyContinue
if(!$ManifestPath) {
Write-Warning "This Build script expects a '${ModuleName}.psd1' in the '$SourcePath' folder."
throw "Can't find module source files"
}
# Figure out the new build version
[Version]$Version = Get-Metadata $ManifestPath -PropertyName ModuleVersion
# If the RevisionNumber is specified as ZERO, this is a release build ...
# If the RevisionNumber is not specified, this is a dev box build
# If the RevisionNumber is specified, we assume this is a CI build
if($RevisionNumber -ge 0) {
# For CI builds we don't increment the build number
$Build = if($Version.Build -le 0) { 0 } else { $Version.Build }
} else {
# For dev builds, assume we're working on the NEXT release
$Build = if($Version.Build -le 0) { 1 } else { $Version.Build + 1}
}
if([string]::IsNullOrEmpty($RevisionNumber)) {
$Version = New-Object Version $Version.Major, $Version.Minor, $Build
} else {
$Version = New-Object Version $Version.Major, $Version.Minor, $Build, $RevisionNumber
}
# The release path is where the final module goes
$ReleasePath = Join-Path $Path $Version
Write-Verbose "OUTPUT Release Path: $ReleasePath"
if(Test-Path $ReleasePath) {
Write-Verbose " Clean up old build"
Write-Verbose "DELETE $ReleasePath\"
Remove-Item $ReleasePath -Recurse -Force -ErrorAction SilentlyContinue
Write-Verbose "DELETE $OutputPath\build.log"
Remove-Item $OutputPath\build.log -Recurse -Force -ErrorAction SilentlyContinue
}
## Find dependency Package Files
$PackagesConfig = (Join-Path $Path packages.config)
if(Test-Path $PackagesConfig) {
Write-Verbose " Copying Packages"
foreach($Package in ([xml](Get-Content $PackagesConfig)).packages.package) {
$LibPath = "$ReleasePath\lib"
$folder = Join-Path $Path "packages\$($Package.id)*"
# The git NativeBinaries are special -- we need to copy all the "windows" binaries:
if($Package.id -eq "LibGit2Sharp.NativeBinaries") {
$targets = Join-Path $folder 'libgit2\windows'
$LibPath = Join-Path $LibPath "NativeBinaries"
} else {
# Check for each TargetFramework, in order of preference, fall back to using the lib folder
$targets = ($TargetFramework -replace '^','lib\') + 'lib' | ForEach-Object { Join-Path $folder $_ }
}
$PackageSource = Get-Item $targets -ErrorAction SilentlyContinue | Select -First 1 -Expand FullName
if(!$PackageSource) {
throw "Could not find a lib folder for $($Package.id) from package. You may need to run Setup.ps1"
}
Write-Verbose "robocopy $PackageSource $LibPath /E /NP /LOG+:'$OutputPath\build.log' /R:2 /W:15"
$null = robocopy $PackageSource $LibPath /E /NP /LOG+:"$OutputPath\build.log" /R:2 /W:15
if($LASTEXITCODE -ne 0 -and $LASTEXITCODE -ne 1 -and $LASTEXITCODE -ne 3) {
throw "Failed to copy Package $($Package.id) (${LASTEXITCODE}), see build.log for details"
}
}
}
## Copy PowerShell source Files
$ReleaseManifest = Join-Path $ReleasePath "${ModuleName}.psd1"
# if the Source folder has "Public" and optionally "Private" in it, then the psm1 must be assembled:
if(Test-Path (Join-Path $SourcePath Public) -Type Container){
Write-Verbose " Collating Module Source"
$RootModule = Get-Metadata -Path $ManifestPath -PropertyName RootModule -ErrorAction SilentlyContinue
if(!$RootModule) {
$RootModule = Get-Metadata -Path $ManifestPath -PropertyName ModuleToProcess -ErrorAction SilentlyContinue
if(!$RootModule) {
$RootModule = "${ModuleName}.psm1"
}
}
$null = mkdir $ReleasePath -Force
$ReleaseModule = Join-Path $ReleasePath ${RootModule}
Write-Verbose " Setting content for $ReleaseModule"
$FunctionsToExport = Join-Path $SourcePath Public\*.ps1 -Resolve | % { [System.IO.Path]::GetFileNameWithoutExtension($_) }
Set-Content $ReleaseModule ((
(Get-Content (Join-Path $SourcePath Private\*.ps1) -Raw) +
(Get-Content (Join-Path $SourcePath Public\*.ps1) -Raw)) -join "`r`n`r`n`r`n") -Encoding UTF8
# If there are any folders that aren't Public, Private, Tests, or Specs ...
$OtherFolders = Get-ChildItem $SourcePath -Directory -Exclude Public, Private, Tests, Specs
# Then we need to copy everything in them
Copy-Item $OtherFolders -Recurse -Destination $ReleasePath
# Finally, we need to copy any files in the Source directory
Get-ChildItem $SourcePath -File |
Where Name -ne $RootModule |
Copy-Item -Destination $ReleasePath
Update-Manifest $ReleaseManifest -Property FunctionsToExport -Value $FunctionsToExport
} else {
# Legacy modules just have "stuff" in the source folder and we need to copy all of it
Write-Verbose " Copying Module Source"
Write-Verbose "COPY $SourcePath\"
$null = robocopy $SourcePath\ $ReleasePath /E /NP /LOG+:"$OutputPath\build.log" /R:2 /W:15
if($LASTEXITCODE -ne 3) {
throw "Failed to copy Module (${LASTEXITCODE}), see build.log for details"
}
}
## Touch the PSD1 Version:
Write-Verbose " Update Module Version"
Update-Metadata -Path $ReleaseManifest -PropertyName 'ModuleVersion' -Value $Version