From d27df11e8efda6b002923cfcc68f69c5835b56f6 Mon Sep 17 00:00:00 2001 From: mazhelez Date: Wed, 24 Jan 2024 15:49:47 +0100 Subject: [PATCH 01/33] Validate version number --- Actions/IncrementVersionNumber/IncrementVersionNumber.ps1 | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Actions/IncrementVersionNumber/IncrementVersionNumber.ps1 b/Actions/IncrementVersionNumber/IncrementVersionNumber.ps1 index e51b55bd3..22eb9859e 100644 --- a/Actions/IncrementVersionNumber/IncrementVersionNumber.ps1 +++ b/Actions/IncrementVersionNumber/IncrementVersionNumber.ps1 @@ -26,6 +26,11 @@ try { import-module (Join-Path -path $PSScriptRoot -ChildPath "..\TelemetryHelper.psm1" -Resolve) $telemetryScope = CreateScope -eventId 'DO0076' -parentTelemetryScopeJson $parentTelemetryScopeJson + # Validate version number. It should match . or +. + if(-not ($versionNumber -match '^\+?\d+\.\d+$') { + throw "Version number ($versionNumber) is malformed. A version number must be structured as . or +." + } + $addToVersionNumber = "$versionNumber".StartsWith('+') if ($addToVersionNumber) { $versionNumber = $versionNumber.Substring(1) From 736252ee516ff83635e8280ead46ee1ccf802097 Mon Sep 17 00:00:00 2001 From: mazhelez Date: Tue, 30 Jan 2024 15:09:47 +0100 Subject: [PATCH 02/33] Fix increment version logic to work with wild cards in appFolders and testFolders --- .../IncrementVersionNumber.ps1 | 87 ++++--------------- .../IncrementVersionNumber.psm1 | 74 ++++++++++++++++ 2 files changed, 91 insertions(+), 70 deletions(-) create mode 100644 Actions/IncrementVersionNumber/IncrementVersionNumber.psm1 diff --git a/Actions/IncrementVersionNumber/IncrementVersionNumber.ps1 b/Actions/IncrementVersionNumber/IncrementVersionNumber.ps1 index 22eb9859e..6d8a8baac 100644 --- a/Actions/IncrementVersionNumber/IncrementVersionNumber.ps1 +++ b/Actions/IncrementVersionNumber/IncrementVersionNumber.ps1 @@ -19,97 +19,44 @@ $telemetryScope = $null try { . (Join-Path -Path $PSScriptRoot -ChildPath "..\AL-Go-Helper.ps1" -Resolve) + Import-Module (Join-Path -path $PSScriptRoot -ChildPath "IncrementVersionNumber.psm1" -Resolve) + $serverUrl, $branch = CloneIntoNewFolder -actor $actor -token $token -updateBranch $updateBranch -DirectCommit $directCommit -newBranchPrefix 'increment-version-number' $baseFolder = (Get-Location).path DownloadAndImportBcContainerHelper -baseFolder $baseFolder - import-module (Join-Path -path $PSScriptRoot -ChildPath "..\TelemetryHelper.psm1" -Resolve) + Import-Module (Join-Path -path $PSScriptRoot -ChildPath "..\TelemetryHelper.psm1" -Resolve) $telemetryScope = CreateScope -eventId 'DO0076' -parentTelemetryScopeJson $parentTelemetryScopeJson # Validate version number. It should match . or +. - if(-not ($versionNumber -match '^\+?\d+\.\d+$') { + if(-not ($versionNumber -match '^\+?\d+\.\d+$')) { throw "Version number ($versionNumber) is malformed. A version number must be structured as . or +." } $addToVersionNumber = "$versionNumber".StartsWith('+') if ($addToVersionNumber) { - $versionNumber = $versionNumber.Substring(1) - } - try { - $newVersion = [System.Version]"$($versionNumber).0.0" - } - catch { - throw "Version number ($versionNumber) is malformed. A version number must be structured as . or +." + $versionNumber = $versionNumber.Substring(1) # Remove the + sign } + $newVersion = [System.Version]"$($versionNumber).0.0" + + # Change repoVersion in repository settings + Set-RepoSetting -baseFolder $baseFolder -settingName 'repoVersion' -newValue $newVersion -incremental:$addToVersionNumber + $settings = $env:Settings | ConvertFrom-Json $projectList = @(GetProjectsFromRepository -baseFolder $baseFolder -projectsFromSettings $settings.projects -selectProjects $projects) - foreach($project in $projectList) { - try { - Write-Host "Reading settings from $project\$ALGoSettingsFile" - $settingsJson = Get-Content "$project\$ALGoSettingsFile" -Encoding UTF8 | ConvertFrom-Json - if ($settingsJson.PSObject.Properties.Name -eq "repoVersion") { - $oldVersion = [System.Version]"$($settingsJson.repoVersion).0.0" - if ((!$addToVersionNumber) -and $newVersion -le $oldVersion) { - throw "The new version number ($($newVersion.Major).$($newVersion.Minor)) must be larger than the old version number ($($oldVersion.Major).$($oldVersion.Minor))" - } - $repoVersion = $newVersion - if ($addToVersionNumber) { - $repoVersion = [System.Version]"$($newVersion.Major+$oldVersion.Major).$($newVersion.Minor+$oldVersion.Minor).0.0" - } - $settingsJson.repoVersion = "$($repoVersion.Major).$($repoVersion.Minor)" - } - else { - $repoVersion = $newVersion - if ($addToVersionNumber) { - $repoVersion = [System.Version]"$($newVersion.Major+1).$($newVersion.Minor).0.0" - } - Add-Member -InputObject $settingsJson -NotePropertyName "repoVersion" -NotePropertyValue "$($repoVersion.Major).$($repoVersion.Minor)" | Out-Null - } - $useRepoVersion = (($settingsJson.PSObject.Properties.Name -eq "versioningStrategy") -and (($settingsJson.versioningStrategy -band 16) -eq 16)) - $settingsJson - $settingsJson | Set-JsonContentLF -path "$project\$ALGoSettingsFile" - } - catch { - throw "Settings file $project\$ALGoSettingsFile is malformed.$([environment]::Newline) $($_.Exception.Message)." - } - $folders = @('appFolders', 'testFolders' | ForEach-Object { if ($SettingsJson.PSObject.Properties.Name -eq $_) { $settingsJson."$_" } }) - if (-not ($folders)) { - $folders = Get-ChildItem -Path $project | Where-Object { $_.PSIsContainer -and (Test-Path (Join-Path $_.FullName 'app.json')) } | ForEach-Object { $_.Name } - } - $folders | ForEach-Object { - Write-Host "Modifying app.json in folder $project\$_" - $appJsonFile = Join-Path "$project\$_" "app.json" - if (Test-Path $appJsonFile) { - try { - $appJson = Get-Content $appJsonFile -Encoding UTF8 | ConvertFrom-Json - $oldVersion = [System.Version]$appJson.Version - if ($useRepoVersion) { - $appVersion = $repoVersion - } - elseif ($addToVersionNumber) { - $appVersion = [System.Version]"$($newVersion.Major+$oldVersion.Major).$($newVersion.Minor+$oldVersion.Minor).0.0" - } - else { - $appVersion = $newVersion - } - $appJson.Version = "$appVersion" - $appJson | Set-JsonContentLF -path $appJsonFile - } - catch { - throw "Application manifest file($appJsonFile) is malformed." - } - } - } + foreach($project in $projectList) { + Set-ProjectVersion -baseFolder $baseFolder -project $project -newVersion $newVersion -addToVersionNumber $addToVersionNumber } + + $commitMessage = "New Version number $($newVersion.Major).$($newVersion.Minor)" if ($addToVersionNumber) { - CommitFromNewFolder -serverUrl $serverUrl -commitMessage "Increment Version number by $($newVersion.Major).$($newVersion.Minor)" -branch $branch | Out-Null - } - else { - CommitFromNewFolder -serverUrl $serverUrl -commitMessage "New Version number $($newVersion.Major).$($newVersion.Minor)" -branch $branch | Out-Null + $commitMessage = "Incremented Version number by $versionNumber" } + CommitFromNewFolder -serverUrl $serverUrl -commitMessage $commitMessage -branch $branch | Out-Null + TrackTrace -telemetryScope $telemetryScope } catch { diff --git a/Actions/IncrementVersionNumber/IncrementVersionNumber.psm1 b/Actions/IncrementVersionNumber/IncrementVersionNumber.psm1 new file mode 100644 index 000000000..7196eb406 --- /dev/null +++ b/Actions/IncrementVersionNumber/IncrementVersionNumber.psm1 @@ -0,0 +1,74 @@ +function Set-RepoSetting($baseFolder, $settingsFilePath, $settingName, $newValue, [switch] $incremental) { + $settingFilePath = Join-Path $baseFolder $settingsFilePath + if (-not (Test-Path $settingFilePath)) { + throw "Repository settings file ($settingFilePath) not found." + } + + Write-Host "Reading settings from $settingFilePath" + $settingFileContent = Get-Content $settingFilePath -Encoding UTF8 | ConvertFrom-Json + + if (-not ($settingFileContent.PSObject.Properties.Name -eq $settingName)) { + Write-Host "Setting $settingName not found in $settingFilePath" + return + } + + $oldValue = $settingFileContent.$settingName + + if ($incremental) { + $oldValue = [System.Version]$oldValue + $newValue = [System.Version]$newValue + + $newValue = "$($newValue.Major + $oldValue.Major).$($newValue.Minor + $oldValue.Minor)" + } + + Write-Host "Changing $settingName from $oldValue to $newValue in $settingFilePath" + $settingFileContent.$settingName = $newValue + $settingFileContent | Set-JsonContentLF -path $settingFilePath +} + +function Set-ProjectVersion($baseFolder, $project, $newVersion, [switch] $incremental) { + $projectSettingsPath = Join-Path $project $ALGoSettingsFile + Set-RepoSetting -baseFolder $baseFolder -settingsFilePath $projectSettingsPath -settingName 'repoVersion' -newValue $newVersion -incremental:$incremental | Out-Null + + # Resolve project folders to get all app folders that contain an app.json file + $projectSettings = ReadSettings -baseFolder $baseFolder -project $project + ResolveProjectFolders -baseFolder $baseFolder -project $project -projectSettings ([ref] $projectSettings) + + # Check if the project uses repoVersion versioning strategy + $useRepoVersion = (($projectSettings.PSObject.Properties.Name -eq "versioningStrategy") -and (($projectSettings.versioningStrategy -band 16) -eq 16)) + + $folders = @($projectSettings.appFolders) + @($projectSettings.testFolders) + + Write-Host "Folders: $folders" + + $folders | ForEach-Object { + $folder = $_ + $folder = Join-Path $project $folder + $folder = Join-Path $baseFolder $folder + Write-Host "Modifying app.json in folder $folder" + + $appJsonFile = Join-Path $folder "app.json" + try { + $appJson = Get-Content $appJsonFile -Encoding UTF8 | ConvertFrom-Json + if ($useRepoVersion) { + $appVersion = $projectSettings.repoVersion + } + elseif ($incremental) { + $oldVersion = [System.Version] $appJson.Version + $newVersion = [System.Version] $newVersion + + $appVersion = [System.Version]"$($newVersion.Major+$oldVersion.Major).$($newVersion.Minor+$oldVersion.Minor).0.0" + } + else { + $appVersion = $newVersion + } + $appJson.Version = "$appVersion" + $appJson | Set-JsonContentLF -path $appJsonFile + } + catch { + throw $_ # "Application manifest file($appJsonFile) is malformed." + } + } +} + +Export-ModuleMember -Function Set-RepoSetting, Set-ProjectVersion \ No newline at end of file From 7eef2a891336cfd00e7f8677f7f132dd8c860f4b Mon Sep 17 00:00:00 2001 From: mazhelez Date: Tue, 30 Jan 2024 15:19:32 +0100 Subject: [PATCH 03/33] Better naming + doc --- .../IncrementVersionNumber.ps1 | 2 +- .../IncrementVersionNumber.psm1 | 35 ++++++++++++++++--- 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/Actions/IncrementVersionNumber/IncrementVersionNumber.ps1 b/Actions/IncrementVersionNumber/IncrementVersionNumber.ps1 index 6d8a8baac..5ebb6c5c6 100644 --- a/Actions/IncrementVersionNumber/IncrementVersionNumber.ps1 +++ b/Actions/IncrementVersionNumber/IncrementVersionNumber.ps1 @@ -41,7 +41,7 @@ try { $newVersion = [System.Version]"$($versionNumber).0.0" # Change repoVersion in repository settings - Set-RepoSetting -baseFolder $baseFolder -settingName 'repoVersion' -newValue $newVersion -incremental:$addToVersionNumber + Set-SettingInFile -settingsFilePath (Join-Path $baseFolder $RepoSettingsFile) -settingName 'repoVersion' -newValue $newVersion -incremental:$addToVersionNumber $settings = $env:Settings | ConvertFrom-Json $projectList = @(GetProjectsFromRepository -baseFolder $baseFolder -projectsFromSettings $settings.projects -selectProjects $projects) diff --git a/Actions/IncrementVersionNumber/IncrementVersionNumber.psm1 b/Actions/IncrementVersionNumber/IncrementVersionNumber.psm1 index 7196eb406..795b52619 100644 --- a/Actions/IncrementVersionNumber/IncrementVersionNumber.psm1 +++ b/Actions/IncrementVersionNumber/IncrementVersionNumber.psm1 @@ -1,7 +1,18 @@ -function Set-RepoSetting($baseFolder, $settingsFilePath, $settingName, $newValue, [switch] $incremental) { - $settingFilePath = Join-Path $baseFolder $settingsFilePath +<# + .Synopsis + Changes a setting value in a settings file. + .Parameter settingsFilePath + Path to the settings file. + .Parameter settingName + Name of the setting to change. + .Parameter newValue + New value of the setting. + .Parameter incremental + If set, the new value will be added to the old value. The old value must be a version number. +#> +function Set-SettingInFile($settingsFilePath, $settingName, $newValue, [switch] $incremental) { if (-not (Test-Path $settingFilePath)) { - throw "Repository settings file ($settingFilePath) not found." + throw "Settings file ($settingFilePath) not found." } Write-Host "Reading settings from $settingFilePath" @@ -26,9 +37,23 @@ $settingFileContent | Set-JsonContentLF -path $settingFilePath } +<# + .Synopsis + Changes the version number of a project. + .Description + Changes the version number of a project. The version number is changed in the project settings file (value for 'repoVersion') and in the app.json files of all apps in the project. + .Parameter baseFolder + Base folder of the repository. + .Parameter project + Name of the project (relative to the base folder). + .Parameter newVersion + New version number. + .Parameter incremental + If set, the new version number will be added to the old version number. +#> function Set-ProjectVersion($baseFolder, $project, $newVersion, [switch] $incremental) { $projectSettingsPath = Join-Path $project $ALGoSettingsFile - Set-RepoSetting -baseFolder $baseFolder -settingsFilePath $projectSettingsPath -settingName 'repoVersion' -newValue $newVersion -incremental:$incremental | Out-Null + Set-SettingInFile -settingsFilePath (Join-Path $baseFolder $projectSettingsPath) -settingName 'repoVersion' -newValue $newVersion -incremental:$incremental | Out-Null # Resolve project folders to get all app folders that contain an app.json file $projectSettings = ReadSettings -baseFolder $baseFolder -project $project @@ -71,4 +96,4 @@ function Set-ProjectVersion($baseFolder, $project, $newVersion, [switch] $increm } } -Export-ModuleMember -Function Set-RepoSetting, Set-ProjectVersion \ No newline at end of file +Export-ModuleMember -Function Set-SettingInFile, Set-ProjectVersion \ No newline at end of file From 9c821bb44b8b555e39713e2fc3478b52c108f451 Mon Sep 17 00:00:00 2001 From: mazhelez Date: Tue, 30 Jan 2024 15:44:48 +0100 Subject: [PATCH 04/33] Rename value --- .../IncrementVersionNumber/IncrementVersionNumber.psm1 | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Actions/IncrementVersionNumber/IncrementVersionNumber.psm1 b/Actions/IncrementVersionNumber/IncrementVersionNumber.psm1 index 795b52619..8a128b3da 100644 --- a/Actions/IncrementVersionNumber/IncrementVersionNumber.psm1 +++ b/Actions/IncrementVersionNumber/IncrementVersionNumber.psm1 @@ -11,15 +11,15 @@ If set, the new value will be added to the old value. The old value must be a version number. #> function Set-SettingInFile($settingsFilePath, $settingName, $newValue, [switch] $incremental) { - if (-not (Test-Path $settingFilePath)) { - throw "Settings file ($settingFilePath) not found." + if (-not (Test-Path $settingsFilePath)) { + throw "Settings file ($settingsFilePath) not found." } - Write-Host "Reading settings from $settingFilePath" - $settingFileContent = Get-Content $settingFilePath -Encoding UTF8 | ConvertFrom-Json + Write-Host "Reading settings from $settingsFilePath" + $settingFileContent = Get-Content $settingsFilePath -Encoding UTF8 | ConvertFrom-Json if (-not ($settingFileContent.PSObject.Properties.Name -eq $settingName)) { - Write-Host "Setting $settingName not found in $settingFilePath" + Write-Host "Setting $settingName not found in $settingsFilePath" return } From 7d5dbf6eb1abbc21a0b6ed5368439236791684a5 Mon Sep 17 00:00:00 2001 From: mazhelez Date: Tue, 30 Jan 2024 15:48:41 +0100 Subject: [PATCH 05/33] Fix var name --- Actions/IncrementVersionNumber/IncrementVersionNumber.psm1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Actions/IncrementVersionNumber/IncrementVersionNumber.psm1 b/Actions/IncrementVersionNumber/IncrementVersionNumber.psm1 index 8a128b3da..4f62ac5cd 100644 --- a/Actions/IncrementVersionNumber/IncrementVersionNumber.psm1 +++ b/Actions/IncrementVersionNumber/IncrementVersionNumber.psm1 @@ -32,9 +32,9 @@ function Set-SettingInFile($settingsFilePath, $settingName, $newValue, [switch] $newValue = "$($newValue.Major + $oldValue.Major).$($newValue.Minor + $oldValue.Minor)" } - Write-Host "Changing $settingName from $oldValue to $newValue in $settingFilePath" + Write-Host "Changing $settingName from $oldValue to $newValue in $settingsFilePath" $settingFileContent.$settingName = $newValue - $settingFileContent | Set-JsonContentLF -path $settingFilePath + $settingFileContent | Set-JsonContentLF -path $settingsFilePath } <# From bac2fa44856b1d69b78ec638fb406a90637b01e4 Mon Sep 17 00:00:00 2001 From: mazhelez Date: Tue, 30 Jan 2024 15:48:53 +0100 Subject: [PATCH 06/33] Add tests for Set-SettingInFile --- Tests/IncrementVersionNumber.Action.Test.ps1 | 71 ++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/Tests/IncrementVersionNumber.Action.Test.ps1 b/Tests/IncrementVersionNumber.Action.Test.ps1 index a2ef7485a..968e97702 100644 --- a/Tests/IncrementVersionNumber.Action.Test.ps1 +++ b/Tests/IncrementVersionNumber.Action.Test.ps1 @@ -27,5 +27,76 @@ Describe "IncrementVersionNumber Action Tests" { } # Call action +} + +Describe "Set-SettingInFile tests" { + BeforeAll { + . (Join-Path -Path $PSScriptRoot -ChildPath "..\Actions\AL-Go-Helper.ps1" -Resolve) + Import-Module (Join-Path -path $PSScriptRoot -ChildPath "..\Actions\IncrementVersionNumber\IncrementVersionNumber.psm1" -Resolve) -Force + } + + BeforeEach { + # Create test JSON settings file in the temp folder + $settingsFilePath = Join-Path ([System.IO.Path]::GetTempPath()) "$([Guid]::NewGuid().ToString()).json" + $settingsFileContent = [ordered]@{ + "repoVersion" = "0.1" + "otherSetting" = "otherSettingValue" + } + $settingsFileContent | ConvertTo-Json | Set-Content $settingsFilePath -Encoding UTF8 + } + It 'Set-SettingInFile -settingsFilePath not found' { + $nonExistingFile = Join-Path ([System.IO.Path]::GetTempPath()) "UnknownFile.json" + $settingName = 'repoVersion' + $newValue = '1.0' + $incremental = $false + + { Set-SettingInFile -settingsFilePath $nonExistingFile -settingName $settingName -newValue $newValue -incremental:$incremental } | Should -Throw "Settings file ($nonExistingFile) not found." + } + + It 'Set-SettingInFile -settingName not found' { + $settingName = 'repoVersion2' + $newValue = '1.0' + $incremental = $false + + { Set-SettingInFile -settingsFilePath $settingsFilePath -settingName $settingName -newValue $newValue -incremental:$incremental } | Should -Not -Throw + + $newSettingsContent = Get-Content $settingsFilePath -Encoding UTF8 | ConvertFrom-Json + $newSettingsContent | Should -Not -Contain $settingName + # Check that the other setting are not changed + $newSettingsContent.otherSetting | Should -Be "otherSettingValue" + $newSettingsContent.repoVersion | Should -Be "0.1" + } + + It 'Set-SettingInFile -newValue is set' { + $settingName = 'repoVersion' + $newValue = '1.0' + $incremental = $false + + Set-SettingInFile -settingsFilePath $settingsFilePath -settingName $settingName -newValue $newValue -incremental:$incremental + + $newSettingsContent = Get-Content $settingsFilePath -Encoding UTF8 | ConvertFrom-Json + $newSettingsContent.$settingName | Should -Be "1.0" + + # Check that the other setting are not changed + $newSettingsContent.otherSetting | Should -Be "otherSettingValue" + } + + It 'Set-SettingInFile -newValue is added to the old value' { + $settingName = 'repoVersion' + $newValue = '0.1' + $incremental = $true + + Set-SettingInFile -settingsFilePath $settingsFilePath -settingName $settingName -newValue $newValue -incremental:$incremental + + $newSettingsContent = Get-Content $settingsFilePath -Encoding UTF8 | ConvertFrom-Json + $newSettingsContent.$settingName | Should -Be "0.2" + + # Check that the other setting are not changed + $newSettingsContent.otherSetting | Should -Be "otherSettingValue" + } + + AfterEach { + Remove-Item $settingsFilePath -Force + } } From f0ab04d8c37e6568c72e87e1260d1675fb47ca71 Mon Sep 17 00:00:00 2001 From: mazhelez Date: Wed, 31 Jan 2024 11:02:13 +0100 Subject: [PATCH 07/33] Make Set-ProjectVersion work with projectSettings --- .../IncrementVersionNumber.ps1 | 8 +++++++- .../IncrementVersionNumber.psm1 | 15 +++++---------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/Actions/IncrementVersionNumber/IncrementVersionNumber.ps1 b/Actions/IncrementVersionNumber/IncrementVersionNumber.ps1 index 5ebb6c5c6..80dc93419 100644 --- a/Actions/IncrementVersionNumber/IncrementVersionNumber.ps1 +++ b/Actions/IncrementVersionNumber/IncrementVersionNumber.ps1 @@ -47,7 +47,13 @@ try { $projectList = @(GetProjectsFromRepository -baseFolder $baseFolder -projectsFromSettings $settings.projects -selectProjects $projects) foreach($project in $projectList) { - Set-ProjectVersion -baseFolder $baseFolder -project $project -newVersion $newVersion -addToVersionNumber $addToVersionNumber + # Resolve project folders to get all app folders that contain an app.json file + $projectSettings = ReadSettings -baseFolder $baseFolder -project $project + ResolveProjectFolders -baseFolder $baseFolder -project $project -projectSettings ([ref] $projectSettings) + + $projectPath = Join-Path $baseFolder $project + + Set-ProjectVersion -project $projectPath -projectSettings $projectSettings -newVersion $newVersion -incremental:$addToVersionNumber } $commitMessage = "New Version number $($newVersion.Major).$($newVersion.Minor)" diff --git a/Actions/IncrementVersionNumber/IncrementVersionNumber.psm1 b/Actions/IncrementVersionNumber/IncrementVersionNumber.psm1 index 4f62ac5cd..43541140a 100644 --- a/Actions/IncrementVersionNumber/IncrementVersionNumber.psm1 +++ b/Actions/IncrementVersionNumber/IncrementVersionNumber.psm1 @@ -51,13 +51,9 @@ function Set-SettingInFile($settingsFilePath, $settingName, $newValue, [switch] .Parameter incremental If set, the new version number will be added to the old version number. #> -function Set-ProjectVersion($baseFolder, $project, $newVersion, [switch] $incremental) { - $projectSettingsPath = Join-Path $project $ALGoSettingsFile - Set-SettingInFile -settingsFilePath (Join-Path $baseFolder $projectSettingsPath) -settingName 'repoVersion' -newValue $newVersion -incremental:$incremental | Out-Null - - # Resolve project folders to get all app folders that contain an app.json file - $projectSettings = ReadSettings -baseFolder $baseFolder -project $project - ResolveProjectFolders -baseFolder $baseFolder -project $project -projectSettings ([ref] $projectSettings) +function Set-ProjectVersion($projectPath, $projectSettings, $newVersion, [switch] $incremental) { + $projectSettingsPath = Join-Path $projectPath $ALGoSettingsFile + Set-SettingInFile -settingsFilePath $projectSettingsPath -settingName 'repoVersion' -newValue $newVersion -incremental:$incremental | Out-Null # Check if the project uses repoVersion versioning strategy $useRepoVersion = (($projectSettings.PSObject.Properties.Name -eq "versioningStrategy") -and (($projectSettings.versioningStrategy -band 16) -eq 16)) @@ -68,8 +64,7 @@ function Set-ProjectVersion($baseFolder, $project, $newVersion, [switch] $increm $folders | ForEach-Object { $folder = $_ - $folder = Join-Path $project $folder - $folder = Join-Path $baseFolder $folder + $folder = Join-Path $projectPath $folder Write-Host "Modifying app.json in folder $folder" $appJsonFile = Join-Path $folder "app.json" @@ -91,7 +86,7 @@ function Set-ProjectVersion($baseFolder, $project, $newVersion, [switch] $increm $appJson | Set-JsonContentLF -path $appJsonFile } catch { - throw $_ # "Application manifest file($appJsonFile) is malformed." + throw "Application manifest file($appJsonFile) is malformed: $_" } } } From cfdda559cda2931634ecaec25c58446c455351ff Mon Sep 17 00:00:00 2001 From: mazhelez Date: Wed, 31 Jan 2024 11:26:27 +0100 Subject: [PATCH 08/33] Refactor to re-use Set-SettingInFile --- .../IncrementVersionNumber.psm1 | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/Actions/IncrementVersionNumber/IncrementVersionNumber.psm1 b/Actions/IncrementVersionNumber/IncrementVersionNumber.psm1 index 43541140a..e4dadd4f3 100644 --- a/Actions/IncrementVersionNumber/IncrementVersionNumber.psm1 +++ b/Actions/IncrementVersionNumber/IncrementVersionNumber.psm1 @@ -59,9 +59,6 @@ function Set-ProjectVersion($projectPath, $projectSettings, $newVersion, [switch $useRepoVersion = (($projectSettings.PSObject.Properties.Name -eq "versioningStrategy") -and (($projectSettings.versioningStrategy -band 16) -eq 16)) $folders = @($projectSettings.appFolders) + @($projectSettings.testFolders) - - Write-Host "Folders: $folders" - $folders | ForEach-Object { $folder = $_ $folder = Join-Path $projectPath $folder @@ -69,21 +66,12 @@ function Set-ProjectVersion($projectPath, $projectSettings, $newVersion, [switch $appJsonFile = Join-Path $folder "app.json" try { - $appJson = Get-Content $appJsonFile -Encoding UTF8 | ConvertFrom-Json if ($useRepoVersion) { $appVersion = $projectSettings.repoVersion + $incremental = $false # Don't increment the version number if the project uses repoVersion versioning strategy } - elseif ($incremental) { - $oldVersion = [System.Version] $appJson.Version - $newVersion = [System.Version] $newVersion - $appVersion = [System.Version]"$($newVersion.Major+$oldVersion.Major).$($newVersion.Minor+$oldVersion.Minor).0.0" - } - else { - $appVersion = $newVersion - } - $appJson.Version = "$appVersion" - $appJson | Set-JsonContentLF -path $appJsonFile + Set-SettingInFile -settingsFilePath $appJsonFile -settingName 'version' -newValue $appVersion -incremental:$incremental | Out-Null } catch { throw "Application manifest file($appJsonFile) is malformed: $_" From b6dee6df8554f6c313d705c492f37de50ca00e9b Mon Sep 17 00:00:00 2001 From: mazhelez Date: Wed, 31 Jan 2024 11:27:18 +0100 Subject: [PATCH 09/33] Fix newVersion reference --- Actions/IncrementVersionNumber/IncrementVersionNumber.psm1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Actions/IncrementVersionNumber/IncrementVersionNumber.psm1 b/Actions/IncrementVersionNumber/IncrementVersionNumber.psm1 index e4dadd4f3..75b101831 100644 --- a/Actions/IncrementVersionNumber/IncrementVersionNumber.psm1 +++ b/Actions/IncrementVersionNumber/IncrementVersionNumber.psm1 @@ -67,11 +67,11 @@ function Set-ProjectVersion($projectPath, $projectSettings, $newVersion, [switch $appJsonFile = Join-Path $folder "app.json" try { if ($useRepoVersion) { - $appVersion = $projectSettings.repoVersion + $newVersion = $projectSettings.repoVersion $incremental = $false # Don't increment the version number if the project uses repoVersion versioning strategy } - Set-SettingInFile -settingsFilePath $appJsonFile -settingName 'version' -newValue $appVersion -incremental:$incremental | Out-Null + Set-SettingInFile -settingsFilePath $appJsonFile -settingName 'version' -newValue $newVersion -incremental:$incremental | Out-Null } catch { throw "Application manifest file($appJsonFile) is malformed: $_" From 34aa16b90099f0eff2ef8dd7710b14f82b661fa2 Mon Sep 17 00:00:00 2001 From: mazhelez Date: Wed, 31 Jan 2024 11:39:30 +0100 Subject: [PATCH 10/33] Clean-up, add comments, etc. --- .../IncrementVersionNumber.ps1 | 2 +- .../IncrementVersionNumber.psm1 | 39 +++++++++---------- 2 files changed, 20 insertions(+), 21 deletions(-) diff --git a/Actions/IncrementVersionNumber/IncrementVersionNumber.ps1 b/Actions/IncrementVersionNumber/IncrementVersionNumber.ps1 index 80dc93419..da5c63917 100644 --- a/Actions/IncrementVersionNumber/IncrementVersionNumber.ps1 +++ b/Actions/IncrementVersionNumber/IncrementVersionNumber.ps1 @@ -41,7 +41,7 @@ try { $newVersion = [System.Version]"$($versionNumber).0.0" # Change repoVersion in repository settings - Set-SettingInFile -settingsFilePath (Join-Path $baseFolder $RepoSettingsFile) -settingName 'repoVersion' -newValue $newVersion -incremental:$addToVersionNumber + Set-SettingInFile -settingsFilePath (Join-Path $baseFolder $RepoSettingsFile) -settingName 'repoVersion' -newValue $newVersion -incremental:$addToVersionNumber # $RepoSettingsFile is defined in AL-Go-Helper.ps1 $settings = $env:Settings | ConvertFrom-Json $projectList = @(GetProjectsFromRepository -baseFolder $baseFolder -projectsFromSettings $settings.projects -selectProjects $projects) diff --git a/Actions/IncrementVersionNumber/IncrementVersionNumber.psm1 b/Actions/IncrementVersionNumber/IncrementVersionNumber.psm1 index 75b101831..4955149c7 100644 --- a/Actions/IncrementVersionNumber/IncrementVersionNumber.psm1 +++ b/Actions/IncrementVersionNumber/IncrementVersionNumber.psm1 @@ -8,7 +8,7 @@ .Parameter newValue New value of the setting. .Parameter incremental - If set, the new value will be added to the old value. The old value must be a version number. + If set, the new value will be added to the old value. The old value must be a version number. The new value must be a version number. #> function Set-SettingInFile($settingsFilePath, $settingName, $newValue, [switch] $incremental) { if (-not (Test-Path $settingsFilePath)) { @@ -16,7 +16,12 @@ function Set-SettingInFile($settingsFilePath, $settingName, $newValue, [switch] } Write-Host "Reading settings from $settingsFilePath" - $settingFileContent = Get-Content $settingsFilePath -Encoding UTF8 | ConvertFrom-Json + try { + $settingFileContent = Get-Content $settingsFilePath -Encoding UTF8 | ConvertFrom-Json + } + catch { + throw "Settings file ($settingsFilePath) is malformed: $_" + } if (-not ($settingFileContent.PSObject.Properties.Name -eq $settingName)) { Write-Host "Setting $settingName not found in $settingsFilePath" @@ -49,33 +54,27 @@ function Set-SettingInFile($settingsFilePath, $settingName, $newValue, [switch] .Parameter newVersion New version number. .Parameter incremental - If set, the new version number will be added to the old version number. + If set, the new version number will be added to the old version number. The old version number must be a version number. #> function Set-ProjectVersion($projectPath, $projectSettings, $newVersion, [switch] $incremental) { - $projectSettingsPath = Join-Path $projectPath $ALGoSettingsFile + # Set repoVersion in project settings (if it exists) + $projectSettingsPath = Join-Path $projectPath $ALGoSettingsFile # $ALGoSettingsFile is defined in AL-Go-Helper.ps1 Set-SettingInFile -settingsFilePath $projectSettingsPath -settingName 'repoVersion' -newValue $newVersion -incremental:$incremental | Out-Null # Check if the project uses repoVersion versioning strategy $useRepoVersion = (($projectSettings.PSObject.Properties.Name -eq "versioningStrategy") -and (($projectSettings.versioningStrategy -band 16) -eq 16)) + if ($useRepoVersion) { + $newVersion = $projectSettings.repoVersion + $incremental = $false # Don't increment the version number if the project uses repoVersion versioning strategy + } - $folders = @($projectSettings.appFolders) + @($projectSettings.testFolders) - $folders | ForEach-Object { - $folder = $_ - $folder = Join-Path $projectPath $folder - Write-Host "Modifying app.json in folder $folder" - + # Set version in app.json files + $allAppFolders = @($projectSettings.appFolders) + @($projectSettings.testFolders) + @($projectSettings.bcptTestFolders) + $allAppFolders | ForEach-Object { + $folder = Join-Path $projectPath $_ $appJsonFile = Join-Path $folder "app.json" - try { - if ($useRepoVersion) { - $newVersion = $projectSettings.repoVersion - $incremental = $false # Don't increment the version number if the project uses repoVersion versioning strategy - } - Set-SettingInFile -settingsFilePath $appJsonFile -settingName 'version' -newValue $newVersion -incremental:$incremental | Out-Null - } - catch { - throw "Application manifest file($appJsonFile) is malformed: $_" - } + Set-SettingInFile -settingsFilePath $appJsonFile -settingName 'version' -newValue $newVersion -incremental:$incremental | Out-Null } } From c5e21e8d8d6bfb99e5e5089a72683d49ded61dd6 Mon Sep 17 00:00:00 2001 From: mazhelez Date: Wed, 31 Jan 2024 17:02:00 +0100 Subject: [PATCH 11/33] Fix parameter name --- Actions/IncrementVersionNumber/IncrementVersionNumber.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Actions/IncrementVersionNumber/IncrementVersionNumber.ps1 b/Actions/IncrementVersionNumber/IncrementVersionNumber.ps1 index da5c63917..aaa246c0a 100644 --- a/Actions/IncrementVersionNumber/IncrementVersionNumber.ps1 +++ b/Actions/IncrementVersionNumber/IncrementVersionNumber.ps1 @@ -53,7 +53,7 @@ try { $projectPath = Join-Path $baseFolder $project - Set-ProjectVersion -project $projectPath -projectSettings $projectSettings -newVersion $newVersion -incremental:$addToVersionNumber + Set-ProjectVersion -projectPath $projectPath -projectSettings $projectSettings -newVersion $newVersion -incremental:$addToVersionNumber } $commitMessage = "New Version number $($newVersion.Major).$($newVersion.Minor)" From fdd6c321e47b61747c2ad602b9cf69eff6282223 Mon Sep 17 00:00:00 2001 From: mazhelez Date: Thu, 1 Feb 2024 15:29:44 +0100 Subject: [PATCH 12/33] Improve version changing functionality --- .../IncrementVersionNumber.ps1 | 2 +- .../IncrementVersionNumber.psm1 | 39 +++++++++++++++---- 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/Actions/IncrementVersionNumber/IncrementVersionNumber.ps1 b/Actions/IncrementVersionNumber/IncrementVersionNumber.ps1 index aaa246c0a..c62869787 100644 --- a/Actions/IncrementVersionNumber/IncrementVersionNumber.ps1 +++ b/Actions/IncrementVersionNumber/IncrementVersionNumber.ps1 @@ -38,7 +38,7 @@ try { $versionNumber = $versionNumber.Substring(1) # Remove the + sign } - $newVersion = [System.Version]"$($versionNumber).0.0" + $newVersion = [System.Version]"$($versionNumber)" # Change repoVersion in repository settings Set-SettingInFile -settingsFilePath (Join-Path $baseFolder $RepoSettingsFile) -settingName 'repoVersion' -newValue $newVersion -incremental:$addToVersionNumber # $RepoSettingsFile is defined in AL-Go-Helper.ps1 diff --git a/Actions/IncrementVersionNumber/IncrementVersionNumber.psm1 b/Actions/IncrementVersionNumber/IncrementVersionNumber.psm1 index 4955149c7..8d9a06d66 100644 --- a/Actions/IncrementVersionNumber/IncrementVersionNumber.psm1 +++ b/Actions/IncrementVersionNumber/IncrementVersionNumber.psm1 @@ -10,7 +10,7 @@ .Parameter incremental If set, the new value will be added to the old value. The old value must be a version number. The new value must be a version number. #> -function Set-SettingInFile($settingsFilePath, $settingName, $newValue, [switch] $incremental) { +function Set-SettingInFile($settingsFilePath, $settingName, [System.Version] $newValue, [switch] $incremental) { if (-not (Test-Path $settingsFilePath)) { throw "Settings file ($settingsFilePath) not found." } @@ -28,17 +28,42 @@ function Set-SettingInFile($settingsFilePath, $settingName, $newValue, [switch] return } - $oldValue = $settingFileContent.$settingName + $oldValue = [System.Version] $settingFileContent.$settingName - if ($incremental) { - $oldValue = [System.Version]$oldValue - $newValue = [System.Version]$newValue + # If Build or Revision is -1 (not set), use the old value + $newBuildValue = $newValue.Build + if($newBuildValue -eq -1) { + $newBuildValue = $oldValue.Build + } + + $newRevisionValue = $newValue.Revision + if($newRevisionValue -eq -1) { + $newRevisionValue = $oldValue.Revision + } + + $newMajorValue = $newValue.Major + if($incremental) { + $newMajorValue = $oldValue.Major + $newValue.Major + } - $newValue = "$($newValue.Major + $oldValue.Major).$($newValue.Minor + $oldValue.Minor)" + $newMinorValue = $newValue.Minor + if($incremental) { + $newMinorValue = $oldValue.Minor + $newValue.Minor } + $versions = @($newMajorValue, $newMinorValue) + if($newBuildValue -ne -1) { + $versions += $newBuildValue + } + + if($newRevisionValue -ne -1) { + $versions += $newRevisionValue + } + + $newValue = [System.Version] $($versions -join '.') + Write-Host "Changing $settingName from $oldValue to $newValue in $settingsFilePath" - $settingFileContent.$settingName = $newValue + $settingFileContent.$settingName = $newValue.ToString() $settingFileContent | Set-JsonContentLF -path $settingsFilePath } From 4f038c9775ae49696e4a78c68a3496316d913ee6 Mon Sep 17 00:00:00 2001 From: mazhelez Date: Thu, 1 Feb 2024 15:53:59 +0100 Subject: [PATCH 13/33] Move "incremental" logic in Set-VersionSettingInFile --- .../IncrementVersionNumber.ps1 | 15 ++----- .../IncrementVersionNumber.psm1 | 23 ++++++----- Tests/IncrementVersionNumber.Action.Test.ps1 | 39 ++++++++++++------- 3 files changed, 42 insertions(+), 35 deletions(-) diff --git a/Actions/IncrementVersionNumber/IncrementVersionNumber.ps1 b/Actions/IncrementVersionNumber/IncrementVersionNumber.ps1 index c62869787..be51dc58b 100644 --- a/Actions/IncrementVersionNumber/IncrementVersionNumber.ps1 +++ b/Actions/IncrementVersionNumber/IncrementVersionNumber.ps1 @@ -33,15 +33,8 @@ try { throw "Version number ($versionNumber) is malformed. A version number must be structured as . or +." } - $addToVersionNumber = "$versionNumber".StartsWith('+') - if ($addToVersionNumber) { - $versionNumber = $versionNumber.Substring(1) # Remove the + sign - } - - $newVersion = [System.Version]"$($versionNumber)" - # Change repoVersion in repository settings - Set-SettingInFile -settingsFilePath (Join-Path $baseFolder $RepoSettingsFile) -settingName 'repoVersion' -newValue $newVersion -incremental:$addToVersionNumber # $RepoSettingsFile is defined in AL-Go-Helper.ps1 + Set-VersionSettingInFile -settingsFilePath (Join-Path $baseFolder $RepoSettingsFile) -settingName 'repoVersion' -newValue $versionNumber # $RepoSettingsFile is defined in AL-Go-Helper.ps1 $settings = $env:Settings | ConvertFrom-Json $projectList = @(GetProjectsFromRepository -baseFolder $baseFolder -projectsFromSettings $settings.projects -selectProjects $projects) @@ -53,11 +46,11 @@ try { $projectPath = Join-Path $baseFolder $project - Set-ProjectVersion -projectPath $projectPath -projectSettings $projectSettings -newVersion $newVersion -incremental:$addToVersionNumber + Set-ProjectVersion -projectPath $projectPath -projectSettings $projectSettings -newVersion $versionNumber } - $commitMessage = "New Version number $($newVersion.Major).$($newVersion.Minor)" - if ($addToVersionNumber) { + $commitMessage = "New Version number $versionNumber" + if ($versionNumber.StartsWith('+')) { $commitMessage = "Incremented Version number by $versionNumber" } diff --git a/Actions/IncrementVersionNumber/IncrementVersionNumber.psm1 b/Actions/IncrementVersionNumber/IncrementVersionNumber.psm1 index 8d9a06d66..cbcb5a0f0 100644 --- a/Actions/IncrementVersionNumber/IncrementVersionNumber.psm1 +++ b/Actions/IncrementVersionNumber/IncrementVersionNumber.psm1 @@ -1,16 +1,16 @@ <# .Synopsis - Changes a setting value in a settings file. + Changes a version setting value in a settings file. The setting value must be a version number in one of the following formats: 1.0 or 1.2.3.4 .Parameter settingsFilePath - Path to the settings file. + Path to the settings file. The settings file must be a JSON file. .Parameter settingName Name of the setting to change. .Parameter newValue - New value of the setting. + New value of the setting. If the value starts with a +, the new value will be added to the old value. Else the new value will replace the old value. .Parameter incremental If set, the new value will be added to the old value. The old value must be a version number. The new value must be a version number. #> -function Set-SettingInFile($settingsFilePath, $settingName, [System.Version] $newValue, [switch] $incremental) { +function Set-VersionSettingInFile($settingsFilePath, $settingName, $newValue) { if (-not (Test-Path $settingsFilePath)) { throw "Settings file ($settingsFilePath) not found." } @@ -28,7 +28,13 @@ function Set-SettingInFile($settingsFilePath, $settingName, [System.Version] $ne return } + $incremental = "$newValue".StartsWith('+') + if ($incremental) { + $newValue = $newValue.TrimStart('+') + } + $oldValue = [System.Version] $settingFileContent.$settingName + $newValue = [System.Version] $newValue # Convert to System.Version to make sure the system properties (Build, Revision) are set # If Build or Revision is -1 (not set), use the old value $newBuildValue = $newValue.Build @@ -81,16 +87,15 @@ function Set-SettingInFile($settingsFilePath, $settingName, [System.Version] $ne .Parameter incremental If set, the new version number will be added to the old version number. The old version number must be a version number. #> -function Set-ProjectVersion($projectPath, $projectSettings, $newVersion, [switch] $incremental) { +function Set-ProjectVersion($projectPath, $projectSettings, $newVersion) { # Set repoVersion in project settings (if it exists) $projectSettingsPath = Join-Path $projectPath $ALGoSettingsFile # $ALGoSettingsFile is defined in AL-Go-Helper.ps1 - Set-SettingInFile -settingsFilePath $projectSettingsPath -settingName 'repoVersion' -newValue $newVersion -incremental:$incremental | Out-Null + Set-VersionSettingInFile -settingsFilePath $projectSettingsPath -settingName 'repoVersion' -newValue $newVersion | Out-Null # Check if the project uses repoVersion versioning strategy $useRepoVersion = (($projectSettings.PSObject.Properties.Name -eq "versioningStrategy") -and (($projectSettings.versioningStrategy -band 16) -eq 16)) if ($useRepoVersion) { $newVersion = $projectSettings.repoVersion - $incremental = $false # Don't increment the version number if the project uses repoVersion versioning strategy } # Set version in app.json files @@ -99,8 +104,8 @@ function Set-ProjectVersion($projectPath, $projectSettings, $newVersion, [switch $folder = Join-Path $projectPath $_ $appJsonFile = Join-Path $folder "app.json" - Set-SettingInFile -settingsFilePath $appJsonFile -settingName 'version' -newValue $newVersion -incremental:$incremental | Out-Null + Set-VersionSettingInFile -settingsFilePath $appJsonFile -settingName 'version' -newValue $newVersion | Out-Null } } -Export-ModuleMember -Function Set-SettingInFile, Set-ProjectVersion \ No newline at end of file +Export-ModuleMember -Function Set-VersionSettingInFile, Set-ProjectVersion \ No newline at end of file diff --git a/Tests/IncrementVersionNumber.Action.Test.ps1 b/Tests/IncrementVersionNumber.Action.Test.ps1 index 968e97702..6f54d20d1 100644 --- a/Tests/IncrementVersionNumber.Action.Test.ps1 +++ b/Tests/IncrementVersionNumber.Action.Test.ps1 @@ -29,7 +29,7 @@ Describe "IncrementVersionNumber Action Tests" { # Call action } -Describe "Set-SettingInFile tests" { +Describe "Set-VersionSettingInFile tests" { BeforeAll { . (Join-Path -Path $PSScriptRoot -ChildPath "..\Actions\AL-Go-Helper.ps1" -Resolve) Import-Module (Join-Path -path $PSScriptRoot -ChildPath "..\Actions\IncrementVersionNumber\IncrementVersionNumber.psm1" -Resolve) -Force @@ -45,21 +45,19 @@ Describe "Set-SettingInFile tests" { $settingsFileContent | ConvertTo-Json | Set-Content $settingsFilePath -Encoding UTF8 } - It 'Set-SettingInFile -settingsFilePath not found' { + It 'Set-VersionSettingInFile -settingsFilePath not found' { $nonExistingFile = Join-Path ([System.IO.Path]::GetTempPath()) "UnknownFile.json" $settingName = 'repoVersion' $newValue = '1.0' - $incremental = $false - { Set-SettingInFile -settingsFilePath $nonExistingFile -settingName $settingName -newValue $newValue -incremental:$incremental } | Should -Throw "Settings file ($nonExistingFile) not found." + { Set-VersionSettingInFile -settingsFilePath $nonExistingFile -settingName $settingName -newValue $newValue } | Should -Throw "Settings file ($nonExistingFile) not found." } - It 'Set-SettingInFile -settingName not found' { + It 'Set-VersionSettingInFile -settingName not found' { $settingName = 'repoVersion2' $newValue = '1.0' - $incremental = $false - { Set-SettingInFile -settingsFilePath $settingsFilePath -settingName $settingName -newValue $newValue -incremental:$incremental } | Should -Not -Throw + { Set-VersionSettingInFile -settingsFilePath $settingsFilePath -settingName $settingName -newValue $newValue } | Should -Not -Throw $newSettingsContent = Get-Content $settingsFilePath -Encoding UTF8 | ConvertFrom-Json $newSettingsContent | Should -Not -Contain $settingName @@ -68,12 +66,11 @@ Describe "Set-SettingInFile tests" { $newSettingsContent.repoVersion | Should -Be "0.1" } - It 'Set-SettingInFile -newValue is set' { + It 'Set-VersionSettingInFile -newValue is set' { $settingName = 'repoVersion' $newValue = '1.0' - $incremental = $false - Set-SettingInFile -settingsFilePath $settingsFilePath -settingName $settingName -newValue $newValue -incremental:$incremental + Set-VersionSettingInFile -settingsFilePath $settingsFilePath -settingName $settingName -newValue $newValue $newSettingsContent = Get-Content $settingsFilePath -Encoding UTF8 | ConvertFrom-Json $newSettingsContent.$settingName | Should -Be "1.0" @@ -82,15 +79,27 @@ Describe "Set-SettingInFile tests" { $newSettingsContent.otherSetting | Should -Be "otherSettingValue" } - It 'Set-SettingInFile -newValue is added to the old value' { + It 'Set-VersionSettingInFile -newValue is added to the old value (minor version)' { $settingName = 'repoVersion' - $newValue = '0.1' - $incremental = $true + $newValue = '+0.2' - Set-SettingInFile -settingsFilePath $settingsFilePath -settingName $settingName -newValue $newValue -incremental:$incremental + Set-VersionSettingInFile -settingsFilePath $settingsFilePath -settingName $settingName -newValue $newValue $newSettingsContent = Get-Content $settingsFilePath -Encoding UTF8 | ConvertFrom-Json - $newSettingsContent.$settingName | Should -Be "0.2" + $newSettingsContent.$settingName | Should -Be "0.3" + + # Check that the other setting are not changed + $newSettingsContent.otherSetting | Should -Be "otherSettingValue" + } + + It 'Set-VersionSettingInFile -newValue is added to the old value (major value)' { + $settingName = 'repoVersion' + $newValue = '+1.0' + + Set-VersionSettingInFile -settingsFilePath $settingsFilePath -settingName $settingName -newValue $newValue + + $newSettingsContent = Get-Content $settingsFilePath -Encoding UTF8 | ConvertFrom-Json + $newSettingsContent.$settingName | Should -Be "1.1" # Check that the other setting are not changed $newSettingsContent.otherSetting | Should -Be "otherSettingValue" From 7a0ac9592ca7bbd9f6d1ca2d34c0639e5417da85 Mon Sep 17 00:00:00 2001 From: mazhelez Date: Thu, 1 Feb 2024 16:04:38 +0100 Subject: [PATCH 14/33] Add/improve tests --- Tests/IncrementVersionNumber.Action.Test.ps1 | 72 +++++++++++++++++--- 1 file changed, 63 insertions(+), 9 deletions(-) diff --git a/Tests/IncrementVersionNumber.Action.Test.ps1 b/Tests/IncrementVersionNumber.Action.Test.ps1 index 6f54d20d1..426185569 100644 --- a/Tests/IncrementVersionNumber.Action.Test.ps1 +++ b/Tests/IncrementVersionNumber.Action.Test.ps1 @@ -33,16 +33,24 @@ Describe "Set-VersionSettingInFile tests" { BeforeAll { . (Join-Path -Path $PSScriptRoot -ChildPath "..\Actions\AL-Go-Helper.ps1" -Resolve) Import-Module (Join-Path -path $PSScriptRoot -ChildPath "..\Actions\IncrementVersionNumber\IncrementVersionNumber.psm1" -Resolve) -Force - } - BeforeEach { - # Create test JSON settings file in the temp folder - $settingsFilePath = Join-Path ([System.IO.Path]::GetTempPath()) "$([Guid]::NewGuid().ToString()).json" - $settingsFileContent = [ordered]@{ - "repoVersion" = "0.1" - "otherSetting" = "otherSettingValue" + $settingsFilePath = "" # Might be used in tests, used in AfterEach + + function New-TestSettingsFile { + param( + [string] $repoVersion = '0.1' + ) + + # Create test JSON settings file in the temp folder + $settingsFilePath = Join-Path ([System.IO.Path]::GetTempPath()) "$([Guid]::NewGuid().ToString()).json" + $settingsFileContent = [ordered]@{ + "repoVersion" = $repoVersion + "otherSetting" = "otherSettingValue" + } + $settingsFileContent | ConvertTo-Json | Set-Content $settingsFilePath -Encoding UTF8 + + return $settingsFilePath } - $settingsFileContent | ConvertTo-Json | Set-Content $settingsFilePath -Encoding UTF8 } It 'Set-VersionSettingInFile -settingsFilePath not found' { @@ -54,6 +62,7 @@ Describe "Set-VersionSettingInFile tests" { } It 'Set-VersionSettingInFile -settingName not found' { + $settingsFilePath = New-TestSettingsFile $settingName = 'repoVersion2' $newValue = '1.0' @@ -67,6 +76,7 @@ Describe "Set-VersionSettingInFile tests" { } It 'Set-VersionSettingInFile -newValue is set' { + $settingsFilePath = New-TestSettingsFile $settingName = 'repoVersion' $newValue = '1.0' @@ -80,6 +90,7 @@ Describe "Set-VersionSettingInFile tests" { } It 'Set-VersionSettingInFile -newValue is added to the old value (minor version)' { + $settingsFilePath = New-TestSettingsFile $settingName = 'repoVersion' $newValue = '+0.2' @@ -93,6 +104,7 @@ Describe "Set-VersionSettingInFile tests" { } It 'Set-VersionSettingInFile -newValue is added to the old value (major value)' { + $settingsFilePath = New-TestSettingsFile $settingName = 'repoVersion' $newValue = '+1.0' @@ -105,7 +117,49 @@ Describe "Set-VersionSettingInFile tests" { $newSettingsContent.otherSetting | Should -Be "otherSettingValue" } + It 'Set-VersionSettingInFile -newValue is added to the old value (major and minor value)' { + $settingsFilePath = New-TestSettingsFile -repoVersion '1.2' + $settingName = 'repoVersion' + $newValue = '+3.4' + + Set-VersionSettingInFile -settingsFilePath $settingsFilePath -settingName $settingName -newValue $newValue + + $newSettingsContent = Get-Content $settingsFilePath -Encoding UTF8 | ConvertFrom-Json + $newSettingsContent.$settingName | Should -Be "4.6" + + # Check that the other setting are not changed + $newSettingsContent.otherSetting | Should -Be "otherSettingValue" + } + + It 'Set-VersionSettingInFile -newValue is set and build and revision are kept from the old value'{ + $settingsFilePath = New-TestSettingsFile -repoVersion '1.2.0.0' + $settingName = 'repoVersion' + $newValue = '2.1' + + Set-VersionSettingInFile -settingsFilePath $settingsFilePath -settingName $settingName -newValue $newValue + + $newSettingsContent = Get-Content $settingsFilePath -Encoding UTF8 | ConvertFrom-Json + $newSettingsContent.$settingName | Should -Be "2.1.0.0" + + # Check that the other setting are not changed + $newSettingsContent.otherSetting | Should -Be "otherSettingValue" + } + + It 'Set-VersionSettingInFile -newValue is added and build and revision are kept from the old value' { + $settingsFilePath = New-TestSettingsFile -repoVersion '1.2.3.4' + $settingName = 'repoVersion' + $newValue = '+0.1' + + Set-VersionSettingInFile -settingsFilePath $settingsFilePath -settingName $settingName -newValue $newValue + + $newSettingsContent = Get-Content $settingsFilePath -Encoding UTF8 | ConvertFrom-Json + $newSettingsContent.$settingName | Should -Be "1.3.3.4" + + # Check that the other setting are not changed + $newSettingsContent.otherSetting | Should -Be "otherSettingValue" + } + AfterEach { - Remove-Item $settingsFilePath -Force + Remove-Item $settingsFilePath -Force -ErrorAction Ignore } } From df513c8f4f8c7c7dcecba58dc174317f76458a0b Mon Sep 17 00:00:00 2001 From: mazhelez Date: Thu, 1 Feb 2024 16:07:23 +0100 Subject: [PATCH 15/33] Add some code comments --- Actions/IncrementVersionNumber/IncrementVersionNumber.psm1 | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Actions/IncrementVersionNumber/IncrementVersionNumber.psm1 b/Actions/IncrementVersionNumber/IncrementVersionNumber.psm1 index cbcb5a0f0..8b9af6d69 100644 --- a/Actions/IncrementVersionNumber/IncrementVersionNumber.psm1 +++ b/Actions/IncrementVersionNumber/IncrementVersionNumber.psm1 @@ -28,6 +28,7 @@ function Set-VersionSettingInFile($settingsFilePath, $settingName, $newValue) { return } + # Check if the new value is incremental. $incremental = "$newValue".StartsWith('+') if ($incremental) { $newValue = $newValue.TrimStart('+') @@ -57,6 +58,7 @@ function Set-VersionSettingInFile($settingsFilePath, $settingName, $newValue) { $newMinorValue = $oldValue.Minor + $newValue.Minor } + # Convert to array to make sure the version properties (Build, Revision) are set $versions = @($newMajorValue, $newMinorValue) if($newBuildValue -ne -1) { $versions += $newBuildValue @@ -66,7 +68,8 @@ function Set-VersionSettingInFile($settingsFilePath, $settingName, $newValue) { $versions += $newRevisionValue } - $newValue = [System.Version] $($versions -join '.') + # Construct the new version number. Cast to System.Version to validate if the version number is valid. + $newValue = [System.Version] "$($versions -join '.')" Write-Host "Changing $settingName from $oldValue to $newValue in $settingsFilePath" $settingFileContent.$settingName = $newValue.ToString() From 0e5076852777dd43c46d2601f19c3feb08d026ab Mon Sep 17 00:00:00 2001 From: mazhelez Date: Thu, 1 Feb 2024 16:18:03 +0100 Subject: [PATCH 16/33] Clean up doc --- Actions/IncrementVersionNumber/IncrementVersionNumber.psm1 | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/Actions/IncrementVersionNumber/IncrementVersionNumber.psm1 b/Actions/IncrementVersionNumber/IncrementVersionNumber.psm1 index 8b9af6d69..109112b63 100644 --- a/Actions/IncrementVersionNumber/IncrementVersionNumber.psm1 +++ b/Actions/IncrementVersionNumber/IncrementVersionNumber.psm1 @@ -7,8 +7,6 @@ Name of the setting to change. .Parameter newValue New value of the setting. If the value starts with a +, the new value will be added to the old value. Else the new value will replace the old value. - .Parameter incremental - If set, the new value will be added to the old value. The old value must be a version number. The new value must be a version number. #> function Set-VersionSettingInFile($settingsFilePath, $settingName, $newValue) { if (-not (Test-Path $settingsFilePath)) { @@ -86,9 +84,7 @@ function Set-VersionSettingInFile($settingsFilePath, $settingName, $newValue) { .Parameter project Name of the project (relative to the base folder). .Parameter newVersion - New version number. - .Parameter incremental - If set, the new version number will be added to the old version number. The old version number must be a version number. + New version number. If the version number starts with a +, the new version number will be added to the old version number. Else the new version number will replace the old version number. #> function Set-ProjectVersion($projectPath, $projectSettings, $newVersion) { # Set repoVersion in project settings (if it exists) From cfad51edcec67d4e60ae0b7604a3708aff463884 Mon Sep 17 00:00:00 2001 From: mazhelez Date: Thu, 1 Feb 2024 16:31:43 +0100 Subject: [PATCH 17/33] Add release notes --- RELEASENOTES.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/RELEASENOTES.md b/RELEASENOTES.md index 232e65cf7..064dc5603 100644 --- a/RELEASENOTES.md +++ b/RELEASENOTES.md @@ -8,9 +8,10 @@ Note that when using the preview version of AL-Go for GitHub, we recommend you U ### Issues - Support release branches that start with releases/ - Issue 870 Improve Error Handling when CLI is missing +- Issue 889 CreateRelease and IncrementVersionNumber workflow did not handle wild characters in `appFolders`, `testFolders` or `bcptTestFolders` settings. -### Build modes -AL-Go ships with Default, Translated and Clean mode out of the box. Now you can also define custom build modes in addition to the ones shipped with AL-Go. This allows you to define your own build modes, which can be used to build your apps in different ways. By default, a custom build mode will build the apps similarly to the Default mode but this behavior can be overridden in e.g. script overrides in your repository. +### Build modes +AL-Go ships with Default, Translated and Clean mode out of the box. Now you can also define custom build modes in addition to the ones shipped with AL-Go. This allows you to define your own build modes, which can be used to build your apps in different ways. By default, a custom build mode will build the apps similarly to the Default mode but this behavior can be overridden in e.g. script overrides in your repository. ## v4.1 @@ -45,7 +46,7 @@ If false, the templateSha repository setting is used to download specific AL-Go - **footer** = Footer for the documentation site. (Default: Made with...) - **defaultIndexMD** = Markdown for the landing page of the documentation site. (Default: Reference documentation...) - **defaultReleaseMD** = Markdown for the landing page of the release sites. (Default: Release reference documentation...) - - *Note that in header, footer, defaultIndexMD and defaultReleaseMD you can use the following placeholders: {REPOSITORY}, {VERSION}, {INDEXTEMPLATERELATIVEPATH}, {RELEASENOTES}* + - *Note that in header, footer, defaultIndexMD and defaultReleaseMD you can use the following placeholders: {REPOSITORY}, {VERSION}, {INDEXTEMPLATERELATIVEPATH}, {RELEASENOTES}* ### New Workflows - **Deploy Reference Documentation** is a workflow, which you can invoke manually or on a schedule to generate and deploy reference documentation using the aldoc tool, using the ALDoc setting properties described above. From 4e1e67ac5e8aa0bb7afcaae08f5e0fa6f2e3bebf Mon Sep 17 00:00:00 2001 From: mazhelez Date: Thu, 1 Feb 2024 17:16:18 +0100 Subject: [PATCH 18/33] Pass ALGOsettingsFile as param --- Actions/IncrementVersionNumber/IncrementVersionNumber.ps1 | 2 +- Actions/IncrementVersionNumber/IncrementVersionNumber.psm1 | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Actions/IncrementVersionNumber/IncrementVersionNumber.ps1 b/Actions/IncrementVersionNumber/IncrementVersionNumber.ps1 index be51dc58b..97bdf987e 100644 --- a/Actions/IncrementVersionNumber/IncrementVersionNumber.ps1 +++ b/Actions/IncrementVersionNumber/IncrementVersionNumber.ps1 @@ -46,7 +46,7 @@ try { $projectPath = Join-Path $baseFolder $project - Set-ProjectVersion -projectPath $projectPath -projectSettings $projectSettings -newVersion $versionNumber + Set-ProjectVersion -projectPath $projectPath -projectSettings $projectSettings -newVersion $versionNumber -ALGOsettingsFile $ALGoSettingsFile # $ALGoSettingsFile is defined in AL-Go-Helper.ps1 } $commitMessage = "New Version number $versionNumber" diff --git a/Actions/IncrementVersionNumber/IncrementVersionNumber.psm1 b/Actions/IncrementVersionNumber/IncrementVersionNumber.psm1 index 109112b63..b95f394d7 100644 --- a/Actions/IncrementVersionNumber/IncrementVersionNumber.psm1 +++ b/Actions/IncrementVersionNumber/IncrementVersionNumber.psm1 @@ -86,9 +86,9 @@ function Set-VersionSettingInFile($settingsFilePath, $settingName, $newValue) { .Parameter newVersion New version number. If the version number starts with a +, the new version number will be added to the old version number. Else the new version number will replace the old version number. #> -function Set-ProjectVersion($projectPath, $projectSettings, $newVersion) { +function Set-ProjectVersion($projectPath, $projectSettings, $newVersion, $ALGOsettingsFile) { # Set repoVersion in project settings (if it exists) - $projectSettingsPath = Join-Path $projectPath $ALGoSettingsFile # $ALGoSettingsFile is defined in AL-Go-Helper.ps1 + $projectSettingsPath = Join-Path $projectPath $ALGoSettingsFile Set-VersionSettingInFile -settingsFilePath $projectSettingsPath -settingName 'repoVersion' -newValue $newVersion | Out-Null # Check if the project uses repoVersion versioning strategy From 55a5d587d213db349d21e568f3e62e41e844d07e Mon Sep 17 00:00:00 2001 From: mazhelez Date: Mon, 5 Feb 2024 15:51:45 +0100 Subject: [PATCH 19/33] Set version property in the dependencies array in the app.json --- .../IncrementVersionNumber.psm1 | 37 +++++++++++++++++-- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/Actions/IncrementVersionNumber/IncrementVersionNumber.psm1 b/Actions/IncrementVersionNumber/IncrementVersionNumber.psm1 index b95f394d7..f69e839a8 100644 --- a/Actions/IncrementVersionNumber/IncrementVersionNumber.psm1 +++ b/Actions/IncrementVersionNumber/IncrementVersionNumber.psm1 @@ -71,14 +71,21 @@ function Set-VersionSettingInFile($settingsFilePath, $settingName, $newValue) { Write-Host "Changing $settingName from $oldValue to $newValue in $settingsFilePath" $settingFileContent.$settingName = $newValue.ToString() - $settingFileContent | Set-JsonContentLF -path $settingsFilePath + $settingFileContent | Set-JsonContentLF -Path $settingsFilePath + + return @{ + name = $settingFileContent.name + version = $settingFileContent.version + publisher = $settingFileContent.publisher + } } <# .Synopsis Changes the version number of a project. .Description - Changes the version number of a project. The version number is changed in the project settings file (value for 'repoVersion') and in the app.json files of all apps in the project. + Changes the version number of a project. + The version number is changed in the project settings file (value for 'repoVersion') and in the app.json files of all apps in the project, as well as all references to the apps in the dependencies of the app.json files. .Parameter baseFolder Base folder of the repository. .Parameter project @@ -97,13 +104,35 @@ function Set-ProjectVersion($projectPath, $projectSettings, $newVersion, $ALGOse $newVersion = $projectSettings.repoVersion } - # Set version in app.json files $allAppFolders = @($projectSettings.appFolders) + @($projectSettings.testFolders) + @($projectSettings.bcptTestFolders) + # Set version in app.json files + $appInfos = $allAppFolders | ForEach-Object { + $folder = Join-Path $projectPath $_ + $appJsonFile = Join-Path $folder "app.json" + + $appInfo = Set-VersionSettingInFile -settingsFilePath $appJsonFile -settingName 'version' -newValue $newVersion + return $appInfo + } + + # Set the version in the dependencies in app.json files $allAppFolders | ForEach-Object { $folder = Join-Path $projectPath $_ $appJsonFile = Join-Path $folder "app.json" - Set-VersionSettingInFile -settingsFilePath $appJsonFile -settingName 'version' -newValue $newVersion | Out-Null + $appJsonContent = Get-Content $appJsonFile -Raw -Encoding UTF8 | ConvertFrom-Json + $dependencies = $appJsonContent.dependencies + if ($null -ne $dependencies) { + $dependencies | ForEach-Object { + $dependency = $_ + # Find the version of the dependency in the appInfos. If it's found, it means the dependency is part of the project and the version should be set. + $dependencyAppInfo = $appInfos | Where-Object { $_.name -eq $dependency.name -and $_.publisher -eq $dependency.publisher } + if ($null -ne $dependencyAppInfo) { + $dependency.version = $dependencyAppInfo.version + } + } + $appJsonContent.dependencies = $dependencies + $appJsonContent | Set-JsonContentLF -Path $appJsonFile + } } } From 5ed647054f4cc7de4a0d66ea0ab6ebcf5e46bd6c Mon Sep 17 00:00:00 2001 From: mazhelez Date: Thu, 8 Feb 2024 15:53:46 +0100 Subject: [PATCH 20/33] Change design of incrementing version: +1: increments major version and sets minor to 0 +0.1: increments minor version major.minor: sets the specified versions --- .../IncrementVersionNumber.psm1 | 84 +++++++------ Tests/IncrementVersionNumber.Action.Test.ps1 | 111 +++++++++++++++--- 2 files changed, 145 insertions(+), 50 deletions(-) diff --git a/Actions/IncrementVersionNumber/IncrementVersionNumber.psm1 b/Actions/IncrementVersionNumber/IncrementVersionNumber.psm1 index f69e839a8..87214c07a 100644 --- a/Actions/IncrementVersionNumber/IncrementVersionNumber.psm1 +++ b/Actions/IncrementVersionNumber/IncrementVersionNumber.psm1 @@ -1,14 +1,24 @@ <# .Synopsis - Changes a version setting value in a settings file. The setting value must be a version number in one of the following formats: 1.0 or 1.2.3.4 + Changes a version setting value in a settings file. .Parameter settingsFilePath Path to the settings file. The settings file must be a JSON file. .Parameter settingName - Name of the setting to change. + Name of the setting to change. The setting must be a version number. .Parameter newValue - New value of the setting. If the value starts with a +, the new value will be added to the old value. Else the new value will replace the old value. + New value of the setting. Allowed values are: +1 (increment major version number), +0.1 (increment minor version number), or a version number in the format Major.Minor (e.g. 1.0 or 1.2 #> -function Set-VersionSettingInFile($settingsFilePath, $settingName, $newValue) { +function Set-VersionSettingInFile { + param( + [Parameter(Mandatory = $true, ValueFromPipeline = $true)] + [string] $settingsFilePath, + [Parameter(Mandatory = $true)] + [string] $settingName, + [Parameter(Mandatory = $true)] + [string] $newValue + ) + + #region Validate parameters if (-not (Test-Path $settingsFilePath)) { throw "Settings file ($settingsFilePath) not found." } @@ -26,44 +36,52 @@ function Set-VersionSettingInFile($settingsFilePath, $settingName, $newValue) { return } - # Check if the new value is incremental. - $incremental = "$newValue".StartsWith('+') - if ($incremental) { - $newValue = $newValue.TrimStart('+') - } - - $oldValue = [System.Version] $settingFileContent.$settingName - $newValue = [System.Version] $newValue # Convert to System.Version to make sure the system properties (Build, Revision) are set + # Validate new version value + if ($newValue.StartsWith('+')) { + # Handle incremental version number - # If Build or Revision is -1 (not set), use the old value - $newBuildValue = $newValue.Build - if($newBuildValue -eq -1) { - $newBuildValue = $oldValue.Build + $allowedIncrementalVersionNumbers = @('+1', '+0.1') + if (-not $allowedIncrementalVersionNumbers.Contains($newValue)) { + throw "Incremental version number $newValue is not allowed. Allowed incremental version numbers are: $($allowedIncrementalVersionNumbers -join ', ')" + } } + else { + # Handle absolute version number - $newRevisionValue = $newValue.Revision - if($newRevisionValue -eq -1) { - $newRevisionValue = $oldValue.Revision + $versionNumberFormat = '^\d+\.\d+$' # Major.Minor + if (-not ($newValue -match $versionNumberFormat)) { + throw "Version number $newValue is not in the correct format. The version number must be in the format Major.Minor (e.g. 1.0 or 1.2)" + } } + #endregion - $newMajorValue = $newValue.Major - if($incremental) { - $newMajorValue = $oldValue.Major + $newValue.Major - } + $oldValue = [System.Version] $settingFileContent.$settingName + $versions = @() # an array to hold the version numbers: major, minor, build, revision - $newMinorValue = $newValue.Minor - if($incremental) { - $newMinorValue = $oldValue.Minor + $newValue.Minor - } + switch($newValue) { + '+1' { + # Increment major version number + $versions += $oldValue.Major + 1 + $versions += 0 + } + '+0.1' { + # Increment minor version number + $versions += $oldValue.Major + $versions += $oldValue.Minor + 1 - # Convert to array to make sure the version properties (Build, Revision) are set - $versions = @($newMajorValue, $newMinorValue) - if($newBuildValue -ne -1) { - $versions += $newBuildValue + } + default { + # Absolute version number + $versions += $newValue.Split('.') + } } - if($newRevisionValue -ne -1) { - $versions += $newRevisionValue + # Include build and revision numbers if they exist in the old version number + if ($oldValue.Build -ne -1) { + $versions += $oldValue.Build + if ($oldValue.Revision -ne -1) { + $versions += $oldValue.Revision + } } # Construct the new version number. Cast to System.Version to validate if the version number is valid. diff --git a/Tests/IncrementVersionNumber.Action.Test.ps1 b/Tests/IncrementVersionNumber.Action.Test.ps1 index 426185569..25624e779 100644 --- a/Tests/IncrementVersionNumber.Action.Test.ps1 +++ b/Tests/IncrementVersionNumber.Action.Test.ps1 @@ -75,57 +75,134 @@ Describe "Set-VersionSettingInFile tests" { $newSettingsContent.repoVersion | Should -Be "0.1" } - It 'Set-VersionSettingInFile -newValue is set' { + It 'Set-VersionSettingInFile -newValue +0.2 is not allowed' { $settingsFilePath = New-TestSettingsFile $settingName = 'repoVersion' - $newValue = '1.0' + $newValue = '+0.2' - Set-VersionSettingInFile -settingsFilePath $settingsFilePath -settingName $settingName -newValue $newValue + { Set-VersionSettingInFile -settingsFilePath $settingsFilePath -settingName $settingName -newValue $newValue } | Should -Throw "Incremental version number $newValue is not allowed. Allowed incremental version numbers are: +1, +0.1" + # Check that the settings are not changed $newSettingsContent = Get-Content $settingsFilePath -Encoding UTF8 | ConvertFrom-Json - $newSettingsContent.$settingName | Should -Be "1.0" + $newSettingsContent.$settingName | Should -Be "0.1" + $newSettingsContent.otherSetting | Should -Be "otherSettingValue" + } - # Check that the other setting are not changed + It 'Set-VersionSettingInFile -newValue +3 is not allowed' { + $settingsFilePath = New-TestSettingsFile + $settingName = 'repoVersion' + $newValue = '+3' + + { Set-VersionSettingInFile -settingsFilePath $settingsFilePath -settingName $settingName -newValue $newValue } | Should -Throw "Incremental version number $newValue is not allowed. Allowed incremental version numbers are: +1, +0.1" + + # Check that the settings are not changed + $newSettingsContent = Get-Content $settingsFilePath -Encoding UTF8 | ConvertFrom-Json + $newSettingsContent.$settingName | Should -Be "0.1" $newSettingsContent.otherSetting | Should -Be "otherSettingValue" } - It 'Set-VersionSettingInFile -newValue is added to the old value (minor version)' { + It 'Set-VersionSettingInFile -newValue 1.2.3 is not allowed' { $settingsFilePath = New-TestSettingsFile $settingName = 'repoVersion' - $newValue = '+0.2' + $newValue = '1.2.3' + + { Set-VersionSettingInFile -settingsFilePath $settingsFilePath -settingName $settingName -newValue $newValue } | Should -Throw "Version number $newValue is not in the correct format. The version number must be in the format Major.Minor (e.g. 1.0 or 1.2)" + + # Check that the settings are not changed + $newSettingsContent = Get-Content $settingsFilePath -Encoding UTF8 | ConvertFrom-Json + $newSettingsContent.$settingName | Should -Be "0.1" + $newSettingsContent.otherSetting | Should -Be "otherSettingValue" + } + + It 'Set-VersionSettingInFile -newValue -1 is not allowed' { + $settingsFilePath = New-TestSettingsFile + $settingName = 'repoVersion' + $newValue = '-1' + + { Set-VersionSettingInFile -settingsFilePath $settingsFilePath -settingName $settingName -newValue $newValue } | Should -Throw "Version number $newValue is not in the correct format. The version number must be in the format Major.Minor (e.g. 1.0 or 1.2)" + + # Check that the settings are not changed + $newSettingsContent = Get-Content $settingsFilePath -Encoding UTF8 | ConvertFrom-Json + $newSettingsContent.$settingName | Should -Be "0.1" + $newSettingsContent.otherSetting | Should -Be "otherSettingValue" + } + + It 'Set-VersionSettingInFile -newValue -1 is not allowed' { + $settingsFilePath = New-TestSettingsFile + $settingName = 'repoVersion' + $newValue = '-1' + + { Set-VersionSettingInFile -settingsFilePath $settingsFilePath -settingName $settingName -newValue $newValue } | Should -Throw "Version number $newValue is not in the correct format. The version number must be in the format Major.Minor (e.g. 1.0 or 1.2)" + + # Check that the settings are not changed + $newSettingsContent = Get-Content $settingsFilePath -Encoding UTF8 | ConvertFrom-Json + $newSettingsContent.$settingName | Should -Be "0.1" + $newSettingsContent.otherSetting | Should -Be "otherSettingValue" + } + + It 'Set-VersionSettingInFile -newValue abcd is not allowed' { + $settingsFilePath = New-TestSettingsFile + $settingName = 'repoVersion' + $newValue = 'abcd' + + { Set-VersionSettingInFile -settingsFilePath $settingsFilePath -settingName $settingName -newValue $newValue } | Should -Throw "Version number $newValue is not in the correct format. The version number must be in the format Major.Minor (e.g. 1.0 or 1.2)" + + # Check that the settings are not changed + $newSettingsContent = Get-Content $settingsFilePath -Encoding UTF8 | ConvertFrom-Json + $newSettingsContent.$settingName | Should -Be "0.1" + $newSettingsContent.otherSetting | Should -Be "otherSettingValue" + } + + It 'Set-VersionSettingInFile -newValue a.b is not allowed' { + $settingsFilePath = New-TestSettingsFile + $settingName = 'repoVersion' + $newValue = 'a.b' + + { Set-VersionSettingInFile -settingsFilePath $settingsFilePath -settingName $settingName -newValue $newValue } | Should -Throw "Version number $newValue is not in the correct format. The version number must be in the format Major.Minor (e.g. 1.0 or 1.2)" + + # Check that the settings are not changed + $newSettingsContent = Get-Content $settingsFilePath -Encoding UTF8 | ConvertFrom-Json + $newSettingsContent.$settingName | Should -Be "0.1" + $newSettingsContent.otherSetting | Should -Be "otherSettingValue" + } + + It 'Set-VersionSettingInFile -newValue 1.0 is set' { + $settingsFilePath = New-TestSettingsFile + $settingName = 'repoVersion' + $newValue = '1.0' Set-VersionSettingInFile -settingsFilePath $settingsFilePath -settingName $settingName -newValue $newValue $newSettingsContent = Get-Content $settingsFilePath -Encoding UTF8 | ConvertFrom-Json - $newSettingsContent.$settingName | Should -Be "0.3" + $newSettingsContent.$settingName | Should -Be "1.0" # Check that the other setting are not changed $newSettingsContent.otherSetting | Should -Be "otherSettingValue" } - It 'Set-VersionSettingInFile -newValue is added to the old value (major value)' { + It 'Set-VersionSettingInFile -newValue +1 increments the major version number and sets the minor version number to 0' { $settingsFilePath = New-TestSettingsFile $settingName = 'repoVersion' - $newValue = '+1.0' + $newValue = '+1' Set-VersionSettingInFile -settingsFilePath $settingsFilePath -settingName $settingName -newValue $newValue $newSettingsContent = Get-Content $settingsFilePath -Encoding UTF8 | ConvertFrom-Json - $newSettingsContent.$settingName | Should -Be "1.1" + $newSettingsContent.$settingName | Should -Be "1.0" # Check that the other setting are not changed $newSettingsContent.otherSetting | Should -Be "otherSettingValue" } - It 'Set-VersionSettingInFile -newValue is added to the old value (major and minor value)' { + It 'Set-VersionSettingInFile -newValue +0.1 increments the minor version number' { $settingsFilePath = New-TestSettingsFile -repoVersion '1.2' $settingName = 'repoVersion' - $newValue = '+3.4' + $newValue = '+0.1' Set-VersionSettingInFile -settingsFilePath $settingsFilePath -settingName $settingName -newValue $newValue $newSettingsContent = Get-Content $settingsFilePath -Encoding UTF8 | ConvertFrom-Json - $newSettingsContent.$settingName | Should -Be "4.6" + $newSettingsContent.$settingName | Should -Be "1.3" # Check that the other setting are not changed $newSettingsContent.otherSetting | Should -Be "otherSettingValue" @@ -145,15 +222,15 @@ Describe "Set-VersionSettingInFile tests" { $newSettingsContent.otherSetting | Should -Be "otherSettingValue" } - It 'Set-VersionSettingInFile -newValue is added and build and revision are kept from the old value' { - $settingsFilePath = New-TestSettingsFile -repoVersion '1.2.3.4' + It 'Set-VersionSettingInFile -newValue +0.1 increments the minor version number and build and revision are kept from the old value' { + $settingsFilePath = New-TestSettingsFile -repoVersion '1.2.0.0' $settingName = 'repoVersion' $newValue = '+0.1' Set-VersionSettingInFile -settingsFilePath $settingsFilePath -settingName $settingName -newValue $newValue $newSettingsContent = Get-Content $settingsFilePath -Encoding UTF8 | ConvertFrom-Json - $newSettingsContent.$settingName | Should -Be "1.3.3.4" + $newSettingsContent.$settingName | Should -Be "1.3.0.0" # Check that the other setting are not changed $newSettingsContent.otherSetting | Should -Be "otherSettingValue" From d367b952d24fa485e69549277fa6cca8bf119388 Mon Sep 17 00:00:00 2001 From: mazhelez Date: Thu, 8 Feb 2024 17:19:36 +0100 Subject: [PATCH 21/33] Set dependencies version in app manifest files (app.json) --- .../IncrementVersionNumber.ps1 | 20 ++- .../IncrementVersionNumber.psm1 | 79 +++++------ Tests/IncrementVersionNumber.Action.Test.ps1 | 124 +++++++++--------- 3 files changed, 121 insertions(+), 102 deletions(-) diff --git a/Actions/IncrementVersionNumber/IncrementVersionNumber.ps1 b/Actions/IncrementVersionNumber/IncrementVersionNumber.ps1 index 97bdf987e..d7fb2d5c1 100644 --- a/Actions/IncrementVersionNumber/IncrementVersionNumber.ps1 +++ b/Actions/IncrementVersionNumber/IncrementVersionNumber.ps1 @@ -34,21 +34,35 @@ try { } # Change repoVersion in repository settings - Set-VersionSettingInFile -settingsFilePath (Join-Path $baseFolder $RepoSettingsFile) -settingName 'repoVersion' -newValue $versionNumber # $RepoSettingsFile is defined in AL-Go-Helper.ps1 + Set-VersionInSettingsFile -settingsFilePath (Join-Path $baseFolder $RepoSettingsFile) -settingName 'repoVersion' -newValue $versionNumber | Out-Null # $RepoSettingsFile is defined in AL-Go-Helper.ps1 $settings = $env:Settings | ConvertFrom-Json $projectList = @(GetProjectsFromRepository -baseFolder $baseFolder -projectsFromSettings $settings.projects -selectProjects $projects) + $allAppFolders = @() foreach($project in $projectList) { + $projectPath = Join-Path $baseFolder $project + + # Set repoVersion in project settings (if it exists) + $projectSettingsPath = Join-Path $projectPath $ALGoSettingsFile # $ALGoSettingsFile is defined in AL-Go-Helper.ps1 + Set-VersionInSettingsFile -settingsFilePath $projectSettingsPath -settingName 'repoVersion' -newValue $newVersion + # Resolve project folders to get all app folders that contain an app.json file $projectSettings = ReadSettings -baseFolder $baseFolder -project $project ResolveProjectFolders -baseFolder $baseFolder -project $project -projectSettings ([ref] $projectSettings) - $projectPath = Join-Path $baseFolder $project + # Set version in app manifests (app.json files) + Set-VersionInAppManifests -projectPath $projectPath -projectSettings $projectSettings -newValue $newVersion - Set-ProjectVersion -projectPath $projectPath -projectSettings $projectSettings -newVersion $versionNumber -ALGOsettingsFile $ALGoSettingsFile # $ALGoSettingsFile is defined in AL-Go-Helper.ps1 + # Collect all project's app folders + $allAppFolders += $projectSettings.appFolders | ForEach-Object { Join-Path $projectPath $_ -Resolve } + $allAppFolders += $projectSettings.testFolders | ForEach-Object { Join-Path $projectPath $_ -Resolve } + $allAppFolders += $projectSettings.bcptTestFolders | ForEach-Object { Join-Path $projectPath $_ -Resolve } } + # Set dependencies in app manifests + Set-DependenciesVersionInAppManifests -allAppFolders $allAppFolders + $commitMessage = "New Version number $versionNumber" if ($versionNumber.StartsWith('+')) { $commitMessage = "Incremented Version number by $versionNumber" diff --git a/Actions/IncrementVersionNumber/IncrementVersionNumber.psm1 b/Actions/IncrementVersionNumber/IncrementVersionNumber.psm1 index 87214c07a..000eb9ab5 100644 --- a/Actions/IncrementVersionNumber/IncrementVersionNumber.psm1 +++ b/Actions/IncrementVersionNumber/IncrementVersionNumber.psm1 @@ -2,13 +2,13 @@ .Synopsis Changes a version setting value in a settings file. .Parameter settingsFilePath - Path to the settings file. The settings file must be a JSON file. + Path to a JSON file containing the settings. .Parameter settingName Name of the setting to change. The setting must be a version number. .Parameter newValue New value of the setting. Allowed values are: +1 (increment major version number), +0.1 (increment minor version number), or a version number in the format Major.Minor (e.g. 1.0 or 1.2 #> -function Set-VersionSettingInFile { +function Set-VersionInSettingsFile { param( [Parameter(Mandatory = $true, ValueFromPipeline = $true)] [string] $settingsFilePath, @@ -90,12 +90,6 @@ function Set-VersionSettingInFile { Write-Host "Changing $settingName from $oldValue to $newValue in $settingsFilePath" $settingFileContent.$settingName = $newValue.ToString() $settingFileContent | Set-JsonContentLF -Path $settingsFilePath - - return @{ - name = $settingFileContent.name - version = $settingFileContent.version - publisher = $settingFileContent.publisher - } } <# @@ -108,50 +102,61 @@ function Set-VersionSettingInFile { Base folder of the repository. .Parameter project Name of the project (relative to the base folder). - .Parameter newVersion + .Parameter newValue New version number. If the version number starts with a +, the new version number will be added to the old version number. Else the new version number will replace the old version number. #> -function Set-ProjectVersion($projectPath, $projectSettings, $newVersion, $ALGOsettingsFile) { - # Set repoVersion in project settings (if it exists) - $projectSettingsPath = Join-Path $projectPath $ALGoSettingsFile - Set-VersionSettingInFile -settingsFilePath $projectSettingsPath -settingName 'repoVersion' -newValue $newVersion | Out-Null +function Set-VersionInAppManifests($projectPath, $projectSettings, $newValue) { # Check if the project uses repoVersion versioning strategy $useRepoVersion = (($projectSettings.PSObject.Properties.Name -eq "versioningStrategy") -and (($projectSettings.versioningStrategy -band 16) -eq 16)) if ($useRepoVersion) { - $newVersion = $projectSettings.repoVersion + $newValue = $projectSettings.repoVersion } $allAppFolders = @($projectSettings.appFolders) + @($projectSettings.testFolders) + @($projectSettings.bcptTestFolders) # Set version in app.json files - $appInfos = $allAppFolders | ForEach-Object { - $folder = Join-Path $projectPath $_ - $appJsonFile = Join-Path $folder "app.json" + $allAppFolders | ForEach-Object { + $appFolder = Join-Path $projectPath $_ + $appJson = Join-Path $appFolder "app.json" - $appInfo = Set-VersionSettingInFile -settingsFilePath $appJsonFile -settingName 'version' -newValue $newVersion - return $appInfo + Set-VersionInSettingsFile -settingsFilePath $appJson -settingName 'version' -newValue $newValue } +} - # Set the version in the dependencies in app.json files - $allAppFolders | ForEach-Object { - $folder = Join-Path $projectPath $_ - $appJsonFile = Join-Path $folder "app.json" - - $appJsonContent = Get-Content $appJsonFile -Raw -Encoding UTF8 | ConvertFrom-Json - $dependencies = $appJsonContent.dependencies - if ($null -ne $dependencies) { - $dependencies | ForEach-Object { - $dependency = $_ - # Find the version of the dependency in the appInfos. If it's found, it means the dependency is part of the project and the version should be set. - $dependencyAppInfo = $appInfos | Where-Object { $_.name -eq $dependency.name -and $_.publisher -eq $dependency.publisher } - if ($null -ne $dependencyAppInfo) { - $dependency.version = $dependencyAppInfo.version - } +function Set-DependenciesVersionInAppManifests { + param( + [Parameter(Mandatory = $true)] + [string[]] $appFolders + ) + + # Get all apps info: app ID and app version + $appsInfos = @($appFolders | ForEach-Object { + $appJson = Join-Path $_ "app.json" + $app = Get-Content -Path $appJson -Raw | ConvertFrom-Json + return [PSCustomObject]@{ + Id = $app.id + Version = $app.version + } + }) + + # Update dependencies in app.json files + $appFolders | ForEach-Object { + $appJsonPath = Join-Path $_ "app.json" + + $appJson = Get-Content -Path $appJsonPath -Raw | ConvertFrom-Json + + $dependencies = $appJson.dependencies + + $dependencies | ForEach-Object { + $dependency = $_ + $appInfo = $appsInfos | Where-Object { $_.Id -eq $dependency.id } + if ($appInfo) { + $dependency.version = $appInfo.Version } - $appJsonContent.dependencies = $dependencies - $appJsonContent | Set-JsonContentLF -Path $appJsonFile } + + $appJson | ConvertTo-Json -Depth 99 | Set-Content -Path $appJsonPath } } -Export-ModuleMember -Function Set-VersionSettingInFile, Set-ProjectVersion \ No newline at end of file +Export-ModuleMember -Function Set-VersionInSettingsFile, Set-VersionInAppManifests, Set-DependenciesVersionInAppManifests \ No newline at end of file diff --git a/Tests/IncrementVersionNumber.Action.Test.ps1 b/Tests/IncrementVersionNumber.Action.Test.ps1 index 25624e779..683a9451d 100644 --- a/Tests/IncrementVersionNumber.Action.Test.ps1 +++ b/Tests/IncrementVersionNumber.Action.Test.ps1 @@ -29,207 +29,207 @@ Describe "IncrementVersionNumber Action Tests" { # Call action } -Describe "Set-VersionSettingInFile tests" { +Describe "Set-VersionInSettingsFile tests" { BeforeAll { . (Join-Path -Path $PSScriptRoot -ChildPath "..\Actions\AL-Go-Helper.ps1" -Resolve) Import-Module (Join-Path -path $PSScriptRoot -ChildPath "..\Actions\IncrementVersionNumber\IncrementVersionNumber.psm1" -Resolve) -Force - $settingsFilePath = "" # Might be used in tests, used in AfterEach + $settingsFile = "" # Might be used in tests, used in AfterEach - function New-TestSettingsFile { + function New-TestSettingsFilePath { param( [string] $repoVersion = '0.1' ) # Create test JSON settings file in the temp folder - $settingsFilePath = Join-Path ([System.IO.Path]::GetTempPath()) "$([Guid]::NewGuid().ToString()).json" + $settingsFile = Join-Path ([System.IO.Path]::GetTempPath()) "$([Guid]::NewGuid().ToString()).json" $settingsFileContent = [ordered]@{ "repoVersion" = $repoVersion "otherSetting" = "otherSettingValue" } - $settingsFileContent | ConvertTo-Json | Set-Content $settingsFilePath -Encoding UTF8 + $settingsFileContent | ConvertTo-Json | Set-Content $settingsFile -Encoding UTF8 - return $settingsFilePath + return $settingsFile } } - It 'Set-VersionSettingInFile -settingsFilePath not found' { - $nonExistingFile = Join-Path ([System.IO.Path]::GetTempPath()) "UnknownFile.json" + It 'Set-VersionInSettingsFile -settingsFilePath not found' { + $nonExistingFile = Join-Path ([System.IO.Path]::GetTempPath()) "unknown.json" $settingName = 'repoVersion' $newValue = '1.0' - { Set-VersionSettingInFile -settingsFilePath $nonExistingFile -settingName $settingName -newValue $newValue } | Should -Throw "Settings file ($nonExistingFile) not found." + { Set-VersionInSettingsFile -settingsFilePath $nonExistingFile -settingName $settingName -newValue $newValue } | Should -Throw "Settings file ($nonExistingFile) not found." } - It 'Set-VersionSettingInFile -settingName not found' { - $settingsFilePath = New-TestSettingsFile + It 'Set-VersionInSettingsFile version setting not found' { + $settingsFile = New-TestSettingsFilePath $settingName = 'repoVersion2' $newValue = '1.0' - { Set-VersionSettingInFile -settingsFilePath $settingsFilePath -settingName $settingName -newValue $newValue } | Should -Not -Throw + { Set-VersionInSettingsFile -settingsFilePath $settingsFile -settingName $settingName -newValue $newValue } | Should -Not -Throw - $newSettingsContent = Get-Content $settingsFilePath -Encoding UTF8 | ConvertFrom-Json + $newSettingsContent = Get-Content $settingsFile -Encoding UTF8 | ConvertFrom-Json $newSettingsContent | Should -Not -Contain $settingName # Check that the other setting are not changed $newSettingsContent.otherSetting | Should -Be "otherSettingValue" $newSettingsContent.repoVersion | Should -Be "0.1" } - It 'Set-VersionSettingInFile -newValue +0.2 is not allowed' { - $settingsFilePath = New-TestSettingsFile + It 'Set-VersionInSettingsFile -newValue +0.2 is not allowed' { + $settingsFile = New-TestSettingsFilePath $settingName = 'repoVersion' $newValue = '+0.2' - { Set-VersionSettingInFile -settingsFilePath $settingsFilePath -settingName $settingName -newValue $newValue } | Should -Throw "Incremental version number $newValue is not allowed. Allowed incremental version numbers are: +1, +0.1" + { Set-VersionInSettingsFile -settingsFilePath $settingsFile -settingName $settingName -newValue $newValue } | Should -Throw "Incremental version number $newValue is not allowed. Allowed incremental version numbers are: +1, +0.1" # Check that the settings are not changed - $newSettingsContent = Get-Content $settingsFilePath -Encoding UTF8 | ConvertFrom-Json + $newSettingsContent = Get-Content $settingsFile -Encoding UTF8 | ConvertFrom-Json $newSettingsContent.$settingName | Should -Be "0.1" $newSettingsContent.otherSetting | Should -Be "otherSettingValue" } - It 'Set-VersionSettingInFile -newValue +3 is not allowed' { - $settingsFilePath = New-TestSettingsFile + It 'Set-VersionInSettingsFile -newValue +3 is not allowed' { + $settingsFile = New-TestSettingsFilePath $settingName = 'repoVersion' $newValue = '+3' - { Set-VersionSettingInFile -settingsFilePath $settingsFilePath -settingName $settingName -newValue $newValue } | Should -Throw "Incremental version number $newValue is not allowed. Allowed incremental version numbers are: +1, +0.1" + { Set-VersionInSettingsFile -settingsFilePath $settingsFile -settingName $settingName -newValue $newValue } | Should -Throw "Incremental version number $newValue is not allowed. Allowed incremental version numbers are: +1, +0.1" # Check that the settings are not changed - $newSettingsContent = Get-Content $settingsFilePath -Encoding UTF8 | ConvertFrom-Json + $newSettingsContent = Get-Content $settingsFile -Encoding UTF8 | ConvertFrom-Json $newSettingsContent.$settingName | Should -Be "0.1" $newSettingsContent.otherSetting | Should -Be "otherSettingValue" } - It 'Set-VersionSettingInFile -newValue 1.2.3 is not allowed' { - $settingsFilePath = New-TestSettingsFile + It 'Set-VersionInSettingsFile -newValue 1.2.3 is not allowed' { + $settingsFile = New-TestSettingsFilePath $settingName = 'repoVersion' $newValue = '1.2.3' - { Set-VersionSettingInFile -settingsFilePath $settingsFilePath -settingName $settingName -newValue $newValue } | Should -Throw "Version number $newValue is not in the correct format. The version number must be in the format Major.Minor (e.g. 1.0 or 1.2)" + { Set-VersionInSettingsFile -settingsFilePath $settingsFile -settingName $settingName -newValue $newValue } | Should -Throw "Version number $newValue is not in the correct format. The version number must be in the format Major.Minor (e.g. 1.0 or 1.2)" # Check that the settings are not changed - $newSettingsContent = Get-Content $settingsFilePath -Encoding UTF8 | ConvertFrom-Json + $newSettingsContent = Get-Content $settingsFile -Encoding UTF8 | ConvertFrom-Json $newSettingsContent.$settingName | Should -Be "0.1" $newSettingsContent.otherSetting | Should -Be "otherSettingValue" } - It 'Set-VersionSettingInFile -newValue -1 is not allowed' { - $settingsFilePath = New-TestSettingsFile + It 'Set-VersionInSettingsFile -newValue -1 is not allowed' { + $settingsFile = New-TestSettingsFilePath $settingName = 'repoVersion' $newValue = '-1' - { Set-VersionSettingInFile -settingsFilePath $settingsFilePath -settingName $settingName -newValue $newValue } | Should -Throw "Version number $newValue is not in the correct format. The version number must be in the format Major.Minor (e.g. 1.0 or 1.2)" + { Set-VersionInSettingsFile -settingsFilePath $settingsFile -settingName $settingName -newValue $newValue } | Should -Throw "Version number $newValue is not in the correct format. The version number must be in the format Major.Minor (e.g. 1.0 or 1.2)" # Check that the settings are not changed - $newSettingsContent = Get-Content $settingsFilePath -Encoding UTF8 | ConvertFrom-Json + $newSettingsContent = Get-Content $settingsFile -Encoding UTF8 | ConvertFrom-Json $newSettingsContent.$settingName | Should -Be "0.1" $newSettingsContent.otherSetting | Should -Be "otherSettingValue" } - It 'Set-VersionSettingInFile -newValue -1 is not allowed' { - $settingsFilePath = New-TestSettingsFile + It 'Set-VersionInSettingsFile -newValue -1 is not allowed' { + $settingsFile = New-TestSettingsFilePath $settingName = 'repoVersion' $newValue = '-1' - { Set-VersionSettingInFile -settingsFilePath $settingsFilePath -settingName $settingName -newValue $newValue } | Should -Throw "Version number $newValue is not in the correct format. The version number must be in the format Major.Minor (e.g. 1.0 or 1.2)" + { Set-VersionInSettingsFile -settingsFilePath $settingsFile -settingName $settingName -newValue $newValue } | Should -Throw "Version number $newValue is not in the correct format. The version number must be in the format Major.Minor (e.g. 1.0 or 1.2)" # Check that the settings are not changed - $newSettingsContent = Get-Content $settingsFilePath -Encoding UTF8 | ConvertFrom-Json + $newSettingsContent = Get-Content $settingsFile -Encoding UTF8 | ConvertFrom-Json $newSettingsContent.$settingName | Should -Be "0.1" $newSettingsContent.otherSetting | Should -Be "otherSettingValue" } - It 'Set-VersionSettingInFile -newValue abcd is not allowed' { - $settingsFilePath = New-TestSettingsFile + It 'Set-VersionInSettingsFile -newValue abcd is not allowed' { + $settingsFile = New-TestSettingsFilePath $settingName = 'repoVersion' $newValue = 'abcd' - { Set-VersionSettingInFile -settingsFilePath $settingsFilePath -settingName $settingName -newValue $newValue } | Should -Throw "Version number $newValue is not in the correct format. The version number must be in the format Major.Minor (e.g. 1.0 or 1.2)" + { Set-VersionInSettingsFile -settingsFilePath $settingsFile -settingName $settingName -newValue $newValue } | Should -Throw "Version number $newValue is not in the correct format. The version number must be in the format Major.Minor (e.g. 1.0 or 1.2)" # Check that the settings are not changed - $newSettingsContent = Get-Content $settingsFilePath -Encoding UTF8 | ConvertFrom-Json + $newSettingsContent = Get-Content $settingsFile -Encoding UTF8 | ConvertFrom-Json $newSettingsContent.$settingName | Should -Be "0.1" $newSettingsContent.otherSetting | Should -Be "otherSettingValue" } - It 'Set-VersionSettingInFile -newValue a.b is not allowed' { - $settingsFilePath = New-TestSettingsFile + It 'Set-VersionInSettingsFile -newValue a.b is not allowed' { + $settingsFile = New-TestSettingsFilePath $settingName = 'repoVersion' $newValue = 'a.b' - { Set-VersionSettingInFile -settingsFilePath $settingsFilePath -settingName $settingName -newValue $newValue } | Should -Throw "Version number $newValue is not in the correct format. The version number must be in the format Major.Minor (e.g. 1.0 or 1.2)" + { Set-VersionInSettingsFile -settingsFilePath $settingsFile -settingName $settingName -newValue $newValue } | Should -Throw "Version number $newValue is not in the correct format. The version number must be in the format Major.Minor (e.g. 1.0 or 1.2)" # Check that the settings are not changed - $newSettingsContent = Get-Content $settingsFilePath -Encoding UTF8 | ConvertFrom-Json + $newSettingsContent = Get-Content $settingsFile -Encoding UTF8 | ConvertFrom-Json $newSettingsContent.$settingName | Should -Be "0.1" $newSettingsContent.otherSetting | Should -Be "otherSettingValue" } - It 'Set-VersionSettingInFile -newValue 1.0 is set' { - $settingsFilePath = New-TestSettingsFile + It 'Set-VersionInSettingsFile -newValue 1.0 is set' { + $settingsFile = New-TestSettingsFilePath $settingName = 'repoVersion' $newValue = '1.0' - Set-VersionSettingInFile -settingsFilePath $settingsFilePath -settingName $settingName -newValue $newValue + Set-VersionInSettingsFile -settingsFilePath $settingsFile -settingName $settingName -newValue $newValue - $newSettingsContent = Get-Content $settingsFilePath -Encoding UTF8 | ConvertFrom-Json + $newSettingsContent = Get-Content $settingsFile -Encoding UTF8 | ConvertFrom-Json $newSettingsContent.$settingName | Should -Be "1.0" # Check that the other setting are not changed $newSettingsContent.otherSetting | Should -Be "otherSettingValue" } - It 'Set-VersionSettingInFile -newValue +1 increments the major version number and sets the minor version number to 0' { - $settingsFilePath = New-TestSettingsFile + It 'Set-VersionInSettingsFile -newValue +1 increments the major version number and sets the minor version number to 0' { + $settingsFile = New-TestSettingsFilePath $settingName = 'repoVersion' $newValue = '+1' - Set-VersionSettingInFile -settingsFilePath $settingsFilePath -settingName $settingName -newValue $newValue + Set-VersionInSettingsFile -settingsFilePath $settingsFile -settingName $settingName -newValue $newValue - $newSettingsContent = Get-Content $settingsFilePath -Encoding UTF8 | ConvertFrom-Json + $newSettingsContent = Get-Content $settingsFile -Encoding UTF8 | ConvertFrom-Json $newSettingsContent.$settingName | Should -Be "1.0" # Check that the other setting are not changed $newSettingsContent.otherSetting | Should -Be "otherSettingValue" } - It 'Set-VersionSettingInFile -newValue +0.1 increments the minor version number' { - $settingsFilePath = New-TestSettingsFile -repoVersion '1.2' + It 'Set-VersionInSettingsFile -newValue +0.1 incremenFilents the minor version number' { + $settingsFile = New-TestSettingsFilePath -repoVersion '1.2' $settingName = 'repoVersion' $newValue = '+0.1' - Set-VersionSettingInFile -settingsFilePath $settingsFilePath -settingName $settingName -newValue $newValue + Set-VersionInSettingsFile -settingsFilePath $settingsFile -settingName $settingName -newValue $newValue - $newSettingsContent = Get-Content $settingsFilePath -Encoding UTF8 | ConvertFrom-Json + $newSettingsContent = Get-Content $settingsFile -Encoding UTF8 | ConvertFrom-Json $newSettingsContent.$settingName | Should -Be "1.3" # Check that the other setting are not changed $newSettingsContent.otherSetting | Should -Be "otherSettingValue" } - It 'Set-VersionSettingInFile -newValue is set and build and revision are kept from the old value'{ - $settingsFilePath = New-TestSettingsFile -repoVersion '1.2.0.0' + It 'Set-VersionInSettingsFile -newValue is set and bnFileuild and revision are kept from the old value'{ + $settingsFile = New-TestSettingsFilePath -repoVersion '1.2.0.0' $settingName = 'repoVersion' $newValue = '2.1' - Set-VersionSettingInFile -settingsFilePath $settingsFilePath -settingName $settingName -newValue $newValue + Set-VersionInSettingsFile -settingsFilePath $settingsFile -settingName $settingName -newValue $newValue - $newSettingsContent = Get-Content $settingsFilePath -Encoding UTF8 | ConvertFrom-Json + $newSettingsContent = Get-Content $settingsFile -Encoding UTF8 | ConvertFrom-Json $newSettingsContent.$settingName | Should -Be "2.1.0.0" # Check that the other setting are not changed $newSettingsContent.otherSetting | Should -Be "otherSettingValue" } - It 'Set-VersionSettingInFile -newValue +0.1 increments the minor version number and build and revision are kept from the old value' { - $settingsFilePath = New-TestSettingsFile -repoVersion '1.2.0.0' + It 'Set-VersionInSettingsFile -newValue +0.1 incremenFilents the minor version number and build and revision are kept from the old value' { + $settingsFile = New-TestSettingsFilePath -repoVersion '1.2.0.0' $settingName = 'repoVersion' $newValue = '+0.1' - Set-VersionSettingInFile -settingsFilePath $settingsFilePath -settingName $settingName -newValue $newValue + Set-VersionInSettingsFile -settingsFilePath $settingsFile -settingName $settingName -newValue $newValue - $newSettingsContent = Get-Content $settingsFilePath -Encoding UTF8 | ConvertFrom-Json + $newSettingsContent = Get-Content $settingsFile -Encoding UTF8 | ConvertFrom-Json $newSettingsContent.$settingName | Should -Be "1.3.0.0" # Check that the other setting are not changed @@ -237,6 +237,6 @@ Describe "Set-VersionSettingInFile tests" { } AfterEach { - Remove-Item $settingsFilePath -Force -ErrorAction Ignore + Remove-Item $settingsFile -Force -ErrorAction Ignore } } From b20a6cdc1ea3d28578b4379202855d12ce8fb66e Mon Sep 17 00:00:00 2001 From: mazhelez Date: Thu, 8 Feb 2024 17:26:55 +0100 Subject: [PATCH 22/33] Add doc for Set-DependenciesVersionInAppManifests --- .../IncrementVersionNumber/IncrementVersionNumber.psm1 | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Actions/IncrementVersionNumber/IncrementVersionNumber.psm1 b/Actions/IncrementVersionNumber/IncrementVersionNumber.psm1 index 000eb9ab5..653837d85 100644 --- a/Actions/IncrementVersionNumber/IncrementVersionNumber.psm1 +++ b/Actions/IncrementVersionNumber/IncrementVersionNumber.psm1 @@ -123,6 +123,15 @@ function Set-VersionInAppManifests($projectPath, $projectSettings, $newValue) { } } +<# + .Synopsis + Changes the version number of dependencies in app.json files. + .Description + Changes the version number of dependencies in app.json files. + The version number of the dependencies is changed to the version number of the app that the dependency refers to. If the app is not found, the version number of the dependency is not changed. + .Parameter appFolders + Array of paths to the app folders. Each app folder must contain an app.json file. The apps are used to get the version number of the dependencies. +#> function Set-DependenciesVersionInAppManifests { param( [Parameter(Mandatory = $true)] From 3f885fd86aa38a1d677d033577c354fd2e365d4e Mon Sep 17 00:00:00 2001 From: mazhelez Date: Thu, 8 Feb 2024 17:27:31 +0100 Subject: [PATCH 23/33] Remove ValueFromPipeline (unused anyway) --- Actions/IncrementVersionNumber/IncrementVersionNumber.psm1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Actions/IncrementVersionNumber/IncrementVersionNumber.psm1 b/Actions/IncrementVersionNumber/IncrementVersionNumber.psm1 index 653837d85..4915c31b1 100644 --- a/Actions/IncrementVersionNumber/IncrementVersionNumber.psm1 +++ b/Actions/IncrementVersionNumber/IncrementVersionNumber.psm1 @@ -10,7 +10,7 @@ #> function Set-VersionInSettingsFile { param( - [Parameter(Mandatory = $true, ValueFromPipeline = $true)] + [Parameter(Mandatory = $true)] [string] $settingsFilePath, [Parameter(Mandatory = $true)] [string] $settingName, From 5138ce869b246cc26ec26cf79630ed64a3fe11df Mon Sep 17 00:00:00 2001 From: mazhelez Date: Thu, 8 Feb 2024 17:28:27 +0100 Subject: [PATCH 24/33] Remove version validation (it's validated when set) --- Actions/IncrementVersionNumber/IncrementVersionNumber.ps1 | 5 ----- 1 file changed, 5 deletions(-) diff --git a/Actions/IncrementVersionNumber/IncrementVersionNumber.ps1 b/Actions/IncrementVersionNumber/IncrementVersionNumber.ps1 index d7fb2d5c1..ad1e5b90d 100644 --- a/Actions/IncrementVersionNumber/IncrementVersionNumber.ps1 +++ b/Actions/IncrementVersionNumber/IncrementVersionNumber.ps1 @@ -28,11 +28,6 @@ try { Import-Module (Join-Path -path $PSScriptRoot -ChildPath "..\TelemetryHelper.psm1" -Resolve) $telemetryScope = CreateScope -eventId 'DO0076' -parentTelemetryScopeJson $parentTelemetryScopeJson - # Validate version number. It should match . or +. - if(-not ($versionNumber -match '^\+?\d+\.\d+$')) { - throw "Version number ($versionNumber) is malformed. A version number must be structured as . or +." - } - # Change repoVersion in repository settings Set-VersionInSettingsFile -settingsFilePath (Join-Path $baseFolder $RepoSettingsFile) -settingName 'repoVersion' -newValue $versionNumber | Out-Null # $RepoSettingsFile is defined in AL-Go-Helper.ps1 From b7ee172ac2d13a78221fb217822ce3e9f3f6515b Mon Sep 17 00:00:00 2001 From: mazhelez Date: Thu, 8 Feb 2024 17:32:41 +0100 Subject: [PATCH 25/33] Fix var ref --- Actions/IncrementVersionNumber/IncrementVersionNumber.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Actions/IncrementVersionNumber/IncrementVersionNumber.ps1 b/Actions/IncrementVersionNumber/IncrementVersionNumber.ps1 index ad1e5b90d..282c976ad 100644 --- a/Actions/IncrementVersionNumber/IncrementVersionNumber.ps1 +++ b/Actions/IncrementVersionNumber/IncrementVersionNumber.ps1 @@ -40,14 +40,14 @@ try { # Set repoVersion in project settings (if it exists) $projectSettingsPath = Join-Path $projectPath $ALGoSettingsFile # $ALGoSettingsFile is defined in AL-Go-Helper.ps1 - Set-VersionInSettingsFile -settingsFilePath $projectSettingsPath -settingName 'repoVersion' -newValue $newVersion + Set-VersionInSettingsFile -settingsFilePath $projectSettingsPath -settingName 'repoVersion' -newValue $versionNumber # Resolve project folders to get all app folders that contain an app.json file $projectSettings = ReadSettings -baseFolder $baseFolder -project $project ResolveProjectFolders -baseFolder $baseFolder -project $project -projectSettings ([ref] $projectSettings) # Set version in app manifests (app.json files) - Set-VersionInAppManifests -projectPath $projectPath -projectSettings $projectSettings -newValue $newVersion + Set-VersionInAppManifests -projectPath $projectPath -projectSettings $projectSettings -newValue $versionNumber # Collect all project's app folders $allAppFolders += $projectSettings.appFolders | ForEach-Object { Join-Path $projectPath $_ -Resolve } From 62375775e5e739e7650aa9884ff46f95e29bcd3f Mon Sep 17 00:00:00 2001 From: mazhelez Date: Thu, 8 Feb 2024 17:36:21 +0100 Subject: [PATCH 26/33] Fix versionNumber doc --- Actions/IncrementVersionNumber/IncrementVersionNumber.ps1 | 2 +- Actions/IncrementVersionNumber/action.yaml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Actions/IncrementVersionNumber/IncrementVersionNumber.ps1 b/Actions/IncrementVersionNumber/IncrementVersionNumber.ps1 index 282c976ad..f91a019b4 100644 --- a/Actions/IncrementVersionNumber/IncrementVersionNumber.ps1 +++ b/Actions/IncrementVersionNumber/IncrementVersionNumber.ps1 @@ -7,7 +7,7 @@ [string] $parentTelemetryScopeJson = '7b7d', [Parameter(HelpMessage = "List of project names if the repository is setup for multiple projects (* for all projects)", Mandatory = $false)] [string] $projects = '*', - [Parameter(HelpMessage = "Updated Version Number. Use Major.Minor for absolute change, use +Major.Minor for incremental change.", Mandatory = $true)] + [Parameter(HelpMessage = "The version to update to. Use Major.Minor for absolute change, use +1 to bump to the next major version, use +0.1 to bump to the next minor version", Mandatory = $true)] [string] $versionNumber, [Parameter(HelpMessage = "Set the branch to update", Mandatory = $false)] [string] $updateBranch, diff --git a/Actions/IncrementVersionNumber/action.yaml b/Actions/IncrementVersionNumber/action.yaml index b31c64be9..58866ff22 100644 --- a/Actions/IncrementVersionNumber/action.yaml +++ b/Actions/IncrementVersionNumber/action.yaml @@ -25,7 +25,7 @@ inputs: required: false default: '*' versionNumber: - description: Updated Version Number. Use Major.Minor for absolute change, use +Major.Minor for incremental change. + description: The version to update to. Use Major.Minor for absolute change, use +1 to bump to the next major version, use +0.1 to bump to the next minor version required: true updateBranch: description: Set the branch to update From df2a6c740c0a28fc199da837d899528300d15c20 Mon Sep 17 00:00:00 2001 From: mazhelez Date: Thu, 8 Feb 2024 17:36:53 +0100 Subject: [PATCH 27/33] Fix versionNumber doc (2) --- Actions/IncrementVersionNumber/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Actions/IncrementVersionNumber/README.md b/Actions/IncrementVersionNumber/README.md index e77578bf5..cdfcaaf76 100644 --- a/Actions/IncrementVersionNumber/README.md +++ b/Actions/IncrementVersionNumber/README.md @@ -16,7 +16,7 @@ Increment version number in AL-Go repository | token | | The GitHub token running the action | github.token | | parentTelemetryScopeJson | | Specifies the parent telemetry scope for the telemetry signal | {} | | projects | | List of project names if the repository is setup for multiple projects (* for all projects) | * | -| versionNumber | Yes | Updated Version Number. Use Major.Minor for absolute change, use +Major.Minor for incremental change | | +| versionNumber | Yes | The version to update to. Use Major.Minor for absolute change, use +1 to bump to the next major version, use +0.1 to bump to the next minor version | | | updateBranch | | Which branch should the app be added to | github.ref_name | | directCommit | | true if the action should create a direct commit against the branch or false to create a Pull Request | false | From cc05a3a070b034fe20304dea910eecaaec490e01 Mon Sep 17 00:00:00 2001 From: mazhelez Date: Thu, 8 Feb 2024 17:37:46 +0100 Subject: [PATCH 28/33] Fix param refernce --- Actions/IncrementVersionNumber/IncrementVersionNumber.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Actions/IncrementVersionNumber/IncrementVersionNumber.ps1 b/Actions/IncrementVersionNumber/IncrementVersionNumber.ps1 index f91a019b4..7abfe6261 100644 --- a/Actions/IncrementVersionNumber/IncrementVersionNumber.ps1 +++ b/Actions/IncrementVersionNumber/IncrementVersionNumber.ps1 @@ -56,7 +56,7 @@ try { } # Set dependencies in app manifests - Set-DependenciesVersionInAppManifests -allAppFolders $allAppFolders + Set-DependenciesVersionInAppManifests -appFolders $allAppFolders $commitMessage = "New Version number $versionNumber" if ($versionNumber.StartsWith('+')) { From abbfa8d468bbb4e06929f6eceac13f6083fbf9a2 Mon Sep 17 00:00:00 2001 From: mazhelez Date: Fri, 9 Feb 2024 23:19:45 +0100 Subject: [PATCH 29/33] Use -Encoding UTF8 when reading JSON files --- Actions/IncrementVersionNumber/IncrementVersionNumber.psm1 | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Actions/IncrementVersionNumber/IncrementVersionNumber.psm1 b/Actions/IncrementVersionNumber/IncrementVersionNumber.psm1 index 4915c31b1..336a79394 100644 --- a/Actions/IncrementVersionNumber/IncrementVersionNumber.psm1 +++ b/Actions/IncrementVersionNumber/IncrementVersionNumber.psm1 @@ -25,7 +25,7 @@ function Set-VersionInSettingsFile { Write-Host "Reading settings from $settingsFilePath" try { - $settingFileContent = Get-Content $settingsFilePath -Encoding UTF8 | ConvertFrom-Json + $settingFileContent = Get-Content $settingsFilePath -Encoding UTF8 -Raw | ConvertFrom-Json } catch { throw "Settings file ($settingsFilePath) is malformed: $_" @@ -141,7 +141,7 @@ function Set-DependenciesVersionInAppManifests { # Get all apps info: app ID and app version $appsInfos = @($appFolders | ForEach-Object { $appJson = Join-Path $_ "app.json" - $app = Get-Content -Path $appJson -Raw | ConvertFrom-Json + $app = Get-Content -Path $appJson -Encoding UTF8 -Raw | ConvertFrom-Json return [PSCustomObject]@{ Id = $app.id Version = $app.version @@ -152,7 +152,7 @@ function Set-DependenciesVersionInAppManifests { $appFolders | ForEach-Object { $appJsonPath = Join-Path $_ "app.json" - $appJson = Get-Content -Path $appJsonPath -Raw | ConvertFrom-Json + $appJson = Get-Content -Path $appJsonPath -Encoding UTF8 -Raw | ConvertFrom-Json $dependencies = $appJson.dependencies From 8770682bde9633a9a43abbac0658990eccbb6e38 Mon Sep 17 00:00:00 2001 From: mazhelez Date: Sat, 10 Feb 2024 00:25:27 +0100 Subject: [PATCH 30/33] Use Set-JsonContentLF --- Actions/IncrementVersionNumber/IncrementVersionNumber.psm1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Actions/IncrementVersionNumber/IncrementVersionNumber.psm1 b/Actions/IncrementVersionNumber/IncrementVersionNumber.psm1 index 336a79394..527505c33 100644 --- a/Actions/IncrementVersionNumber/IncrementVersionNumber.psm1 +++ b/Actions/IncrementVersionNumber/IncrementVersionNumber.psm1 @@ -164,7 +164,7 @@ function Set-DependenciesVersionInAppManifests { } } - $appJson | ConvertTo-Json -Depth 99 | Set-Content -Path $appJsonPath + $appJson | Set-JsonContentLF -Path $appJsonPath } } From 1441efbf8219480ab0b900a5e2adba52312d48d1 Mon Sep 17 00:00:00 2001 From: mazhelez Date: Sat, 10 Feb 2024 00:32:18 +0100 Subject: [PATCH 31/33] Add logs --- Actions/IncrementVersionNumber/IncrementVersionNumber.psm1 | 1 + 1 file changed, 1 insertion(+) diff --git a/Actions/IncrementVersionNumber/IncrementVersionNumber.psm1 b/Actions/IncrementVersionNumber/IncrementVersionNumber.psm1 index 527505c33..e7db8ec6c 100644 --- a/Actions/IncrementVersionNumber/IncrementVersionNumber.psm1 +++ b/Actions/IncrementVersionNumber/IncrementVersionNumber.psm1 @@ -160,6 +160,7 @@ function Set-DependenciesVersionInAppManifests { $dependency = $_ $appInfo = $appsInfos | Where-Object { $_.Id -eq $dependency.id } if ($appInfo) { + Write-Host "Updating dependency app $($dependency.id) in $appJsonPath from $($dependency.version) to $($appInfo.Version)" $dependency.version = $appInfo.Version } } From 1dba93deaf941d3a951736b5f8dda2fc59aea375 Mon Sep 17 00:00:00 2001 From: mazhelez Date: Sat, 10 Feb 2024 00:39:28 +0100 Subject: [PATCH 32/33] Add docs --- Actions/IncrementVersionNumber/IncrementVersionNumber.ps1 | 2 +- Actions/IncrementVersionNumber/IncrementVersionNumber.psm1 | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/Actions/IncrementVersionNumber/IncrementVersionNumber.ps1 b/Actions/IncrementVersionNumber/IncrementVersionNumber.ps1 index 7abfe6261..b8520541c 100644 --- a/Actions/IncrementVersionNumber/IncrementVersionNumber.ps1 +++ b/Actions/IncrementVersionNumber/IncrementVersionNumber.ps1 @@ -29,7 +29,7 @@ try { $telemetryScope = CreateScope -eventId 'DO0076' -parentTelemetryScopeJson $parentTelemetryScopeJson # Change repoVersion in repository settings - Set-VersionInSettingsFile -settingsFilePath (Join-Path $baseFolder $RepoSettingsFile) -settingName 'repoVersion' -newValue $versionNumber | Out-Null # $RepoSettingsFile is defined in AL-Go-Helper.ps1 + Set-VersionInSettingsFile -settingsFilePath (Join-Path $baseFolder $RepoSettingsFile) -settingName 'repoVersion' -newValue $versionNumber # $RepoSettingsFile is defined in AL-Go-Helper.ps1 $settings = $env:Settings | ConvertFrom-Json $projectList = @(GetProjectsFromRepository -baseFolder $baseFolder -projectsFromSettings $settings.projects -selectProjects $projects) diff --git a/Actions/IncrementVersionNumber/IncrementVersionNumber.psm1 b/Actions/IncrementVersionNumber/IncrementVersionNumber.psm1 index e7db8ec6c..6d4e5032c 100644 --- a/Actions/IncrementVersionNumber/IncrementVersionNumber.psm1 +++ b/Actions/IncrementVersionNumber/IncrementVersionNumber.psm1 @@ -1,6 +1,9 @@ <# .Synopsis Changes a version setting value in a settings file. + .Description + Changes a version setting value in a settings file. + If the setting does not exist in the settings file, the function does nothing. .Parameter settingsFilePath Path to a JSON file containing the settings. .Parameter settingName From 78bba8229e8c55b7fc95e0c559986ef3b02061c8 Mon Sep 17 00:00:00 2001 From: mazhelez Date: Sat, 10 Feb 2024 01:36:07 +0100 Subject: [PATCH 33/33] Ensure repoVersion is set in the repo settings. Add Force parameter on Set-VersionInSettingsFile --- .../IncrementVersionNumber.ps1 | 8 ++- .../IncrementVersionNumber.psm1 | 61 +++++++++++++------ Tests/IncrementVersionNumber.Action.Test.ps1 | 30 +++++++++ 3 files changed, 79 insertions(+), 20 deletions(-) diff --git a/Actions/IncrementVersionNumber/IncrementVersionNumber.ps1 b/Actions/IncrementVersionNumber/IncrementVersionNumber.ps1 index b8520541c..298a89bec 100644 --- a/Actions/IncrementVersionNumber/IncrementVersionNumber.ps1 +++ b/Actions/IncrementVersionNumber/IncrementVersionNumber.ps1 @@ -28,10 +28,14 @@ try { Import-Module (Join-Path -path $PSScriptRoot -ChildPath "..\TelemetryHelper.psm1" -Resolve) $telemetryScope = CreateScope -eventId 'DO0076' -parentTelemetryScopeJson $parentTelemetryScopeJson + $settings = $env:Settings | ConvertFrom-Json + + # Ensure the repoVersion setting exists in the repository settings. Defaults to 1.0 if it doesn't exist. + Set-VersionInSettingsFile -settingsFilePath (Join-Path $baseFolder $RepoSettingsFile) -settingName 'repoVersion' -newValue $settings.repoVersion -Force # $RepoSettingsFile is defined in AL-Go-Helper.ps1 + # Change repoVersion in repository settings - Set-VersionInSettingsFile -settingsFilePath (Join-Path $baseFolder $RepoSettingsFile) -settingName 'repoVersion' -newValue $versionNumber # $RepoSettingsFile is defined in AL-Go-Helper.ps1 + Set-VersionInSettingsFile -settingsFilePath (Join-Path $baseFolder $RepoSettingsFile) -settingName 'repoVersion' -newValue $versionNumber - $settings = $env:Settings | ConvertFrom-Json $projectList = @(GetProjectsFromRepository -baseFolder $baseFolder -projectsFromSettings $settings.projects -selectProjects $projects) $allAppFolders = @() diff --git a/Actions/IncrementVersionNumber/IncrementVersionNumber.psm1 b/Actions/IncrementVersionNumber/IncrementVersionNumber.psm1 index 6d4e5032c..bc3cbe6d8 100644 --- a/Actions/IncrementVersionNumber/IncrementVersionNumber.psm1 +++ b/Actions/IncrementVersionNumber/IncrementVersionNumber.psm1 @@ -3,13 +3,15 @@ Changes a version setting value in a settings file. .Description Changes a version setting value in a settings file. - If the setting does not exist in the settings file, the function does nothing. + If the setting does not exist in the settings file, the function does nothing, unless the Force parameter is specified. .Parameter settingsFilePath Path to a JSON file containing the settings. .Parameter settingName Name of the setting to change. The setting must be a version number. .Parameter newValue New value of the setting. Allowed values are: +1 (increment major version number), +0.1 (increment minor version number), or a version number in the format Major.Minor (e.g. 1.0 or 1.2 + .Parameter Force + If specified, the function will create the setting if it does not exist in the settings file. #> function Set-VersionInSettingsFile { param( @@ -18,7 +20,8 @@ function Set-VersionInSettingsFile { [Parameter(Mandatory = $true)] [string] $settingName, [Parameter(Mandatory = $true)] - [string] $newValue + [string] $newValue, + [switch] $Force ) #region Validate parameters @@ -28,17 +31,24 @@ function Set-VersionInSettingsFile { Write-Host "Reading settings from $settingsFilePath" try { - $settingFileContent = Get-Content $settingsFilePath -Encoding UTF8 -Raw | ConvertFrom-Json + $settingsJson = Get-Content $settingsFilePath -Encoding UTF8 -Raw | ConvertFrom-Json } catch { throw "Settings file ($settingsFilePath) is malformed: $_" } - if (-not ($settingFileContent.PSObject.Properties.Name -eq $settingName)) { + $settingExists = [bool] ($settingsJson.PSObject.Properties.Name -eq $settingName) + if ((-not $settingExists) -and (-not $Force)) { Write-Host "Setting $settingName not found in $settingsFilePath" return } + # Add the setting if it does not exist + if (-not $settingExists) { + $settingsJson | Add-Member -MemberType NoteProperty -Name $settingName -Value $null + } + + $oldValue = [System.Version] $settingsJson.$settingName # Validate new version value if ($newValue.StartsWith('+')) { # Handle incremental version number @@ -47,6 +57,11 @@ function Set-VersionInSettingsFile { if (-not $allowedIncrementalVersionNumbers.Contains($newValue)) { throw "Incremental version number $newValue is not allowed. Allowed incremental version numbers are: $($allowedIncrementalVersionNumbers -join ', ')" } + + # Defensive check. Should never happen. + if($null -eq $oldValue) { + throw "The setting $settingName does not exist in the settings file. It must exist to be able to increment the version number." + } } else { # Handle absolute version number @@ -58,41 +73,51 @@ function Set-VersionInSettingsFile { } #endregion - $oldValue = [System.Version] $settingFileContent.$settingName - $versions = @() # an array to hold the version numbers: major, minor, build, revision + $versionNumbers = @() # an array to hold the version numbers: major, minor, build, revision switch($newValue) { '+1' { # Increment major version number - $versions += $oldValue.Major + 1 - $versions += 0 + $versionNumbers += $oldValue.Major + 1 + $versionNumbers += 0 } '+0.1' { # Increment minor version number - $versions += $oldValue.Major - $versions += $oldValue.Minor + 1 + $versionNumbers += $oldValue.Major + $versionNumbers += $oldValue.Minor + 1 } default { # Absolute version number - $versions += $newValue.Split('.') + $versionNumbers += $newValue.Split('.') } } # Include build and revision numbers if they exist in the old version number - if ($oldValue.Build -ne -1) { - $versions += $oldValue.Build + if ($oldValue -and ($oldValue.Build -ne -1)) { + $versionNumbers += $oldValue.Build if ($oldValue.Revision -ne -1) { - $versions += $oldValue.Revision + $versionNumbers += $oldValue.Revision } } # Construct the new version number. Cast to System.Version to validate if the version number is valid. - $newValue = [System.Version] "$($versions -join '.')" + $newValue = [System.Version] "$($versionNumbers -join '.')" + + if($newValue -eq $oldValue) { + Write-Host "The setting $settingName is already set to $newValue in $settingsFilePath" + return + } + + if($null -eq $oldValue) { + Write-Host "Setting setting $settingName to $newValue in $settingsFilePath" + } + else { + Write-Host "Changing $settingName from $oldValue to $newValue in $settingsFilePath" + } - Write-Host "Changing $settingName from $oldValue to $newValue in $settingsFilePath" - $settingFileContent.$settingName = $newValue.ToString() - $settingFileContent | Set-JsonContentLF -Path $settingsFilePath + $settingsJson.$settingName = $newValue.ToString() + $settingsJson | Set-JsonContentLF -Path $settingsFilePath } <# diff --git a/Tests/IncrementVersionNumber.Action.Test.ps1 b/Tests/IncrementVersionNumber.Action.Test.ps1 index 683a9451d..1045e7861 100644 --- a/Tests/IncrementVersionNumber.Action.Test.ps1 +++ b/Tests/IncrementVersionNumber.Action.Test.ps1 @@ -75,6 +75,36 @@ Describe "Set-VersionInSettingsFile tests" { $newSettingsContent.repoVersion | Should -Be "0.1" } + It 'Set-VersionInSettingsFile version setting not found and Force is set' { + $settingsFile = New-TestSettingsFilePath + $settingName = 'repoVersion2' + $newValue = '1.0' + + { Set-VersionInSettingsFile -settingsFilePath $settingsFile -settingName $settingName -newValue $newValue -Force } | Should -Not -Throw + + $newSettingsContent = Get-Content $settingsFile -Encoding UTF8 | ConvertFrom-Json + # Check the new setting is created + $newSettingsContent.$settingName | Should -Be "1.0" + + # Check that the other setting are not changed + $newSettingsContent.otherSetting | Should -Be "otherSettingValue" + $newSettingsContent.repoVersion | Should -Be "0.1" + } + + It 'Set-VersionInSettingsFile setting same value' { + $settingsFile = New-TestSettingsFilePath -repoVersion '1.0' + $settingName = 'repoVersion' + $newValue = '1.0' + + { Set-VersionInSettingsFile -settingsFilePath $settingsFile -settingName $settingName -newValue $newValue -Force } | Should -Not -Throw + + $newSettingsContent = Get-Content $settingsFile -Encoding UTF8 | ConvertFrom-Json + + # Check that the other setting are not changed + $newSettingsContent.$settingName | Should -Be "1.0" + $newSettingsContent.otherSetting | Should -Be "otherSettingValue" + } + It 'Set-VersionInSettingsFile -newValue +0.2 is not allowed' { $settingsFile = New-TestSettingsFilePath $settingName = 'repoVersion'