Skip to content

Commit 6334745

Browse files
authored
DPG stater package script for cadl and doc (Azure#33113)
* update ci script * support local swagger
1 parent 38a13d1 commit 6334745

7 files changed

+196
-88
lines changed

doc/DataPlaneCodeGeneration/Autorest_DataPlane_Quickstart.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ This tutorial has following sections:
1010

1111
- [Azure SDK Code Generation Quickstart Tutorial (Data Plane)](#azure-sdk-code-generation-quickstart-tutorial-data-plane)
1212
- [Prerequisites](#prerequisites)
13-
- [Setup your repo](#setup-your-repo)
13+
- [Setup your repository](#setup-your-repository)
1414
- [Create starter package](#create-starter-package)
1515
- [Add package ship requirements](#add-package-ship-requirements)
1616
- [Tests](#tests)
@@ -31,9 +31,9 @@ This tutorial has following sections:
3131

3232
## Prerequisites
3333

34-
- Install VS 2022 (Community or higher) and make sure you have the [latest updates](https://www.visualstudio.com/).
34+
- Install Visual Studio 2022 (Community or higher) and make sure you have the [latest updates](https://www.visualstudio.com/).
3535
- Need at least .NET Framework 4.6.1 and 4.7 development tools
36-
- Install the **.NET Core cross-platform development** workloads in VisualStudio
36+
- Install the **.NET Core cross-platform development** workloads in Visual Studio
3737
- Install **.NET 6.0 SDK** for your specific platform. (or a higher version)
3838
- Install the latest version of [git](https://git-scm.com/downloads)
3939
- Install [PowerShell](https://docs.microsoft.com/powershell/scripting/install/installing-powershell), version 7 or higher.

doc/DataPlaneCodeGeneration/AzureSDKCodeGeneration_DataPlane_Quickstart.md

Lines changed: 39 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,11 @@ This tutorial has following sections:
1010

1111
- [Azure SDK Code Generation Quickstart Tutorial (Data Plane)](#azure-sdk-code-generation-quickstart-tutorial-data-plane)
1212
- [Prerequisites](#prerequisites)
13-
- [Setup your repo](#setup-your-repo)
13+
- [Setup your repository](#setup-your-repository)
1414
- [Create starter package](#create-starter-package)
1515
- [Use Cadl as Input](#use-cadl-as-input)
1616
- [Create Cadl project](#create-cadl-project)
17-
- [Create a sdk project folder](#create-a-sdk-project-folder)
18-
- [Generate the library source code](#generate-the-library-source-code)
19-
- [Build the library project](#build-the-library-project)
20-
- [Export the library's public API](#export-the-librarys-public-api)
17+
- [Create sdk starter package](#create-sdk-starter-package)
2118
- [Use swagger as Input](#use-swagger-as-input)
2219
- [Add package ship requirements](#add-package-ship-requirements)
2320
- [Tests](#tests)
@@ -32,18 +29,17 @@ This tutorial has following sections:
3229

3330
## Prerequisites
3431

35-
- Install VS 2020 (Community or higher) and make sure you have the [latest updates](https://www.visualstudio.com/).
32+
- Install Visual Studio 2022 (Community or higher) and make sure you have the [latest updates](https://www.visualstudio.com/).
3633
- Need at least .NET Framework 4.6.1 and 4.7 development tools
37-
- Install the **.NET Core cross-platform development** workloads in VisualStudio
34+
- Install the **.NET Core cross-platform development** workloads in Visual Studio
3835
- Install **.NET 6.0 SDK** for your specific platform. (or a higher version)
39-
- Install **.NET core 3.1** for your specific platform.
4036
- Install the latest version of [git](https://git-scm.com/downloads)
4137
- Install [PowerShell](https://docs.microsoft.com/powershell/scripting/install/installing-powershell), version 7 or higher.
4238
- Install [NodeJS](https://nodejs.org/) (16.x.x or above).
4339

44-
## Setup your repo
40+
## Setup your repository
4541

46-
- Fork and clone an [azure-sdk-for-net](https://github.com/Azure/azure-sdk-for-net) repo. Follow the instructions in the [.NET CONTRIBUTING.md](https://github.com/Azure/azure-sdk-for-net/issues/12903) to fork and clone the `azure-sdk-for-net` repo.
42+
- Fork and clone the [azure-sdk-for-net](https://github.com/Azure/azure-sdk-for-net) repository. Instructions for doing so can be found in the [.NET CONTRIBUTING.md](https://github.com/Azure/azure-sdk-for-net/blob/main/CONTRIBUTING.md).
4743
- Create a branch to work in.
4844

4945
## Create starter package
@@ -123,60 +119,50 @@ emitters:
123119
- `clear-output-folder` indicate if you want to clear up the output folder.
124120

125121

126-
#### Create a sdk project folder
122+
#### Create sdk starter package
127123

128-
You can manually create the project folder. Please refer to [Azure.Template](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/template/Azure.Template) as an example.
124+
We will use the Azure SDK template [Azure.Template](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/template/Azure.Template) to create the initial project skeleton.
129125

130-
**Note**:
126+
You can run `eng\scripts\automation\Invoke-CadlDataPlaneGenerateSDKPackage.ps1` to generate the starting SDK client library package directly as following:
131127

132-
- Please refer to [sdk-directory-layout](https://github.com/Azure/azure-sdk/blob/main/docs/policies/repostructure.md#sdk-directory-layout) for detail information.
133-
- remove `autorest.md` from sdk\<service name>\<package name>\src
134-
- add `cadl-location.yaml` under sdk\<service name>\<package name>\src
135-
136-
Modify `cadl-location.yaml` to add some project meta data if needed, Otherwise just add an empty file.
137-
138-
e.g. if you want specify cadl project folder in azure-rest-api-spec repo, you can define (directory, commit, repo) to specify the cadl project.
139-
140-
```yml
141-
directory: specification/cognitiveservices/data-plane/AnomalyDetector/stable/v1.1/cadl
142-
commit: 8804b10cf61267b81e06ebccbd1dd46677a54425
143-
repo: Azure/azure-rest-api-specs
144-
cleanup: false
145-
```
146-
147-
#### Generate the library source code
148-
149-
Generate the library source code files to the directory `<sdkPath>/sdk/<service>/<namespace>/src/Generated`
128+
```powershell
129+
eng/scripts/automation/Invoke-CadlDataPlaneGenerateSDKPackage.ps1 -service <servicename> -namespace Azure.<group>.<service> -sdkPath <sdkrepoRootPath> -cadlRelativeFolder <relativeCadlProjectFolderPath> [-commit <commitId>] [-repo <specRepo>] [-specRoot <specRepoRootPath>] [-additionalSubDirectories <relativeFolders>]
130+
```
150131

151-
Enter `Cadl Project Folder`, run
132+
e.g.
133+
Use git url
152134

153-
```shell
154-
npm install
155-
npx cadl compile --emit @azure-tools/cadl-csharp --output-path <Path-to-source-code-folder> <path-to-cadl-file>
156-
```
157-
158-
e.g.
135+
```powershell
136+
pwsh /home/azure-sdk-for-net/eng/scripts/automation/Invoke-CadlDataPlaneGenerateSDKPackage.ps1 -service anomalydetector -namespace Azure.AI.AnomalyDetector -sdkPath /home/azure-sdk-for-net -cadlRelativeFolder specification/cognitiveservices/AnomalyDetector -commit ac8e06a2ed0fc1c54663c98f12c8a073f8026b90 -repo Azure/azure-rest-api-specs
137+
```
138+
or
139+
Use local Cadl project
159140

160-
```shell
161-
npx cadl compile --emit @azure-tools/cadl-csharp --output-path /home/azure-sdk-for-net/sdk/deviceupdate/Azure.IoT.DeviceUpdate/src main.cadl
162-
```
141+
```powershell
142+
pwsh /home/azure-sdk-for-net/eng/scripts/automation/Invoke-CadlDataPlaneGenerateSDKPackage.ps1 -service anomalydetector -namespace Azure.AI.AnomalyDetector -sdkPath /home/azure-sdk-for-net -cadlRelativeFolder specification/cognitiveservices/AnomalyDetector -specRoot /home/azure-rest-api-specs
143+
```
163144
**Note**:
164145

165-
- `<path-to-cadl-file>` - Should be the entrypoint cadl file of the cadl project. e.g. When you define `client.cadl` to customize the client, the `client.cadl` will be the entrypoint cadl file, and you should specify the path to the `client.cadl`.
166-
167-
#### Build the library project
146+
- `-service` takes Azure client service directory name. ie. purview. It equals to the name of the directory in the specification folder of the azure-rest-api-specs repo that contains the REST API definition file.
147+
- For `- namespace`, please use one of the pre-approved namespace groups on the [.NET Azure SDK Guidelines Approved Namespaces list](https://azure.github.io/azure-sdk/dotnet_introduction.html#dotnet-namespaces-approved-list). This value will also provide the name for the shipped package, and should be of the form `Azure.<group>.<service>`.
148+
- `-sdkPath` takes the address of the root directory of sdk repo. e.g. /home/azure-sdk-for-net
149+
- `cadlRelativeFolder` takes the relative path of the cadl project folder in spec repo. e.g. specification/cognitiveservices/AnomalyDetector
150+
- `-additionalSubDirectories` takes the relative paths of the additional directories needed by the cadl project, such as share library folder, separated by semicolon if there is more than one folder.
151+
- `-commit` takes the git commit hash (e.g. ac8e06a2ed0fc1c54663c98f12c8a073f8026b90)
152+
- `-repo` takes the `<owner>/<repo>` of the REST API specification repository. (e.g. Azure/azure-rest-api-specs)
153+
- `-specRoot` takes the file system path of the spec repo. e.g. /home/azure-rest-api-specs
154+
- You need to provide one of (`-commit`, `-repo`) pair to refer to an URL path of the cadl project and `-specRoot` parameters. If you provide both, `-specRoot` will be ignored.
168155

169-
Run `dotnet build` under project folder ``<sdkPath>/sdk/<service>/<namespace>` to build the project to create the starter package binary.
156+
When you run `eng\scripts\automation\Invoke-CadlDataPlaneGenerateSDKPackage.ps1`, it will:
170157

171-
#### Export the library's public API
158+
- Create a project folder, install template files from `sdk/template/Azure.Template`, and create `.csproj` and `.sln` files for your new library.
172159

173-
You will need to run the `eng\scripts\Export-API.ps1` script to update the public API listing. This will generate a file in the library's directory `<sdkPath>/sdk/<service>/<namespace>/api` similar to the example found in [sdk\template\Azure.Template\api\Azure.Template.netstandard2.0.cs](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/template/Azure.Template/api/Azure.Template.netstandard2.0.cs).
160+
These files are created following the guidance for the [Azure SDK Repository Structure](https://github.com/Azure/azure-sdk/blob/master/docs/policies/repostructure.md).
174161

175-
e.g. Running the script for a project in `sdk\deviceupdate` would look like this:
162+
- Generate the library source code files to the directory `<sdkPath>/sdk/<service>/<namespace>/src/Generated`
163+
- Build the library project to create the starter package binary.
164+
- Export the library's public API to the directory `<sdkPath>/sdk/<service>/<namespace>/api`
176165

177-
```powershell
178-
eng\scripts\Export-API.ps1 deviceupdate
179-
```
180166

181167
### Use swagger as Input
182168

@@ -237,7 +223,8 @@ Adding convenience APIs is not required for Azure SDK data plane generated libra
237223

238224
You can add convienice APIs by adding a customization layer on top of the generated code. Please see the [autorest.csharp README](https://github.com/Azure/autorest.csharp#setup) for the details of adding the customization layer. This is the preferred method for adding convenience APIs to your generated client.
239225

240-
If other modifications are needed to the generated API, you can consider making them directly to the Open API specification, which will have the benefit of making the changes to the library in all languages you generate the library in. As a last resort, you can add modifications with swagger transforms in the `autorest.md` file. [AnomalyDetector autorest.md](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/anomalydetector/Azure.AI.AnomalyDetector/src/autorest.md) shows and example of how this can be accomplished.
226+
227+
If other modifications are needed to the generated API, you can consider making them directly to the Open API specification, which will have the benefit of making the changes to the library in all languages you generate the library in. As a last resort, you can add modifications with swagger transforms in the `autorest.md` file. Details for various transforms can be found in [Customizing the generated code](https://github.com/Azure/autorest.csharp#customizing-the-generated-code).
241228

242229
Once you've made changes to the public API, you will need to run the `eng\scripts\Export-API.ps1` script to update the public API listing. This will generate a file in the library's directory similar to the example found in [sdk\template\Azure.Template\api\Azure.Template.netstandard2.0.cs](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/template/Azure.Template/api/Azure.Template.netstandard2.0.cs).
243230

eng/scripts/Cadl-Project-Generate.ps1

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,20 +42,27 @@ $npmWorkingDir = Resolve-Path $tempFolder/$innerFolder
4242
$mainCadlFile = If (Test-Path "$npmWorkingDir/client.cadl") { Resolve-Path "$npmWorkingDir/client.cadl" } Else { Resolve-Path "$npmWorkingDir/main.cadl"}
4343
$resolvedProjectDirectory = Resolve-Path "$ProjectDirectory/src"
4444

45+
$HasEmitterOutputDir = $false;
4546
if (Test-Path "$npmWorkingDir/cadl-project.yaml") {
4647
$cadlProjectYaml = Get-Content -Path "$npmWorkingDir/cadl-project.yaml" -Raw
4748
$yml = ConvertFrom-YAML $cadlProjectYaml
48-
if ( $yml["emitters"]["@azure-tools/cadl-csharp"]["sdk-folder"]) {
49-
$yml["emitters"]["@azure-tools/cadl-csharp"]["sdk-folder"] = ""
49+
if ($yml -And $yml["options"] -And $yml["options"]["@azure-tools/cadl-csharp"] -And $yml["options"]["@azure-tools/cadl-csharp"]["emitter-output-dir"]) {
50+
$HasEmitterOutputDir = $true
5051
}
51-
(ConvertTo-Yaml $yml) | Out-File "$npmWorkingDir/cadl-project.yaml"
5252
}
5353

5454
try {
5555
Push-Location $npmWorkingDir
5656
NpmInstallForProject $npmWorkingDir
57-
Write-Host("npx cadl compile $mainCadlFile --emit `"`@azure-tools/cadl-csharp`" --output-path `"$resolvedProjectDirectory`"")
58-
npx cadl compile $mainCadlFile --emit "@azure-tools/cadl-csharp" --output-path "$resolvedProjectDirectory"
57+
if ($HasEmitterOutputDir) {
58+
$csharpSdkFolder = (Join-Path $ProjectDirectory ".." ".." "..")
59+
$csharpSdkFolder = Resolve-Path $csharpSdkFolder
60+
Write-Host("npx cadl compile $mainCadlFile --emit `"`@azure-tools/cadl-csharp`" --arg `"csharp-sdk-folder=$csharpSdkFolder`"")
61+
npx cadl compile $mainCadlFile --emit "@azure-tools/cadl-csharp" --arg "csharp-sdk-folder=$csharpSdkFolder"
62+
} else {
63+
Write-Host("npx cadl compile $mainCadlFile --emit `"`@azure-tools/cadl-csharp`" --option `"@azure-tools/cadl-csharp.emitter-output-dir=$resolvedProjectDirectory`"")
64+
npx cadl compile $mainCadlFile --emit "@azure-tools/cadl-csharp" --option @azure-tools/cadl-csharp.emitter-output-dir=$resolvedProjectDirectory
65+
}
5966
}
6067
finally {
6168
Pop-Location

eng/scripts/Invoke-GenerateAndBuildV2.ps1

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -119,16 +119,20 @@ if ($relatedCadlProjectFolder) {
119119

120120
Install-ModuleIfNotInstalled "powershell-yaml" "0.4.1" | Import-Module
121121
$yml = ConvertFrom-YAML $cadlProjectYaml
122-
$sdkFolder = $yml["emitters"]["@azure-tools/cadl-csharp"]["sdk-folder"]
123-
$projectFolder = (Join-Path $sdkPath $sdkFolder)
124-
# $projectFolder = $projectFolder -replace "\\", "/"
125-
if ($projectFolder) {
126-
$directories = $projectFolder -split "/|\\"
127-
$count = $directories.Count
128-
$projectFolder = $directories[0 .. ($count-2)] -join "/"
129-
$service = $directories[-3];
130-
$namespace = $directories[-2];
122+
$service = ""
123+
$namespace = ""
124+
if ($yml) {
125+
if ($yml["parameters"] -And $yml["parameters"]["service-directory-name"]) {
126+
$service = $yml["parameters"]["service-directory-name"]["default"];
127+
}
128+
if ($yml["options"] -And $yml["options"]["@azure-tools/cadl-csharp"] -And $yml["options"]["@azure-tools/cadl-csharp"]["namespace"]) {
129+
$namespace = $yml["options"]["@azure-tools/cadl-csharp"]["namespace"]
130+
}
131131
}
132+
if (!$service || !$namespace) {
133+
throw "Not provide service name or namespace."
134+
}
135+
$projectFolder = (Join-Path $sdkPath "sdk" $service $namespace)
132136
New-CADLPackageFolder `
133137
-service $service `
134138
-namespace $namespace `

eng/scripts/automation/GenerateAndBuildLib.ps1

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -366,15 +366,18 @@ function CreateOrUpdateCadlConfigFile() {
366366
[string]$directory,
367367
[string]$commit = "",
368368
[string]$repo = "",
369-
[string]$specRoot = ""
369+
[string]$specRoot = "",
370+
[string]$additionalSubDirectories="" #additional directories needed, separated by semicolon if more than one
371+
370372
)
371373
if (!(Test-Path -Path $cadlConfigurationFile)) {
372374
New-Item -Path $cadlConfigurationFile
373375
}
374376

377+
Install-ModuleIfNotInstalled "powershell-yaml" "0.4.1" | Import-Module
375378
$configuration = Get-Content -Path $cadlConfigurationFile -Raw | ConvertFrom-Yaml
376379
if ( !$configuration) {
377-
$configuration = [System.Collections.Generic.Dictionary[string,string]](New-Object 'System.Collections.Generic.Dictionary[string,string]')
380+
$configuration = @{}
378381
}
379382
$configuration["directory"] = $directory
380383
if ($commit) {
@@ -394,14 +397,14 @@ function CreateOrUpdateCadlConfigFile() {
394397
$configuration.Remove("spec-root-dir")
395398
}
396399

397-
$fileContent = ""
398-
foreach ( $key in $configuration.keys) {
399-
if ($configuration[$key]) {
400-
$fileContent += ( $key + ": " + $configuration[$key] + [Environment]::NewLine)
401-
}
400+
if ($additionalSubDirectories) {
401+
$directoryArray = [string[]]$additionalSubDirectories.Split(";")
402+
$configuration["additionalDirectories"] = [Collections.Generic.List[string]]$directoryArray;
403+
} else {
404+
$configuration.Remove("additionalDirectories")
402405
}
403406

404-
$fileContent | Out-File $cadlConfigurationFile
407+
$configuration |ConvertTo-Yaml | Out-File $cadlConfigurationFile
405408
}
406409

407410
function New-CADLPackageFolder() {
@@ -413,6 +416,7 @@ function New-CADLPackageFolder() {
413416
[string]$commit = "",
414417
[string]$repo = "",
415418
[string]$specRoot = "",
419+
[string]$additionalSubDirectories="", #additional directories needed, separated by semicolon if more than one
416420
[string]$outputJsonFile = "$PWD/output.json"
417421
)
418422
$serviceFolder = (Join-Path $sdkPath "sdk" $service)
@@ -435,7 +439,8 @@ function New-CADLPackageFolder() {
435439
-directory $relatedCadlProjectFolder `
436440
-commit $commit `
437441
-repo $repo `
438-
-specRoot $specRoot
442+
-specRoot $specRoot `
443+
-additionalSubDirectories $additionalSubDirectories
439444

440445
Update-CIYmlFile -ciFilePath $ciymlFilePath -artifact $namespace
441446
} else {
@@ -484,7 +489,8 @@ function New-CADLPackageFolder() {
484489
-directory $relatedCadlProjectFolder `
485490
-commit $commit `
486491
-repo $repo `
487-
-specRoot $specRoot
492+
-specRoot $specRoot `
493+
-additionalSubDirectories $additionalSubDirectories
488494

489495
dotnet sln remove src/$namespace.csproj
490496
dotnet sln add src/$namespace.csproj

0 commit comments

Comments
 (0)