From 5f664928021d3df0d17fde902e0d0728975c2a33 Mon Sep 17 00:00:00 2001 From: Patrick Hallisey Date: Fri, 8 Sep 2023 21:40:30 -0700 Subject: [PATCH 01/13] Add autorest-preview pipeline --- .../templates/steps/sparse-checkout.yml | 11 ++- eng/common/scripts/Cadl-Project-Generate.ps1 | 50 ++---------- eng/common/scripts/Get-PullRequestUrl.ps1 | 19 +++++ .../Helpers/CommandInvocation-Helpers.ps1 | 51 ++++++++++++ eng/common/scripts/Invoke-NpmInstall.ps1 | 66 ++++++++++++++++ eng/common/scripts/New-RegenerateMatrix.ps1 | 71 +++++++++++++++++ .../scripts/TypeSpec-Project-Generate.ps1 | 79 ++----------------- eng/common/scripts/Update-GeneratedSdks.ps1 | 16 ++++ eng/common/scripts/common.ps1 | 4 +- 9 files changed, 244 insertions(+), 123 deletions(-) create mode 100644 eng/common/scripts/Get-PullRequestUrl.ps1 create mode 100644 eng/common/scripts/Helpers/CommandInvocation-Helpers.ps1 create mode 100644 eng/common/scripts/Invoke-NpmInstall.ps1 create mode 100644 eng/common/scripts/New-RegenerateMatrix.ps1 create mode 100644 eng/common/scripts/Update-GeneratedSdks.ps1 diff --git a/eng/common/pipelines/templates/steps/sparse-checkout.yml b/eng/common/pipelines/templates/steps/sparse-checkout.yml index ab95453954cb..448cb2c2e313 100644 --- a/eng/common/pipelines/templates/steps/sparse-checkout.yml +++ b/eng/common/pipelines/templates/steps/sparse-checkout.yml @@ -29,7 +29,7 @@ steps: if (!$dir) { $dir = "./$($repository.Name)" } - New-Item $dir -ItemType Directory -Force + New-Item $dir -ItemType Directory -Force | Out-Null Push-Location $dir if (Test-Path .git/info/sparse-checkout) { @@ -70,9 +70,14 @@ steps: # sparse-checkout commands after initial checkout will auto-checkout again if (!$hasInitialized) { - Write-Host "git -c advice.detachedHead=false checkout $($repository.Commitish)" + # Remove refs/heads/ prefix from branch names + $commitish = $repository.Commitish -replace '^refs/heads/', '' + + # use -- to prevent git from interpreting the commitish as a path + Write-Host "git -c advice.detachedHead=false checkout $commitish --" + # This will use the default branch if repo.Commitish is empty - git -c advice.detachedHead=false checkout $($repository.Commitish) + git -c advice.detachedHead=false checkout $commitish -- } else { Write-Host "Skipping checkout as repo has already been initialized" } diff --git a/eng/common/scripts/Cadl-Project-Generate.ps1 b/eng/common/scripts/Cadl-Project-Generate.ps1 index 3e7ee781b053..6e97e2f2cc6a 100644 --- a/eng/common/scripts/Cadl-Project-Generate.ps1 +++ b/eng/common/scripts/Cadl-Project-Generate.ps1 @@ -11,48 +11,10 @@ param ( $ErrorActionPreference = "Stop" . $PSScriptRoot/Helpers/PSModule-Helpers.ps1 +. $PSScriptRoot/Helpers/CommandInvocation-Helpers.ps1 . $PSScriptRoot/common.ps1 Install-ModuleIfNotInstalled "powershell-yaml" "0.4.1" | Import-Module -function NpmInstallForProject([string]$workingDirectory) { - Push-Location $workingDirectory - try { - $currentDur = Resolve-Path "." - Write-Host "Generating from $currentDur" - - if (Test-Path "package.json") { - Remove-Item -Path "package.json" -Force - } - - if (Test-Path ".npmrc") { - Remove-Item -Path ".npmrc" -Force - } - - if (Test-Path "node_modules") { - Remove-Item -Path "node_modules" -Force -Recurse - } - - if (Test-Path "package-lock.json") { - Remove-Item -Path "package-lock.json" -Force - } - - #default to root/eng/emitter-package.json but you can override by writing - #Get-${Language}-EmitterPackageJsonPath in your Language-Settings.ps1 - $replacementPackageJson = "$PSScriptRoot/../../emitter-package.json" - if (Test-Path "Function:$GetEmitterPackageJsonPathFn") { - $replacementPackageJson = &$GetEmitterPackageJsonPathFn - } - - Write-Host("Copying package.json from $replacementPackageJson") - Copy-Item -Path $replacementPackageJson -Destination "package.json" -Force - npm install --no-lock-file - if ($LASTEXITCODE) { exit $LASTEXITCODE } - } - finally { - Pop-Location - } -} - $resolvedProjectDirectory = Resolve-Path $ProjectDirectory $emitterName = &$GetEmitterNameFn $cadlConfigurationFile = Resolve-Path "$ProjectDirectory/cadl-location.yaml" @@ -67,9 +29,9 @@ $tempFolder = "$ProjectDirectory/TempCadlFiles" $npmWorkingDir = Resolve-Path $tempFolder/$innerFolder $mainCadlFile = If (Test-Path "$npmWorkingDir/client.cadl") { Resolve-Path "$npmWorkingDir/client.cadl" } Else { Resolve-Path "$npmWorkingDir/main.cadl"} +Push-Location $npmWorkingDir try { - Push-Location $npmWorkingDir - NpmInstallForProject $npmWorkingDir + . $PSScriptRoot/Invoke-NpmInstall.ps1 if ($LASTEXITCODE) { exit $LASTEXITCODE } @@ -79,15 +41,15 @@ try { $emitterAdditionalOptions = " $emitterAdditionalOptions" } } - $cadlCompileCommand = "npx cadl compile $mainCadlFile --emit $emitterName$emitterAdditionalOptions" + $cadlCompileCommand = "npx --no cadl compile $mainCadlFile --emit $emitterName$emitterAdditionalOptions" if ($CadlAdditionalOptions) { $options = $CadlAdditionalOptions.Split(";"); foreach ($option in $options) { $cadlCompileCommand += " --option $emitterName.$option" } } - Write-Host($cadlCompileCommand) - Invoke-Expression $cadlCompileCommand + + Invoke-LoggedCommand $cadlCompileCommand if ($LASTEXITCODE) { exit $LASTEXITCODE } } diff --git a/eng/common/scripts/Get-PullRequestUrl.ps1 b/eng/common/scripts/Get-PullRequestUrl.ps1 new file mode 100644 index 000000000000..54d231a4ea4f --- /dev/null +++ b/eng/common/scripts/Get-PullRequestUrl.ps1 @@ -0,0 +1,19 @@ +param( + [string]$RepositoryName, + [string]$SourceBranch, + [string]$Variable, + [switch]$IsOutput +) + +$PRUrl = $SourceBranch +if ($SourceBranch -match "refs/pull/(\d+)/(head|merge)") { + $PRUrl = "https://github.com/$RepositoryName/pull/$($Matches[1])" +} + +if ($IsOutput) { + Write-Host "Setting output variable '$Variable' to $PRUrl" + Write-Host "##vso[task.setvariable variable=$Variable;isoutput=true]$PRUrl" +} else { + Write-Host "Setting variable '$Variable' to $PRUrl" + Write-Host "##vso[task.setvariable variable=$Variable]$PRUrl" +} diff --git a/eng/common/scripts/Helpers/CommandInvocation-Helpers.ps1 b/eng/common/scripts/Helpers/CommandInvocation-Helpers.ps1 new file mode 100644 index 000000000000..a583276d3f03 --- /dev/null +++ b/eng/common/scripts/Helpers/CommandInvocation-Helpers.ps1 @@ -0,0 +1,51 @@ +function Invoke-LoggedCommand($Command, $ExecutePath, [switch]$GroupOutput) +{ + $pipelineBuild = !!$env:TF_BUILD + $startTime = Get-Date + + if($pipelineBuild -and $GroupOutput) { + Write-Host "##[group]$Command" + } else { + Write-Host "> $Command" + } + + if($ExecutePath) { + Push-Location $ExecutePath + } + + try { + if ($IsLinux -or $IsMacOs) + { + sh -c "$Command 2>&1" + } + else + { + cmd /c "$Command 2>&1" + } + + $duration = (Get-Date) - $startTime + + if($pipelineBuild -and $GroupOutput) { + Write-Host "##[endgroup]" + } + + if($LastExitCode -ne 0) + { + if($pipelineBuild) { + Write-Error "##[error]Command failed to execute ($duration): $Command`n" + } else { + Write-Error "Command failed to execute ($duration): $Command`n" + } + } + else { + Write-Host "Command succeeded ($duration)`n" + } + } + finally { + if($ExecutePath) { + Pop-Location + } + } +} + +Set-Alias -Name invoke -Value Invoke-LoggedCommand \ No newline at end of file diff --git a/eng/common/scripts/Invoke-NpmInstall.ps1 b/eng/common/scripts/Invoke-NpmInstall.ps1 new file mode 100644 index 000000000000..9e32cb907752 --- /dev/null +++ b/eng/common/scripts/Invoke-NpmInstall.ps1 @@ -0,0 +1,66 @@ +. $PSScriptRoot/Helpers/PSModule-Helpers.ps1 +. $PSScriptRoot/Helpers/CommandInvocation-Helpers.ps1 +. $PSScriptRoot/common.ps1 + +Push-Location $RepoRoot +try { + $currentDur = Resolve-Path "." + + if (Test-Path "node_modules") { + Write-Host "node_modules folder already exists. Skipping npm install." + exit 0 + } + + Write-Host "Installing npm dependencies in $currentDur" + + if (Test-Path "package.json") { + Write-Host "Removing existing package.json" + Remove-Item -Path "package.json" -Force + } + + if (Test-Path "package-lock.json") { + Write-Host "Removing existing package-lock.json" + Remove-Item -Path "package-lock.json" -Force + } + + if (Test-Path ".npmrc") { + Write-Host "Removing existing .nprc" + Remove-Item -Path ".npmrc" -Force + } + + Write-Host("Copying package.json from eng/emitter-package.json") + Copy-Item -Path './eng/emitter-package.json' -Destination "package.json" -Force + + $usingLockFile = Test-Path './eng/emitter-package-lock.json' + + if ($usingLockFile) { + Write-Host("Copying package-lock.json from eng/emitter-package-lock.json") + Copy-Item -Path './eng/emitter-package-lock.json' -Destination "package-lock.json" -Force + } + + $useAlphaNpmRegistry = (Get-Content 'package.json' -Raw).Contains("-alpha.") + + if ($useAlphaNpmRegistry) { + Write-Host "Package.json contains '-alpha.' in the version, Creating .npmrc using public/azure-sdk-for-js-test-autorest feed." + "registry=https://pkgs.dev.azure.com/azure-sdk/public/_packaging/azure-sdk-for-js-test-autorest@local/npm/registry/" | Out-File '.npmrc' + } + + if ($usingLockFile) { + Invoke-LoggedCommand "npm ci" + } + else { + Invoke-LoggedCommand "npm install" + } + + Remove-Item -Path "package.json" -Force + Remove-Item -Path "package-lock.json" -Force + + if ($useAlphaNpmRegistry) { + Remove-Item -Path ".npmrc" -Force + } + + if ($LASTEXITCODE) { exit $LASTEXITCODE } +} +finally { + Pop-Location +} \ No newline at end of file diff --git a/eng/common/scripts/New-RegenerateMatrix.ps1 b/eng/common/scripts/New-RegenerateMatrix.ps1 new file mode 100644 index 000000000000..840f2503feb7 --- /dev/null +++ b/eng/common/scripts/New-RegenerateMatrix.ps1 @@ -0,0 +1,71 @@ +[CmdLetBinding()] +param ( + [Parameter()] + [string]$OutputDirectory, + + [Parameter()] + [string]$OutputVariableName, + + [Parameter()] + [int]$JobCount = 8 +) + +. (Join-Path $PSScriptRoot common.ps1) + +# Divide the items into groups of approximately equal size. +function Split-Items([array]$Items) { + # given $Items.Length = 22 and $JobCount = 5 + # then $itemsPerGroup = 4 + # and $largeJobCount = 2 + # and $group.Length = 5, 5, 4, 4, 4 + $itemsPerGroup = [math]::Floor($Items.Length / $JobCount) + $largeJobCount = $Items.Length % $itemsPerGroup + $groups = [object[]]::new($JobCount) + + $i = 0 + for ($g = 0; $g -lt $JobCount; $g++) { + $groupLength = if ($g -lt $largeJobCount) { $itemsPerGroup + 1 } else { $itemsPerGroup } + $group = [object[]]::new($groupLength) + $groups[$g] = $group + for ($gi = 0; $gi -lt $groupLength; $gi++) { + $group[$gi] = $Items[$i++] + } + } + + return , $groups +} + +# ensure the output directory exists +New-Item -ItemType Directory -Path $OutputDirectory -Force | Out-Null + +if (Test-Path "Function:$GetFoldersForGenerationFn") { + $foldersForGeneration = &$GetFoldersForGenerationFn +} else { + $foldersForGeneration = Get-ChildItem "$RepoRoot/sdk" -Directory | Get-ChildItem -Directory +} + +[array]$packageFolders = $foldersForGeneration + | Sort-Object -Property FullName + | ForEach-Object { + [ordered]@{ + "PackageFolder" = "$($_.Parent.Name)/$($_.Name)" + "ServiceArea" = $_.Parent.Name + } + } + + +$batches = Split-Items -Items $packageFolders + +$matrix = [ordered]@{} +for ($i = 0; $i -lt $batches.Length; $i++) { + $batch = $batches[$i] + $firstPrefix = $batch[0].ServiceArea.Substring(0, 2) + $lastPrefix = $batch[-1].ServiceArea.Substring(0, 2) + $key = "Batch_$i`_$firstPrefix`_$lastPrefix" + $fileName = "$i.json" + $batch.PackageFolder | ConvertTo-Json -AsArray | Out-File "$OutputDirectory/$fileName" + $matrix[$key] = [ordered]@{ "JobKey" = $key; "FolderList" = $fileName } +} + +$compressed = ConvertTo-Json $matrix -Depth 100 -Compress +Write-Output "##vso[task.setVariable variable=$OutputVariableName;isOutput=true]$compressed" diff --git a/eng/common/scripts/TypeSpec-Project-Generate.ps1 b/eng/common/scripts/TypeSpec-Project-Generate.ps1 index e323f42aff6d..a58f579db7cb 100644 --- a/eng/common/scripts/TypeSpec-Project-Generate.ps1 +++ b/eng/common/scripts/TypeSpec-Project-Generate.ps1 @@ -11,78 +11,10 @@ param ( $ErrorActionPreference = "Stop" . $PSScriptRoot/Helpers/PSModule-Helpers.ps1 +. $PSScriptRoot/Helpers/CommandInvocation-Helpers.ps1 . $PSScriptRoot/common.ps1 Install-ModuleIfNotInstalled "powershell-yaml" "0.4.1" | Import-Module -function NpmInstallForProject([string]$workingDirectory) { - Push-Location $workingDirectory - try { - $currentDur = Resolve-Path "." - Write-Host "Generating from $currentDur" - - if (Test-Path "package.json") { - Remove-Item -Path "package.json" -Force - } - - if (Test-Path ".npmrc") { - Remove-Item -Path ".npmrc" -Force - } - - if (Test-Path "node_modules") { - Remove-Item -Path "node_modules" -Force -Recurse - } - - if (Test-Path "package-lock.json") { - Remove-Item -Path "package-lock.json" -Force - } - - #default to root/eng/emitter-package.json but you can override by writing - #Get-${Language}-EmitterPackageJsonPath in your Language-Settings.ps1 - $replacementPackageJson = Join-Path $PSScriptRoot "../../emitter-package.json" - if (Test-Path "Function:$GetEmitterPackageJsonPathFn") { - $replacementPackageJson = &$GetEmitterPackageJsonPathFn - } - - Write-Host("Copying package.json from $replacementPackageJson") - Copy-Item -Path $replacementPackageJson -Destination "package.json" -Force - - #default to root/eng/emitter-package-lock.json but you can override by writing - #Get-${Language}-EmitterPackageLockPath in your Language-Settings.ps1 - $emitterPackageLock = Join-Path $PSScriptRoot "../../emitter-package-lock.json" - if (Test-Path "Function:$GetEmitterPackageLockPathFn") { - $emitterPackageLock = &$GetEmitterPackageLockPathFn - } - - $usingLockFile = Test-Path $emitterPackageLock - - if ($usingLockFile) { - Write-Host("Copying package-lock.json from $emitterPackageLock") - Copy-Item -Path $emitterPackageLock -Destination "package-lock.json" -Force - } - - $useAlphaNpmRegistry = (Get-Content $replacementPackageJson -Raw).Contains("-alpha.") - - if($useAlphaNpmRegistry) { - Write-Host "Package.json contains '-alpha.' in the version, Creating .npmrc using public/azure-sdk-for-js-test-autorest feed." - "registry=https://pkgs.dev.azure.com/azure-sdk/public/_packaging/azure-sdk-for-js-test-autorest@local/npm/registry/ `n`nalways-auth=true" | Out-File '.npmrc' - } - - if ($usingLockFile) { - Write-Host "> npm ci" - npm ci - } - else { - Write-Host "> npm install" - npm install - } - - if ($LASTEXITCODE) { exit $LASTEXITCODE } - } - finally { - Pop-Location - } -} - $resolvedProjectDirectory = Resolve-Path $ProjectDirectory $emitterName = &$GetEmitterNameFn $typespecConfigurationFile = Resolve-Path "$ProjectDirectory/tsp-location.yaml" @@ -97,9 +29,9 @@ $tempFolder = "$ProjectDirectory/TempTypeSpecFiles" $npmWorkingDir = Resolve-Path $tempFolder/$innerFolder $mainTypeSpecFile = If (Test-Path "$npmWorkingDir/client.*") { Resolve-Path "$npmWorkingDir/client.*" } Else { Resolve-Path "$npmWorkingDir/main.*"} +Push-Location $npmWorkingDir try { - Push-Location $npmWorkingDir - NpmInstallForProject $npmWorkingDir + . $PSScriptRoot/Invoke-NpmInstall.ps1 if ($LASTEXITCODE) { exit $LASTEXITCODE } @@ -109,7 +41,7 @@ try { $emitterAdditionalOptions = " $emitterAdditionalOptions" } } - $typespecCompileCommand = "npx tsp compile $mainTypeSpecFile --emit $emitterName$emitterAdditionalOptions" + $typespecCompileCommand = "npx --no tsp compile $mainTypeSpecFile --emit $emitterName$emitterAdditionalOptions" if ($TypespecAdditionalOptions) { $options = $TypespecAdditionalOptions.Split(";"); foreach ($option in $options) { @@ -121,8 +53,7 @@ try { $typespecCompileCommand += " --option $emitterName.save-inputs=true" } - Write-Host($typespecCompileCommand) - Invoke-Expression $typespecCompileCommand + Invoke-LoggedCommand $typespecCompileCommand if ($LASTEXITCODE) { exit $LASTEXITCODE } } diff --git a/eng/common/scripts/Update-GeneratedSdks.ps1 b/eng/common/scripts/Update-GeneratedSdks.ps1 new file mode 100644 index 000000000000..10c2a290de22 --- /dev/null +++ b/eng/common/scripts/Update-GeneratedSdks.ps1 @@ -0,0 +1,16 @@ +[CmdLetBinding()] +param( + [Parameter(Mandatory)] + [string]$PackageFoldersFile +) + +. $PSScriptRoot/common.ps1 +. $PSScriptRoot/Helpers/CommandInvocation-Helpers.ps1 + +$ErrorActionPreference = 'Stop' + +if (Test-Path "Function:$UpdateGeneratedSdksFn") { + &$UpdateGeneratedSdksFn $PackageFoldersFile +} else { + Write-Error "Function $UpdateGeneratedSdksFn not implemented in Language-Settings.ps1" +} diff --git a/eng/common/scripts/common.ps1 b/eng/common/scripts/common.ps1 index 39d65cdd6818..0517a2d811f1 100644 --- a/eng/common/scripts/common.ps1 +++ b/eng/common/scripts/common.ps1 @@ -60,8 +60,8 @@ $GetPackageLevelReadmeFn = "Get-${Language}-PackageLevelReadme" $GetRepositoryLinkFn = "Get-${Language}-RepositoryLink" $GetEmitterAdditionalOptionsFn = "Get-${Language}-EmitterAdditionalOptions" $GetEmitterNameFn = "Get-${Language}-EmitterName" -$GetEmitterPackageJsonPathFn = "Get-${Language}-EmitterPackageJsonPath" -$GetEmitterPackageLockPathFn = "Get-${Language}-EmitterPackageLockPath" +$GetFoldersForGenerationFn = "Get-${Language}-FoldersForGeneration" +$UpdateGeneratedSdksFn = "Update-${Language}-GeneratedSdks" # Expected to be set in eng/scripts/docs/Docs-Onboarding.ps1 $SetDocsPackageOnboarding = "Set-${Language}-DocsPackageOnboarding" From fc09614536bd96b86e51f2b936ede76d8a9298c2 Mon Sep 17 00:00:00 2001 From: Patrick Hallisey Date: Fri, 8 Sep 2023 22:59:30 -0700 Subject: [PATCH 02/13] Add emitternpminstall --- eng/common/scripts/Cadl-Project-Generate.ps1 | 2 +- ...oke-NpmInstall.ps1 => Invoke-EmitterNpmInstall.ps1} | 0 eng/common/scripts/TypeSpec-Project-Generate.ps1 | 10 ++++++---- 3 files changed, 7 insertions(+), 5 deletions(-) rename eng/common/scripts/{Invoke-NpmInstall.ps1 => Invoke-EmitterNpmInstall.ps1} (100%) diff --git a/eng/common/scripts/Cadl-Project-Generate.ps1 b/eng/common/scripts/Cadl-Project-Generate.ps1 index 6e97e2f2cc6a..78f9d7dd43ff 100644 --- a/eng/common/scripts/Cadl-Project-Generate.ps1 +++ b/eng/common/scripts/Cadl-Project-Generate.ps1 @@ -31,7 +31,7 @@ $mainCadlFile = If (Test-Path "$npmWorkingDir/client.cadl") { Resolve-Path "$npm Push-Location $npmWorkingDir try { - . $PSScriptRoot/Invoke-NpmInstall.ps1 + . $PSScriptRoot/Invoke-EmitterNpmInstall.ps1 if ($LASTEXITCODE) { exit $LASTEXITCODE } diff --git a/eng/common/scripts/Invoke-NpmInstall.ps1 b/eng/common/scripts/Invoke-EmitterNpmInstall.ps1 similarity index 100% rename from eng/common/scripts/Invoke-NpmInstall.ps1 rename to eng/common/scripts/Invoke-EmitterNpmInstall.ps1 diff --git a/eng/common/scripts/TypeSpec-Project-Generate.ps1 b/eng/common/scripts/TypeSpec-Project-Generate.ps1 index a58f579db7cb..a0af0defbfaa 100644 --- a/eng/common/scripts/TypeSpec-Project-Generate.ps1 +++ b/eng/common/scripts/TypeSpec-Project-Generate.ps1 @@ -6,7 +6,8 @@ param ( [ValidateNotNullOrEmpty()] [string] $ProjectDirectory, [string] $TypespecAdditionalOptions = $null, ## addtional typespec emitter options, separated by semicolon if more than one, e.g. option1=value1;option2=value2 - [switch] $SaveInputs = $false ## saves the temporary files during execution, default false + [switch] $SaveInputs = $false, ## saves the temporary files during execution, default false + [switch] $SkipNpmInstall = $false ## skips npm install, default false ) $ErrorActionPreference = "Stop" @@ -31,9 +32,10 @@ $mainTypeSpecFile = If (Test-Path "$npmWorkingDir/client.*") { Resolve-Path "$np Push-Location $npmWorkingDir try { - . $PSScriptRoot/Invoke-NpmInstall.ps1 - - if ($LASTEXITCODE) { exit $LASTEXITCODE } + if (!$SkipNpmInstall) { + . $PSScriptRoot/Invoke-EmitterNpmInstall.ps1 + if ($LASTEXITCODE) { exit $LASTEXITCODE } + } if (Test-Path "Function:$GetEmitterAdditionalOptionsFn") { $emitterAdditionalOptions = &$GetEmitterAdditionalOptionsFn $resolvedProjectDirectory From 5107ee3d41e09a8c8fd2c2f2442e36e2e48ccc54 Mon Sep 17 00:00:00 2001 From: Patrick Hallisey Date: Fri, 8 Sep 2023 23:05:53 -0700 Subject: [PATCH 03/13] Always install --- eng/common/scripts/Invoke-EmitterNpmInstall.ps1 | 5 ----- 1 file changed, 5 deletions(-) diff --git a/eng/common/scripts/Invoke-EmitterNpmInstall.ps1 b/eng/common/scripts/Invoke-EmitterNpmInstall.ps1 index 9e32cb907752..d0fe4176f331 100644 --- a/eng/common/scripts/Invoke-EmitterNpmInstall.ps1 +++ b/eng/common/scripts/Invoke-EmitterNpmInstall.ps1 @@ -6,11 +6,6 @@ Push-Location $RepoRoot try { $currentDur = Resolve-Path "." - if (Test-Path "node_modules") { - Write-Host "node_modules folder already exists. Skipping npm install." - exit 0 - } - Write-Host "Installing npm dependencies in $currentDur" if (Test-Path "package.json") { From dfa2450bb18bf2f8d0aaeb5e2759e04aa004fa4e Mon Sep 17 00:00:00 2001 From: Patrick Hallisey Date: Sat, 9 Sep 2023 00:17:42 -0700 Subject: [PATCH 04/13] Use shorter leg name --- eng/common/scripts/New-RegenerateMatrix.ps1 | 24 ++++++++------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/eng/common/scripts/New-RegenerateMatrix.ps1 b/eng/common/scripts/New-RegenerateMatrix.ps1 index 840f2503feb7..ea0c2972e984 100644 --- a/eng/common/scripts/New-RegenerateMatrix.ps1 +++ b/eng/common/scripts/New-RegenerateMatrix.ps1 @@ -38,21 +38,15 @@ function Split-Items([array]$Items) { # ensure the output directory exists New-Item -ItemType Directory -Path $OutputDirectory -Force | Out-Null -if (Test-Path "Function:$GetFoldersForGenerationFn") { - $foldersForGeneration = &$GetFoldersForGenerationFn -} else { - $foldersForGeneration = Get-ChildItem "$RepoRoot/sdk" -Directory | Get-ChildItem -Directory -} - -[array]$packageFolders = $foldersForGeneration - | Sort-Object -Property FullName - | ForEach-Object { - [ordered]@{ - "PackageFolder" = "$($_.Parent.Name)/$($_.Name)" - "ServiceArea" = $_.Parent.Name - } +[array]$packageFolders = Get-ChildItem "$RepoRoot/sdk" -Directory +| Get-ChildItem -Directory +| Sort-Object -Property FullName +| ForEach-Object { + [ordered]@{ + "PackageFolder" = "$($_.Parent.Name)/$($_.Name)" + "ServiceArea" = $_.Parent.Name } - +} $batches = Split-Items -Items $packageFolders @@ -61,7 +55,7 @@ for ($i = 0; $i -lt $batches.Length; $i++) { $batch = $batches[$i] $firstPrefix = $batch[0].ServiceArea.Substring(0, 2) $lastPrefix = $batch[-1].ServiceArea.Substring(0, 2) - $key = "Batch_$i`_$firstPrefix`_$lastPrefix" + $key = "$firstPrefix`_$lastPrefix`_$i" $fileName = "$i.json" $batch.PackageFolder | ConvertTo-Json -AsArray | Out-File "$OutputDirectory/$fileName" $matrix[$key] = [ordered]@{ "JobKey" = $key; "FolderList" = $fileName } From e97927e0e42f52da2c4a2a7874dc40147f6ac0e1 Mon Sep 17 00:00:00 2001 From: Patrick Hallisey Date: Tue, 12 Sep 2023 15:21:42 -0700 Subject: [PATCH 05/13] Add short circuiting to emitter install --- .../scripts/Invoke-EmitterNpmInstall.ps1 | 25 +++++++++++++++++-- .../scripts/TypeSpec-Project-Generate.ps1 | 10 ++++---- 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/eng/common/scripts/Invoke-EmitterNpmInstall.ps1 b/eng/common/scripts/Invoke-EmitterNpmInstall.ps1 index d0fe4176f331..2e1399bb5bec 100644 --- a/eng/common/scripts/Invoke-EmitterNpmInstall.ps1 +++ b/eng/common/scripts/Invoke-EmitterNpmInstall.ps1 @@ -1,3 +1,7 @@ +param( + [switch] $Force +) + . $PSScriptRoot/Helpers/PSModule-Helpers.ps1 . $PSScriptRoot/Helpers/CommandInvocation-Helpers.ps1 . $PSScriptRoot/common.ps1 @@ -5,9 +9,28 @@ Push-Location $RepoRoot try { $currentDur = Resolve-Path "." + $usingLockFile = Test-Path './eng/emitter-package-lock.json' Write-Host "Installing npm dependencies in $currentDur" + if (!$Force -and $usingLockFile -and (Test-Path "node_modules/.package-lock.json")) { + # If we have a lock file and a node_modules/.package-lock.json we may + # be able to skip npm install. + + # After install, the lock file in node_modules should match outer lock + # file, except for an empty string "self" package that only exists in + # the outer lock file. + $outerLockFile = Get-Content './eng/emitter-package-lock.json' -Raw | ConvertFrom-Json -AsHashtable + $outerLockFile.packages.Remove('') + + $nodeModulesLockFile = Get-Content 'node_modules/.package-lock.json' -Raw | ConvertFrom-Json -AsHashtable + + if (($outerLockFile | ConvertTo-Json -Depth 100 -Compress) -eq ($nodeModulesLockFile | ConvertTo-Json -Depth 100 -Compress)) { + Write-Host "Skipping npm install because node_modules is up to date" + exit 0 + } + } + if (Test-Path "package.json") { Write-Host "Removing existing package.json" Remove-Item -Path "package.json" -Force @@ -26,8 +49,6 @@ try { Write-Host("Copying package.json from eng/emitter-package.json") Copy-Item -Path './eng/emitter-package.json' -Destination "package.json" -Force - $usingLockFile = Test-Path './eng/emitter-package-lock.json' - if ($usingLockFile) { Write-Host("Copying package-lock.json from eng/emitter-package-lock.json") Copy-Item -Path './eng/emitter-package-lock.json' -Destination "package-lock.json" -Force diff --git a/eng/common/scripts/TypeSpec-Project-Generate.ps1 b/eng/common/scripts/TypeSpec-Project-Generate.ps1 index a0af0defbfaa..392c86047b77 100644 --- a/eng/common/scripts/TypeSpec-Project-Generate.ps1 +++ b/eng/common/scripts/TypeSpec-Project-Generate.ps1 @@ -30,13 +30,13 @@ $tempFolder = "$ProjectDirectory/TempTypeSpecFiles" $npmWorkingDir = Resolve-Path $tempFolder/$innerFolder $mainTypeSpecFile = If (Test-Path "$npmWorkingDir/client.*") { Resolve-Path "$npmWorkingDir/client.*" } Else { Resolve-Path "$npmWorkingDir/main.*"} +if (!$SkipNpmInstall) { + . $PSScriptRoot/Invoke-EmitterNpmInstall.ps1 + if ($LASTEXITCODE) { exit $LASTEXITCODE } +} + Push-Location $npmWorkingDir try { - if (!$SkipNpmInstall) { - . $PSScriptRoot/Invoke-EmitterNpmInstall.ps1 - if ($LASTEXITCODE) { exit $LASTEXITCODE } - } - if (Test-Path "Function:$GetEmitterAdditionalOptionsFn") { $emitterAdditionalOptions = &$GetEmitterAdditionalOptionsFn $resolvedProjectDirectory if ($emitterAdditionalOptions.Length -gt 0) { From 4f23871225235da0fba709b8f2ee8c8f6092e119 Mon Sep 17 00:00:00 2001 From: Patrick Hallisey Date: Tue, 12 Sep 2023 17:30:15 -0700 Subject: [PATCH 06/13] Use language matrix function --- eng/common/scripts/New-RegenerateMatrix.ps1 | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/eng/common/scripts/New-RegenerateMatrix.ps1 b/eng/common/scripts/New-RegenerateMatrix.ps1 index ea0c2972e984..48542266d86b 100644 --- a/eng/common/scripts/New-RegenerateMatrix.ps1 +++ b/eng/common/scripts/New-RegenerateMatrix.ps1 @@ -38,7 +38,13 @@ function Split-Items([array]$Items) { # ensure the output directory exists New-Item -ItemType Directory -Path $OutputDirectory -Force | Out-Null -[array]$packageFolders = Get-ChildItem "$RepoRoot/sdk" -Directory +if (Test-Path "Function:$GetFoldersForGenerationFn") { + $foldersForGeneration = &$GetFoldersForGenerationFn +} else { + $foldersForGeneration = Get-ChildItem "$RepoRoot/sdk" -Directory | Get-ChildItem -Directory +} + +[array]$packageFolders = $foldersForGeneration | Get-ChildItem -Directory | Sort-Object -Property FullName | ForEach-Object { From 3468a4f81e7315ec8941db4cb32937728556fe4e Mon Sep 17 00:00:00 2001 From: Patrick Hallisey Date: Wed, 13 Sep 2023 08:40:12 -0700 Subject: [PATCH 07/13] Don't look for subfolders in the matrix package folders --- eng/common/scripts/New-RegenerateMatrix.ps1 | 1 - 1 file changed, 1 deletion(-) diff --git a/eng/common/scripts/New-RegenerateMatrix.ps1 b/eng/common/scripts/New-RegenerateMatrix.ps1 index 48542266d86b..f034296c7aad 100644 --- a/eng/common/scripts/New-RegenerateMatrix.ps1 +++ b/eng/common/scripts/New-RegenerateMatrix.ps1 @@ -45,7 +45,6 @@ if (Test-Path "Function:$GetFoldersForGenerationFn") { } [array]$packageFolders = $foldersForGeneration -| Get-ChildItem -Directory | Sort-Object -Property FullName | ForEach-Object { [ordered]@{ From 244720a0b9627afd930554f94ea5914e5d4696d8 Mon Sep 17 00:00:00 2001 From: Patrick Hallisey Date: Wed, 13 Sep 2023 20:34:12 -0700 Subject: [PATCH 08/13] Revert unnecessary eng/common changes --- eng/common/scripts/Cadl-Project-Generate.ps1 | 50 +++++++++-- .../scripts/Invoke-EmitterNpmInstall.ps1 | 82 ------------------- .../scripts/TypeSpec-Project-Generate.ps1 | 78 +++++++++++++++--- 3 files changed, 112 insertions(+), 98 deletions(-) delete mode 100644 eng/common/scripts/Invoke-EmitterNpmInstall.ps1 diff --git a/eng/common/scripts/Cadl-Project-Generate.ps1 b/eng/common/scripts/Cadl-Project-Generate.ps1 index 78f9d7dd43ff..3e7ee781b053 100644 --- a/eng/common/scripts/Cadl-Project-Generate.ps1 +++ b/eng/common/scripts/Cadl-Project-Generate.ps1 @@ -11,10 +11,48 @@ param ( $ErrorActionPreference = "Stop" . $PSScriptRoot/Helpers/PSModule-Helpers.ps1 -. $PSScriptRoot/Helpers/CommandInvocation-Helpers.ps1 . $PSScriptRoot/common.ps1 Install-ModuleIfNotInstalled "powershell-yaml" "0.4.1" | Import-Module +function NpmInstallForProject([string]$workingDirectory) { + Push-Location $workingDirectory + try { + $currentDur = Resolve-Path "." + Write-Host "Generating from $currentDur" + + if (Test-Path "package.json") { + Remove-Item -Path "package.json" -Force + } + + if (Test-Path ".npmrc") { + Remove-Item -Path ".npmrc" -Force + } + + if (Test-Path "node_modules") { + Remove-Item -Path "node_modules" -Force -Recurse + } + + if (Test-Path "package-lock.json") { + Remove-Item -Path "package-lock.json" -Force + } + + #default to root/eng/emitter-package.json but you can override by writing + #Get-${Language}-EmitterPackageJsonPath in your Language-Settings.ps1 + $replacementPackageJson = "$PSScriptRoot/../../emitter-package.json" + if (Test-Path "Function:$GetEmitterPackageJsonPathFn") { + $replacementPackageJson = &$GetEmitterPackageJsonPathFn + } + + Write-Host("Copying package.json from $replacementPackageJson") + Copy-Item -Path $replacementPackageJson -Destination "package.json" -Force + npm install --no-lock-file + if ($LASTEXITCODE) { exit $LASTEXITCODE } + } + finally { + Pop-Location + } +} + $resolvedProjectDirectory = Resolve-Path $ProjectDirectory $emitterName = &$GetEmitterNameFn $cadlConfigurationFile = Resolve-Path "$ProjectDirectory/cadl-location.yaml" @@ -29,9 +67,9 @@ $tempFolder = "$ProjectDirectory/TempCadlFiles" $npmWorkingDir = Resolve-Path $tempFolder/$innerFolder $mainCadlFile = If (Test-Path "$npmWorkingDir/client.cadl") { Resolve-Path "$npmWorkingDir/client.cadl" } Else { Resolve-Path "$npmWorkingDir/main.cadl"} -Push-Location $npmWorkingDir try { - . $PSScriptRoot/Invoke-EmitterNpmInstall.ps1 + Push-Location $npmWorkingDir + NpmInstallForProject $npmWorkingDir if ($LASTEXITCODE) { exit $LASTEXITCODE } @@ -41,15 +79,15 @@ try { $emitterAdditionalOptions = " $emitterAdditionalOptions" } } - $cadlCompileCommand = "npx --no cadl compile $mainCadlFile --emit $emitterName$emitterAdditionalOptions" + $cadlCompileCommand = "npx cadl compile $mainCadlFile --emit $emitterName$emitterAdditionalOptions" if ($CadlAdditionalOptions) { $options = $CadlAdditionalOptions.Split(";"); foreach ($option in $options) { $cadlCompileCommand += " --option $emitterName.$option" } } - - Invoke-LoggedCommand $cadlCompileCommand + Write-Host($cadlCompileCommand) + Invoke-Expression $cadlCompileCommand if ($LASTEXITCODE) { exit $LASTEXITCODE } } diff --git a/eng/common/scripts/Invoke-EmitterNpmInstall.ps1 b/eng/common/scripts/Invoke-EmitterNpmInstall.ps1 deleted file mode 100644 index 2e1399bb5bec..000000000000 --- a/eng/common/scripts/Invoke-EmitterNpmInstall.ps1 +++ /dev/null @@ -1,82 +0,0 @@ -param( - [switch] $Force -) - -. $PSScriptRoot/Helpers/PSModule-Helpers.ps1 -. $PSScriptRoot/Helpers/CommandInvocation-Helpers.ps1 -. $PSScriptRoot/common.ps1 - -Push-Location $RepoRoot -try { - $currentDur = Resolve-Path "." - $usingLockFile = Test-Path './eng/emitter-package-lock.json' - - Write-Host "Installing npm dependencies in $currentDur" - - if (!$Force -and $usingLockFile -and (Test-Path "node_modules/.package-lock.json")) { - # If we have a lock file and a node_modules/.package-lock.json we may - # be able to skip npm install. - - # After install, the lock file in node_modules should match outer lock - # file, except for an empty string "self" package that only exists in - # the outer lock file. - $outerLockFile = Get-Content './eng/emitter-package-lock.json' -Raw | ConvertFrom-Json -AsHashtable - $outerLockFile.packages.Remove('') - - $nodeModulesLockFile = Get-Content 'node_modules/.package-lock.json' -Raw | ConvertFrom-Json -AsHashtable - - if (($outerLockFile | ConvertTo-Json -Depth 100 -Compress) -eq ($nodeModulesLockFile | ConvertTo-Json -Depth 100 -Compress)) { - Write-Host "Skipping npm install because node_modules is up to date" - exit 0 - } - } - - if (Test-Path "package.json") { - Write-Host "Removing existing package.json" - Remove-Item -Path "package.json" -Force - } - - if (Test-Path "package-lock.json") { - Write-Host "Removing existing package-lock.json" - Remove-Item -Path "package-lock.json" -Force - } - - if (Test-Path ".npmrc") { - Write-Host "Removing existing .nprc" - Remove-Item -Path ".npmrc" -Force - } - - Write-Host("Copying package.json from eng/emitter-package.json") - Copy-Item -Path './eng/emitter-package.json' -Destination "package.json" -Force - - if ($usingLockFile) { - Write-Host("Copying package-lock.json from eng/emitter-package-lock.json") - Copy-Item -Path './eng/emitter-package-lock.json' -Destination "package-lock.json" -Force - } - - $useAlphaNpmRegistry = (Get-Content 'package.json' -Raw).Contains("-alpha.") - - if ($useAlphaNpmRegistry) { - Write-Host "Package.json contains '-alpha.' in the version, Creating .npmrc using public/azure-sdk-for-js-test-autorest feed." - "registry=https://pkgs.dev.azure.com/azure-sdk/public/_packaging/azure-sdk-for-js-test-autorest@local/npm/registry/" | Out-File '.npmrc' - } - - if ($usingLockFile) { - Invoke-LoggedCommand "npm ci" - } - else { - Invoke-LoggedCommand "npm install" - } - - Remove-Item -Path "package.json" -Force - Remove-Item -Path "package-lock.json" -Force - - if ($useAlphaNpmRegistry) { - Remove-Item -Path ".npmrc" -Force - } - - if ($LASTEXITCODE) { exit $LASTEXITCODE } -} -finally { - Pop-Location -} \ No newline at end of file diff --git a/eng/common/scripts/TypeSpec-Project-Generate.ps1 b/eng/common/scripts/TypeSpec-Project-Generate.ps1 index 392c86047b77..05a0e0bdfd45 100644 --- a/eng/common/scripts/TypeSpec-Project-Generate.ps1 +++ b/eng/common/scripts/TypeSpec-Project-Generate.ps1 @@ -6,8 +6,7 @@ param ( [ValidateNotNullOrEmpty()] [string] $ProjectDirectory, [string] $TypespecAdditionalOptions = $null, ## addtional typespec emitter options, separated by semicolon if more than one, e.g. option1=value1;option2=value2 - [switch] $SaveInputs = $false, ## saves the temporary files during execution, default false - [switch] $SkipNpmInstall = $false ## skips npm install, default false + [switch] $SaveInputs = $false ## saves the temporary files during execution, default false ) $ErrorActionPreference = "Stop" @@ -16,6 +15,65 @@ $ErrorActionPreference = "Stop" . $PSScriptRoot/common.ps1 Install-ModuleIfNotInstalled "powershell-yaml" "0.4.1" | Import-Module +function NpmInstallForProject([string]$workingDirectory) { + Push-Location $workingDirectory + try { + $currentDur = Resolve-Path "." + Write-Host "Generating from $currentDur" + + if (Test-Path "package.json") { + Write-Host "Removing existing package.json" + Remove-Item -Path "package.json" -Force + } + + if (Test-Path ".npmrc") { + Write-Host "Removing existing .nprc" + Remove-Item -Path ".npmrc" -Force + } + + if (Test-Path "node_modules") { + Write-Host "Removing existing node_modules" + Remove-Item -Path "node_modules" -Force -Recurse + } + + if (Test-Path "package-lock.json") { + Write-Host "Removing existing package-lock.json" + Remove-Item -Path "package-lock.json" -Force + } + + $replacementPackageJson = Join-Path $PSScriptRoot "../../emitter-package.json" + + Write-Host("Copying package.json from $replacementPackageJson") + Copy-Item -Path $replacementPackageJson -Destination "package.json" -Force + $emitterPackageLock = Join-Path $PSScriptRoot "../../emitter-package-lock.json" + $usingLockFile = Test-Path $emitterPackageLock + + if ($usingLockFile) { + Write-Host("Copying package-lock.json from $emitterPackageLock") + Copy-Item -Path $emitterPackageLock -Destination "package-lock.json" -Force + } + + $useAlphaNpmRegistry = (Get-Content $replacementPackageJson -Raw).Contains("-alpha.") + + if($useAlphaNpmRegistry) { + Write-Host "Package.json contains '-alpha.' in the version, Creating .npmrc using public/azure-sdk-for-js-test-autorest feed." + "registry=https://pkgs.dev.azure.com/azure-sdk/public/_packaging/azure-sdk-for-js-test-autorest@local/npm/registry/ `n`nalways-auth=true" | Out-File '.npmrc' + } + + if ($usingLockFile) { + Invoke-LoggedCommand "npm ci" + } + else { + Invoke-LoggedCommand "npm install" + } + + if ($LASTEXITCODE) { exit $LASTEXITCODE } + } + finally { + Pop-Location + } +} + $resolvedProjectDirectory = Resolve-Path $ProjectDirectory $emitterName = &$GetEmitterNameFn $typespecConfigurationFile = Resolve-Path "$ProjectDirectory/tsp-location.yaml" @@ -30,20 +88,19 @@ $tempFolder = "$ProjectDirectory/TempTypeSpecFiles" $npmWorkingDir = Resolve-Path $tempFolder/$innerFolder $mainTypeSpecFile = If (Test-Path "$npmWorkingDir/client.*") { Resolve-Path "$npmWorkingDir/client.*" } Else { Resolve-Path "$npmWorkingDir/main.*"} -if (!$SkipNpmInstall) { - . $PSScriptRoot/Invoke-EmitterNpmInstall.ps1 - if ($LASTEXITCODE) { exit $LASTEXITCODE } -} - -Push-Location $npmWorkingDir try { + Push-Location $npmWorkingDir + NpmInstallForProject $npmWorkingDir + + if ($LASTEXITCODE) { exit $LASTEXITCODE } + if (Test-Path "Function:$GetEmitterAdditionalOptionsFn") { $emitterAdditionalOptions = &$GetEmitterAdditionalOptionsFn $resolvedProjectDirectory if ($emitterAdditionalOptions.Length -gt 0) { $emitterAdditionalOptions = " $emitterAdditionalOptions" } } - $typespecCompileCommand = "npx --no tsp compile $mainTypeSpecFile --emit $emitterName$emitterAdditionalOptions" + $typespecCompileCommand = "npx tsp compile $mainTypeSpecFile --emit $emitterName$emitterAdditionalOptions" if ($TypespecAdditionalOptions) { $options = $TypespecAdditionalOptions.Split(";"); foreach ($option in $options) { @@ -55,7 +112,8 @@ try { $typespecCompileCommand += " --option $emitterName.save-inputs=true" } - Invoke-LoggedCommand $typespecCompileCommand + Write-Host($typespecCompileCommand) + Invoke-Expression $typespecCompileCommand if ($LASTEXITCODE) { exit $LASTEXITCODE } } From 30941dbe38fa90dc9f2e7b75d4ae87d78f3acbdb Mon Sep 17 00:00:00 2001 From: Patrick Hallisey Date: Wed, 13 Sep 2023 22:53:00 -0700 Subject: [PATCH 09/13] Rewrite GetPullRequestUrl to Get-BuildSourceDescription --- .../scripts/Get-BuildSourceDescription.ps1 | 24 +++++++++++++++++++ eng/common/scripts/Get-PullRequestUrl.ps1 | 19 --------------- 2 files changed, 24 insertions(+), 19 deletions(-) create mode 100644 eng/common/scripts/Get-BuildSourceDescription.ps1 delete mode 100644 eng/common/scripts/Get-PullRequestUrl.ps1 diff --git a/eng/common/scripts/Get-BuildSourceDescription.ps1 b/eng/common/scripts/Get-BuildSourceDescription.ps1 new file mode 100644 index 000000000000..b0856101538d --- /dev/null +++ b/eng/common/scripts/Get-BuildSourceDescription.ps1 @@ -0,0 +1,24 @@ +param( + [string]$Variable, + [switch]$IsOutput +) + +$repoUrl = $env:BUILD_REPOSITORY_URI +$sourceBranch = $env:BUILD_SOURCEBRANCH + +$description = "[$sourceBranch]($repoUrl/tree/$sourceBranch)" +if ($sourceBranch -match "^refs/heads/(.+)$") { + $description = "Branch: [$($Matches[1])]($repoUrl/tree/$sourceBranch)" +} elseif ($sourceBranch -match "^refs/tags/(.+)$") { + $description = "Tag: [$($Matches[1])]($repoUrl/tree/$sourceBranch)" +} elseif ($sourceBranch -match "^refs/pull/(\d+)/(head|merge)$") { + $description = "Pull request: $repoUrl/pull/$($Matches[1])" +} + +if ($IsOutput) { + Write-Host "Setting output variable '$Variable' to '$description'" + Write-Host "##vso[task.setvariable variable=$Variable;isoutput=true]$description" +} else { + Write-Host "Setting variable '$Variable' to '$description'" + Write-Host "##vso[task.setvariable variable=$Variable]$description" +} diff --git a/eng/common/scripts/Get-PullRequestUrl.ps1 b/eng/common/scripts/Get-PullRequestUrl.ps1 deleted file mode 100644 index 54d231a4ea4f..000000000000 --- a/eng/common/scripts/Get-PullRequestUrl.ps1 +++ /dev/null @@ -1,19 +0,0 @@ -param( - [string]$RepositoryName, - [string]$SourceBranch, - [string]$Variable, - [switch]$IsOutput -) - -$PRUrl = $SourceBranch -if ($SourceBranch -match "refs/pull/(\d+)/(head|merge)") { - $PRUrl = "https://github.com/$RepositoryName/pull/$($Matches[1])" -} - -if ($IsOutput) { - Write-Host "Setting output variable '$Variable' to $PRUrl" - Write-Host "##vso[task.setvariable variable=$Variable;isoutput=true]$PRUrl" -} else { - Write-Host "Setting variable '$Variable' to $PRUrl" - Write-Host "##vso[task.setvariable variable=$Variable]$PRUrl" -} From 7a4a185b09933856d43360eb5d22b8ac0e834f9d Mon Sep 17 00:00:00 2001 From: Patrick Hallisey Date: Wed, 13 Sep 2023 22:58:38 -0700 Subject: [PATCH 10/13] Remove alias from Invoke-LoggedCommand --- eng/common/scripts/Helpers/CommandInvocation-Helpers.ps1 | 2 -- 1 file changed, 2 deletions(-) diff --git a/eng/common/scripts/Helpers/CommandInvocation-Helpers.ps1 b/eng/common/scripts/Helpers/CommandInvocation-Helpers.ps1 index a583276d3f03..1db9bf225644 100644 --- a/eng/common/scripts/Helpers/CommandInvocation-Helpers.ps1 +++ b/eng/common/scripts/Helpers/CommandInvocation-Helpers.ps1 @@ -47,5 +47,3 @@ function Invoke-LoggedCommand($Command, $ExecutePath, [switch]$GroupOutput) } } } - -Set-Alias -Name invoke -Value Invoke-LoggedCommand \ No newline at end of file From 2642f4306e253f35898536cec259dbcd77f962d8 Mon Sep 17 00:00:00 2001 From: Patrick Hallisey Date: Wed, 13 Sep 2023 23:09:36 -0700 Subject: [PATCH 11/13] Use invoke-expression instead of shelling out --- eng/common/scripts/Helpers/CommandInvocation-Helpers.ps1 | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/eng/common/scripts/Helpers/CommandInvocation-Helpers.ps1 b/eng/common/scripts/Helpers/CommandInvocation-Helpers.ps1 index 1db9bf225644..5dc0c8c7da1a 100644 --- a/eng/common/scripts/Helpers/CommandInvocation-Helpers.ps1 +++ b/eng/common/scripts/Helpers/CommandInvocation-Helpers.ps1 @@ -14,14 +14,7 @@ function Invoke-LoggedCommand($Command, $ExecutePath, [switch]$GroupOutput) } try { - if ($IsLinux -or $IsMacOs) - { - sh -c "$Command 2>&1" - } - else - { - cmd /c "$Command 2>&1" - } + Invoke-Expression $Command $duration = (Get-Date) - $startTime From 587b271ee5a509f28ceb83af4252cad96366c6c5 Mon Sep 17 00:00:00 2001 From: Patrick Hallisey Date: Wed, 20 Sep 2023 15:33:40 -0700 Subject: [PATCH 12/13] Add better job splitting --- eng/common/scripts/New-RegenerateMatrix.ps1 | 50 +++++++++++++++++---- 1 file changed, 41 insertions(+), 9 deletions(-) diff --git a/eng/common/scripts/New-RegenerateMatrix.ps1 b/eng/common/scripts/New-RegenerateMatrix.ps1 index f034296c7aad..ddd7a49d6fa9 100644 --- a/eng/common/scripts/New-RegenerateMatrix.ps1 +++ b/eng/common/scripts/New-RegenerateMatrix.ps1 @@ -7,19 +7,35 @@ param ( [string]$OutputVariableName, [Parameter()] - [int]$JobCount = 8 + [int]$JobCount = 8, + + # The minimum number of items per job. If the number of items is less than this, then the number of jobs will be reduced. + [Parameter()] + [int]$MinimumPerJob = 10, + + [Parameter()] + [string]$OnlyTypespec ) . (Join-Path $PSScriptRoot common.ps1) +[bool]$OnlyTypespec = $OnlyTypespec -in @("true", "t", "1", "yes", "y") + # Divide the items into groups of approximately equal size. function Split-Items([array]$Items) { # given $Items.Length = 22 and $JobCount = 5 # then $itemsPerGroup = 4 # and $largeJobCount = 2 # and $group.Length = 5, 5, 4, 4, 4 - $itemsPerGroup = [math]::Floor($Items.Length / $JobCount) - $largeJobCount = $Items.Length % $itemsPerGroup + $itemCount = $Items.Length + $jobsForMinimum = $itemCount -lt $MinimumPerJob ? 1 : [math]::Floor($itemCount / $MinimumPerJob) + + if ($JobCount -gt $jobsForMinimum) { + $JobCount = $jobsForMinimum + } + + $itemsPerGroup = [math]::Floor($itemCount / $JobCount) + $largeJobCount = $itemCount % $itemsPerGroup $groups = [object[]]::new($JobCount) $i = 0 @@ -32,6 +48,8 @@ function Split-Items([array]$Items) { } } + Write-Host "$itemCount items split into $JobCount groups of approximately $itemsPerGroup items each." + return , $groups } @@ -39,17 +57,22 @@ function Split-Items([array]$Items) { New-Item -ItemType Directory -Path $OutputDirectory -Force | Out-Null if (Test-Path "Function:$GetFoldersForGenerationFn") { - $foldersForGeneration = &$GetFoldersForGenerationFn -} else { + $foldersForGeneration = &$GetFoldersForGenerationFn -OnlyTypespec $OnlyTypespec +} +else { $foldersForGeneration = Get-ChildItem "$RepoRoot/sdk" -Directory | Get-ChildItem -Directory + + if ($OnlyTypespec) { + $foldersForGeneration = $foldersForGeneration | Where-Object { Test-Path "$_/tsp-location.yaml" } + } } [array]$packageFolders = $foldersForGeneration | Sort-Object -Property FullName | ForEach-Object { [ordered]@{ - "PackageFolder" = "$($_.Parent.Name)/$($_.Name)" - "ServiceArea" = $_.Parent.Name + "PackageFolder" = "$($_.Parent.Name)/$($_.Name)" + "ServiceArea" = $_.Parent.Name } } @@ -58,11 +81,20 @@ $batches = Split-Items -Items $packageFolders $matrix = [ordered]@{} for ($i = 0; $i -lt $batches.Length; $i++) { $batch = $batches[$i] + $json = $batch.PackageFolder | ConvertTo-Json -AsArray + $firstPrefix = $batch[0].ServiceArea.Substring(0, 2) $lastPrefix = $batch[-1].ServiceArea.Substring(0, 2) + $key = "$firstPrefix`_$lastPrefix`_$i" - $fileName = "$i.json" - $batch.PackageFolder | ConvertTo-Json -AsArray | Out-File "$OutputDirectory/$fileName" + $fileName = "$key.json" + + Write-Host "`n`n==================================" + Write-Host $fileName + Write-Host "==================================" + $json | Out-Host + $json | Out-File "$OutputDirectory/$fileName" + $matrix[$key] = [ordered]@{ "JobKey" = $key; "FolderList" = $fileName } } From 3ceea9320a3b6a37e1dd0cc5ebdf4f7f09a86b92 Mon Sep 17 00:00:00 2001 From: Patrick Hallisey Date: Wed, 20 Sep 2023 17:46:40 -0700 Subject: [PATCH 13/13] Replace Folder with Directory --- eng/common/scripts/New-RegenerateMatrix.ps1 | 24 ++++++++++----------- eng/common/scripts/Update-GeneratedSdks.ps1 | 4 ++-- eng/common/scripts/common.ps1 | 2 +- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/eng/common/scripts/New-RegenerateMatrix.ps1 b/eng/common/scripts/New-RegenerateMatrix.ps1 index ddd7a49d6fa9..1df97420c25d 100644 --- a/eng/common/scripts/New-RegenerateMatrix.ps1 +++ b/eng/common/scripts/New-RegenerateMatrix.ps1 @@ -56,32 +56,32 @@ function Split-Items([array]$Items) { # ensure the output directory exists New-Item -ItemType Directory -Path $OutputDirectory -Force | Out-Null -if (Test-Path "Function:$GetFoldersForGenerationFn") { - $foldersForGeneration = &$GetFoldersForGenerationFn -OnlyTypespec $OnlyTypespec +if (Test-Path "Function:$GetDirectoriesForGenerationFn") { + $directoriesForGeneration = &$GetDirectoriesForGenerationFn } else { - $foldersForGeneration = Get-ChildItem "$RepoRoot/sdk" -Directory | Get-ChildItem -Directory - - if ($OnlyTypespec) { - $foldersForGeneration = $foldersForGeneration | Where-Object { Test-Path "$_/tsp-location.yaml" } - } + $directoriesForGeneration = Get-ChildItem "$RepoRoot/sdk" -Directory | Get-ChildItem -Directory +} + +if ($OnlyTypespec) { + $directoriesForGeneration = $directoriesForGeneration | Where-Object { Test-Path "$_/tsp-location.yaml" } } -[array]$packageFolders = $foldersForGeneration +[array]$packageDirectories = $directoriesForGeneration | Sort-Object -Property FullName | ForEach-Object { [ordered]@{ - "PackageFolder" = "$($_.Parent.Name)/$($_.Name)" + "PackageDirectory" = "$($_.Parent.Name)/$($_.Name)" "ServiceArea" = $_.Parent.Name } } -$batches = Split-Items -Items $packageFolders +$batches = Split-Items -Items $packageDirectories $matrix = [ordered]@{} for ($i = 0; $i -lt $batches.Length; $i++) { $batch = $batches[$i] - $json = $batch.PackageFolder | ConvertTo-Json -AsArray + $json = $batch.PackageDirectory | ConvertTo-Json -AsArray $firstPrefix = $batch[0].ServiceArea.Substring(0, 2) $lastPrefix = $batch[-1].ServiceArea.Substring(0, 2) @@ -95,7 +95,7 @@ for ($i = 0; $i -lt $batches.Length; $i++) { $json | Out-Host $json | Out-File "$OutputDirectory/$fileName" - $matrix[$key] = [ordered]@{ "JobKey" = $key; "FolderList" = $fileName } + $matrix[$key] = [ordered]@{ "JobKey" = $key; "DirectoryList" = $fileName } } $compressed = ConvertTo-Json $matrix -Depth 100 -Compress diff --git a/eng/common/scripts/Update-GeneratedSdks.ps1 b/eng/common/scripts/Update-GeneratedSdks.ps1 index 10c2a290de22..dd671f6d8ad8 100644 --- a/eng/common/scripts/Update-GeneratedSdks.ps1 +++ b/eng/common/scripts/Update-GeneratedSdks.ps1 @@ -1,7 +1,7 @@ [CmdLetBinding()] param( [Parameter(Mandatory)] - [string]$PackageFoldersFile + [string]$PackageDirectoriesFile ) . $PSScriptRoot/common.ps1 @@ -10,7 +10,7 @@ param( $ErrorActionPreference = 'Stop' if (Test-Path "Function:$UpdateGeneratedSdksFn") { - &$UpdateGeneratedSdksFn $PackageFoldersFile + &$UpdateGeneratedSdksFn $PackageDirectoriesFile } else { Write-Error "Function $UpdateGeneratedSdksFn not implemented in Language-Settings.ps1" } diff --git a/eng/common/scripts/common.ps1 b/eng/common/scripts/common.ps1 index 0517a2d811f1..cef0b23c5620 100644 --- a/eng/common/scripts/common.ps1 +++ b/eng/common/scripts/common.ps1 @@ -60,7 +60,7 @@ $GetPackageLevelReadmeFn = "Get-${Language}-PackageLevelReadme" $GetRepositoryLinkFn = "Get-${Language}-RepositoryLink" $GetEmitterAdditionalOptionsFn = "Get-${Language}-EmitterAdditionalOptions" $GetEmitterNameFn = "Get-${Language}-EmitterName" -$GetFoldersForGenerationFn = "Get-${Language}-FoldersForGeneration" +$GetDirectoriesForGenerationFn = "Get-${Language}-DirectoriesForGeneration" $UpdateGeneratedSdksFn = "Update-${Language}-GeneratedSdks" # Expected to be set in eng/scripts/docs/Docs-Onboarding.ps1