From 8cfd3cca2298c09389471b01981336c529a670f3 Mon Sep 17 00:00:00 2001 From: Chris Hamons Date: Tue, 27 Jul 2021 08:19:53 -0500 Subject: [PATCH] Many build.ps1 improvements (#15135) - Fixes https://github.com/Azure/azure-sdk-for-go/issues/15128 - Fixes https://github.com/Azure/azure-sdk-for-go/issues/15127 - Part of https://github.com/Azure/azure-sdk-for-go/issues/15102 --- eng/scripts/build.ps1 | 85 ++++++++++++++++------------- eng/scripts/create_go_workspace.ps1 | 10 ++-- eng/scripts/get_module_dirs.ps1 | 24 +++++--- eng/scripts/get_test_dirs.ps1 | 16 +++--- eng/scripts/scoped_discover.ps1 | 8 +-- 5 files changed, 78 insertions(+), 65 deletions(-) diff --git a/eng/scripts/build.ps1 b/eng/scripts/build.ps1 index 1284974235f6..7e281ccf28d5 100644 --- a/eng/scripts/build.ps1 +++ b/eng/scripts/build.ps1 @@ -1,42 +1,19 @@ #Requires -Version 7.0 -param($filter, [switch]$clean, [switch]$vet, [switch]$generate, [switch]$skipBuild) +param([string]$filter, [switch]$clean, [switch]$vet, [switch]$generate, [switch]$skipBuild, [string]$config = "autorest.md", [string]$outputFolder) . $PSScriptRoot/meta_generation.ps1 +. $PSScriptRoot/get_module_dirs.ps1 -$startingDirectory = Get-Location -$root = Resolve-Path ($PSScriptRoot + "/../..") -Set-Location $root -$sdks = @{}; - -foreach ($sdk in (./eng/scripts/get_module_dirs.ps1 -serviceDir 'sdk/...')) { - $name = $sdk | split-path -leaf - $sdks[$name] = @{ - 'path' = $sdk; - 'clean' = $clean; - 'vet' = $vet; - 'generate' = $generate; - 'skipBuild' = $skipBuild; - 'root' = $root; - } -} - -$keys = $sdks.Keys | Sort-Object; -if (![string]::IsNullOrWhiteSpace($filter)) { - Write-Host "Using filter: $filter" - $keys = $keys.Where( { $_ -match $filter }) -} -$keys | ForEach-Object { $sdks[$_] } | ForEach-Object { - Push-Location $_.path - - if ($_.clean) { - Write-Host "##[command]Executing go clean -v ./... in " $_.path +function Process-Sdk ($path) { + if ($clean) { + Write-Host "##[command]Executing go clean -v ./... in " $path go clean -v ./... } - if ($_.generate) { - Write-Host "##[command]Executing autorest.go in " $_.path - $autorestPath = $_.path + "/autorest.md" + if ($generate) { + Write-Host "##[command]Executing autorest.go in " $path + $autorestPath = $path + "/" + $config if (ShouldGenerate-AutorestConfig $autorestPath) { Generate-AutorestConfig $autorestPath @@ -44,24 +21,54 @@ $keys | ForEach-Object { $sdks[$_] } | ForEach-Object { } $autorestVersion = "@autorest/go@4.0.0-preview.23" - $outputFolder = $_.path - $root = $_.root + if ($outputFolder -eq '') { + $outputFolder = $path + } autorest --use=$autorestVersion --go --track2 --go-sdk-folder=$root --output-folder=$outputFolder --file-prefix="zz_generated_" --clear-output-folder=false $autorestPath if ($removeAutorestFile) { Remove-Item $autorestPath } } - if (!$_.skipBuild) { - Write-Host "##[command]Executing go build -v ./... in " $_.path + + if (!$skipBuild) { + Write-Host "##[command]Executing go build -x -v ./... in " $path go build -x -v ./... Write-Host "##[command]Build Complete!" } - if ($_.vet) { - Write-Host "##[command]Executing go vet ./... in " $_.path + + if ($vet) { + Write-Host "##[command]Executing go vet ./... in " $path go vet ./... } - Pop-Location + } -Set-Location $startingDirectory +$startingDirectory = Get-Location +$root = Resolve-Path ($PSScriptRoot + "/../..") +Set-Location $root +$sdks = @{}; + +foreach ($sdk in (Get-ModuleDirs 'sdk/...')) { + $name = $sdk | split-path -leaf + $sdks[$name] = @{ + 'path' = $sdk; + } +} + +$keys = $sdks.Keys | Sort-Object; +if (![string]::IsNullOrWhiteSpace($filter)) { + Write-Host "Using filter: $filter" + $keys = $keys.Where( { $_ -match $filter }) +} + +try { + $keys | ForEach-Object { $sdks[$_] } | ForEach-Object { + Push-Location $_.path + Process-Sdk $_.path + Pop-Location + } +} +finally { + Set-Location $startingDirectory +} diff --git a/eng/scripts/create_go_workspace.ps1 b/eng/scripts/create_go_workspace.ps1 index fae45b1b76f4..8cd8f4a9b0dc 100644 --- a/eng/scripts/create_go_workspace.ps1 +++ b/eng/scripts/create_go_workspace.ps1 @@ -4,9 +4,9 @@ # GO_WORKSPACE_PATH <- location of copied sources directory # GO_PATH <- The value that should be set for the GO_PATH environment variable Param( - [string] $goWorkSpaceDir, - [string] $orgOrUser = "Azure", - [string] $repo = "azure-sdk-for-go" + [string] $goWorkSpaceDir, + [string] $orgOrUser = "Azure", + [string] $repo = "azure-sdk-for-go" ) $repoRoot = Resolve-Path "$PSScriptRoot/../../" @@ -24,6 +24,6 @@ Write-Host "Root of new Go Workspace is $goWorkSpaceDir" Copy-Item -Container -Recurse -Path "$repoRoot/*" -Destination $CreatedGoWorkspaceSrc return New-Object PSObject -Property @{ - GO_WORKSPACE_PATH = Resolve-Path $CreatedGoWorkspaceSrc - GO_PATH = Resolve-Path $goWorkSpaceDir + GO_WORKSPACE_PATH = Resolve-Path $CreatedGoWorkspaceSrc + GO_PATH = Resolve-Path $goWorkSpaceDir } diff --git a/eng/scripts/get_module_dirs.ps1 b/eng/scripts/get_module_dirs.ps1 index f8cddb955163..2033f8ca3947 100644 --- a/eng/scripts/get_module_dirs.ps1 +++ b/eng/scripts/get_module_dirs.ps1 @@ -1,15 +1,21 @@ Param( - [string] $serviceDir + [string] $serviceDir ) -$modDirs = [Collections.Generic.List[String]]@() +function Get-ModuleDirs ([string] $serviceDir) { + $modDirs = [Collections.Generic.List[String]]@() -# find each module directory under $serviceDir -Get-ChildItem -recurse -path $serviceDir -filter go.mod | ForEach-Object { - $cdir = $_.Directory - Write-Host "Adding $cdir to list of module paths" - $modDirs.Add($cdir) + # find each module directory under $serviceDir + Get-ChildItem -recurse -path $serviceDir -filter go.mod | ForEach-Object { + $cdir = $_.Directory + Write-Host "Adding $cdir to list of module paths" + $modDirs.Add($cdir) + } + + # return the list of module directories + return $modDirs } -# return the list of module directories -return $modDirs +if ($MyInvocation.InvocationName -ne ".") { + Get-ModuleDirs $serviceDir +} diff --git a/eng/scripts/get_test_dirs.ps1 b/eng/scripts/get_test_dirs.ps1 index c599382af411..ac38f0509249 100644 --- a/eng/scripts/get_test_dirs.ps1 +++ b/eng/scripts/get_test_dirs.ps1 @@ -1,20 +1,20 @@ Param( - [string] $serviceDir + [string] $serviceDir ) $testDirs = [Collections.Generic.List[String]]@() # find each directory under $serviceDir that contains Go test files Get-ChildItem -recurse -path $serviceDir -filter *_test.go | ForEach-Object { - $cdir = $_.Directory - $tests = Select-String -Path $_ 'Test' -AllMatches + $cdir = $_.Directory + $tests = Select-String -Path $_ 'Test' -AllMatches - if ($tests.Count -gt 0) { - if (!$testDirs.Contains($cdir)) { - Write-Host "Adding $cdir to list of test directories" - $testDirs.Add($cdir) + if ($tests.Count -gt 0) { + if (!$testDirs.Contains($cdir)) { + Write-Host "Adding $cdir to list of test directories" + $testDirs.Add($cdir) + } } - } } # return the list of test directories diff --git a/eng/scripts/scoped_discover.ps1 b/eng/scripts/scoped_discover.ps1 index d7ed7e9213d5..87e8b5dce36f 100644 --- a/eng/scripts/scoped_discover.ps1 +++ b/eng/scripts/scoped_discover.ps1 @@ -1,12 +1,12 @@ Param( - [string] $serviceDir = "" + [string] $serviceDir = "" ) -if($serviceDir){ - $targetDir = "$PSScriptRoot/../../sdk/$serviceDir" +if ($serviceDir) { + $targetDir = "$PSScriptRoot/../../sdk/$serviceDir" } else { - $targetDir = "$PSScriptRoot/../../sdk" + $targetDir = "$PSScriptRoot/../../sdk" } $path = Resolve-Path -Path $targetDir