Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

automation script to generate sdk from cadl #32206

Merged
merged 10 commits into from
Nov 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions eng/scripts/Invoke-GenerateAndBuildV2.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ $commitid = $inputJson.headSha
$repoHttpsUrl = $inputJson.repoHttpsUrl
$downloadUrlPrefix = $inputJson.installInstructionInput.downloadUrlPrefix
$autorestConfig = $inputJson.autorestConfig
$relatedCadlProjectFolder = $inputJson.relatedCadlProjectFolder

$autorestConfigYaml = ""
if ($autorestConfig) {
Expand Down Expand Up @@ -108,6 +109,40 @@ if ($inputFileToGen) {
UpdateExistingSDKByInputFiles -inputFilePaths $inputFileToGen -sdkRootPath $sdkPath -headSha $commitid -repoHttpsUrl $repoHttpsUrl -downloadUrlPrefix "$downloadUrlPrefix" -generatedSDKPackages $generatedSDKPackages
}

# generate sdk from cadl file
if ($relatedCadlProjectFolder) {
foreach ($cadlRelativeFolder in $relatedCadlProjectFolder) {
$cadlFolder = Resolve-Path (Join-Path $swaggerDir $cadlRelativeFolder)
$newPackageOutput = "newPackageOutput.json"

Push-Location $cadlFolder
trap {Pop-Location}
$cadlProjectYaml = Get-Content -Path (Join-Path "$cadlFolder" "cadl-project.yaml") -Raw

Install-ModuleIfNotInstalled "powershell-yaml" "0.4.1" | Import-Module
$yml = ConvertFrom-YAML $cadlProjectYaml
$sdkFolder = $yml["emitters"]["@azure-tools/cadl-csharp"]["sdk-folder"]
$projectFolder = (Join-Path $sdkPath $sdkFolder)
# $projectFolder = $projectFolder -replace "\\", "/"
if ($projectFolder) {
$directories = $projectFolder -split "/|\\"
$count = $directories.Count
$projectFolder = $directories[0 .. ($count-2)] -join "/"
$service = $directories[-3];
$namespace = $directories[-2];
}
New-CADLPackageFolder -service $service -namespace $namespace -sdkPath $sdkPath -cadlInput $cadlFolder/main.cadl -outputJsonFile $newpackageoutput
$newPackageOutputJson = Get-Content $newPackageOutput -Raw | ConvertFrom-Json
$relativeSdkPath = $newPackageOutputJson.path
npm install
heaths marked this conversation as resolved.
Show resolved Hide resolved
npx cadl compile --output-path $sdkPath --emit @azure-tools/cadl-csharp .
if ( !$?) {
Throw "Failed to generate sdk for cadl. exit code: $?"
}
GeneratePackage -projectFolder $projectFolder -sdkRootPath $sdkPath -path $relativeSdkPath -downloadUrlPrefix $downloadUrlPrefix -skipGenerate -generatedSDKPackages $generatedSDKPackages
Pop-Location
chunyu3 marked this conversation as resolved.
Show resolved Hide resolved
}
}
$outputJson = [PSCustomObject]@{
packages = $generatedSDKPackages
}
Expand Down
99 changes: 98 additions & 1 deletion eng/scripts/automation/GenerateAndBuildLib.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,100 @@ function New-MgmtPackageFolder() {
return $projectFolder
}

function New-CADLPackageFolder() {
param(
[string]$service,
[string]$namespace,
[string]$sdkPath = "",
[string]$cadlInput ="",
[string]$outputJsonFile = "$PWD/output.json"
)
$serviceFolder = (Join-Path $sdkPath "sdk" $service)
$projectFolder=(Join-Path $sdkPath "sdk" $service $namespace)
$ciymlFilePath =(Join-Path $sdkPath "sdk" $service $CI_YAML_FILE)
$apifolder = (Join-Path $projectFolder "api")
Write-Host "projectFolder:$projectFolder, apifolder:$apifolder"
if ((Test-Path -Path $projectFolder) -And (Test-Path -Path $apifolder)) {
Write-Host "Path exists!"
if (Test-Path -Path $projectFolder/src/autorest.md) {
Remove-Item -Path $projectFolder/src/autorest.md
}
$projFile = (Join-Path $projectFolder "src" "$namespace.csproj")
$fileContent = Get-Content -Path $projFile
$match = ($fileContent | Select-String -Pattern "<AutoRestInput>").LineNumber
if ($match.count -gt 0) {
$fileContent[$match[0] - 1] = "<AutoRestInput>$cadlInput</AutoRestInput>";
} else {
$startNum = ($fileContent | Select-String -Pattern '</PropertyGroup>').LineNumber[0]
$fileContent[$startNum - 2] += ([Environment]::NewLine + "<AutoRestInput>$cadlInput</AutoRestInput>")
}
$fileContent | Out-File $projFile

Update-CIYmlFile -ciFilePath $ciymlFilePath -artifact $namespace
} else {
Write-Host "Path doesn't exist. create template."
dotnet new -i $sdkPath/sdk/template
Write-Host "Create project folder $projectFolder"
if (Test-Path -Path $projectFolder) {
Remove-Item -Path $projectFolder -ItemType Directory
}

Push-Location $serviceFolder
$namespaceArray = $namespace.Split(".")
if ( $namespaceArray.Count -lt 3) {
Throw "Error: invalid namespace name."
}

$endIndex = $namespaceArray.Count - 2
$clientName = $namespaceArray[-1]
$groupName = $namespaceArray[1..$endIndex] -join "."
$dotnetNewCmd = "dotnet new azsdkdpg --name $namespace --clientName $clientName --groupName $groupName --serviceDirectory $service --force"

if (Test-Path -Path $ciymlFilePath) {
Write-Host "ci.yml already exists. update it to include the new serviceDirectory."
Update-CIYmlFile -ciFilePath $ciymlFilePath -artifact $namespace

$dotnetNewCmd = $dotnetNewCmd + " --includeCI false"
}
# dotnet new azsdkdpg --name $namespace --clientName $clientName --groupName $groupName --serviceDirectory $service --swagger $inputfile --securityScopes $securityScope --securityHeaderName $securityHeaderName --includeCI true --force
Write-Host "Invoke dotnet new command: $dotnetNewCmd"
Invoke-Expression $dotnetNewCmd

$projFile = (Join-Path $projectFolder "src" "$namespace.csproj")
$fileContent = Get-Content -Path $projFile
$fileContent = $fileContent -replace '<Version>[^<]+</Version>', '<Version>1.0.0-beta.1</Version>'
$startNum = ($fileContent | Select-String -Pattern '</PropertyGroup>').LineNumber[0]
$fileContent[$startNum - 2] += ([Environment]::NewLine + "<AutoRestInput>$cadlInput</AutoRestInput>")
$fileContent | Out-File $projFile
# (Get-Content $projFile) -replace "<Version>*.*.*-*.*</Version>", "<Version>1.0.0-beta.1</Version>" | -replace "<AutoRestInput>*</AutoRestInput>", "<AutoRestInput>$cadlInput</AutoRestInput>" |Set-Content $projFile
Pop-Location
chunyu3 marked this conversation as resolved.
Show resolved Hide resolved
# dotnet sln
Push-Location $projectFolder
if (Test-Path -Path $projectFolder/src/autorest.md) {
Remove-Item -Path $projectFolder/src/autorest.md
}
dotnet sln remove src/$namespace.csproj
dotnet sln add src/$namespace.csproj
dotnet sln remove tests/$namespace.Tests.csproj
dotnet sln add tests/$namespace.Tests.csproj
Pop-Location
}

Push-Location $sdkPath
$relativeFolderPath = Resolve-Path $projectFolder -Relative
Pop-Location

$outputJson = [PSCustomObject]@{
service = $service
packageName = $namespace
projectFolder = $projectFolder
path = @($relativeFolderPath)
}

$outputJson | ConvertTo-Json -depth 100 | Out-File $outputJsonFile
return $projectFolder
}

function Get-ResourceProviderFromReadme($readmeFile) {
$readmeFile = $readmeFile -replace "\\", "/"
$pathArray = $readmeFile.Split("/");
Expand Down Expand Up @@ -523,6 +617,7 @@ function GeneratePackage()
[string]$sdkRootPath,
[string]$path,
[string]$downloadUrlPrefix="",
[switch]$skipGenerate,
[object]$generatedSDKPackages
)

Expand All @@ -540,7 +635,9 @@ function GeneratePackage()
# Generate Code
Write-Host "Start to generate sdk $projectFolder"
$srcPath = Join-Path $projectFolder 'src'
dotnet build /t:GenerateCode $srcPath
if (!$skipGenerate) {
dotnet build /t:GenerateCode $srcPath
}
if ( !$?) {
Write-Error "Failed to generate sdk. exit code: $?"
$result = "failed"
Expand Down
56 changes: 45 additions & 11 deletions eng/scripts/automation/Invoke-GenerateAndBuild.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ $downloadUrlPrefix = $inputJson.installInstructionInput.downloadUrlPrefix
$autorestConfig = $inputJson.autorestConfig

$autorestConfig = $inputJson.autorestConfig
$relatedCadlProjectFolder = $inputJson.relatedCadlProjectFolder

$autorestConfigYaml = ""
if ($autorestConfig -ne "") {
if ($autorestConfig) {
$autorestConfig | Set-Content "config.md"
$autorestConfigYaml = Get-Content -Path .\config.md
$range = ($autorestConfigYaml | Select-String -Pattern '```').LineNumber
Expand All @@ -41,27 +42,60 @@ if ($autorestConfig -ne "") {
}
}

Write-Host "swaggerDir:$swaggerDir, readmeFile:$readmeFile"
$generatedSDKPackages = New-Object 'Collections.Generic.List[System.Object]'

# $service, $serviceType = Get-ResourceProviderFromReadme $readmeFile
$sdkPath = (Join-Path $PSScriptRoot .. .. ..)
$sdkPath = Resolve-Path $sdkPath
$sdkPath = $sdkPath -replace "\\", "/"

$readme = ""
if ($commitid -ne "") {
if ($repoHttpsUrl -ne "") {
$readme = "$repoHttpsUrl/blob/$commitid/$readmeFile"
if ($readmeFile) {
Write-Host "swaggerDir:$swaggerDir, readmeFile:$readmeFile"

$readme = ""
if ($commitid -ne "") {
if ($repoHttpsUrl -ne "") {
$readme = "$repoHttpsUrl/blob/$commitid/$readmeFile"
} else {
$readme = "https://github.com/$org/azure-rest-api-specs/blob/$commitid/$readmeFile"
chunyu3 marked this conversation as resolved.
Show resolved Hide resolved
}
} else {
$readme = "https://github.com/$org/azure-rest-api-specs/blob/$commitid/$readmeFile"
$readme = (Join-Path $swaggerDir $readmeFile)
}
} else {
$readme = (Join-Path $swaggerDir $readmeFile)
Invoke-GenerateAndBuildSDK -readmeAbsolutePath $readme -sdkRootPath $sdkPath -autorestConfigYaml "$autorestConfigYaml" -downloadUrlPrefix "$downloadUrlPrefix" -generatedSDKPackages $generatedSDKPackages
chunyu3 marked this conversation as resolved.
Show resolved Hide resolved
}

$generatedSDKPackages = New-Object 'Collections.Generic.List[System.Object]'
Invoke-GenerateAndBuildSDK -readmeAbsolutePath $readme -sdkRootPath $sdkPath -autorestConfigYaml "$autorestConfigYaml" -downloadUrlPrefix "$downloadUrlPrefix" -generatedSDKPackages $generatedSDKPackages
if ($relatedCadlProjectFolder) {
$cadlFolder = Resolve-Path (Join-Path $swaggerDir $relatedCadlProjectFolder)
$newPackageOutput = "newPackageOutput.json"

Push-Location $cadlFolder
trap {Pop-Location}
$cadlProjectYaml = Get-Content -Path (Join-Path "$cadlFolder" "cadl-project.yaml") -Raw

Install-ModuleIfNotInstalled "powershell-yaml" "0.4.1" | Import-Module
$yml = ConvertFrom-YAML $cadlProjectYaml
$sdkFolder = $yml["emitters"]["@azure-tools/cadl-csharp"]["sdk-folder"]
$projectFolder = (Join-Path $sdkPath $sdkFolder)
# $projectFolder = $projectFolder -replace "\\", "/"
if ($projectFolder) {
$directories = $projectFolder -split "/|\\"
$count = $directories.Count
$projectFolder = $directories[0 .. ($count-2)] -join "/"
$service = $directories[-3];
$namespace = $directories[-2];
}
New-CADLPackageFolder -service $service -namespace $namespace -sdkPath $sdkPath -cadlInput $cadlFolder/main.cadl -outputJsonFile $newpackageoutput
$newPackageOutputJson = Get-Content $newPackageOutput -Raw | ConvertFrom-Json
$relativeSdkPath = $newPackageOutputJson.path
npm install
npx cadl compile --output-path $sdkPath --emit @azure-tools/cadl-csharp .
if ( !$?) {
Throw "Failed to generate sdk for cadl. exit code: $?"
}
GeneratePackage -projectFolder $projectFolder -sdkRootPath $sdkPath -path $relativeSdkPath -downloadUrlPrefix $downloadUrlPrefix -skipGenerate -generatedSDKPackages $generatedSDKPackages
Pop-Location
chunyu3 marked this conversation as resolved.
Show resolved Hide resolved
}
$outputJson = [PSCustomObject]@{
packages = $generatedSDKPackages
}
Expand Down