Skip to content

Commit d990aaa

Browse files
committed
Add script to update app version number
Signed-off-by: Christoph Hofmann <[email protected]>
1 parent 486aa87 commit d990aaa

File tree

3 files changed

+313
-2
lines changed

3 files changed

+313
-2
lines changed

.github/workflows/main.yml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,15 @@ jobs:
1616
with:
1717
path: src
1818
clean: true
19-
19+
20+
- name: Update Version Number
21+
shell: pwsh
22+
run: ./scripts/Set-Version.ps1 -SourceDirectory '${{github.workspace}}\' -Major '2021' -Minor '1' -Build '9'
23+
24+
- name: Update Version Number
25+
shell: pwsh
26+
run: echo ${{$BUILD_NUMBER}}
27+
2028
- name: Install latest Nuget
2129
uses: NuGet/[email protected]
2230
with:
Lines changed: 302 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,302 @@
1+
<#
2+
.Synopsis
3+
Sets the assembly version of all assemblies in the source directory.
4+
.DESCRIPTION
5+
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.
6+
It uses the name of the build to extract the version number and updates all AssemblyInfo.cs files to use the new version.
7+
.EXAMPLE
8+
Set-AssemblyVersion
9+
.EXAMPLE
10+
Set-AssemblyVersion -SourceDirectory $Env:BUILD_SOURCESDIRECTORY -BuildNumber $Env:BUILD_BUILDNUMBER
11+
.EXAMPLE
12+
Set-AssemblyVersion -SourceDirectory ".\SourceDir" -BuildNumber "Dev_1.0.20150922.01" -VersionFormat "\d+\.\d+\.\d+\.\d+"
13+
#>
14+
15+
[CmdletBinding(SupportsShouldProcess=$true, ConfirmImpact='Medium')]
16+
[Alias()]
17+
[OutputType([int])]
18+
Param
19+
(
20+
# The path to the source directory. Default $Env:BUILD_SOURCESDIRECTORY is set by TFS.
21+
[Parameter(Mandatory=$false)]
22+
[ValidateNotNullOrEmpty()]
23+
[string]$SourceDirectory = $Env:BUILD_SOURCESDIRECTORY,
24+
25+
# Major Part of Version Number
26+
[Parameter(Mandatory=$false)]
27+
[string]$Major,
28+
29+
# Minor Part of Version Number
30+
[Parameter(Mandatory=$false)]
31+
[string]$Minor,
32+
33+
# Build Part of Version Number
34+
[Parameter(Mandatory=$false)]
35+
[string]$Build,
36+
37+
# Minor Part of Version Number
38+
[Parameter(Mandatory=$false)]
39+
[string]$Revision,
40+
41+
# Set the version number also in all AppManifest.xml files.
42+
[Parameter(Mandatory=$false)]
43+
[switch]$SetVersion,
44+
45+
[Parameter(Mandatory=$false)]
46+
[string]$VersionFormat = "\d+\.\d+\.\d+\.\d+"
47+
)
48+
49+
<#
50+
.Synopsis
51+
Sets the assembly version of all assemblies in the source directory.
52+
.DESCRIPTION
53+
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.
54+
It uses the name of the build to extract the version number and updates all AssemblyInfo.cs files to use the new version.
55+
.EXAMPLE
56+
Set-AssemblyVersion
57+
.EXAMPLE
58+
Set-AssemblyVersion -SourceDirectory $Env:BUILD_SOURCESDIRECTORY -BuildNumber $Env:BUILD_BUILDNUMBER
59+
.EXAMPLE
60+
Set-AssemblyVersion -SourceDirectory ".\SourceDir" -BuildNumber "Dev_1.0.20150922.01" -VersionFormat "\d+\.\d+\.\d+\.\d+"
61+
#>
62+
function Set-AssemblyVersion
63+
{
64+
[CmdletBinding(SupportsShouldProcess=$true, ConfirmImpact='Medium')]
65+
[Alias()]
66+
[OutputType([int])]
67+
Param
68+
(
69+
# The path to the source directory.
70+
[Parameter(Mandatory=$false)]
71+
[ValidateNotNullOrEmpty()]
72+
[string]$SourceDirectory ,
73+
74+
# Major Part of Version Number
75+
[Parameter(Mandatory=$false)]
76+
[string]$Major,
77+
78+
# Minor Part of Version Number
79+
[Parameter(Mandatory=$false)]
80+
[string]$Minor,
81+
82+
# Build Part of Version Number
83+
[Parameter(Mandatory=$false)]
84+
[string]$Build,
85+
86+
# Minor Part of Version Number
87+
[Parameter(Mandatory=$false)]
88+
[string]$Revision,
89+
90+
# Set the version number also in all AppManifest.xml files.
91+
[Parameter(Mandatory=$false)]
92+
[switch]$SetVersion,
93+
94+
[Parameter(Mandatory=$false)]
95+
[string]$VersionFormat
96+
)
97+
98+
if (-not (Test-Path $SourceDirectory)) {
99+
throw "The directory '$SourceDirectory' does not exist."
100+
}
101+
102+
$files = Get-Files -SourceDirectory $SourceDirectory
103+
$appFiles = Get-AppManifest -SourceDirectory $SourceDirectory
104+
105+
106+
if (-not $Env:BUILD_BUILDNUMBER)
107+
{
108+
[System.Version] $Version = Get-VersionFromAppManifest ($appFiles | Select-Object -First 1)
109+
}
110+
else
111+
{
112+
[System.Version] $Version = [System.Version]$Env:BUILD_BUILDNUMBER
113+
}
114+
115+
[System.Version] $UpdatedVersion = Create-NewVersionNumber -AppManifestVersion $Version -Major $Major -Minor $Minor -Build $Build -Revision $Revision
116+
117+
if(-not $UpdatedVersion){
118+
throw "Can not created an updated version object"
119+
}
120+
121+
Write-Host "Update BUILD_BUILDNUMBER to $($UpdatedVersion.ToString())"
122+
echo "BUILD_NUMBER=$($UpdatedVersion.ToString())" >> $GITHUB_ENV
123+
124+
if($SetVersion.IsPresent){
125+
Set-FileContent -Files $files -Version $UpdatedVersion -VersionFormat $VersionFormat
126+
Set-AppManifest -Files $appFiles -Version $UpdatedVersion
127+
}
128+
}
129+
130+
function Create-NewVersionNumber{
131+
[CmdletBinding(SupportsShouldProcess=$true, ConfirmImpact='Medium')]
132+
[Alias()]
133+
[OutputType([System.Version])]
134+
Param
135+
(
136+
# Version number from AppManifest.xml
137+
[Parameter(Mandatory=$false)]
138+
[ValidateNotNullOrEmpty()]
139+
[System.Version]$AppManifestVersion,
140+
141+
# Major Part of Version Number
142+
[Parameter(Mandatory=$false)]
143+
[string]$Major,
144+
145+
# Minor Part of Version Number
146+
[Parameter(Mandatory=$false)]
147+
[string]$Minor,
148+
149+
# Build Part of Version Number
150+
[Parameter(Mandatory=$false)]
151+
[string]$Build,
152+
153+
# Minor Part of Version Number
154+
[Parameter(Mandatory=$false)]
155+
[string]$Revision
156+
)
157+
158+
$majorNumber = $AppManifestVersion.Major
159+
if(-not [string]::IsNullOrEmpty($Major)){
160+
$majorNumber = $Major
161+
}
162+
163+
$minorNumber = $AppManifestVersion.Minor
164+
if(-not [string]::IsNullOrEmpty($Minor)){
165+
$minorNumber = $Minor
166+
}
167+
168+
$buildNumber = $AppManifestVersion.Build
169+
if(-not [string]::IsNullOrEmpty($Build)){
170+
$buildNumber = $Build.Replace("C","")
171+
}
172+
173+
$revisionNumber = $AppManifestVersion.Revision
174+
if(-not [string]::IsNullOrEmpty($Revision)){
175+
$revisionNumber = $Revision
176+
}
177+
178+
$newVersion = New-Object -TypeName System.Version -ArgumentList $majorNumber,$minorNumber,$buildNumber,$revisionNumber
179+
return $newVersion
180+
}
181+
182+
function Get-VersionFromAppManifest
183+
{
184+
[CmdletBinding()]
185+
[Alias()]
186+
[OutputType([System.Version])]
187+
Param
188+
(
189+
[Parameter(Mandatory=$true)]
190+
[ValidateNotNullOrEmpty()]
191+
[System.IO.FileSystemInfo]$file
192+
)
193+
194+
if(-not $file){
195+
throw "Could not find AppManifest.xml"
196+
}
197+
198+
[xml]$xml = Get-Content $file.FullName
199+
$version = [System.Version]::Parse($xml.Package.Identity.Version)
200+
201+
return $version
202+
}
203+
204+
function Get-Files
205+
{
206+
[CmdletBinding()]
207+
[Alias()]
208+
[OutputType([System.IO.FileSystemInfo[]])]
209+
Param
210+
(
211+
[Parameter(Mandatory=$true)]
212+
[ValidateNotNullOrEmpty()]
213+
[string]$SourceDirectory
214+
)
215+
216+
$folders = Get-ChildItem $SourceDirectory -Recurse -Include "*Properties*" | Where-Object { $_.PSIsContainer }
217+
218+
return $folders | ForEach-Object { Get-ChildItem -Path $_.FullName -Recurse -include AssemblyInfo.* }
219+
}
220+
221+
function Get-AppManifest
222+
{
223+
[CmdletBinding()]
224+
[Alias()]
225+
[OutputType([System.IO.FileSystemInfo[]])]
226+
Param
227+
(
228+
[Parameter(Mandatory=$true)]
229+
[ValidateNotNullOrEmpty()]
230+
[string]$SourceDirectory
231+
)
232+
233+
return Get-ChildItem -Path $SourceDirectory -Recurse -Filter "Package.appxmanifest"
234+
}
235+
236+
function Set-FileContent
237+
{
238+
[CmdletBinding(SupportsShouldProcess=$true, ConfirmImpact='Medium')]
239+
[OutputType([int])]
240+
Param
241+
(
242+
[Parameter(Mandatory=$true, Position=0)]
243+
[System.IO.FileSystemInfo[]]$Files,
244+
245+
[Parameter(Mandatory=$true, Position=1)]
246+
[string]$Version,
247+
248+
[Parameter(Mandatory=$true, Position=2)]
249+
[string]$VersionFormat
250+
)
251+
252+
foreach ($file in $Files)
253+
{
254+
$filecontent = Get-Content $file
255+
256+
if ($PSCmdlet.ShouldProcess("$file", "Set-AssemblyVersion"))
257+
{
258+
attrib $file -r
259+
$filecontent -replace $VersionFormat, $Version | Out-File $file
260+
Write-Host "Applied Version '$Version' $($file.FullName) - version applied"
261+
}
262+
}
263+
}
264+
265+
function Set-AppManifest()
266+
{
267+
Param
268+
(
269+
[Parameter(Mandatory=$false)]
270+
[System.IO.FileSystemInfo[]]$Files,
271+
272+
[Parameter(Mandatory=$true)]
273+
[System.Version]$Version
274+
)
275+
276+
if(-not $Files)
277+
{
278+
Write-Host "No Mainfest files to update"
279+
return;
280+
}
281+
282+
foreach ($file in $Files)
283+
{
284+
[xml]$xml = Get-Content $file.FullName
285+
286+
$xml.Package.Identity.Version = $Version.ToString()
287+
288+
if ($PSCmdlet.ShouldProcess("$file", "Set-AppManifest")){
289+
$xml.Save($file.FullName)
290+
}
291+
}
292+
}
293+
294+
if (-not ($myinvocation.line.Contains("`$here\`$sut"))) {
295+
296+
Write-Host "Source Directory: $SourceDirectory"
297+
Write-Host "Major: $Major Minor: $Minor Build: $Build Revision: $Revision"
298+
Write-Host "VersionFormat: $VersionFormat"
299+
Write-Host "SetVersion: $SetVersion"
300+
301+
Set-AssemblyVersion -SourceDirectory $SourceDirectory -Major $Major -Minor $Minor -Build $Build -Revision $Revision -VersionFormat $VersionFormat -SetVersion:$SetVersion
302+
}

OpenHAB.Windows.sln

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
1717
.editorconfig = .editorconfig
1818
EndProjectSection
1919
EndProject
20-
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "builds", "builds", "{A82DABD0-6620-49F0-8C10-11FF7F339D77}"
20+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "workflows", "workflows", "{A82DABD0-6620-49F0-8C10-11FF7F339D77}"
2121
ProjectSection(SolutionItems) = preProject
2222
.github\workflows\main.yml = .github\workflows\main.yml
23+
.github\workflows\scripts\Set-Version.ps1 = .github\workflows\scripts\Set-Version.ps1
2324
EndProjectSection
2425
EndProject
2526
Global

0 commit comments

Comments
 (0)