|
1 | 1 | <# |
2 | 2 | .SYNOPSIS |
3 | | - Creates buildjob matrix based on the specified file names in the tree. |
| 3 | + Creates a job-matrix variable for downstream pipeline jobs. |
4 | 4 |
|
5 | 5 | .DESCRIPTION |
6 | | - The script traverses the build root to find all folders with a matching |
7 | | - file and populates the matrix. The matrix is used to spawn jobs that |
8 | | - run on multiple different environments for each file. E.g. build all |
9 | | - solution files on all platforms, run tests per particular folder of |
10 | | - the tree, etc. |
| 6 | + Discovers files matching a pattern in the repository (or uses an explicit |
| 7 | + caller-supplied list) and emits a pipeline output variable named |
| 8 | + "jobMatrix" containing one matrix entry per (file × agent × configuration) |
| 9 | + tuple. Either the agent or the configuration dimension may be left empty |
| 10 | + in which case the matrix is not fanned out across that dimension. |
| 11 | +
|
| 12 | + Each matrix entry carries these variables: |
| 13 | + folder - directory containing the file (relative to BuildRoot) |
| 14 | + fullFolder - directory containing the file (absolute) |
| 15 | + file - file path (relative to BuildRoot) |
| 16 | + agent - agent key (only when -AgentTable is non-empty) |
| 17 | + poolImage - vmImage for the agent (only when -AgentTable is non-empty) |
| 18 | + configuration - build configuration (only when -Configurations is non-empty) |
11 | 19 |
|
12 | 20 | .PARAMETER BuildRoot |
13 | 21 | The root folder to start traversing the repository from. |
14 | 22 |
|
15 | 23 | .PARAMETER FileName |
16 | | - File patterns to match defining the folders and files in the matrix |
| 24 | + File pattern to match defining the files in the matrix. Defaults to |
| 25 | + "Directory.Build.props". Ignored when -Files is supplied. |
17 | 26 |
|
18 | 27 | .PARAMETER ExcludeFileName |
19 | | - Optional file pattern to exclude from the matrix |
| 28 | + Optional file pattern to exclude from discovery. |
20 | 29 |
|
21 | 30 | .PARAMETER JobPrefix |
22 | | - Optional name prefix for each job |
| 31 | + Optional name prefix for each matrix key. |
23 | 32 |
|
24 | 33 | .PARAMETER AgentTable |
25 | | - Table of agents for matrix |
| 34 | + Optional hashtable mapping agent name to vmImage. When non-empty, the |
| 35 | + matrix is fanned out across agents. |
| 36 | +
|
| 37 | + .PARAMETER Configurations |
| 38 | + Optional comma-separated list of build configurations. When non-empty, |
| 39 | + the matrix is fanned out across configurations. |
| 40 | +
|
| 41 | + .PARAMETER Files |
| 42 | + Optional comma-separated list of file paths (relative to BuildRoot). |
| 43 | + When supplied, recursive discovery via -FileName is skipped. |
26 | 44 | #> |
27 | 45 |
|
28 | 46 | Param( |
29 | | - [string] $BuildRoot = $null, |
30 | | - [string] $FileName = $null, |
31 | | - [string] $ExcludeFileName = $null, |
32 | | - [string] $JobPrefix = "", |
33 | | - [hashtable] $AgentTable = $null |
| 47 | + [string] $BuildRoot = $null, |
| 48 | + [string] $FileName = $null, |
| 49 | + [string] $ExcludeFileName = $null, |
| 50 | + [string] $JobPrefix = '', |
| 51 | + [hashtable] $AgentTable = $null, |
| 52 | + [string] $Configurations = '', |
| 53 | + [string] $Files = '' |
34 | 54 | ) |
35 | 55 |
|
36 | 56 | if ([string]::IsNullOrEmpty($BuildRoot)) { |
37 | | - $BuildRoot = & (Join-Path $PSScriptRoot "get-root.ps1") -fileName "*.slnx" |
| 57 | + $BuildRoot = & (Join-Path $PSScriptRoot 'get-root.ps1') -fileName '*.slnx' |
38 | 58 | } |
39 | 59 |
|
40 | 60 | if ([string]::IsNullOrEmpty($FileName)) { |
41 | | - $FileName = "Directory.Build.props" |
| 61 | + $FileName = 'Directory.Build.props' |
42 | 62 | } |
43 | 63 | if (![string]::IsNullOrEmpty($JobPrefix)) { |
44 | 64 | $JobPrefix = "$($JobPrefix)-" |
45 | 65 | } |
46 | 66 |
|
47 | | -if ($AgentTable -eq $null -or $AgentTable.Count -eq 0) |
48 | | -{ |
49 | | - $agents = @{ |
50 | | - windows = "windows-2025-vs2026" |
51 | | - linux = "ubuntu-22.04" |
52 | | - mac = "macOS-15" |
| 67 | +if ($null -eq $AgentTable) { |
| 68 | + # Caller did not supply an AgentTable - do not fan out across agents. |
| 69 | + $AgentTable = @{} |
| 70 | +} |
| 71 | +elseif ($AgentTable.Count -eq 0) { |
| 72 | + # Caller supplied an empty AgentTable - use the cross-platform defaults |
| 73 | + # (preserves the original get-matrix.ps1 behaviour for ci.yml callers). |
| 74 | + $AgentTable = @{ |
| 75 | + windows = 'windows-2025-vs2026' |
| 76 | + linux = 'ubuntu-22.04' |
| 77 | + mac = 'macOS-15' |
53 | 78 | } |
54 | 79 | } |
55 | | -else { |
56 | | - $agents = $AgentTable |
| 80 | +$useAgents = $AgentTable.Count -gt 0 |
| 81 | + |
| 82 | +function Split-CsvList([string] $value) { |
| 83 | + if ([string]::IsNullOrWhiteSpace($value)) { return @() } |
| 84 | + return $value -split ',' | ForEach-Object { $_.Trim() } | Where-Object { $_ } |
57 | 85 | } |
58 | 86 |
|
59 | | -$jobMatrix = @{} |
| 87 | +$configList = Split-CsvList $Configurations |
| 88 | +$useConfigs = $configList.Count -gt 0 |
| 89 | + |
| 90 | +$fileList = Split-CsvList $Files |
| 91 | + |
| 92 | +$jobMatrix = [ordered]@{} |
| 93 | + |
| 94 | +# Discover files when no explicit list was supplied. |
| 95 | +if ($fileList.Count -gt 0) { |
| 96 | + Write-Host "Using caller-supplied file list: $($fileList -join ', ')" |
| 97 | + $buildRootFull = (Resolve-Path -Path $BuildRoot).Path |
| 98 | + $items = @() |
| 99 | + foreach ($rel in $fileList) { |
| 100 | + $full = Join-Path $buildRootFull $rel |
| 101 | + if (Test-Path $full -PathType Leaf) { |
| 102 | + $items += Get-Item -LiteralPath $full |
| 103 | + } |
| 104 | + else { |
| 105 | + Write-Warning "File not found: $rel" |
| 106 | + } |
| 107 | + } |
| 108 | +} |
| 109 | +else { |
| 110 | + Write-Host "Discovering files matching '$FileName' beneath '$BuildRoot'" |
| 111 | + $items = Get-ChildItem $BuildRoot -Recurse ` |
| 112 | + | Where-Object Name -like $FileName ` |
| 113 | + | Where-Object { [string]::IsNullOrEmpty($ExcludeFileName) -or $_.Name -notlike $ExcludeFileName } |
| 114 | +} |
60 | 115 |
|
61 | | -# Traverse from build root and find all files to create job matrix |
62 | | -Get-ChildItem $BuildRoot -Recurse ` |
63 | | - | Where-Object Name -like $FileName ` |
64 | | - | Where-Object { [string]::IsNullOrEmpty($ExcludeFileName) -or $_.Name -notlike $ExcludeFileName } ` |
65 | | - | ForEach-Object { |
| 116 | +foreach ($item in $items) { |
| 117 | + $fullFolder = $item.DirectoryName.Replace('\', '/') |
| 118 | + $folder = $item.DirectoryName.Replace($BuildRoot, '').Replace('\', '/').TrimStart('/') |
| 119 | + $file = $item.FullName.Replace($BuildRoot, '').Replace('\', '/').TrimStart('/') |
66 | 120 |
|
67 | | - $fullFolder = $_.DirectoryName.Replace("\", "/") |
68 | | - $folder = $_.DirectoryName.Replace($BuildRoot, "").Replace("\", "/").TrimStart("/") |
69 | | - $file = $_.FullName.Replace($BuildRoot, "").Replace("\", "/").TrimStart("/") |
70 | | - $postFix = "" |
| 121 | + # Build the key prefix for this file. Preserve the legacy shortcut that |
| 122 | + # treats a single yml file at the repository root as agent-only (so the |
| 123 | + # ci.yml caller continues to produce matrix keys like "windows"/"linux" |
| 124 | + # without a file-name prefix). For every other file include the file |
| 125 | + # stem so that multiple files in the same folder don't collide. |
| 126 | + $postFix = '' |
71 | 127 | if ([string]::IsNullOrEmpty($folder)) { |
72 | | - if (-not $file.contains(".yml")) |
73 | | - { |
74 | | - $postFix = $file.Replace(".", "-") |
75 | | - $postFix = "$($postFix)-" |
| 128 | + if (-not $file.Contains('.yml')) { |
| 129 | + $postFix = [System.IO.Path]::GetFileNameWithoutExtension($file) + '_' |
76 | 130 | } |
77 | 131 | } |
78 | 132 | else { |
79 | | - $postFix = $folder.Replace("/", "-") |
80 | | - $postFix = "$($postFix)-" |
| 133 | + $stem = [System.IO.Path]::GetFileNameWithoutExtension($file) |
| 134 | + $postFix = $folder.Replace('/', '_') + '_' + $stem + '_' |
| 135 | + } |
| 136 | + |
| 137 | + if ($useAgents) { |
| 138 | + $agentKeys = @($AgentTable.Keys) |
| 139 | + } |
| 140 | + else { |
| 141 | + # Use a single sentinel iteration so the inner loop runs once. |
| 142 | + $agentKeys = @('') |
| 143 | + } |
| 144 | + if ($useConfigs) { |
| 145 | + $configKeys = $configList |
| 146 | + } |
| 147 | + else { |
| 148 | + $configKeys = @('') |
81 | 149 | } |
82 | | - $agents.keys | ForEach-Object { |
83 | | - $jobName = "$($JobPrefix)$($postFix)$($_)" |
84 | | - $jobMatrix.Add($jobName, @{ |
85 | | - "poolImage" = $agents.Item($_) |
86 | | - "folder" = $folder |
87 | | - "fullFolder" = $fullFolder |
88 | | - "file" = $file |
89 | | - "agent" = $($_) |
90 | | - }) |
| 150 | + |
| 151 | + foreach ($agentKey in $agentKeys) { |
| 152 | + foreach ($configuration in $configKeys) { |
| 153 | + $entry = @{ |
| 154 | + folder = $folder |
| 155 | + fullFolder = $fullFolder |
| 156 | + file = $file |
| 157 | + } |
| 158 | + $jobName = "$($JobPrefix)$($postFix)" |
| 159 | + if ($useAgents) { |
| 160 | + $entry['agent'] = $agentKey |
| 161 | + $entry['poolImage'] = $AgentTable.Item($agentKey) |
| 162 | + $jobName += $agentKey |
| 163 | + } |
| 164 | + if ($useConfigs) { |
| 165 | + $entry['configuration'] = $configuration |
| 166 | + $jobName += "_$configuration" |
| 167 | + } |
| 168 | + # Matrix keys must be alphanumeric or underscore for Azure DevOps. |
| 169 | + $jobName = ($jobName -replace '[^A-Za-z0-9_]', '_') |
| 170 | + $jobName = ($jobName -replace '_+', '_').Trim('_') |
| 171 | + $jobMatrix.Add($jobName, $entry) |
| 172 | + } |
91 | 173 | } |
92 | 174 | } |
93 | 175 |
|
94 | | -# Set pipeline variable |
| 176 | +Write-Host ("Job matrix:`n" + ($jobMatrix | ConvertTo-Json -Depth 4)) |
95 | 177 | Write-Host ("##vso[task.setVariable variable=jobMatrix;isOutput=true] {0}" ` |
96 | 178 | -f ($jobMatrix | ConvertTo-Json -Compress)) |
0 commit comments