switch tooling over to typespec (#29501)
* switch tooling over to typespec * Update sdk_generator.py * fix --------- Co-authored-by: Yuchao Yan <yuchaoyan@microsoft.com> Co-authored-by: msyyc <70930885+msyyc@users.noreply.github.com>
This commit is contained in:
Родитель
1f9a17615c
Коммит
4fa0545332
|
@ -148,4 +148,4 @@ sdk/cosmos/azure-cosmos/test/test_config.py
|
|||
*_python.json
|
||||
|
||||
# temporary folder to refresh SDK with cadl
|
||||
TempCadlFiles/
|
||||
TempTypeSpecFiles/
|
|
@ -1,101 +0,0 @@
|
|||
# For details see https://github.com/Azure/azure-sdk-tools/blob/main/doc/common/Cadl-Project-Scripts.md
|
||||
|
||||
[CmdletBinding()]
|
||||
param (
|
||||
[Parameter(Position=0)]
|
||||
[ValidateNotNullOrEmpty()]
|
||||
[string] $ProjectDirectory,
|
||||
[Parameter(Position=1)]
|
||||
[string] $CadlAdditionalOptions ## addtional cadl emitter options, separated by semicolon if more than one, e.g. option1=value1;option2=value2
|
||||
)
|
||||
|
||||
$ErrorActionPreference = "Stop"
|
||||
. $PSScriptRoot/Helpers/PSModule-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"
|
||||
|
||||
Write-Host "Reading configuration from $cadlConfigurationFile"
|
||||
$configuration = Get-Content -Path $cadlConfigurationFile -Raw | ConvertFrom-Yaml
|
||||
|
||||
$specSubDirectory = $configuration["directory"]
|
||||
$innerFolder = Split-Path $specSubDirectory -Leaf
|
||||
|
||||
$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"}
|
||||
|
||||
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"
|
||||
}
|
||||
}
|
||||
$cadlCompileCommand = "npx 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
|
||||
|
||||
if ($LASTEXITCODE) { exit $LASTEXITCODE }
|
||||
}
|
||||
finally {
|
||||
Pop-Location
|
||||
}
|
||||
|
||||
$shouldCleanUp = $configuration["cleanup"] ?? $true
|
||||
if ($shouldCleanUp) {
|
||||
Remove-Item $tempFolder -Recurse -Force
|
||||
}
|
|
@ -1,127 +0,0 @@
|
|||
# For details see https://github.com/Azure/azure-sdk-tools/blob/main/doc/common/Cadl-Project-Scripts.md
|
||||
|
||||
[CmdletBinding()]
|
||||
param (
|
||||
[Parameter(Position=0)]
|
||||
[ValidateNotNullOrEmpty()]
|
||||
[string] $ProjectDirectory
|
||||
)
|
||||
|
||||
$ErrorActionPreference = "Stop"
|
||||
. $PSScriptRoot/Helpers/PSModule-Helpers.ps1
|
||||
Install-ModuleIfNotInstalled "powershell-yaml" "0.4.1" | Import-Module
|
||||
$sparseCheckoutFile = ".git/info/sparse-checkout"
|
||||
|
||||
function AddSparseCheckoutPath([string]$subDirectory) {
|
||||
if (!(Test-Path $sparseCheckoutFile) -or !((Get-Content $sparseCheckoutFile).Contains($subDirectory))) {
|
||||
Write-Output $subDirectory >> .git/info/sparse-checkout
|
||||
}
|
||||
}
|
||||
|
||||
function CopySpecToProjectIfNeeded([string]$specCloneRoot, [string]$mainSpecDir, [string]$dest, [string[]]$specAdditionalSubDirectories) {
|
||||
$source = "$specCloneRoot/$mainSpecDir"
|
||||
Copy-Item -Path $source -Destination $dest -Recurse -Force
|
||||
Write-Host "Copying spec from $source to $dest"
|
||||
|
||||
foreach ($additionalDir in $specAdditionalSubDirectories) {
|
||||
$source = "$specCloneRoot/$additionalDir"
|
||||
Write-Host "Copying spec from $source to $dest"
|
||||
Copy-Item -Path $source -Destination $dest -Recurse -Force
|
||||
}
|
||||
}
|
||||
|
||||
function UpdateSparseCheckoutFile([string]$mainSpecDir, [string[]]$specAdditionalSubDirectories) {
|
||||
AddSparseCheckoutPath $mainSpecDir
|
||||
foreach ($subDir in $specAdditionalSubDirectories) {
|
||||
AddSparseCheckoutPath $subDir
|
||||
}
|
||||
}
|
||||
|
||||
function GetGitRemoteValue([string]$repo) {
|
||||
Push-Location $ProjectDirectory
|
||||
$result = ""
|
||||
try {
|
||||
$gitRemotes = (git remote -v)
|
||||
foreach ($remote in $gitRemotes) {
|
||||
if ($remote.StartsWith("origin")) {
|
||||
if ($remote -match 'https://github.com/\S+') {
|
||||
$result = "https://github.com/$repo.git"
|
||||
break
|
||||
} elseif ($remote -match "git@github.com:\S+"){
|
||||
$result = "git@github.com:$repo.git"
|
||||
break
|
||||
} else {
|
||||
throw "Unknown git remote format found: $remote"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
finally {
|
||||
Pop-Location
|
||||
}
|
||||
|
||||
return $result
|
||||
}
|
||||
|
||||
function InitializeSparseGitClone([string]$repo) {
|
||||
git clone --no-checkout --filter=tree:0 $repo .
|
||||
if ($LASTEXITCODE) { exit $LASTEXITCODE }
|
||||
git sparse-checkout init
|
||||
if ($LASTEXITCODE) { exit $LASTEXITCODE }
|
||||
Remove-Item $sparseCheckoutFile -Force
|
||||
}
|
||||
|
||||
function GetSpecCloneDir([string]$projectName) {
|
||||
Push-Location $ProjectDirectory
|
||||
try {
|
||||
$root = git rev-parse --show-toplevel
|
||||
}
|
||||
finally {
|
||||
Pop-Location
|
||||
}
|
||||
|
||||
$sparseSpecCloneDir = "$root/../sparse-spec/$projectName"
|
||||
New-Item $sparseSpecCloneDir -Type Directory -Force | Out-Null
|
||||
$createResult = Resolve-Path $sparseSpecCloneDir
|
||||
return $createResult
|
||||
}
|
||||
|
||||
$cadlConfigurationFile = Resolve-Path "$ProjectDirectory/cadl-location.yaml"
|
||||
Write-Host "Reading configuration from $cadlConfigurationFile"
|
||||
$configuration = Get-Content -Path $cadlConfigurationFile -Raw | ConvertFrom-Yaml
|
||||
|
||||
$pieces = $cadlConfigurationFile.Path.Replace("\","/").Split("/")
|
||||
$projectName = $pieces[$pieces.Count - 2]
|
||||
|
||||
$specSubDirectory = $configuration["directory"]
|
||||
|
||||
if ( $configuration["repo"] -and $configuration["commit"]) {
|
||||
$specCloneDir = GetSpecCloneDir $projectName
|
||||
$gitRemoteValue = GetGitRemoteValue $configuration["repo"]
|
||||
|
||||
Write-Host "Setting up sparse clone for $projectName at $specCloneDir"
|
||||
|
||||
Push-Location $specCloneDir.Path
|
||||
try {
|
||||
if (!(Test-Path ".git")) {
|
||||
InitializeSparseGitClone $gitRemoteValue
|
||||
UpdateSparseCheckoutFile $specSubDirectory $configuration["additionalDirectories"]
|
||||
}
|
||||
git checkout $configuration["commit"]
|
||||
if ($LASTEXITCODE) { exit $LASTEXITCODE }
|
||||
}
|
||||
finally {
|
||||
Pop-Location
|
||||
}
|
||||
} elseif ( $configuration["spec-root-dir"] ) {
|
||||
$specCloneDir = $configuration["spec-root-dir"]
|
||||
}
|
||||
|
||||
|
||||
$tempCadlDir = "$ProjectDirectory/TempCadlFiles"
|
||||
New-Item $tempCadlDir -Type Directory -Force | Out-Null
|
||||
CopySpecToProjectIfNeeded `
|
||||
-specCloneRoot $specCloneDir `
|
||||
-mainSpecDir $specSubDirectory `
|
||||
-dest $tempCadlDir `
|
||||
-specAdditionalSubDirectories $configuration["additionalDirectories"]
|
|
@ -169,14 +169,14 @@ function ValidatePackage
|
|||
{
|
||||
Param(
|
||||
[Parameter(Mandatory=$true)]
|
||||
[string]$packageName,
|
||||
[string]$packageName,
|
||||
[Parameter(Mandatory=$true)]
|
||||
[string]$packageVersion,
|
||||
[Parameter(Mandatory=$false)]
|
||||
[string]$PackageSourceOverride,
|
||||
[Parameter(Mandatory=$false)]
|
||||
[string]$DocValidationImageId
|
||||
)
|
||||
)
|
||||
$installValidationFolder = Join-Path ([System.IO.Path]::GetTempPath()) "validation"
|
||||
if (!(Test-Path $installValidationFolder)) {
|
||||
New-Item -ItemType Directory -Force -Path $installValidationFolder | Out-Null
|
||||
|
@ -187,7 +187,7 @@ function ValidatePackage
|
|||
if (!$DocValidationImageId) {
|
||||
Write-Host "Validating using pip command directly on $packageName."
|
||||
FallbackValidation -packageName "$packageName" -packageVersion "$packageVersion" -workingDirectory $installValidationFolder -PackageSourceOverride $PackageSourceOverride
|
||||
}
|
||||
}
|
||||
else {
|
||||
Write-Host "Validating using $DocValidationImageId on $packageName."
|
||||
DockerValidation -packageName "$packageName" -packageVersion "$packageVersion" `
|
||||
|
@ -197,7 +197,7 @@ function ValidatePackage
|
|||
function DockerValidation{
|
||||
Param(
|
||||
[Parameter(Mandatory=$true)]
|
||||
[string]$packageName,
|
||||
[string]$packageName,
|
||||
[Parameter(Mandatory=$true)]
|
||||
[string]$packageVersion,
|
||||
[Parameter(Mandatory=$false)]
|
||||
|
@ -206,11 +206,11 @@ function DockerValidation{
|
|||
[string]$DocValidationImageId,
|
||||
[Parameter(Mandatory=$false)]
|
||||
[string]$workingDirectory
|
||||
)
|
||||
)
|
||||
if ($PackageSourceOverride) {
|
||||
Write-Host "docker run -v ${workingDirectory}:/workdir/out -e TARGET_PACKAGE=$packageName -e TARGET_VERSION=$packageVersion -e EXTRA_INDEX_URL=$PackageSourceOverride -t $DocValidationImageId"
|
||||
$commandLine = docker run -v "${workingDirectory}:/workdir/out" -e TARGET_PACKAGE=$packageName -e TARGET_VERSION=$packageVersion `
|
||||
-e EXTRA_INDEX_URL=$PackageSourceOverride -t $DocValidationImageId 2>&1
|
||||
-e EXTRA_INDEX_URL=$PackageSourceOverride -t $DocValidationImageId 2>&1
|
||||
}
|
||||
else {
|
||||
Write-Host "docker run -v ${workingDirectory}:/workdir/out -e TARGET_PACKAGE=$packageName -e TARGET_VERSION=$packageVersion -t $DocValidationImageId"
|
||||
|
@ -218,15 +218,15 @@ function DockerValidation{
|
|||
-e TARGET_PACKAGE=$packageName -e TARGET_VERSION=$packageVersion -t $DocValidationImageId 2>&1
|
||||
}
|
||||
# The docker exit codes: https://docs.docker.com/engine/reference/run/#exit-status
|
||||
# If the docker failed because of docker itself instead of the application,
|
||||
# we should skip the validation and keep the packages.
|
||||
|
||||
if ($LASTEXITCODE -eq 125 -Or $LASTEXITCODE -eq 126 -Or $LASTEXITCODE -eq 127) {
|
||||
# If the docker failed because of docker itself instead of the application,
|
||||
# we should skip the validation and keep the packages.
|
||||
|
||||
if ($LASTEXITCODE -eq 125 -Or $LASTEXITCODE -eq 126 -Or $LASTEXITCODE -eq 127) {
|
||||
$commandLine | ForEach-Object { Write-Debug $_ }
|
||||
LogWarning "The `docker` command does not work with exit code $LASTEXITCODE. Fall back to npm install $packageName directly."
|
||||
FallbackValidation -packageName "$packageName" -packageVersion "$packageVersion" -workingDirectory $workingDirectory -PackageSourceOverride $PackageSourceOverride
|
||||
}
|
||||
elseif ($LASTEXITCODE -ne 0) {
|
||||
elseif ($LASTEXITCODE -ne 0) {
|
||||
$commandLine | ForEach-Object { Write-Debug $_ }
|
||||
LogWarning "Package $packageName ref docs validation failed."
|
||||
return $false
|
||||
|
@ -238,14 +238,14 @@ function FallbackValidation
|
|||
{
|
||||
Param(
|
||||
[Parameter(Mandatory=$true)]
|
||||
[string]$packageName,
|
||||
[string]$packageName,
|
||||
[Parameter(Mandatory=$true)]
|
||||
[string]$packageVersion,
|
||||
[Parameter(Mandatory=$true)]
|
||||
[string]$workingDirectory,
|
||||
[Parameter(Mandatory=$false)]
|
||||
[string]$PackageSourceOverride
|
||||
)
|
||||
)
|
||||
$installTargetFolder = Join-Path $workingDirectory $packageName
|
||||
New-Item -ItemType Directory -Force -Path $installTargetFolder | Out-Null
|
||||
$packageExpression = "$packageName$packageVersion"
|
||||
|
@ -334,13 +334,13 @@ function UpdateDocsMsPackages($DocConfigFile, $Mode, $DocsMetadata, $PackageSour
|
|||
$outputPackages = @()
|
||||
foreach ($package in $packageConfig.packages) {
|
||||
$packageName = $package.package_info.name
|
||||
if (!$packageName) {
|
||||
if (!$packageName) {
|
||||
Write-Host "Keeping package with no name: $($package.package_info)"
|
||||
$outputPackages += $package
|
||||
continue
|
||||
}
|
||||
|
||||
if ($package.package_info.install_type -ne 'pypi') {
|
||||
if ($package.package_info.install_type -ne 'pypi') {
|
||||
Write-Host "Keeping package with install_type not 'pypi': $($package.package_info.name)"
|
||||
$outputPackages += $package
|
||||
continue
|
||||
|
@ -359,19 +359,19 @@ function UpdateDocsMsPackages($DocConfigFile, $Mode, $DocsMetadata, $PackageSour
|
|||
continue
|
||||
}
|
||||
|
||||
if ($matchingPublishedPackageArray.Count -gt 1) {
|
||||
if ($matchingPublishedPackageArray.Count -gt 1) {
|
||||
LogWarning "Found more than one matching published package in metadata for $packageName; only updating first entry"
|
||||
}
|
||||
$matchingPublishedPackage = $matchingPublishedPackageArray[0]
|
||||
|
||||
if ($Mode -eq 'preview' -and !$matchingPublishedPackage.VersionPreview.Trim()) {
|
||||
if ($Mode -eq 'preview' -and !$matchingPublishedPackage.VersionPreview.Trim()) {
|
||||
# If we are in preview mode and the package does not have a superseding
|
||||
# preview version, remove the package from the list.
|
||||
# preview version, remove the package from the list.
|
||||
Write-Host "Remove superseded preview package: $packageName"
|
||||
continue
|
||||
}
|
||||
|
||||
if ($Mode -eq 'latest' -and !$matchingPublishedPackage.VersionGA.Trim()) {
|
||||
if ($Mode -eq 'latest' -and !$matchingPublishedPackage.VersionGA.Trim()) {
|
||||
LogWarning "Metadata is missing GA version for GA package $packageName. Keeping existing package."
|
||||
$outputPackages += $package
|
||||
continue
|
||||
|
@ -379,7 +379,7 @@ function UpdateDocsMsPackages($DocConfigFile, $Mode, $DocsMetadata, $PackageSour
|
|||
|
||||
$packageVersion = "==$($matchingPublishedPackage.VersionGA)"
|
||||
if ($Mode -eq 'preview') {
|
||||
if (!$matchingPublishedPackage.VersionPreview.Trim()) {
|
||||
if (!$matchingPublishedPackage.VersionPreview.Trim()) {
|
||||
LogWarning "Metadata is missing preview version for preview package $packageName. Keeping existing package."
|
||||
$outputPackages += $package
|
||||
continue
|
||||
|
@ -402,7 +402,7 @@ function UpdateDocsMsPackages($DocConfigFile, $Mode, $DocsMetadata, $PackageSour
|
|||
-Name 'version' `
|
||||
-Value $packageVersion `
|
||||
-PassThru `
|
||||
-Force
|
||||
-Force
|
||||
if ($PackageSourceOverride) {
|
||||
$package.package_info = Add-Member `
|
||||
-InputObject $package.package_info `
|
||||
|
@ -410,7 +410,7 @@ function UpdateDocsMsPackages($DocConfigFile, $Mode, $DocsMetadata, $PackageSour
|
|||
-Name 'extra_index_url' `
|
||||
-Value $PackageSourceOverride `
|
||||
-PassThru `
|
||||
-Force
|
||||
-Force
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -420,15 +420,15 @@ function UpdateDocsMsPackages($DocConfigFile, $Mode, $DocsMetadata, $PackageSour
|
|||
|
||||
$outputPackagesHash = @{}
|
||||
foreach ($package in $outputPackages) {
|
||||
# In some cases there is no $package.package_info.name, only hash if the
|
||||
# In some cases there is no $package.package_info.name, only hash if the
|
||||
# name is set.
|
||||
if ($package.package_info.name) {
|
||||
if ($package.package_info.name) {
|
||||
$outputPackagesHash[$package.package_info.name] = $true
|
||||
}
|
||||
}
|
||||
|
||||
$remainingPackages = @()
|
||||
if ($Mode -eq 'preview') {
|
||||
$remainingPackages = @()
|
||||
if ($Mode -eq 'preview') {
|
||||
$remainingPackages = $DocsMetadata.Where({
|
||||
$_.VersionPreview.Trim() -and !$outputPackagesHash.ContainsKey($_.Package)
|
||||
})
|
||||
|
@ -536,7 +536,7 @@ function GetExistingPackageVersions ($PackageName, $GroupId=$null)
|
|||
}
|
||||
catch
|
||||
{
|
||||
if ($_.Exception.Response.StatusCode -ne 404)
|
||||
if ($_.Exception.Response.StatusCode -ne 404)
|
||||
{
|
||||
LogError "Failed to retrieve package versions for ${PackageName}. $($_.Exception.Message)"
|
||||
}
|
||||
|
@ -544,15 +544,15 @@ function GetExistingPackageVersions ($PackageName, $GroupId=$null)
|
|||
}
|
||||
}
|
||||
|
||||
function Get-python-DocsMsMetadataForPackage($PackageInfo) {
|
||||
function Get-python-DocsMsMetadataForPackage($PackageInfo) {
|
||||
$readmeName = $PackageInfo.Name.ToLower()
|
||||
Write-Host "Docs.ms Readme name: $($readmeName)"
|
||||
|
||||
# Readme names (which are used in the URL) should not include redundant terms
|
||||
# when viewed in URL form. For example:
|
||||
# when viewed in URL form. For example:
|
||||
# https://docs.microsoft.com/en-us/dotnet/api/overview/azure/storage-blobs-readme
|
||||
# Note how the end of the URL doesn't look like:
|
||||
# ".../azure/azure-storage-blobs-readme"
|
||||
# ".../azure/azure-storage-blobs-readme"
|
||||
|
||||
# This logic eliminates a preceeding "azure." in the readme filename.
|
||||
# "azure-storage-blobs" -> "storage-blobs"
|
||||
|
@ -591,9 +591,9 @@ function Validate-Python-DocMsPackages ($PackageInfo, $PackageInfos, $PackageSou
|
|||
}
|
||||
|
||||
function Get-python-EmitterName() {
|
||||
return "@azure-tools/cadl-python"
|
||||
return "@azure-tools/typespec-python"
|
||||
}
|
||||
|
||||
function Get-python-EmitterAdditionalOptions([string]$projectDirectory) {
|
||||
return "--option @azure-tools/cadl-python.emitter-output-dir=$projectDirectory/"
|
||||
return "--option @azure-tools/typespec-python.emitter-output-dir=$projectDirectory/"
|
||||
}
|
||||
|
|
|
@ -12,5 +12,5 @@ The script is to refresh SDK code
|
|||
|
||||
Step into root folder of this repo and run cmd with sdk folder:
|
||||
```powershell
|
||||
D:\azure-sdk-for-python> python .\scripts\cadl_refresh_sdk\main.py .\sdk\contosowidgetmanager\azure-contosowidgetmanager
|
||||
D:\azure-sdk-for-python> python .\scripts\typespec_refresh_sdk\main.py .\sdk\contosowidgetmanager\azure-contosowidgetmanager
|
||||
```
|
|
@ -6,11 +6,11 @@ from subprocess import check_call
|
|||
|
||||
def main(sdk_folder: str):
|
||||
# install package.json
|
||||
script = Path("eng/common/scripts/Cadl-Project-Sync.ps1")
|
||||
script = Path("eng/common/scripts/TypeSpec-Project-Sync.ps1")
|
||||
check_call(f"pwsh {script} {Path(sdk_folder)}", shell=True)
|
||||
|
||||
# generate SDK
|
||||
cmd = Path("eng/common/scripts/Cadl-Project-Generate.ps1")
|
||||
cmd = Path("eng/common/scripts/TypeSpec-Project-Generate.ps1")
|
||||
check_call(f"pwsh {cmd} {Path(sdk_folder)}", shell=True)
|
||||
|
||||
if __name__ == '__main__':
|
|
@ -34,8 +34,8 @@ def get_package_names(sdk_folder):
|
|||
return package_names
|
||||
|
||||
|
||||
def init_new_service(package_name, folder_name, is_cadl = False):
|
||||
if not is_cadl:
|
||||
def init_new_service(package_name, folder_name, is_typespec = False):
|
||||
if not is_typespec:
|
||||
setup = Path(folder_name, package_name, "setup.py")
|
||||
if not setup.exists():
|
||||
check_call(
|
||||
|
@ -90,7 +90,7 @@ def update_servicemetadata(sdk_folder, data, config, folder_name, package_name,
|
|||
"readme": input_readme,
|
||||
})
|
||||
else:
|
||||
metadata["cadl_src"] = input_readme
|
||||
metadata["typespec_src"] = input_readme
|
||||
metadata.update(config)
|
||||
|
||||
_LOGGER.info("Metadata json:\n {}".format(json.dumps(metadata, indent=2)))
|
||||
|
@ -127,7 +127,7 @@ def update_servicemetadata(sdk_folder, data, config, folder_name, package_name,
|
|||
f.write("".join(includes))
|
||||
|
||||
|
||||
def update_cadl_location(sdk_folder, data, config, folder_name, package_name, input_readme):
|
||||
def update_typespec_location(sdk_folder, data, config, folder_name, package_name, input_readme):
|
||||
if "meta" in config:
|
||||
return
|
||||
|
||||
|
@ -138,14 +138,14 @@ def update_cadl_location(sdk_folder, data, config, folder_name, package_name, in
|
|||
"cleanup": False,
|
||||
}
|
||||
|
||||
_LOGGER.info("cadl-location:\n {}".format(json.dumps(metadata, indent=2)))
|
||||
_LOGGER.info("tsp-location:\n {}".format(json.dumps(metadata, indent=2)))
|
||||
|
||||
package_folder = Path(sdk_folder) / folder_name / package_name
|
||||
if not package_folder.exists():
|
||||
_LOGGER.info(f"Package folder doesn't exist: {package_folder}")
|
||||
return
|
||||
|
||||
metadata_file_path = package_folder / "cadl-location.yaml"
|
||||
metadata_file_path = package_folder / "tsp-location.yaml"
|
||||
with open(metadata_file_path, "w") as writer:
|
||||
yaml.safe_dump(metadata, writer)
|
||||
_LOGGER.info(f"Saved metadata to {metadata_file_path}")
|
||||
|
@ -361,7 +361,7 @@ def get_npm_package_version(package: str) -> Dict[any, any]:
|
|||
if "dependencies" not in data:
|
||||
_LOGGER.info(f"can not find {package}: {data}")
|
||||
return {}
|
||||
|
||||
|
||||
return data["dependencies"]
|
||||
|
||||
def generate_ci(template_path: Path, folder_path: Path, package_name: str) -> None:
|
||||
|
@ -383,22 +383,22 @@ def generate_ci(template_path: Path, folder_path: Path, package_name: str) -> No
|
|||
with open(ci, "w") as file_out:
|
||||
file_out.writelines(content)
|
||||
|
||||
def gen_cadl(cadl_relative_path: str, spec_folder: str) -> Dict[str, Any]:
|
||||
cadl_python = "@azure-tools/cadl-python"
|
||||
def gen_typespec(typespec_relative_path: str, spec_folder: str) -> Dict[str, Any]:
|
||||
typespec_python = "@azure-tools/typespec-python"
|
||||
autorest_python = "@autorest/python"
|
||||
|
||||
# npm install tool
|
||||
origin_path = os.getcwd()
|
||||
with open(Path("eng/emitter-package.json"), "r") as file_in:
|
||||
cadl_python_dep = json.load(file_in)
|
||||
os.chdir(Path(spec_folder) / cadl_relative_path)
|
||||
typespec_python_dep = json.load(file_in)
|
||||
os.chdir(Path(spec_folder) / typespec_relative_path)
|
||||
with open("package.json", "w") as file_out:
|
||||
json.dump(cadl_python_dep, file_out)
|
||||
json.dump(typespec_python_dep, file_out)
|
||||
check_call("npm install", shell=True)
|
||||
|
||||
# generate code
|
||||
cadl_file = "client.cadl" if Path("client.cadl").exists() else "."
|
||||
check_call(f"npx cadl compile {cadl_file} --emit {cadl_python} --arg \"python-sdk-folder={origin_path}\" ", shell=True)
|
||||
typespec_file = "client.tsp" if Path("client.tsp").exists() else "."
|
||||
check_call(f"npx tsp compile {typespec_file} --emit {typespec_python} --arg \"python-sdk-folder={origin_path}\" ", shell=True)
|
||||
|
||||
# get version of codegen used in generation
|
||||
npm_package_verstion = get_npm_package_version(autorest_python)
|
||||
|
|
|
@ -18,8 +18,8 @@ from .generate_utils import (
|
|||
format_samples,
|
||||
gen_dpg,
|
||||
dpg_relative_folder,
|
||||
gen_cadl,
|
||||
update_cadl_location,
|
||||
gen_typespec,
|
||||
update_typespec_location,
|
||||
)
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
@ -40,7 +40,7 @@ def del_outdated_folder(readme: str):
|
|||
if all(p in line for p in pattern):
|
||||
# remove generated_samples
|
||||
sdk_folder = re.findall("[a-z]+/[a-z]+-[a-z]+-[a-z]+", line)[0]
|
||||
sample_folder = Path(f"sdk/{sdk_folder}/generated_samples")
|
||||
sample_folder = Path(f"sdk/{sdk_folder}/generated_samples")
|
||||
if sample_folder.exists():
|
||||
shutil.rmtree(sample_folder)
|
||||
_LOGGER.info(f"remove sample folder: {sample_folder}")
|
||||
|
@ -48,7 +48,7 @@ def del_outdated_folder(readme: str):
|
|||
_LOGGER.info(f"sample folder does not exist: {sample_folder}")
|
||||
# remove old generated SDK code
|
||||
sdk_folder = re.findall("[a-z]+/[a-z]+-[a-z]+-[a-z]+/[a-z]+/[a-z]+/[a-z]+", line)[0]
|
||||
code_folder = Path(f"sdk/{sdk_folder}")
|
||||
code_folder = Path(f"sdk/{sdk_folder}")
|
||||
if is_multiapi and code_folder.exists():
|
||||
if any(item in str(sdk_folder) for item in special_service):
|
||||
for folder in code_folder.iterdir():
|
||||
|
@ -85,15 +85,15 @@ def main(generate_input, generate_output):
|
|||
readme_files = [input_readme]
|
||||
else:
|
||||
# ["specification/confidentialledger/ConfientialLedger"]
|
||||
if isinstance(data["relatedCadlProjectFolder"], str):
|
||||
readme_files = [data["relatedCadlProjectFolder"]]
|
||||
if isinstance(data["relatedTypeSpecProjectFolder"], str):
|
||||
readme_files = [data["relatedTypeSpecProjectFolder"]]
|
||||
else:
|
||||
readme_files = data["relatedCadlProjectFolder"]
|
||||
spec_word = "cadlProject"
|
||||
readme_files = data["relatedTypeSpecProjectFolder"]
|
||||
spec_word = "typeSpecProject"
|
||||
|
||||
for input_readme in readme_files:
|
||||
_LOGGER.info(f"[CODEGEN]({input_readme})codegen begin")
|
||||
is_cadl = False
|
||||
is_typespec = False
|
||||
if "resource-manager" in input_readme:
|
||||
relative_path_readme = str(Path(spec_folder, input_readme))
|
||||
del_outdated_folder(relative_path_readme)
|
||||
|
@ -109,8 +109,8 @@ def main(generate_input, generate_output):
|
|||
elif "data-plane" in input_readme:
|
||||
config = gen_dpg(input_readme, data.get("autorestConfig", ""), dpg_relative_folder(spec_folder))
|
||||
else:
|
||||
config = gen_cadl(input_readme, spec_folder)
|
||||
is_cadl = True
|
||||
config = gen_typespec(input_readme, spec_folder)
|
||||
is_typespec = True
|
||||
package_names = get_package_names(sdk_folder)
|
||||
_LOGGER.info(f"[CODEGEN]({input_readme})codegen end. [(packages:{str(package_names)})]")
|
||||
|
||||
|
@ -132,7 +132,7 @@ def main(generate_input, generate_output):
|
|||
result[package_name][spec_word].append(input_readme)
|
||||
|
||||
# Generate some necessary file for new service
|
||||
init_new_service(package_name, folder_name, is_cadl)
|
||||
init_new_service(package_name, folder_name, is_typespec)
|
||||
format_samples(sdk_code_path)
|
||||
|
||||
# Update metadata
|
||||
|
@ -148,10 +148,10 @@ def main(generate_input, generate_output):
|
|||
)
|
||||
except Exception as e:
|
||||
_LOGGER.info(f"fail to update meta: {str(e)}")
|
||||
|
||||
# update cadl-location.yaml
|
||||
|
||||
# update tsp-location.yaml
|
||||
try:
|
||||
update_cadl_location(
|
||||
update_typespec_location(
|
||||
sdk_folder,
|
||||
data,
|
||||
config,
|
||||
|
@ -160,7 +160,7 @@ def main(generate_input, generate_output):
|
|||
input_readme,
|
||||
)
|
||||
except Exception as e:
|
||||
_LOGGER.info(f"fail to update cadl-location: {str(e)}")
|
||||
_LOGGER.info(f"fail to update tsp-location: {str(e)}")
|
||||
|
||||
# Setup package locally
|
||||
check_call(
|
||||
|
|
Загрузка…
Ссылка в новой задаче