forked from kine/NVRAppDevOps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Set-ALAppBuildNo.ps1
86 lines (83 loc) · 3.56 KB
/
Set-ALAppBuildNo.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
<#
.SYNOPSIS
Set the build no. in the App.json
.DESCRIPTION
Set the build no. in the App.json in the app version. It will keep the Major and Minor version no.
!Warning! It will reformat the App.json. It is recommended to use it only during CI/CD and throw the change away after that.
.EXAMPLE
PS C:\> Read-ALConfiguration -Path <repopath> | Set-ALAppBuildNo
Read the config for the repo and update the build no.
.Parameter RepoPath
Path to the repository - will be mapped as c:\app into the container
.Parameter UpdateDevOpsBuildNo
If set, version in the Build Pipeline name will be updated for Azure DevOps build pipeline
.Parameter AppName
Name of the main app. If app.json for this app is found, the version will be set to the new value.
.Parameter TestAppName
Name of the test app. If app.json for this app is found, the version will be set to the new value.
.Parameter Filters
If set, version will be set in all app.json files included in the filter. Example: 'mainapp\\app.json','testapp\\app.json'
Could be combined with the AppName and TestAppName parameters.
.Parameter Buildno
If set, value will be used as build no and revison will be 0.
#>
function Set-ALAppBuildNo
{
Param (
[Parameter(ValueFromPipelineByPropertyName=$True)]
$RepoPath='',
[Parameter(ValueFromPipelineByPropertyName=$True)]
$AppName='',
[Parameter(ValueFromPipelineByPropertyName=$True)]
$TestAppName='',
[switch]$UpdateDevOpsBuildNo,
[Parameter(ValueFromPipelineByPropertyName=$True)]
$Filters,
[Parameter(ValueFromPipelineByPropertyName=$True)]
$BuildNo=''
)
function Get-NoOfDaysSince20000101
{
$timespan = New-TimeSpan -Start '2000-01-01' -End (Get-Date).ToUniversalTime().ToShortDateString()
return $timespan.TotalDays
}
function Get-NoOfSecondsSinceMidnight
{
$seconds = [math]::Round((Get-Date).ToUniversalTime().TimeOfDay.TotalSeconds)
return $seconds
}
$Apps = Get-ChildItem -Path $RepoPath -Filter app.json -Recurse
if ($BuildNo) {
$Build = $BuildNo
$Revision = 0
} else {
$Build = Get-NoOfDaysSince20000101
$Revision = Get-NoOfSecondsSinceMidnight
}
$PossibleAppJson = @()
if ($Filters) {
foreach($f in $Filters) {
$PossibleAppJson += (Get-ChildItem -Path (Join-path $RepoPath $f)).FullName.ToLower()
}
}
foreach ($App in $Apps) {
$AppSetup = Get-Content -Path $App.FullName -Encoding UTF8| ConvertFrom-Json
if (($AppSetup.name -eq $AppName) -or ($AppSetup.name -eq $TestAppName) -or ($PossibleAppJson.Contains($App.FullName.ToLower())))
{
$Version = [Version]$AppSetup.version
$NewVersion = "$($Version.Major).$($Version.Minor).$Build.$Revision"
Write-Host "Setting version for $($AppSetup.name) to $NewVersion"
$AppSetup.version = $NewVersion
$AppSetup | ConvertTo-Json -Depth 5 -Compress | Set-Content -Path $App.FullName -Encoding UTF8
if (-not $ReturnVersion) {
$ReturnVersion = $NewVersion
#(Get-Content -Path $App.FullName -Encoding UTF8) -replace ""
if ($UpdateDevOpsBuildNo) {
write-host "Updating build pipeline no. to $NewVersion"
write-host "##vso[build.updatebuildnumber]$NewVersion"
}
}
}
}
return $ReturnVersion
}