-
Notifications
You must be signed in to change notification settings - Fork 27
/
build.ps1
142 lines (100 loc) · 4.25 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
[cmdletbinding(
DefaultParameterSetName = 'task'
)]
param(
[parameter(
ParameterSetName = 'task',
Position = 0
)]
[string[]]
$Task = 'default',
[parameter(
ParameterSetName = 'help'
)]
[switch]
$Help,
[switch]
$UpdateModules
)
function Resolve-Module {
[Cmdletbinding()]
param (
[Parameter(
Mandatory,
ValueFromPipeline
)]
[string[]]
$Name,
[switch]
$UpdateModules
)
begin {
Get-PackageProvider -Name Nuget -ForceBootstrap -Verbose:$false | Out-Null
Set-PSRepository -Name PSGallery -InstallationPolicy Trusted -Verbose:$false
$PSDefaultParameterValues = @{
'*-Module:Verbose' = $false
'Install-Module:ErrorAction' = 'Stop'
'Install-Module:Force' = $true
'Install-Module:Scope' = 'CurrentUser'
'Install-Module:Verbose' = $false
'Install-Module:AllowClobber' = $true
'Import-Module:ErrorAction' = 'Stop'
'Import-Module:Verbose' = $false
'Import-Module:Force' = $true
}
}
process {
$Name | ForEach-Object {
$versionToImport = $null
$moduleName = $null
$moduleName = $_
Write-Verbose -Message "Resolving Module [$($moduleName)]"
if ($Module = Get-Module -Name $moduleName -ListAvailable -Verbose:$false) {
#Get local ver and PSGallery ver
$latestLocalVersion = ($Module | Measure-Object -Property Version -Maximum).Maximum
$latestGalleryVersion = (
Find-Module -Name $moduleName -Repository PSGallery |
Measure-Object -Property Version -Maximum
).Maximum
#Check if out of date
if ($latestLocalVersion -lt $latestGalleryVersion) {
if ($UpdateModules) {
Write-Verbose -Message "$($moduleName) installed version [$($latestLocalVersion.ToString())] is outdated. Installing gallery version [$($latestGalleryVersion.ToString())]"
if ($UpdateModules) {
Write-Verbose "Updating module [$moduleName] from [$latestLocalVersion] to [$latestGalleryVersion]"
Install-Module -Name $moduleName -RequiredVersion $latestGalleryVersion
$versionToImport = $latestGalleryVersion
}
} else {
Write-Warning "$($moduleName) is out of date. Latest version on PSGallery is [$latestGalleryVersion]. To update, use the -UpdateModules switch."
}
} else {
$versionToImport = $latestLocalVersion
}
} else {
Write-Verbose -Message "[$($moduleName)] missing. Installing..."
Install-Module -Name $moduleName -Repository PSGallery
$versionToImport = (
Get-Module -Name $moduleName -ListAvailable |
Measure-Object -Property Version -Maximum
).Maximum
}
Write-Verbose -Message "$($moduleName) installed. Importing..."
if (-not [string]::IsNullOrEmpty($versionToImport)) {
Import-module -Name $moduleName -RequiredVersion $versionToImport
} else {
Import-module -Name $moduleName
}
}
}
}
'BuildHelpers', 'psake', 'PSDeploy', 'Pester' | Resolve-Module -UpdateModules:($PSBoundParameters.ContainsKey('UpdateModules'))
if ($PSBoundParameters.ContainsKey('help')) {
Get-PSakeScriptTasks -buildFile "$PSScriptRoot\psake.ps1" |
Sort-Object -Property Name |
Format-Table -Property Name, Description, Alias, DependsOn
} else {
Set-BuildEnvironment -Force
Invoke-psake -buildFile "$PSScriptRoot\psake.ps1" -taskList $Task -nologo -Verbose:($VerbosePreference -eq 'Continue')
if ($psake.build_success -eq $false) {exit 1 } else { exit 0 }
}