Skip to content

Commit 0ea9594

Browse files
azure-sdkraych1
andauthored
Sync eng/common directory with azure-sdk-tools for PR 6272 (Azure#30620)
* Support localspecrepo if pass in this parameter * Support regen sdk code based on local typespecs * Added reference doc to error message --------- Co-authored-by: raychen <raychen@microsoft.com>
1 parent 3369c5c commit 0ea9594

File tree

3 files changed

+162
-118
lines changed

3 files changed

+162
-118
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ function NpmInstallForProject([string]$workingDirectory) {
3838

3939
#default to root/eng/emitter-package.json but you can override by writing
4040
#Get-${Language}-EmitterPackageJsonPath in your Language-Settings.ps1
41-
$replacementPackageJson = "$PSScriptRoot/../../emitter-package.json"
41+
$replacementPackageJson = Join-Path $PSScriptRoot "../../emitter-package.json"
4242
if (Test-Path "Function:$GetEmitterPackageJsonPathFn") {
4343
$replacementPackageJson = &$GetEmitterPackageJsonPathFn
4444
}
@@ -52,7 +52,7 @@ function NpmInstallForProject([string]$workingDirectory) {
5252
Write-Host "Package.json contains '-alpha.' in the version, Creating .npmrc using public/azure-sdk-for-js-test-autorest feed."
5353
"registry=https://pkgs.dev.azure.com/azure-sdk/public/_packaging/azure-sdk-for-js-test-autorest/npm/registry/ `n`nalways-auth=true" | Out-File '.npmrc'
5454
}
55-
55+
5656
npm install --no-lock-file
5757
if ($LASTEXITCODE) { exit $LASTEXITCODE }
5858
}
@@ -112,3 +112,4 @@ $shouldCleanUp = !$SaveInputs
112112
if ($shouldCleanUp) {
113113
Remove-Item $tempFolder -Recurse -Force
114114
}
115+
exit 0

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

Lines changed: 58 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -16,31 +16,20 @@ param (
1616
Install-ModuleIfNotInstalled "powershell-yaml" "0.4.1" | Import-Module
1717

1818
function CreateUpdate-TspLocation([System.Object]$tspConfig, [string]$TypeSpecProjectDirectory, [string]$CommitHash, [string]$repo, [string]$repoRoot) {
19-
$serviceDir = ""
2019
$additionalDirs = @()
21-
22-
# Parse tspcofig.yaml to get service-dir, additionalDirectories, and package-dir
23-
if ($tspConfig["parameters"] -and $tspConfig["parameters"]["service-dir"]) {
24-
$serviceDir = $tspConfig["parameters"]["service-dir"]["default"];
25-
}
26-
else {
27-
Write-Error "Missing service-dir in parameters section of tspconfig.yaml."
28-
exit 1
29-
}
3020
if ($tspConfig["parameters"]["dependencies"] -and $tspConfig["parameters"]["dependencies"]["additionalDirectories"]) {
3121
$additionalDirs = $tspConfig["parameters"]["dependencies"]["additionalDirectories"];
3222
}
3323

34-
$packageDir = Get-PackageDir $tspConfig
35-
3624
# Create service-dir if not exist
37-
$serviceDir = Join-Path $repoRoot $serviceDir
25+
$serviceDir = Get-ServiceDir $tspConfig $repoRoot
3826
if (!(Test-Path -Path $serviceDir)) {
3927
New-Item -Path $serviceDir -ItemType Directory | Out-Null
4028
Write-Host "created service folder $serviceDir"
4129
}
4230

4331
# Create package-dir if not exist
32+
$packageDir = Get-PackageDir $tspConfig
4433
$packageDir = Join-Path $serviceDir $packageDir
4534
if (!(Test-Path -Path $packageDir)) {
4635
New-Item -Path $packageDir -ItemType Directory | Out-Null
@@ -71,6 +60,20 @@ function CreateUpdate-TspLocation([System.Object]$tspConfig, [string]$TypeSpecPr
7160
return $packageDir
7261
}
7362

63+
function Get-ServiceDir([System.Object]$tspConfig, [string]$repoRoot) {
64+
$serviceDir = ""
65+
if ($tspConfig["parameters"] -and $tspConfig["parameters"]["service-dir"]) {
66+
$serviceDir = $tspConfig["parameters"]["service-dir"]["default"];
67+
}
68+
else {
69+
Write-Error "Missing service-dir in parameters section of tspconfig.yaml. Please refer to https://github.com/Azure/azure-rest-api-specs/blob/main/specification/contosowidgetmanager/Contoso.WidgetManager/tspconfig.yaml for the right schema."
70+
exit 1
71+
}
72+
73+
# Create service-dir if not exist
74+
$serviceDir = Join-Path $repoRoot $serviceDir
75+
return $serviceDir
76+
}
7477
function Get-PackageDir([System.Object]$tspConfig) {
7578
$emitterName = ""
7679
if (Test-Path "Function:$GetEmitterNameFn") {
@@ -85,18 +88,27 @@ function Get-PackageDir([System.Object]$tspConfig) {
8588
$packageDir = $tspConfig["options"]["$emitterName"]["package-dir"]
8689
}
8790
else {
88-
Write-Error "Missing package-dir in $emitterName options of tspconfig.yaml."
91+
Write-Error "Missing package-dir in $emitterName options of tspconfig.yaml. Please refer to https://github.com/Azure/azure-rest-api-specs/blob/main/specification/contosowidgetmanager/Contoso.WidgetManager/tspconfig.yaml for the right schema."
8992
exit 1
9093
}
9194
return $packageDir
9295
}
9396

94-
$repoRootPath = (Join-Path $PSScriptRoot .. .. ..)
95-
$repoRootPath = Resolve-Path $repoRootPath
96-
$repoRootPath = $repoRootPath -replace "\\", "/"
97-
$tspConfigPath = Join-Path $repoRootPath 'tspconfig.yaml'
97+
function Get-TspLocationFolder([System.Object]$tspConfig, [string]$repoRoot) {
98+
$serviceDir = Get-ServiceDir $tspConfig $repoRoot
99+
$packageDir = Get-PackageDir $tspConfig
100+
$packageDir = Join-Path $serviceDir $packageDir
101+
return $packageDir
102+
}
103+
104+
$sdkRepoRootPath = (Join-Path $PSScriptRoot .. .. ..)
105+
$sdkRepoRootPath = Resolve-Path $sdkRepoRootPath
106+
$sdkRepoRootPath = $sdkRepoRootPath -replace "\\", "/"
107+
$tspConfigPath = Join-Path $sdkRepoRootPath 'tspconfig.yaml'
98108
$tmpTspConfigPath = $tspConfigPath
99109
$repo = ""
110+
$specRepoRoot = ""
111+
$generateFromLocalTypeSpec = $false
100112
# remote url scenario
101113
# example url of tspconfig.yaml: https://github.com/Azure/azure-rest-api-specs-pr/blob/724ccc4d7ef7655c0b4d5c5ac4a5513f19bbef35/specification/containerservice/Fleet.Management/tspconfig.yaml
102114
if ($TypeSpecProjectDirectory -match '^https://github.com/(?<repo>Azure/azure-rest-api-specs(-pr)?)/blob/(?<commit>[0-9a-f]{40})/(?<path>.*)/tspconfig.yaml$') {
@@ -121,26 +133,26 @@ if ($TypeSpecProjectDirectory -match '^https://github.com/(?<repo>Azure/azure-re
121133
exit 1
122134
}
123135
$TypeSpecProjectDirectory = $TypeSpecProjectDirectory.Replace("\", "/")
124-
if ($TypeSpecProjectDirectory -match "^.*/(?<path>specification/.*)$") {
136+
if ($TypeSpecProjectDirectory -match "(?<repoRoot>^.*)/(?<path>specification/.*)$") {
125137
$TypeSpecProjectDirectory = $Matches["path"]
138+
$specRepoRoot = $Matches["repoRoot"]
126139
} else {
127140
Write-Error "$TypeSpecProjectDirectory doesn't have 'specification' in path."
128141
exit 1
129142
}
130-
if (!$CommitHash) {
131-
Write-Error "Parameter of Commithash is not provided in the local path scenario."
132-
exit 1
143+
if (!$CommitHash -or !$RepoUrl) {
144+
Write-Warning "Parameter of Commithash or RepoUrl are not provided along with the local path of tspconfig.yaml, trying to re-generate sdk code based on the local type specs."
145+
$generateFromLocalTypeSpec = $true
133146
}
134-
if (!$RepoUrl) {
135-
Write-Error "Parameter of RepoUrl:$RepoUrl is not provided in the local path scenario."
136-
exit 1
137-
}
138-
if ($RepoUrl -match "^https://github.com/(?<repo>[^/]*/azure-rest-api-specs(-pr)?).*") {
139-
$repo = $Matches["repo"]
140-
}
141-
else {
142-
Write-Error "Parameter 'RepoUrl' has incorrect value:$RepoUrl. It should be similar like 'https://github.com/Azure/azure-rest-api-specs'"
143-
exit 1
147+
148+
if ($RepoUrl) {
149+
if ($RepoUrl -match "^https://github.com/(?<repo>[^/]*/azure-rest-api-specs(-pr)?).*") {
150+
$repo = $Matches["repo"]
151+
}
152+
else {
153+
Write-Error "Parameter 'RepoUrl' has incorrect value:$RepoUrl. It should be similar like 'https://github.com/Azure/azure-rest-api-specs'"
154+
exit 1
155+
}
144156
}
145157
}
146158

@@ -150,12 +162,23 @@ $tspConfigYaml = Get-Content $tspConfigPath -Raw | ConvertFrom-Yaml
150162
if (Test-Path $tmpTspConfigPath) {
151163
Remove-Item $tspConfigPath
152164
}
153-
# call CreateUpdate-TspLocation function
154-
$sdkProjectFolder = CreateUpdate-TspLocation $tspConfigYaml $TypeSpecProjectDirectory $CommitHash $repo $repoRootPath
165+
166+
$sdkProjectFolder = ""
167+
if ($generateFromLocalTypeSpec) {
168+
$sdkProjectFolder = Get-TspLocationFolder $tspConfigYaml $sdkRepoRootPath
169+
$tspLocationYamlPath = Join-Path $sdkProjectFolder "tsp-location.yaml"
170+
if (!(Test-Path -Path $tspLocationYamlPath)) {
171+
Write-Error "Failed to find tsp-location.yaml in '$sdkProjectFolder', please make sure to provide CommitHash and RepoUrl parameters along with the local path of tspconfig.yaml in order to create tsp-location.yaml."
172+
exit 1
173+
}
174+
} else {
175+
# call CreateUpdate-TspLocation function
176+
$sdkProjectFolder = CreateUpdate-TspLocation $tspConfigYaml $TypeSpecProjectDirectory $CommitHash $repo $sdkRepoRootPath
177+
}
155178

156179
# call TypeSpec-Project-Sync.ps1
157180
$syncScript = Join-Path $PSScriptRoot TypeSpec-Project-Sync.ps1
158-
& $syncScript $sdkProjectFolder
181+
& $syncScript $sdkProjectFolder $specRepoRoot
159182
if ($LASTEXITCODE) { exit $LASTEXITCODE }
160183

161184
# call TypeSpec-Project-Generate.ps1

0 commit comments

Comments
 (0)