|
| 1 | +[CmdLetBinding()] |
| 2 | +param ( |
| 3 | + [Parameter()] |
| 4 | + [string]$OutputDirectory, |
| 5 | + |
| 6 | + [Parameter()] |
| 7 | + [string]$OutputVariableName, |
| 8 | + |
| 9 | + [Parameter()] |
| 10 | + [int]$JobCount = 8, |
| 11 | + |
| 12 | + # The minimum number of items per job. If the number of items is less than this, then the number of jobs will be reduced. |
| 13 | + [Parameter()] |
| 14 | + [int]$MinimumPerJob = 10, |
| 15 | + |
| 16 | + [Parameter()] |
| 17 | + [string]$OnlyTypespec |
| 18 | +) |
| 19 | + |
| 20 | +. (Join-Path $PSScriptRoot common.ps1) |
| 21 | + |
| 22 | +[bool]$OnlyTypespec = $OnlyTypespec -in @("true", "t", "1", "yes", "y") |
| 23 | + |
| 24 | +# Divide the items into groups of approximately equal size. |
| 25 | +function Split-Items([array]$Items) { |
| 26 | + # given $Items.Length = 22 and $JobCount = 5 |
| 27 | + # then $itemsPerGroup = 4 |
| 28 | + # and $largeJobCount = 2 |
| 29 | + # and $group.Length = 5, 5, 4, 4, 4 |
| 30 | + $itemCount = $Items.Length |
| 31 | + $jobsForMinimum = $itemCount -lt $MinimumPerJob ? 1 : [math]::Floor($itemCount / $MinimumPerJob) |
| 32 | + |
| 33 | + if ($JobCount -gt $jobsForMinimum) { |
| 34 | + $JobCount = $jobsForMinimum |
| 35 | + } |
| 36 | + |
| 37 | + $itemsPerGroup = [math]::Floor($itemCount / $JobCount) |
| 38 | + $largeJobCount = $itemCount % $itemsPerGroup |
| 39 | + $groups = [object[]]::new($JobCount) |
| 40 | + |
| 41 | + $i = 0 |
| 42 | + for ($g = 0; $g -lt $JobCount; $g++) { |
| 43 | + $groupLength = if ($g -lt $largeJobCount) { $itemsPerGroup + 1 } else { $itemsPerGroup } |
| 44 | + $group = [object[]]::new($groupLength) |
| 45 | + $groups[$g] = $group |
| 46 | + for ($gi = 0; $gi -lt $groupLength; $gi++) { |
| 47 | + $group[$gi] = $Items[$i++] |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + Write-Host "$itemCount items split into $JobCount groups of approximately $itemsPerGroup items each." |
| 52 | + |
| 53 | + return , $groups |
| 54 | +} |
| 55 | + |
| 56 | +# ensure the output directory exists |
| 57 | +New-Item -ItemType Directory -Path $OutputDirectory -Force | Out-Null |
| 58 | + |
| 59 | +if (Test-Path "Function:$GetDirectoriesForGenerationFn") { |
| 60 | + $directoriesForGeneration = &$GetDirectoriesForGenerationFn |
| 61 | +} |
| 62 | +else { |
| 63 | + $directoriesForGeneration = Get-ChildItem "$RepoRoot/sdk" -Directory | Get-ChildItem -Directory |
| 64 | +} |
| 65 | + |
| 66 | +if ($OnlyTypespec) { |
| 67 | + $directoriesForGeneration = $directoriesForGeneration | Where-Object { Test-Path "$_/tsp-location.yaml" } |
| 68 | +} |
| 69 | + |
| 70 | +[array]$packageDirectories = $directoriesForGeneration |
| 71 | +| Sort-Object -Property FullName |
| 72 | +| ForEach-Object { |
| 73 | + [ordered]@{ |
| 74 | + "PackageDirectory" = "$($_.Parent.Name)/$($_.Name)" |
| 75 | + "ServiceArea" = $_.Parent.Name |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +$batches = Split-Items -Items $packageDirectories |
| 80 | + |
| 81 | +$matrix = [ordered]@{} |
| 82 | +for ($i = 0; $i -lt $batches.Length; $i++) { |
| 83 | + $batch = $batches[$i] |
| 84 | + $json = $batch.PackageDirectory | ConvertTo-Json -AsArray |
| 85 | + |
| 86 | + $firstPrefix = $batch[0].ServiceArea.Substring(0, 2) |
| 87 | + $lastPrefix = $batch[-1].ServiceArea.Substring(0, 2) |
| 88 | + |
| 89 | + $key = "$firstPrefix`_$lastPrefix`_$i" |
| 90 | + $fileName = "$key.json" |
| 91 | + |
| 92 | + Write-Host "`n`n==================================" |
| 93 | + Write-Host $fileName |
| 94 | + Write-Host "==================================" |
| 95 | + $json | Out-Host |
| 96 | + $json | Out-File "$OutputDirectory/$fileName" |
| 97 | + |
| 98 | + $matrix[$key] = [ordered]@{ "JobKey" = $key; "DirectoryList" = $fileName } |
| 99 | +} |
| 100 | + |
| 101 | +$compressed = ConvertTo-Json $matrix -Depth 100 -Compress |
| 102 | +Write-Output "##vso[task.setVariable variable=$OutputVariableName;isOutput=true]$compressed" |
0 commit comments