-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
Copy pathInvoke-GenerateAndBuildV2.ps1
167 lines (149 loc) · 6.78 KB
/
Invoke-GenerateAndBuildV2.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#Requires -Version 7.0
<#
.SYNOPSIS
script for azure-sdk-for-net CI check in Unified Pipeline
.DESCRIPTION
Automatically generate and verify the SDK for both management-plane and data-plane.
Unified pipeline will run this script.
.PARAMETER inputJsonFile
Path to the input json file to contain all the input paramters.
.PARAMETER outputJsonFile
Path to the script output json file.
.EXAMPLE
Run script with default parameters.
Invoke-GenerateAndBuildV2.ps1 -inputJsonFile <inputJsonFile> -outputJsonFile <outputJsonFile>
please refer to eng/scripts/automation/unified-pipeline-test.md for more test scenaros and the sample inputJson and outputJson.
#>
param (
[string]$inputJsonFile,
[string]$outputJsonFile
)
. (Join-Path $PSScriptRoot ".." "common" "scripts" "Helpers" PSModule-Helpers.ps1)
. (Join-Path $PSScriptRoot automation GenerateAndBuildLib.ps1)
$inputJson = Get-Content $inputJsonFile | Out-String | ConvertFrom-Json
$swaggerDir = $inputJson.specFolder
if($swaggerDir) {
$swaggerDir = Resolve-Path $swaggerDir
}
$swaggerDir = $swaggerDir -replace "\\", "/"
[string[]] $inputFilePaths = $inputJson.changedFiles;
$readmeFiles = $inputJson.relatedReadmeMdFiles
$commitid = $inputJson.headSha
$repoHttpsUrl = $inputJson.repoHttpsUrl
$downloadUrlPrefix = $inputJson.installInstructionInput.downloadUrlPrefix
$autorestConfig = $inputJson.autorestConfig
$relatedTypeSpecProjectFolder = $inputJson.relatedTypeSpecProjectFolder
$autorestConfigYaml = ""
if ($autorestConfig) {
$autorestConfig | Set-Content "config.md"
$autorestConfigYaml = Get-Content -Path ./config.md
$range = ($autorestConfigYaml | Select-String -Pattern '```').LineNumber
if ( $range.count -gt 1) {
$startNum = $range[0];
$lines = $range[1] - $range[0] - 1
$autorestConfigYaml = ($autorestConfigYaml | Select -Skip $startNum | Select -First $lines) |Out-String
}
Install-ModuleIfNotInstalled "powershell-yaml" "0.4.1" | Import-Module
$yml = ConvertFrom-YAML $autorestConfigYaml
$requires = $yml["require"]
if ($requires.Count -gt 0) {
$readmeFiles = $requires
}
}
$generatedSDKPackages = New-Object 'Collections.Generic.List[System.Object]'
$sdkPath = (Join-Path $PSScriptRoot .. ..)
$sdkPath = Resolve-Path $sdkPath
$sdkPath = $sdkPath -replace "\\", "/"
for ($i = 0; $i -le $readmeFiles.Count - 1; $i++) {
$readmeFile = $readmeFiles[$i] -replace "\\", "/"
$readme = ""
if ( $swaggerDir) {
$readme = (Join-Path $swaggerDir $readmeFile)
} elseif ( $commitid) {
if ($repoHttpsUrl) {
$readme = "$repoHttpsUrl/blob/$commitid/$readmeFile"
} else {
$readme = "https://github.com/$org/azure-rest-api-specs/blob/$commitid/$readmeFile"
}
} else {
throw "[ERROR] Neither 'specFolder' nor 'headSha' is provided for `$readmeFile`. Please report this issue through https://aka.ms/azsdk/support/specreview-channel and include this pull request."
}
if ($autorestConfigYaml) {
$readmeFiles[$i] = $readme
$autorestConfigYaml = ConvertTo-YAML $yml
}
Invoke-GenerateAndBuildSDK -readmeAbsolutePath $readme -sdkRootPath $sdkPath -autorestConfigYaml "$autorestConfigYaml" -downloadUrlPrefix "$downloadUrlPrefix" -generatedSDKPackages $generatedSDKPackages
$generatedSDKPackages[$generatedSDKPackages.Count - 1]['readmeMd'] = @($readmeFile)
}
#update services without readme.md
$serviceWithReadme = @()
foreach( $readme in $readmeFiles) {
$service, $serviceType = Get-ResourceProviderFromReadme $readme
$serviceWithReadme += @($service)
}
$inputFileToGen = @()
foreach( $file in $inputFilePaths) {
if ( $file -match "specification/(?<specName>.*)/data-plane|resource-manager" ) {
if (!$serviceWithReadme.Contains($matches["specName"])) {
$inputFileToGen += @($file)
}
}
}
if ($inputFileToGen) {
UpdateExistingSDKByInputFiles -inputFilePaths $inputFileToGen -sdkRootPath $sdkPath -headSha $commitid -repoHttpsUrl $repoHttpsUrl -downloadUrlPrefix "$downloadUrlPrefix" -generatedSDKPackages $generatedSDKPackages
}
$exitCode = 0
# generate sdk from typespec file
if ($relatedTypeSpecProjectFolder) {
foreach ($typespecRelativeFolder in $relatedTypeSpecProjectFolder) {
$typespecFolder = Resolve-Path (Join-Path $swaggerDir $typespecRelativeFolder)
$tspConfigFile = Resolve-Path (Join-Path $typespecFolder "tspconfig.yaml")
$sdkProjectFolder = GetSDKProjectFolder -typespecConfigurationFile $tspConfigFile -sdkRepoRoot $sdkPath
$sdkAutorestConfigFile = Join-path $sdkProjectFolder "src" "autorest.md"
if (Test-Path -Path $sdkAutorestConfigFile) {
Write-Host "remove $sdkAutorestConfigFile for sdk from typespec."
Remove-Item -Path $sdkAutorestConfigFile
}
$serviceType = "data-plane"
$packageName = Split-Path $sdkProjectFolder -Leaf
if ($packageName.StartsWith("Azure.ResourceManager.")) {
$serviceType = "resource-manager"
}
$repo = $repoHttpsUrl -replace "https://github.com/", ""
Write-host "Start to call tsp-client to generate package:$packageName"
$tspclientCommand = "npx --package=@azure-tools/typespec-client-generator-cli --yes tsp-client init --tsp-config $tspConfigFile --repo $repo --commit $commitid"
if ($swaggerDir) {
$tspclientCommand += " --local-spec-repo $typespecFolder"
}
Write-Host $tspclientCommand
Invoke-Expression $tspclientCommand
if ($LASTEXITCODE) {
# If Process script call fails, then return with failure to CI and don't need to call GeneratePackage
Write-Error "[ERROR] Failed to generate typespec project:$typespecFolder. Exit code: $LASTEXITCODE."
Write-Error "[ERROR] Please review the detail errors for potential fixes."
Write-Error "[ERROR] If the issue persists, contact the DotNet language support channel at $DotNetSupportChannelLink and include this spec pull request."
$generatedSDKPackages.Add(@{
result = "failed";
path=@("");
})
$exitCode = $LASTEXITCODE
} else {
$relativeSdkPath = Resolve-Path $sdkProjectFolder -Relative
GeneratePackage `
-projectFolder $sdkProjectFolder `
-sdkRootPath $sdkPath `
-path $relativeSdkPath `
-downloadUrlPrefix $downloadUrlPrefix `
-serviceType $serviceType `
-skipGenerate `
-generatedSDKPackages $generatedSDKPackages `
-specRepoRoot $swaggerDir
}
$generatedSDKPackages[$generatedSDKPackages.Count - 1]['typespecProject'] = @($typespecRelativeFolder)
}
}
$outputJson = [PSCustomObject]@{
packages = $generatedSDKPackages
}
$outputJson | ConvertTo-Json -depth 100 | Out-File $outputJsonFile
exit $exitCode