forked from herbertmilhomme/PokemonUnity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Set-AssemblyVersion.ps1
210 lines (176 loc) · 6.61 KB
/
Set-AssemblyVersion.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
<#
.Synopsis
Sets the assembly version of all assemblies in the source directory.
.DESCRIPTION
A build script that can be included in TFS 2015 or Visual Studio Online (VSO) vNevt builds that update the version of all assemblies in a workspace.
It uses the name of the build to extract the version number and updates all AssemblyInfo.cs files to use the new version.
.EXAMPLE
Set-AssemblyVersion
.EXAMPLE
Set-AssemblyVersion -SourceDirectory $Env:BUILD_SOURCESDIRECTORY -BuildNumber $Env:BUILD_BUILDNUMBER
.EXAMPLE
Set-AssemblyVersion -SourceDirectory ".\SourceDir" -BuildNumber "Dev_1.0.20150922.01" -VersionFormat "\d+\.\d+\.\d+\.\d+"
#>
[CmdletBinding(SupportsShouldProcess=$true, ConfirmImpact='Medium')]
[Alias()]
[OutputType([int])]
Param
(
# The path to the source directory. Default $Env:BUILD_SOURCESDIRECTORY is set by TFS.
[Parameter(Mandatory=$false, Position=0)]
[ValidateNotNullOrEmpty()]
[string]$SourceDirectory = $Env:BUILD_SOURCESDIRECTORY,
# The build number. Default $Env:BUILD_BUILDNUMBER is set by TFS and must be configured according your regex.
[Parameter(Mandatory=$false, Position=1)]
[ValidateNotNullOrEmpty()]
[string]$BuildNumber = $Env:BUILD_BUILDNUMBER,
# The build number. Default $Env:BUILD_BUILDNUMBER is set by TFS and must be configured according your regex.
[Parameter(Mandatory=$false, Position=2)]
[ValidateNotNullOrEmpty()]
[string]$VersionFormat = "\d+\.\d+\.\d+\.\d+",
# Set the version number also in all AppManifest.xml files.
[Parameter(Mandatory=$false)]
[switch]$SetAppVersion
)
<#
.Synopsis
Sets the assembly version of all assemblies in the source directory.
.DESCRIPTION
A build script that can be included in TFS 2015 or Visual Studio Online (VSO) vNevt builds that update the version of all assemblies in a workspace.
It uses the name of the build to extract the version number and updates all AssemblyInfo.cs files to use the new version.
.EXAMPLE
Set-AssemblyVersion
.EXAMPLE
Set-AssemblyVersion -SourceDirectory $Env:BUILD_SOURCESDIRECTORY -BuildNumber $Env:BUILD_BUILDNUMBER
.EXAMPLE
Set-AssemblyVersion -SourceDirectory ".\SourceDir" -BuildNumber "Dev_1.0.20150922.01" -VersionFormat "\d+\.\d+\.\d+\.\d+"
#>
function Set-AssemblyVersion
{
[CmdletBinding(SupportsShouldProcess=$true, ConfirmImpact='Medium')]
[Alias()]
[OutputType([int])]
Param
(
# The path to the source directory. Default $Env:BUILD_SOURCESDIRECTORY is set by TFS.
[Parameter(Mandatory=$false, Position=0)]
[ValidateNotNullOrEmpty()]
[string]$SourceDirectory = $Env:BUILD_SOURCESDIRECTORY,
# The build number. Default $Env:BUILD_BUILDNUMBER is set by TFS and must be configured according your regex.
[Parameter(Mandatory=$false, Position=1)]
[ValidateNotNullOrEmpty()]
[string]$BuildNumber = $Env:BUILD_BUILDNUMBER,
# The build number. Default $Env:BUILD_BUILDNUMBER is set by TFS and must be configured according your regex.
[Parameter(Mandatory=$false, Position=2)]
[ValidateNotNullOrEmpty()]
[string]$VersionFormat = "\d+\.\d+\.\d+\.\d+",
# Set the version number also in all AppManifest.xml files.
[Parameter(Mandatory=$false)]
[switch]$SetAppVersion
)
if (-not (Test-Path $SourceDirectory)) {
throw "The directory '$SourceDirectory' does not exist."
}
$Version = Get-Version -BuildNumber $BuildNumber -VersionFormat $VersionFormat
$files = Get-Files -SourceDirectory $SourceDirectory
Set-FileContent -Files $files -Version $Version -VersionFormat $VersionFormat
if ($SetAppVersion.IsPresent)
{
$files = Get-AppManifest -SourceDirectory $SourceDirectory
Set-AppManifest -Files $files -Version $Version
}
}
function Get-Version
{
[CmdletBinding()]
[Alias()]
[OutputType([string])]
Param
(
[Parameter(Mandatory=$true, Position=0)]
[ValidateNotNullOrEmpty()]
[string]$BuildNumber,
[Parameter(Mandatory=$true, Position=1)]
[ValidateNotNullOrEmpty()]
[string]$VersionFormat
)
$VersionData = [regex]::matches($BuildNumber,$VersionFormat)
if ($VersionData.Count -eq 0){
throw "Could not find version number with format '$VersionFormat' in BUILD_BUILDNUMBER '$BuildNumber'."
}
return $VersionData[0]
}
function Get-Files
{
[CmdletBinding()]
[Alias()]
[OutputType([System.IO.FileSystemInfo[]])]
Param
(
[Parameter(Mandatory=$true, Position=0)]
[ValidateNotNullOrEmpty()]
[string]$SourceDirectory
)
$folders = Get-ChildItem $SourceDirectory -Recurse -Include "*Properties*" | Where-Object { $_.PSIsContainer }
return $folders | ForEach-Object { Get-ChildItem -Path $_.FullName -Recurse -include AssemblyInfo.* }
}
function Get-AppManifest
{
[CmdletBinding()]
[Alias()]
[OutputType([System.IO.FileSystemInfo[]])]
Param
(
[Parameter(Mandatory=$true, Position=0)]
[ValidateNotNullOrEmpty()]
[string]$SourceDirectory
)
return Get-ChildItem -Path $SourceDirectory -Recurse -Filter "AppManifest.xml"
}
function Set-FileContent
{
[CmdletBinding(SupportsShouldProcess=$true, ConfirmImpact='Medium')]
[OutputType([int])]
Param
(
[Parameter(Mandatory=$true, Position=0)]
[System.IO.FileSystemInfo[]]$Files,
[Parameter(Mandatory=$true, Position=1)]
[string]$Version,
[Parameter(Mandatory=$true, Position=2)]
[string]$VersionFormat
)
foreach ($file in $Files)
{
$filecontent = Get-Content $file
if ($PSCmdlet.ShouldProcess("$file", "Set-AssemblyVersion"))
{
attrib $file -r
$filecontent -replace $VersionFormat, $Version | Out-File $file
Write-Verbose -Message "Applied Version '$Version' $($file.FullName) - version applied"
}
}
}
function Set-AppManifest
{
[CmdletBinding(SupportsShouldProcess=$true, ConfirmImpact='Medium')]
[OutputType([int])]
Param
(
[Parameter(Mandatory=$true, Position=0)]
[System.IO.FileSystemInfo[]]$Files,
[Parameter(Mandatory=$true, Position=1)]
[string]$Version
)
foreach ($file in $Files)
{
[xml]$xml = Get-Content $file
$xml.App.Version = $Version
if ($PSCmdlet.ShouldProcess("$file", "Set-AppManifest")){
$xml.Save($file.FullName)
}
}
}
if (-not ($myinvocation.line.Contains("`$here\`$sut"))) {
Set-AssemblyVersion -SourceDirectory $SourceDirectory -BuildNumber $BuildNumber -VersionFormat $VersionFormat
}