-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbuild.ps1
111 lines (88 loc) · 4.78 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
Install-module InvokeBuild,Pester,PowerShellGet -Scope CurrentUser
<#>
#Requires -Modules @{ModuleName='InvokeBuild';ModuleVersion='3.2.1'}
#Requires -Modules @{ModuleName='PowerShellGet';ModuleVersion='1.6.0'}
#Requires -Modules @{ModuleName='Pester';ModuleVersion='4.1.1'}
#>
$Script:IsAppveyor = $env:APPVEYOR -ne $null
$Script:ModuleName = Get-Item -Path $BuildRoot | Select-Object -ExpandProperty Name
Get-Module -Name $ModuleName,'helpers' | Remove-Module -Force
Import-Module "$BuildRoot\buildhelpers\helpers.psm1"
if ($env:test) {$test = $env:test} else {$test = 'Min'}
#region Setup
$Private = Get-ChildItem .\Private\* -Recurse |
Where-Object {$_.PSIsContainer -ne $true -and $_.Name -notmatch 'Tests.ps1'}
$Public = Get-ChildItem .\Public\* -Recurse |
Where-Object {$_.PSIsContainer -ne $true -and $_.Name -notmatch 'Tests.ps1'}
#endregion
task Clean {
Remove-Item -Path ".\Bin" -Recurse -Force -ErrorAction SilentlyContinue
}
task TestCode {
Write-Build Yellow "`n`n`nTesting dev code before build"
Write-Build Yellow "Using the $test TestSuite"
$TestResult = Invoke-Pester -Script $BuildRoot\$($ModuleName).$($Test).tests.ps1 -PassThru
if($TestResult.FailedCount -gt 0) {throw 'Tests failed'}
}
task CopyFiles {
$null = New-Item -Path "$BuildRoot\bin\$ModuleName" -ItemType Directory
Copy-Item -Path "$BuildRoot\*.psd1" -Destination "$BuildRoot\bin\$ModuleName"
Get-ChildItem -Path "$BuildRoot\license*" | Copy-Item -Destination "$BuildRoot\bin\$ModuleName"
Copy-Item $BuildRoot\Other -Destination $BuildRoot\bin\$ModuleName\Other -Recurse
}
task MakeHelp -if (Test-Path -Path "$PSScriptRoot\Docs") {
}
task TestBuild {
Write-Build Yellow "`n`n`nTesting compiled module"
$Script = @{Path="$PSScriptRoot\test\Unit"; Parameters=@{ModulePath="$BuildRoot\bin\$ModuleName"}}
$CodeCoverage = Get-Module "$BuildRoot\bin\$ModuleName" -ListAvailable |
Select-Object -ExpandProperty ExportedCommands |
Select-Object -ExpandProperty Keys | Foreach-Object -Process {
@{Path="$BuildRoot\bin\$ModuleName\$ModuleName.psm1";Function=$_}
}
$TestResult = Invoke-Pester -Script $Script -Tag Unit -CodeCoverage $CodeCoverage -PassThru
if($TestResult.FailedCount -gt 0) {throw 'Tests failed'}
}
task MakePSM1 {
Get-ChildItem .\Scripts\init.ps1 | Out-File .\bin\$($ModuleName)\$($ModuleName).psm1 -Encoding utf8
$Private,$Public | ForEach-Object { Get-Content $_.FullName | Out-File .\bin\$($ModuleName)\$($ModuleName).psm1 -Encoding utf8 -Append }
$PublicScriptBlock = Get-ScriptBlockFromFile -Path $public
$PublicFunctions = Get-FunctionFromScriptblock -ScriptBlock $PublicScriptBlock
$PublicAlias = Get-AliasFromScriptblock -ScriptBlock $PublicScriptBlock
$UpdateManifestParam = @{}
if(-Not [String]::IsNullOrEmpty($PublicFunctions)) {
$PublicFunctionParam = "-Function '{0}'" -f ($PublicFunctions -join "','")
$UpdateManifestParam['FunctionsToExport'] = $PublicFunctions
}
if($PublicAlias) {
$PublicAliasParam = "-Alias '{0}'" -f ($PublicAlias -join "','")
$UpdateManifestParam['AliasesToExport'] = $PublicAlias
}
$ExportStrings = 'Export-ModuleMember',$PublicFunctionParam,$PublicAliasParam | Where-Object {-Not [string]::IsNullOrWhiteSpace($_)}
$ExportStrings -join ' ' | Out-File -FilePath "$BuildRoot\bin\$ModuleName\$ModuleName.psm1" -Append -Encoding UTF8
# If we have git and gitversion installed, let's use it to get new module version and Release Notes
if ($(try{Get-Command -Name gitversion -ErrorAction Stop}catch{})) {
$gitversion = gitversion | ConvertFrom-Json
if ($gitversion.CommitsSinceVersionSource -gt 0) {
# Prerelease, raise minor-version by 1 and add prerelease string.
$UpdateManifestParam['ModuleVersion'] = '{0}.{1}.{2}' -f $gitversion.Major, ($gitversion.Minor+1), $gitversion.Patch
$UpdateManifestParam['Prerelease'] = '-beta{0}' -f $gitversion.CommitsSinceVersionSourcePadded
}
else {
# This is a release version
# If there is a tag pointing at HEAD, use that as release notes
$UpdateManifestParam['ModuleVersion'] = $gitversion.MajorMinorPatch
if ($(try{Get-Command -Name git -ErrorAction Stop}catch{})) {
if($CurrentTag = git tag --points-at HEAD) {
$ReleaseNotes = git tag -l -n20 $CurrentTag | Select-Object -Skip 1
$UpdateManifestParam['ReleaseNotes'] = $ReleaseNotes
}
}
}
}
if ($UpdateManifestParam.Count -gt 0) {
Update-ModuleManifest -Path "$BuildRoot\bin\$ModuleName\$ModuleName.psd1" @UpdateManifestParam
}
}
task . Clean, TestCode, Build
task Build CopyFiles, MakePSM1, MakeHelp