Skip to content

Commit

Permalink
Sync eng/common directory with azure-sdk-tools for PR 6272 (#35289)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
azure-sdk and raych1 authored Jun 6, 2023
1 parent 7395f50 commit 7c27d87
Show file tree
Hide file tree
Showing 3 changed files with 162 additions and 118 deletions.
5 changes: 3 additions & 2 deletions eng/common/scripts/TypeSpec-Project-Generate.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ function NpmInstallForProject([string]$workingDirectory) {

#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"
$replacementPackageJson = Join-Path $PSScriptRoot "../../emitter-package.json"
if (Test-Path "Function:$GetEmitterPackageJsonPathFn") {
$replacementPackageJson = &$GetEmitterPackageJsonPathFn
}
Expand All @@ -52,7 +52,7 @@ function NpmInstallForProject([string]$workingDirectory) {
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/npm/registry/ `n`nalways-auth=true" | Out-File '.npmrc'
}

npm install --no-lock-file
if ($LASTEXITCODE) { exit $LASTEXITCODE }
}
Expand Down Expand Up @@ -112,3 +112,4 @@ $shouldCleanUp = !$SaveInputs
if ($shouldCleanUp) {
Remove-Item $tempFolder -Recurse -Force
}
exit 0
93 changes: 58 additions & 35 deletions eng/common/scripts/TypeSpec-Project-Process.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,20 @@ param (
Install-ModuleIfNotInstalled "powershell-yaml" "0.4.1" | Import-Module

function CreateUpdate-TspLocation([System.Object]$tspConfig, [string]$TypeSpecProjectDirectory, [string]$CommitHash, [string]$repo, [string]$repoRoot) {
$serviceDir = ""
$additionalDirs = @()

# Parse tspcofig.yaml to get service-dir, additionalDirectories, and package-dir
if ($tspConfig["parameters"] -and $tspConfig["parameters"]["service-dir"]) {
$serviceDir = $tspConfig["parameters"]["service-dir"]["default"];
}
else {
Write-Error "Missing service-dir in parameters section of tspconfig.yaml."
exit 1
}
if ($tspConfig["parameters"]["dependencies"] -and $tspConfig["parameters"]["dependencies"]["additionalDirectories"]) {
$additionalDirs = $tspConfig["parameters"]["dependencies"]["additionalDirectories"];
}

$packageDir = Get-PackageDir $tspConfig

# Create service-dir if not exist
$serviceDir = Join-Path $repoRoot $serviceDir
$serviceDir = Get-ServiceDir $tspConfig $repoRoot
if (!(Test-Path -Path $serviceDir)) {
New-Item -Path $serviceDir -ItemType Directory | Out-Null
Write-Host "created service folder $serviceDir"
}

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

function Get-ServiceDir([System.Object]$tspConfig, [string]$repoRoot) {
$serviceDir = ""
if ($tspConfig["parameters"] -and $tspConfig["parameters"]["service-dir"]) {
$serviceDir = $tspConfig["parameters"]["service-dir"]["default"];
}
else {
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."
exit 1
}

# Create service-dir if not exist
$serviceDir = Join-Path $repoRoot $serviceDir
return $serviceDir
}
function Get-PackageDir([System.Object]$tspConfig) {
$emitterName = ""
if (Test-Path "Function:$GetEmitterNameFn") {
Expand All @@ -85,18 +88,27 @@ function Get-PackageDir([System.Object]$tspConfig) {
$packageDir = $tspConfig["options"]["$emitterName"]["package-dir"]
}
else {
Write-Error "Missing package-dir in $emitterName options of tspconfig.yaml."
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."
exit 1
}
return $packageDir
}

$repoRootPath = (Join-Path $PSScriptRoot .. .. ..)
$repoRootPath = Resolve-Path $repoRootPath
$repoRootPath = $repoRootPath -replace "\\", "/"
$tspConfigPath = Join-Path $repoRootPath 'tspconfig.yaml'
function Get-TspLocationFolder([System.Object]$tspConfig, [string]$repoRoot) {
$serviceDir = Get-ServiceDir $tspConfig $repoRoot
$packageDir = Get-PackageDir $tspConfig
$packageDir = Join-Path $serviceDir $packageDir
return $packageDir
}

$sdkRepoRootPath = (Join-Path $PSScriptRoot .. .. ..)
$sdkRepoRootPath = Resolve-Path $sdkRepoRootPath
$sdkRepoRootPath = $sdkRepoRootPath -replace "\\", "/"
$tspConfigPath = Join-Path $sdkRepoRootPath 'tspconfig.yaml'
$tmpTspConfigPath = $tspConfigPath
$repo = ""
$specRepoRoot = ""
$generateFromLocalTypeSpec = $false
# remote url scenario
# example url of tspconfig.yaml: https://github.com/Azure/azure-rest-api-specs-pr/blob/724ccc4d7ef7655c0b4d5c5ac4a5513f19bbef35/specification/containerservice/Fleet.Management/tspconfig.yaml
if ($TypeSpecProjectDirectory -match '^https://github.com/(?<repo>Azure/azure-rest-api-specs(-pr)?)/blob/(?<commit>[0-9a-f]{40})/(?<path>.*)/tspconfig.yaml$') {
Expand All @@ -121,26 +133,26 @@ if ($TypeSpecProjectDirectory -match '^https://github.com/(?<repo>Azure/azure-re
exit 1
}
$TypeSpecProjectDirectory = $TypeSpecProjectDirectory.Replace("\", "/")
if ($TypeSpecProjectDirectory -match "^.*/(?<path>specification/.*)$") {
if ($TypeSpecProjectDirectory -match "(?<repoRoot>^.*)/(?<path>specification/.*)$") {
$TypeSpecProjectDirectory = $Matches["path"]
$specRepoRoot = $Matches["repoRoot"]
} else {
Write-Error "$TypeSpecProjectDirectory doesn't have 'specification' in path."
exit 1
}
if (!$CommitHash) {
Write-Error "Parameter of Commithash is not provided in the local path scenario."
exit 1
if (!$CommitHash -or !$RepoUrl) {
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."
$generateFromLocalTypeSpec = $true
}
if (!$RepoUrl) {
Write-Error "Parameter of RepoUrl:$RepoUrl is not provided in the local path scenario."
exit 1
}
if ($RepoUrl -match "^https://github.com/(?<repo>[^/]*/azure-rest-api-specs(-pr)?).*") {
$repo = $Matches["repo"]
}
else {
Write-Error "Parameter 'RepoUrl' has incorrect value:$RepoUrl. It should be similar like 'https://github.com/Azure/azure-rest-api-specs'"
exit 1

if ($RepoUrl) {
if ($RepoUrl -match "^https://github.com/(?<repo>[^/]*/azure-rest-api-specs(-pr)?).*") {
$repo = $Matches["repo"]
}
else {
Write-Error "Parameter 'RepoUrl' has incorrect value:$RepoUrl. It should be similar like 'https://github.com/Azure/azure-rest-api-specs'"
exit 1
}
}
}

Expand All @@ -150,12 +162,23 @@ $tspConfigYaml = Get-Content $tspConfigPath -Raw | ConvertFrom-Yaml
if (Test-Path $tmpTspConfigPath) {
Remove-Item $tspConfigPath
}
# call CreateUpdate-TspLocation function
$sdkProjectFolder = CreateUpdate-TspLocation $tspConfigYaml $TypeSpecProjectDirectory $CommitHash $repo $repoRootPath

$sdkProjectFolder = ""
if ($generateFromLocalTypeSpec) {
$sdkProjectFolder = Get-TspLocationFolder $tspConfigYaml $sdkRepoRootPath
$tspLocationYamlPath = Join-Path $sdkProjectFolder "tsp-location.yaml"
if (!(Test-Path -Path $tspLocationYamlPath)) {
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."
exit 1
}
} else {
# call CreateUpdate-TspLocation function
$sdkProjectFolder = CreateUpdate-TspLocation $tspConfigYaml $TypeSpecProjectDirectory $CommitHash $repo $sdkRepoRootPath
}

# call TypeSpec-Project-Sync.ps1
$syncScript = Join-Path $PSScriptRoot TypeSpec-Project-Sync.ps1
& $syncScript $sdkProjectFolder
& $syncScript $sdkProjectFolder $specRepoRoot
if ($LASTEXITCODE) { exit $LASTEXITCODE }

# call TypeSpec-Project-Generate.ps1
Expand Down
Loading

0 comments on commit 7c27d87

Please sign in to comment.