Skip to content

Commit 3299e11

Browse files
azure-sdkhallipr
andauthored
Sync eng/common directory with azure-sdk-tools for PR 6919 (Azure#21611)
* Add autorest-preview pipeline * Add emitternpminstall * Always install * Use shorter leg name * Add short circuiting to emitter install * Use language matrix function * Don't look for subfolders in the matrix package folders * Revert unnecessary eng/common changes * Rewrite GetPullRequestUrl to Get-BuildSourceDescription * Remove alias from Invoke-LoggedCommand * Use invoke-expression instead of shelling out * Add better job splitting * Replace Folder with Directory --------- Co-authored-by: Patrick Hallisey <[email protected]>
1 parent 747c3fd commit 3299e11

7 files changed

+201
-21
lines changed

eng/common/pipelines/templates/steps/sparse-checkout.yml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ steps:
2929
if (!$dir) {
3030
$dir = "./$($repository.Name)"
3131
}
32-
New-Item $dir -ItemType Directory -Force
32+
New-Item $dir -ItemType Directory -Force | Out-Null
3333
Push-Location $dir
3434
3535
if (Test-Path .git/info/sparse-checkout) {
@@ -70,9 +70,14 @@ steps:
7070
7171
# sparse-checkout commands after initial checkout will auto-checkout again
7272
if (!$hasInitialized) {
73-
Write-Host "git -c advice.detachedHead=false checkout $($repository.Commitish)"
73+
# Remove refs/heads/ prefix from branch names
74+
$commitish = $repository.Commitish -replace '^refs/heads/', ''
75+
76+
# use -- to prevent git from interpreting the commitish as a path
77+
Write-Host "git -c advice.detachedHead=false checkout $commitish --"
78+
7479
# This will use the default branch if repo.Commitish is empty
75-
git -c advice.detachedHead=false checkout $($repository.Commitish)
80+
git -c advice.detachedHead=false checkout $commitish --
7681
} else {
7782
Write-Host "Skipping checkout as repo has already been initialized"
7883
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
param(
2+
[string]$Variable,
3+
[switch]$IsOutput
4+
)
5+
6+
$repoUrl = $env:BUILD_REPOSITORY_URI
7+
$sourceBranch = $env:BUILD_SOURCEBRANCH
8+
9+
$description = "[$sourceBranch]($repoUrl/tree/$sourceBranch)"
10+
if ($sourceBranch -match "^refs/heads/(.+)$") {
11+
$description = "Branch: [$($Matches[1])]($repoUrl/tree/$sourceBranch)"
12+
} elseif ($sourceBranch -match "^refs/tags/(.+)$") {
13+
$description = "Tag: [$($Matches[1])]($repoUrl/tree/$sourceBranch)"
14+
} elseif ($sourceBranch -match "^refs/pull/(\d+)/(head|merge)$") {
15+
$description = "Pull request: $repoUrl/pull/$($Matches[1])"
16+
}
17+
18+
if ($IsOutput) {
19+
Write-Host "Setting output variable '$Variable' to '$description'"
20+
Write-Host "##vso[task.setvariable variable=$Variable;isoutput=true]$description"
21+
} else {
22+
Write-Host "Setting variable '$Variable' to '$description'"
23+
Write-Host "##vso[task.setvariable variable=$Variable]$description"
24+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
function Invoke-LoggedCommand($Command, $ExecutePath, [switch]$GroupOutput)
2+
{
3+
$pipelineBuild = !!$env:TF_BUILD
4+
$startTime = Get-Date
5+
6+
if($pipelineBuild -and $GroupOutput) {
7+
Write-Host "##[group]$Command"
8+
} else {
9+
Write-Host "> $Command"
10+
}
11+
12+
if($ExecutePath) {
13+
Push-Location $ExecutePath
14+
}
15+
16+
try {
17+
Invoke-Expression $Command
18+
19+
$duration = (Get-Date) - $startTime
20+
21+
if($pipelineBuild -and $GroupOutput) {
22+
Write-Host "##[endgroup]"
23+
}
24+
25+
if($LastExitCode -ne 0)
26+
{
27+
if($pipelineBuild) {
28+
Write-Error "##[error]Command failed to execute ($duration): $Command`n"
29+
} else {
30+
Write-Error "Command failed to execute ($duration): $Command`n"
31+
}
32+
}
33+
else {
34+
Write-Host "Command succeeded ($duration)`n"
35+
}
36+
}
37+
finally {
38+
if($ExecutePath) {
39+
Pop-Location
40+
}
41+
}
42+
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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"

eng/common/scripts/TypeSpec-Project-Generate.ps1

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ param (
1111

1212
$ErrorActionPreference = "Stop"
1313
. $PSScriptRoot/Helpers/PSModule-Helpers.ps1
14+
. $PSScriptRoot/Helpers/CommandInvocation-Helpers.ps1
1415
. $PSScriptRoot/common.ps1
1516
Install-ModuleIfNotInstalled "powershell-yaml" "0.4.1" | Import-Module
1617

@@ -21,38 +22,30 @@ function NpmInstallForProject([string]$workingDirectory) {
2122
Write-Host "Generating from $currentDur"
2223

2324
if (Test-Path "package.json") {
25+
Write-Host "Removing existing package.json"
2426
Remove-Item -Path "package.json" -Force
2527
}
2628

2729
if (Test-Path ".npmrc") {
30+
Write-Host "Removing existing .nprc"
2831
Remove-Item -Path ".npmrc" -Force
2932
}
3033

3134
if (Test-Path "node_modules") {
35+
Write-Host "Removing existing node_modules"
3236
Remove-Item -Path "node_modules" -Force -Recurse
3337
}
3438

3539
if (Test-Path "package-lock.json") {
40+
Write-Host "Removing existing package-lock.json"
3641
Remove-Item -Path "package-lock.json" -Force
3742
}
3843

39-
#default to root/eng/emitter-package.json but you can override by writing
40-
#Get-${Language}-EmitterPackageJsonPath in your Language-Settings.ps1
4144
$replacementPackageJson = Join-Path $PSScriptRoot "../../emitter-package.json"
42-
if (Test-Path "Function:$GetEmitterPackageJsonPathFn") {
43-
$replacementPackageJson = &$GetEmitterPackageJsonPathFn
44-
}
4545

4646
Write-Host("Copying package.json from $replacementPackageJson")
4747
Copy-Item -Path $replacementPackageJson -Destination "package.json" -Force
48-
49-
#default to root/eng/emitter-package-lock.json but you can override by writing
50-
#Get-${Language}-EmitterPackageLockPath in your Language-Settings.ps1
5148
$emitterPackageLock = Join-Path $PSScriptRoot "../../emitter-package-lock.json"
52-
if (Test-Path "Function:$GetEmitterPackageLockPathFn") {
53-
$emitterPackageLock = &$GetEmitterPackageLockPathFn
54-
}
55-
5649
$usingLockFile = Test-Path $emitterPackageLock
5750

5851
if ($usingLockFile) {
@@ -68,12 +61,10 @@ function NpmInstallForProject([string]$workingDirectory) {
6861
}
6962

7063
if ($usingLockFile) {
71-
Write-Host "> npm ci"
72-
npm ci
64+
Invoke-LoggedCommand "npm ci"
7365
}
7466
else {
75-
Write-Host "> npm install"
76-
npm install
67+
Invoke-LoggedCommand "npm install"
7768
}
7869

7970
if ($LASTEXITCODE) { exit $LASTEXITCODE }
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[CmdLetBinding()]
2+
param(
3+
[Parameter(Mandatory)]
4+
[string]$PackageDirectoriesFile
5+
)
6+
7+
. $PSScriptRoot/common.ps1
8+
. $PSScriptRoot/Helpers/CommandInvocation-Helpers.ps1
9+
10+
$ErrorActionPreference = 'Stop'
11+
12+
if (Test-Path "Function:$UpdateGeneratedSdksFn") {
13+
&$UpdateGeneratedSdksFn $PackageDirectoriesFile
14+
} else {
15+
Write-Error "Function $UpdateGeneratedSdksFn not implemented in Language-Settings.ps1"
16+
}

eng/common/scripts/common.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ $GetPackageLevelReadmeFn = "Get-${Language}-PackageLevelReadme"
6060
$GetRepositoryLinkFn = "Get-${Language}-RepositoryLink"
6161
$GetEmitterAdditionalOptionsFn = "Get-${Language}-EmitterAdditionalOptions"
6262
$GetEmitterNameFn = "Get-${Language}-EmitterName"
63-
$GetEmitterPackageJsonPathFn = "Get-${Language}-EmitterPackageJsonPath"
64-
$GetEmitterPackageLockPathFn = "Get-${Language}-EmitterPackageLockPath"
63+
$GetDirectoriesForGenerationFn = "Get-${Language}-DirectoriesForGeneration"
64+
$UpdateGeneratedSdksFn = "Update-${Language}-GeneratedSdks"
6565

6666
# Expected to be set in eng/scripts/docs/Docs-Onboarding.ps1
6767
$SetDocsPackageOnboarding = "Set-${Language}-DocsPackageOnboarding"

0 commit comments

Comments
 (0)