Skip to content

Commit 120540b

Browse files
aholstrup1mazhelezfreddydk
authored
Allow useProjectDependencies as a project-level setting (#1393)
Allow useProjectDependencies as a project-level setting --------- Co-authored-by: Maria Zhelezova <[email protected]> Co-authored-by: Freddy Kristiansen <[email protected]>
1 parent dfa8b44 commit 120540b

File tree

4 files changed

+142
-36
lines changed

4 files changed

+142
-36
lines changed

Actions/AL-Go-Helper.ps1

+31-19
Original file line numberDiff line numberDiff line change
@@ -2090,11 +2090,11 @@ function CheckAndCreateProjectFolder {
20902090
Function AnalyzeProjectDependencies {
20912091
Param(
20922092
[string] $baseFolder,
2093-
[string[]] $projects,
2094-
[ref] $buildAlso,
2095-
[ref] $projectDependencies
2093+
[string[]] $projects
20962094
)
20972095

2096+
$additionalProjectsToBuild = @{}
2097+
$projectDependencies = @{}
20982098
$appDependencies = @{}
20992099
Write-Host "Analyzing projects in $baseFolder"
21002100

@@ -2123,9 +2123,17 @@ Function AnalyzeProjectDependencies {
21232123
$unknownDependencies = @()
21242124
$apps = @()
21252125
Sort-AppFoldersByDependencies -appFolders $folders -baseFolder $baseFolder -WarningAction SilentlyContinue -unknownDependencies ([ref]$unknownDependencies) -knownApps ([ref]$apps) | Out-Null
2126+
2127+
# If the project is using project dependencies, add the unknown dependencies to the list of dependencies
2128+
# If not, the unknown dependencies are ignored
2129+
$dependenciesForProject = @()
2130+
if ($projectSettings.useProjectDependencies -eq $true) {
2131+
$dependenciesForProject = @($unknownDependencies | ForEach-Object { $_.Split(':')[0] })
2132+
}
2133+
21262134
$appDependencies."$project" = @{
21272135
"apps" = $apps
2128-
"dependencies" = @($unknownDependencies | ForEach-Object { $_.Split(':')[0] })
2136+
"dependencies" = $dependenciesForProject
21292137
}
21302138
}
21312139
# AppDependencies is a hashtable with the following structure
@@ -2160,42 +2168,42 @@ Function AnalyzeProjectDependencies {
21602168
# Add this project and all projects on which that project has a dependency to the list of dependencies for the current project
21612169
foreach($depProject in $depProjects) {
21622170
$foundDependencies += $depProject
2163-
if ($projectDependencies.Value.Keys -contains $depProject) {
2164-
$foundDependencies += $projectDependencies.value."$depProject"
2171+
if ($projectDependencies.Keys -contains $depProject) {
2172+
$foundDependencies += $projectDependencies."$depProject"
21652173
}
21662174
}
21672175
}
21682176
$foundDependencies = @($foundDependencies | Select-Object -Unique)
21692177
# foundDependencies now contains all projects that the current project has a dependency on
21702178
# Update ref variable projectDependencies for this project
2171-
if ($projectDependencies.Value.Keys -notcontains $project) {
2179+
if ($projectDependencies.Keys -notcontains $project) {
21722180
# Loop through the list of projects for which we already built a dependency list
21732181
# Update the dependency list for that project if it contains the current project, which might lead to a changed dependency list
21742182
# This is needed because we are looping through the projects in a any order
2175-
$keys = @($projectDependencies.value.Keys)
2183+
$keys = @($projectDependencies.Keys)
21762184
foreach($key in $keys) {
2177-
if ($projectDependencies.value."$key" -contains $project) {
2178-
$projectDeps = @( $projectDependencies.value."$key" )
2179-
$projectDependencies.value."$key" = @( @($projectDeps + $foundDependencies) | Select-Object -Unique )
2180-
if (Compare-Object -ReferenceObject $projectDependencies.value."$key" -differenceObject $projectDeps) {
2185+
if ($projectDependencies."$key" -contains $project) {
2186+
$projectDeps = @( $projectDependencies."$key" )
2187+
$projectDependencies."$key" = @( @($projectDeps + $foundDependencies) | Select-Object -Unique )
2188+
if (Compare-Object -ReferenceObject $projectDependencies."$key" -differenceObject $projectDeps) {
21812189
Write-Host "Add ProjectDependencies $($foundDependencies -join ',') to $key"
21822190
}
21832191
}
21842192
}
21852193
Write-Host "Set ProjectDependencies for $project to $($foundDependencies -join ',')"
2186-
$projectDependencies.value."$project" = $foundDependencies
2194+
$projectDependencies."$project" = $foundDependencies
21872195
}
21882196
if ($foundDependencies) {
21892197
Write-Host "Found dependencies to projects: $($foundDependencies -join ", ")"
2190-
# Add project to buildAlso for this dependency to ensure that this project also gets build when the dependency is built
2198+
# Add project to additionalProjectsToBuild for this dependency to ensure that this project also gets build when the dependency is built
21912199
foreach($dependency in $foundDependencies) {
2192-
if ($buildAlso.value.Keys -contains $dependency) {
2193-
if ($buildAlso.value."$dependency" -notcontains $project) {
2194-
$buildAlso.value."$dependency" += @( $project )
2200+
if ($additionalProjectsToBuild.Keys -contains $dependency) {
2201+
if ($additionalProjectsToBuild."$dependency" -notcontains $project) {
2202+
$additionalProjectsToBuild."$dependency" += @( $project )
21952203
}
21962204
}
21972205
else {
2198-
$buildAlso.value."$dependency" = @( $project )
2206+
$additionalProjectsToBuild."$dependency" = @( $project )
21992207
}
22002208
}
22012209
}
@@ -2215,7 +2223,11 @@ Function AnalyzeProjectDependencies {
22152223
$no++
22162224
}
22172225

2218-
return @($projectsOrder)
2226+
return [PSCustomObject]@{
2227+
FullProjectsOrder = $projectsOrder
2228+
AdditionalProjectsToBuild = $additionalProjectsToBuild
2229+
ProjectDependencies = $projectDependencies
2230+
}
22192231
}
22202232

22212233
function GetBaseFolder {

Actions/CheckForUpdates/CheckForUpdates.ps1

+2-3
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ $templateUrl = $templateUrl -replace "^(https:\/\/)(www\.)(.*)$", '$1$3'
4949
# TemplateUrl is now always a full url + @ and a branch name
5050

5151
# CheckForUpdates will read all AL-Go System files from the Template repository and compare them to the ones in the current repository
52-
# CheckForUpdates will apply changes to the AL-Go System files based on AL-Go repo settings, such as "runs-on", "useProjectDependencies", etc.
52+
# CheckForUpdates will apply changes to the AL-Go System files based on AL-Go repo settings, such as "runs-on" etc.
5353
# if $update is set to Y, CheckForUpdates will also update the AL-Go System files in the current repository using a PR or a direct commit (if $directCommit is set to true)
5454
# if $update is set to N, CheckForUpdates will only check for updates and output a warning if there are updates available
5555
# if $downloadLatest is set to true, CheckForUpdates will download the latest version of the template repository, else it will use the templateSha setting in the .github/AL-Go-Settings file
@@ -115,11 +115,10 @@ $updateFiles = @()
115115
# $removeFiles will hold an array of files, which needs to be removed
116116
$removeFiles = @()
117117

118-
# If useProjectDependencies is true, we need to calculate the dependency depth for all projects
119118
# Dependency depth determines how many build jobs we need to run sequentially
120119
# Every build job might spin up multiple jobs in parallel to build the projects without unresolved deependencies
121120
$depth = 1
122-
if ($repoSettings.useProjectDependencies -and $projects.Count -gt 1) {
121+
if ($projects.Count -gt 1) {
123122
Import-Module (Join-Path -Path $PSScriptRoot -ChildPath "..\DetermineProjectsToBuild\DetermineProjectsToBuild.psm1" -Resolve) -DisableNameChecking
124123
$allProjects, $projectsToBuild, $projectDependencies, $buildOrder = Get-ProjectsToBuild -baseFolder $baseFolder -buildAllProjects $true -maxBuildDepth 100
125124
$depth = $buildOrder.Count

Actions/DetermineProjectsToBuild/DetermineProjectsToBuild.psm1

+5-14
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,6 @@ function Get-ProjectsToBuild {
222222
Write-Host "Found AL-Go Projects: $($projects -join ', ')"
223223

224224
$projectsToBuild = @()
225-
$projectDependencies = @{}
226225
$projectsOrderToBuild = @()
227226

228227
if ($projects) {
@@ -238,21 +237,13 @@ function Get-ProjectsToBuild {
238237
$projectsToBuild = @($projects | Where-Object { ShouldBuildProject -baseFolder $baseFolder -project $_ -modifiedFiles $modifiedFilesFullPaths })
239238
}
240239

241-
if($settings.useProjectDependencies) {
242-
$buildAlso = @{}
240+
# Calculate the full projects order
241+
$projectBuildInfo = AnalyzeProjectDependencies -baseFolder $baseFolder -projects $projects
243242

244-
# Calculate the full projects order
245-
$fullProjectsOrder = AnalyzeProjectDependencies -baseFolder $baseFolder -projects $projects -buildAlso ([ref]$buildAlso) -projectDependencies ([ref]$projectDependencies)
246-
247-
$projectsToBuild = @($projectsToBuild | ForEach-Object { $_; if ($buildAlso.Keys -contains $_) { $buildAlso."$_" } } | Select-Object -Unique)
248-
}
249-
else {
250-
# Use a flatten build order (all projects on the same level)
251-
$fullProjectsOrder = @(@{ 'projects' = $projectsToBuild; 'projectsCount' = $projectsToBuild.Count})
252-
}
243+
$projectsToBuild = @($projectsToBuild | ForEach-Object { $_; if ($projectBuildInfo.AdditionalProjectsToBuild.Keys -contains $_) { $projectBuildInfo.AdditionalProjectsToBuild."$_" } } | Select-Object -Unique)
253244

254245
# Create a project order based on the projects to build
255-
foreach($depth in $fullProjectsOrder) {
246+
foreach($depth in $projectBuildInfo.FullProjectsOrder) {
256247
$projectsOnDepth = @($depth.projects | Where-Object { $projectsToBuild -contains $_ })
257248

258249
if ($projectsOnDepth) {
@@ -281,7 +272,7 @@ function Get-ProjectsToBuild {
281272
throw "The build depth is too deep, the maximum build depth is $maxBuildDepth. You need to run 'Update AL-Go System Files' to update the workflows"
282273
}
283274

284-
return $projects, $projectsToBuild, $projectDependencies, $projectsOrderToBuild
275+
return $projects, $projectsToBuild, $projectBuildInfo.projectDependencies, $projectsOrderToBuild
285276
}
286277
finally {
287278
Pop-Location

Tests/DetermineProjectsToBuild.Test.ps1

+104
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,10 @@ Describe "Get-ProjectsToBuild" {
606606

607607
#Add settings file
608608
$alGoSettings = @{ alwaysBuildAllProjects = $false; projects = @(); powerPlatformSolutionFolder = ''; useProjectDependencies = $true }
609+
New-Item -Path "$baseFolder/.github" -type Directory -Force
610+
$alGoSettings | ConvertTo-Json -Depth 99 -Compress | Out-File (Join-Path $baseFolder ".github/AL-Go-Settings.json") -Encoding UTF8
611+
612+
# Add settings as environment variable to simulate we've run ReadSettings
609613
$env:Settings = ConvertTo-Json $alGoSettings -Depth 99 -Compress
610614

611615
$allProjects, $projectsToBuild, $projectDependencies, $buildOrder = Get-ProjectsToBuild -baseFolder $baseFolder
@@ -661,6 +665,102 @@ Describe "Get-ProjectsToBuild" {
661665
$buildOrder[1].buildDimensions[0].project | Should -BeExactly "Project2"
662666
}
663667

668+
It 'loads dependent projects correctly, if useProjectDependencies is set to false in a project setting' {
669+
# Add three dependent projects
670+
# Project 1
671+
# Project 2 depends on Project 1 - useProjectDependencies is set to true from the repo settings
672+
# Project 3 depends on Project 1, but has useProjectDependencies set to false in the project settings
673+
$dependecyAppFile = @{ id = '83fb8305-4079-415d-a25d-8132f0436fd1'; name = 'First App'; publisher = 'Contoso'; version = '1.0.0.0'; dependencies = @() }
674+
New-Item -Path "$baseFolder/Project1/.AL-Go/settings.json" -type File -Force
675+
New-Item -Path "$baseFolder/Project1/app/app.json" -Value (ConvertTo-Json $dependecyAppFile -Depth 10) -type File -Force
676+
677+
$dependantAppFile = @{ id = '83fb8305-4079-415d-a25d-8132f0436fd2'; name = 'Second App'; publisher = 'Contoso'; version = '1.0.0.0'; dependencies = @(@{id = '83fb8305-4079-415d-a25d-8132f0436fd1'; name = 'First App'; publisher = 'Contoso'; version = '1.0.0.0'} ) }
678+
New-Item -Path "$baseFolder/Project2/.AL-Go/settings.json" -type File -Force
679+
New-Item -Path "$baseFolder/Project2/app/app.json" -Value (ConvertTo-Json $dependantAppFile -Depth 10) -type File -Force
680+
681+
# Third project that also depends on the first project, but has useProjectDependencies set to false
682+
$dependantAppFile3 = @{ id = '83fb8305-4079-415d-a25d-8132f0436fd3'; name = 'Third App'; publisher = 'Contoso'; version = '1.0.0.0'; dependencies = @(@{id = '83fb8305-4079-415d-a25d-8132f0436fd1'; name = 'First App'; publisher = 'Contoso'; version = '1.0.0.0'} ) }
683+
New-Item -Path "$baseFolder/Project3/.AL-Go/settings.json" -type File -Force
684+
@{ useProjectDependencies = $false } | ConvertTo-Json -Depth 99 -Compress | Out-File (Join-Path $baseFolder "Project3/.AL-Go/settings.json") -Encoding UTF8
685+
New-Item -Path "$baseFolder/Project3/app/app.json" -Value (ConvertTo-Json $dependantAppFile3 -Depth 10) -type File -Force
686+
687+
#Add settings file
688+
$alGoSettings = @{ alwaysBuildAllProjects = $false; projects = @(); powerPlatformSolutionFolder = ''; useProjectDependencies = $true }
689+
New-Item -Path "$baseFolder/.github" -type Directory -Force
690+
$alGoSettings | ConvertTo-Json -Depth 99 -Compress | Out-File (Join-Path $baseFolder ".github/AL-Go-Settings.json") -Encoding UTF8
691+
692+
# Add settings as environment variable to simulate we've run ReadSettings
693+
$env:Settings = ConvertTo-Json $alGoSettings -Depth 99 -Compress
694+
695+
$allProjects, $projectsToBuild, $projectDependencies, $buildOrder = Get-ProjectsToBuild -baseFolder $baseFolder
696+
697+
$allProjects | Should -BeExactly @("Project1", "Project2", "Project3")
698+
$projectsToBuild | Should -BeExactly @('Project1', 'Project2', 'Project3')
699+
700+
$projectDependencies | Should -BeOfType System.Collections.Hashtable
701+
$projectDependencies['Project1'] | Should -BeExactly @()
702+
$projectDependencies['Project2'] | Should -BeExactly @("Project1")
703+
$projectDependencies['Project3'] | Should -BeExactly @()
704+
705+
# Build order should have the following structure:
706+
#[
707+
#{
708+
# "buildDimensions": [
709+
# {
710+
# "projectName": "Project1",
711+
# "buildMode": "Default",
712+
# "project": "Project1",
713+
# "githubRunnerShell": "powershell",
714+
# "gitHubRunner": "\"windows-latest\""
715+
# },
716+
# {
717+
# "projectName": "Project3",
718+
# "buildMode": "Default",
719+
# "project": "Project3",
720+
# "githubRunnerShell": "powershell",
721+
# "gitHubRunner": "\"windows-latest\""
722+
# }
723+
# ],
724+
# "projectsCount": 2,
725+
# "projects": [
726+
# "Project1",
727+
# "Project3"
728+
# ]
729+
#},
730+
#{
731+
# "buildDimensions": [
732+
# {
733+
# "projectName": "Project2",
734+
# "buildMode": "Default",
735+
# "project": "Project2",
736+
# "githubRunnerShell": "powershell",
737+
# "gitHubRunner": "\"windows-latest\""
738+
# }
739+
# ],
740+
# "projectsCount": 1,
741+
# "projects": [
742+
# "Project2"
743+
# ]
744+
#}
745+
#]
746+
$buildOrder.Count | Should -BeExactly 2
747+
$buildOrder[0] | Should -BeOfType System.Collections.Hashtable
748+
$buildOrder[0].projects | Should -BeExactly @("Project1", "Project3")
749+
$buildOrder[0].projectsCount | Should -BeExactly 2
750+
$buildOrder[0].buildDimensions.Count | Should -BeExactly 2
751+
$buildOrder[0].buildDimensions[0].buildMode | Should -BeExactly "Default"
752+
$buildOrder[0].buildDimensions[0].project | Should -BeExactly "Project1"
753+
$buildOrder[0].buildDimensions[1].buildMode | Should -BeExactly "Default"
754+
$buildOrder[0].buildDimensions[1].project | Should -BeExactly "Project3"
755+
756+
$buildOrder[1] | Should -BeOfType System.Collections.Hashtable
757+
$buildOrder[1].projects | Should -BeExactly @("Project2")
758+
$buildOrder[1].projectsCount | Should -BeExactly 1
759+
$buildOrder[1].buildDimensions.Count | Should -BeExactly 1
760+
$buildOrder[1].buildDimensions[0].buildMode | Should -BeExactly "Default"
761+
$buildOrder[1].buildDimensions[0].project | Should -BeExactly "Project2"
762+
}
763+
664764
It 'throws if the calculated build depth is more than the maximum supported' {
665765
# Two dependent projects
666766
$dependecyAppFile = @{ id = '83fb8305-4079-415d-a25d-8132f0436fd1'; name = 'First App'; publisher = 'Contoso'; version = '1.0.0.0'; dependencies = @() }
@@ -673,6 +773,10 @@ Describe "Get-ProjectsToBuild" {
673773

674774
#Add settings file
675775
$alGoSettings = @{ alwaysBuildAllProjects = $false; projects = @(); powerPlatformSolutionFolder = ''; useProjectDependencies = $true }
776+
New-Item -Path "$baseFolder/.github" -type Directory -Force
777+
$alGoSettings | ConvertTo-Json -Depth 99 -Compress | Out-File (Join-Path $baseFolder ".github/AL-Go-Settings.json") -Encoding UTF8
778+
779+
# Add settings as environment variable to simulate we've run ReadSettings
676780
$env:Settings = ConvertTo-Json $alGoSettings -Depth 99 -Compress
677781

678782
{ Get-ProjectsToBuild -baseFolder $baseFolder -maxBuildDepth 1 } | Should -Throw "The build depth is too deep, the maximum build depth is 1. You need to run 'Update AL-Go System Files' to update the workflows"

0 commit comments

Comments
 (0)