Skip to content

Generate code from TypeSpec

Weidong Xu edited this page Dec 5, 2024 · 18 revisions

Before you Start

Java Azure SDK Design Guidelines is the overall design guideline of the client SDK. Make sure you are familiar with concepts such as "Service Client" and "Packaging".

Make sure you are familiar with Git and Maven, especially the Build Lifecycle.

See Prerequisite of @azure-tools/typespec-java. Make sure that NodeJS, JDK, Maven is installed.

Verify that TypeSpec Source is Correct

☑️ The package structure in the azure-rest-api-specs repository should follow these guidelines.

Here is a sample for tspconfig.yaml. Please make sure service-dir, package-dir, namespace (for typespec-java) is correctly configured.

  • "parameters.service-dir.default" would be sdk/<service>
  • "options.@azure-tools/typespec-java.package-dir" would be <module>

If you have your namespace reviewed and finalized as e.g. azure-resourcemanager-elasticsearch (com.azure.resourcemanager.elasticsearch), <service> would be elasticsearch, module would be azure-resourcemanager-elasticsearch.

SDK module would be generated under the SDK project folder at sdk/<service>/<module>.

Generate SDK

Prerequisite

Install Git. (Verify by running git --version)

Install Node.js 20 or above. (Verify by running node --version)

Install Java 11 or above. (Verify by running java --version)

Install Maven. (Verify by running mvn --version)

Install Python. (Verify by running python --version)

Install Python lib.

pip install --upgrade wheel PyYAML requests

Install tsp-client.

npm install -g "@azure-tools/typespec-client-generator-cli"`

Local clone the repositories

Make sure that you have forked Azure/azure-rest-api-specs and Azure/azure-sdk-for-java.

Clone the fork to your local

git clone "https://github.com/{YOUR_GITHUB_USERNAME}/azure-rest-api-specs.git"
git clone "https://github.com/{YOUR_GITHUB_USERNAME}/azure-sdk-for-java.git"

If you already have the repositories on your local, make sure you are on main branch, and the code is latest.

git checkout main
git pull

Generate

Create a local json file named generatedInput.json with content similar to that shown below.

{
  "dryRun": false,
  "specFolder": "LOCAL_AZURE-REST-API-SPECS_REPO_ROOT",
  "headSha": "SHA_OF_AZURE-REST-API-SPECS_REPO",
  "repoHttpsUrl": "https://github.com/Azure/azure-rest-api-specs",
  "relatedTypeSpecProjectFolder": [
    "specification/SERVICE_DIRECTORY_NAME/PACKAGE_DIRECTORY_NAME/"
  ]
}

Run the generate script at the root folder of your cloned azure-sdk-for-java.

python eng/automation/generate.py "PATH_TO_generatedInput.json" "generatedOutput.json"

Review the generated files.

Appendix

Generate code without the script

For initial set up, from the root of the SDK repo, run script

tsp-client init -c "URL_TO_TSPCONFIG_YAML"

For updating TypeSpec generated SDK, run script below in the SDK module folder (sdk/<service>/<module>) where tsp-location.yaml exists

tsp-client update
Clone this wiki locally