-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.ps1
236 lines (200 loc) · 6.76 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
[cmdletbinding()]
Param(
[string]$ModuleFileName = "IdentityManager.psm1"
,
[switch]$Major
,
[switch]$Minor
,
[switch]$LoadModule
,
[string]$description = "Work with Microsoft Identity Manager with Powershell"
,
[string]$ProjectUri = ""
)
END{
write-verbose -message "ModuleFilename = $moduleFileName"
cd C:\Users\Tore\Dropbox\SourceTreeRepros\IdentityManager -ErrorAction SilentlyContinue
$F = $MyInvocation.InvocationName
Write-Verbose -Message "$F - Starting build, getting files"
if(Get-Module -Name IdentityManager)
{
Write-Verbose "$F - Removing FIMautomation snapIn and IdentityManager module"
Remove-FIMsnapin -Verbose
Remove-Module IdentityManager -Verbose:$false
}
$fileList = Get-ChildItem -Filter .\functions\*.ps1 | where name -NotLike "*Tests*"
#$ModuleName = (Get-ChildItem -Path $ModuleFileName -ErrorAction SilentlyContinue).BaseName
$ModuleName = $ModuleFileName.Split('.') | Select-Object -first 1
Write-Verbose -Message "$f - Modulename is $ModuleName"
if([string]::IsNullOrEmpty($moduleName))
{
write-warning -message "Modulename is null or empty"
break
}
$ExportedFunctions = New-Object System.Collections.ArrayList
$fileList | foreach {
Write-Verbose -Message "$F - Function = $($_.BaseName) added"
$null = $ExportedFunctions.Add($_.BaseName)
}
$ModuleLevelFunctions = $null
foreach($function in $ModuleLevelFunctions)
{
Write-Verbose -Message "$f - Checking function $function"
if($ExportedFunctions -contains $function)
{
write-verbose -Message "$f - Removing function $function from exportlist"
$ExportedFunctions.Remove($function)
}
else
{
Write-Verbose -Message "$f - Exported functions does not contain $function"
}
}
Write-Verbose -Message "$f - Constructing content of module file"
[string]$ModuleFile = ""
foreach($file in $fileList)
{
$filecontent = Get-Content -Path $file.FullName -Raw -Encoding UTF8
$filecontent = "$filecontent`n`n"
$ModuleFile += $filecontent
}
[System.Version]$ver = $null
if((Test-Path -Path $moduleFileName -ErrorAction SilentlyContinue) -eq $true)
{
Write-Verbose -Message "$f - Getting version info"
Import-Module -Name ".\$ModuleName.psd1" -Verbose:$false
$ver = (Get-Module $Modulename).Version
Remove-FIMsnapin
Remove-Module $ModuleName -Verbose:$false
Write-Verbose -Message "$f - Removing previous version of $ModuleFileName"
Remove-Item -Path $ModuleFileName
}
function Get-NextVersion
{
[cmdletbinding()]
[outputtype([System.Version])]
Param(
[System.Version]$CurrentVersion
,
[switch]$Major
,
[switch]$Minor
)
[System.Version]$newVersion = $null
$f = $MyInvocation.InvocationName
Write-Verbose -Message "$f - START"
if($Major)
{
Write-Verbose -Message "$F - Bumping Major version"
$build = $CurrentVersion.Build
$ma = $CurrentVersion.Major + 1
$mi = $CurrentVersion.Minor
}
if($Minor)
{
Write-Verbose -Message "$f - Bumping Minor version"
$build = $CurrentVersion.Build
$ma = $CurrentVersion.Major
$mi = $CurrentVersion.Minor + 1
}
if($Minor -and $Major)
{
Write-Verbose -Message "$f - Bumping Major and Minor version"
$build = $CurrentVersion.Build
$ma = $CurrentVersion.Major + 1
$mi = $CurrentVersion.Minor + 1
}
if(-not $Minor -and -not $Major)
{
Write-Verbose -Message "$f - Bumping build version"
$build = $CurrentVersion.Build + 1
$ma = $CurrentVersion.Major
$mi = $CurrentVersion.Minor
}
$newVersion = New-Object System.Version("$Ma.$Mi.$build.0")
return $newVersion
}
if(-not $ver)
{
Write-Verbose -Message "$f - No previous version found, creating new version"
$ver = New-Object System.Version("1.0.0.0")
}
if($Major)
{
$ver = Get-NextVersion -CurrentVersion $ver -Major
}
if($Minor)
{
$ver = Get-NextVersion -CurrentVersion $ver -Minor
}
if($Minor -and $Major)
{
$ver = Get-NextVersion -CurrentVersion $ver -Minor -Major
}
if(-not $Minor -and -not $Major)
{
Write-Verbose -Message "$f - Defaults to bump build version"
$ver = Get-NextVersion -CurrentVersion $ver
}
Write-Verbose -Message "$f - New version is $($ver.ToString())"
Write-Verbose -Message "$f - Writing contents to modulefile"
Set-Content -Path $ModuleFileName -Value $ModuleFile -Encoding UTF8
$ManifestName = "$((Get-ChildItem -Path $ModuleFileName -ErrorAction SilentlyContinue).BaseName).psd1"
Write-Verbose -Message "$f - ManifestfileName is $ManifestName"
if((Test-Path -Path $ManifestName -ErrorAction SilentlyContinue) -eq $true)
{
Write-Verbose -Message "$f - Removing previous version of $ManifestName"
Remove-Item -Path $ManifestName
}
$FormatsToProcess = New-Object -TypeName System.Collections.ArrayList
foreach($file in (Get-ChildItem -Path "$PSScriptRoot\formats"))
{
Write-Verbose -Message "Adding formats file $($file.FullName)"
$null = $FormatsToProcess.Add($file.FullName)
}
Write-Verbose -Message "$f - Creating manifestfile"
$newModuleManifest = @{
Path = "$PSScriptRoot\$ManifestName"
Author = "Tore Grøneng @toregroneng [email protected]"
Copyright = "(c) 2015 Tore Grøneng @toregroneng [email protected]"
CompanyName = "Firstpoint AS"
ModuleVersion = $ver.ToString()
FunctionsToExport = $ExportedFunctions
RootModule = "$ModuleFileName"
Description = "$description"
PowerShellVersion = "4.0"
ProjectUri = "$ProjectUri"
FormatsToProcess = $FormatsToProcess.ToArray()
}
if($FormatsToProcess.count -gt 0)
{
write-verbose -Message "$f - Adding formats to process in manifest"
$null = $newModuleManifest.Add("FormatsToProcess",$FormatsToProcess.ToArray())
}
New-ModuleManifest @newModuleManifest -NestedModules @("FIMmodule\FIMmodule.psd1")
Write-Verbose -Message "$f - Reading back content to convert to UTF8 (content management tracking)"
Set-Content -Path $ManifestName -Value (Get-Content -Path $ManifestName -Raw) -Encoding UTF8
$Answer = "n"
if(-not $LoadModule)
{
$Answer = Read-Host -Prompt "Load module $ModuleName? (Yes/No)"
}
if($Answer -eq "y" -or $Answer -eq "yes" -or $LoadModule)
{
Write-Verbose -Message "$f - Loading module"
if(Test-Path -Path $ManifestName)
{
Import-Module $PSScriptRoot\$ManifestName
}
else
{
Write-Warning -Message "Modulefile $ManifestName not found, module not imported"
}
}
else
{
Write-Verbose -Message "$f - Module not loaded"
}
Write-Verbose -Message "$f - END"
}