Skip to content

Commit

Permalink
Set dependencies version in app manifest files (app.json)
Browse files Browse the repository at this point in the history
  • Loading branch information
mazhelez committed Feb 8, 2024
1 parent 5ed6470 commit d367b95
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 102 deletions.
20 changes: 17 additions & 3 deletions Actions/IncrementVersionNumber/IncrementVersionNumber.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
79 changes: 42 additions & 37 deletions Actions/IncrementVersionNumber/IncrementVersionNumber.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Check warning

Code scanning / PSScriptAnalyzer

Command accepts pipeline input but has not defined a process block. Warning

Command accepts pipeline input but has not defined a process block.
Expand Down Expand Up @@ -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
}
}

<#
Expand All @@ -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 {

Check notice

Code scanning / PSScriptAnalyzer

The cmdlet 'Set-DependenciesVersionInAppManifests' does not have a help comment. Note

The cmdlet 'Set-DependenciesVersionInAppManifests' does not have a help comment.
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
Export-ModuleMember -Function Set-VersionInSettingsFile, Set-VersionInAppManifests, Set-DependenciesVersionInAppManifests
Loading

0 comments on commit d367b95

Please sign in to comment.