Skip to content

Commit

Permalink
refactor-modular-code-a-little-and-add-unit-test-for-it (#1989)
Browse files Browse the repository at this point in the history
* refactor-modular-code-a-little-and-add-unit-test-for-it

* fix format

* fix ci

* replace all possible cadl with typespec

* rename other files

* fix format
  • Loading branch information
qiaozha committed Aug 28, 2023
1 parent d350fb2 commit 62cd637
Show file tree
Hide file tree
Showing 99 changed files with 818 additions and 706 deletions.
22 changes: 11 additions & 11 deletions .scripts/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ parameters:

variables:
AUTOREST_TESTSERVER_COVERAGE_DIRECTORY: "$(Build.SourcesDirectory)/packages/autorest.typescript/coverage/"
CADL_TESTSERVER_COVERAGE_RLC_REPORT: "$(Build.SourcesDirectory)/packages/typespec-ts/coverage/cadl-ranch-coverage-typescript-rlc.json"
CADL_TESTSERVER_COVERAGE_MODULAR_REPORT: "$(Build.SourcesDirectory)/packages/typespec-ts/coverage/cadl-ranch-coverage-typescript-modular.json"
TypeSpec_TESTSERVER_COVERAGE_RLC_REPORT: "$(Build.SourcesDirectory)/packages/typespec-ts/coverage/cadl-ranch-coverage-typescript-rlc.json"
TypeSpec_TESTSERVER_COVERAGE_MODULAR_REPORT: "$(Build.SourcesDirectory)/packages/typespec-ts/coverage/cadl-ranch-coverage-typescript-modular.json"

pool:
vmImage: ubuntu-20.04
Expand Down Expand Up @@ -220,7 +220,7 @@ stages:
name: SmokeTest_RLC
tag: ci_rlc

- job: Cadl_Smoke_Test
- job: TypeSpec_Smoke_Test
dependsOn: Build
variables:
- template: globals.yml
Expand All @@ -245,7 +245,7 @@ stages:
- template: build.yml
- script: node $(Build.SourcesDirectory)/common/scripts/install-run-rushx.js smoke-test
workingDirectory: $(Build.SourcesDirectory)/packages/typespec-test
displayName: "Generate Code From Cadl"
displayName: "Generate Code From TypeSpec"
- script: npm run check:tree
workingDirectory: $(Build.SourcesDirectory)/packages/typespec-test
displayName: "Check git Tree"
Expand Down Expand Up @@ -278,25 +278,25 @@ stages:
displayName: "Lint check for rlc common lib"
- script: npm run lint
workingDirectory: $(Build.SourcesDirectory)/packages/typespec-ts
displayName: "Lint check for Cadl emitter"
displayName: "Lint check for TypeSpec emitter"
- script: npm run check-format
workingDirectory: $(Build.SourcesDirectory)/packages/typespec-ts
displayName: "Format check for Cadl emitter"
displayName: "Format check for TypeSpec emitter"
- script: npm run test
workingDirectory: $(Build.SourcesDirectory)/packages/typespec-ts
displayName: "Generate RLC Test Clients from Cadl-ranch"
- script: npm run check:tree
workingDirectory: $(Build.SourcesDirectory)/packages/typespec-ts
displayName: "Check git Tree"
- script: npx cadl-ranch check-coverage ./node_modules/@azure-tools/cadl-ranch-specs/http --coverageFiles $(CADL_TESTSERVER_COVERAGE_RLC_REPORT) --coverageFiles $(CADL_TESTSERVER_COVERAGE_MODULAR_REPORT)
- script: npx cadl-ranch check-coverage ./node_modules/@azure-tools/cadl-ranch-specs/http --coverageFiles $(TypeSpec_TESTSERVER_COVERAGE_RLC_REPORT) --coverageFiles $(TypeSpec_TESTSERVER_COVERAGE_MODULAR_REPORT)
displayName: "Check Coverage"
workingDirectory: $(Build.SourcesDirectory)/packages/typespec-ts
continueOnError: true
- publish: $(CADL_TESTSERVER_COVERAGE_RLC_REPORT)
- publish: $(TypeSpec_TESTSERVER_COVERAGE_RLC_REPORT)
artifact: RLCCoverageReport
displayName: "Publish rlc coverage report"
condition: and(ne(variables['Build.Reason'], 'PullRequest'), eq(variables['System.TeamProject'], 'internal'), eq( variables['Agent.OS'], 'Linux'))
- publish: $(CADL_TESTSERVER_COVERAGE_MODULAR_REPORT)
- publish: $(TypeSpec_TESTSERVER_COVERAGE_MODULAR_REPORT)
artifact: ModularCoverageReport
displayName: "Publish modular coverage report"
condition: and(ne(variables['Build.Reason'], 'PullRequest'), eq(variables['System.TeamProject'], 'internal'), eq( variables['Agent.OS'], 'Linux'))
Expand All @@ -309,8 +309,8 @@ stages:
scriptLocation: "inlineScript"
workingDirectory: $(Build.SourcesDirectory)/packages/typespec-ts
inlineScript: |
npx cadl-ranch upload-coverage --coverageFile $(CADL_TESTSERVER_COVERAGE_RLC_REPORT) --generatorName typescript/rlc --storageAccountName azuresdkcadlranch --generatorVersion $(node -p -e "require('./package.json').version")
npx cadl-ranch upload-coverage --coverageFile $(CADL_TESTSERVER_COVERAGE_MODULAR_REPORT) --generatorName typescript/modular --storageAccountName azuresdkcadlranch --generatorVersion $(node -p -e "require('./package.json').version")
npx cadl-ranch upload-coverage --coverageFile $(TypeSpec_TESTSERVER_COVERAGE_RLC_REPORT) --generatorName typescript/rlc --storageAccountName azuresdkcadlranch --generatorVersion $(node -p -e "require('./package.json').version")
npx cadl-ranch upload-coverage --coverageFile $(TypeSpec_TESTSERVER_COVERAGE_MODULAR_REPORT) --generatorName typescript/modular --storageAccountName azuresdkcadlranch --generatorVersion $(node -p -e "require('./package.json').version")
# The Prerelease and Release stages are conditioned on whether we are building a pull request and the branch.
- ${{if and(ne(variables['Build.Reason'], 'PullRequest'), eq(variables['System.TeamProject'], 'internal'), eq(parameters.IncludeRelease,true))}}:
Expand Down
2 changes: 1 addition & 1 deletion .scripts/nightly.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ stages:
- template: nightly-prepare.yml
- script: node $(Build.SourcesDirectory)/common/scripts/install-run-rushx.js smoke-test
workingDirectory: $(Build.SourcesDirectory)/packages/typespec-test
displayName: "Generate Code From Cadl"
displayName: "Generate Code From TypeSpec"
4 changes: 2 additions & 2 deletions .scripts/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -278,12 +278,12 @@ stages:
npm publish $(TAR_NAME) --access public --tag dev
npm config delete //registry.npmjs.org/:_authToken
displayName: "Publish @autorest/typescript to NPM"
- deployment: PublishCadlAutorestToNpm
- deployment: PublishTypeSpecTSToNpm
condition: or(eq(variables['SetDevVersion'], 'true'), and(eq(variables['SetDevVersion'], ''), eq(variables['Build.Reason'],'Schedule'), eq(variables['System.TeamProject'], 'internal')))
variables:
TAR_NAME: $[ stageDependencies.BuildStage.Build.outputs['PackTypeSpecTSArtifacts.TAR_NAME'] ]
environment: npmtsjssdk
displayName: Publish @azure-tools/cadl-autorest dev version to NPM
displayName: Publish @azure-tools/typespec-ts dev version to NPM
pool:
name: ${{ parameters.LinuxPool }}
vmImage: ${{ parameters.OSVmImage }}
Expand Down
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ In order contribute to this project, You will need to install some prerequisite
- Rush will automatically manage the specific version needed by this repo as long as you have any v5 version installed.
- If you're unable to install a global tool, you can instead call the wrapper script `node <repo root>/common/scripts/install-run-rush.js` any time the guide instructs you to run `rush`. The wrapper script will install a managed copy of Rush in a temporary directory for you.
- [Autorest](https://www.npmjs.com/package/autorest), if you're planning contribute to the generator code from swagger either for high level client or for rest level client.
- [Cadl Compiler](https://www.npmjs.com/package/@cadl-lang/compiler), if you're planning contribute to the generator code from cadl.
- [TypeSpec Compiler](https://www.npmjs.com/package/@typespec/compiler), if you're planning contribute to the generator code from TypeSpec.

### Things to keep in mind when contributing

Expand Down
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ This repository is the code generator libraries for Azure TypeScript SDK and Azu
| Name | Changelog | Latest | Next |
| --------------------------------------------------------------------------- | -------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------- |
| [@autorest/typescript][autorest_typescript_src] | [Changelog][autorest_typescript_chg] | [![](https://img.shields.io/npm/v/@autorest/typescript)](https://www.npmjs.com/package/@autorest/typescript) | ![](https://img.shields.io/npm/v/@autorest/typescript/next) |
| [@azure-tools/cadl-typescript][cadl-typescript_src] | [Changelog][cadl-typescript_chg] | [![](https://img.shields.io/npm/v/@azure-tools/cadl-typescript)](https://www.npmjs.com/package/@azure-tools/cadl-typescript) | ![](https://img.shields.io/npm/@azure-tools/cadl-azure-core/next) |
| [@azure-tools/typespec-ts][typespec-ts_src] | [Changelog][typespec-ts_chg] | [![](https://img.shields.io/npm/v/@azure-tools/typespec-ts)](https://www.npmjs.com/package/@azure-tools/typespec-ts) | ![](https://img.shields.io/npm/@azure-tools/typespec-azure-core/next) |
| [@azure-tools/rlc-common][rlc-common_src] | [Changelog][rlc-common_chg] | [![](https://img.shields.io/npm/v/@azure-tools/rlc-common)](https://www.npmjs.com/package/@azure-tools/rlc-common) | ![](https://img.shields.io/npm/@azure-tools/rlc-common/next) |


[autorest_typescript_src]: packages/autorest.typescript/
[autorest_typescript_chg]: packages/autorest.typescript/CHANGELOG.md
[cadl-typescript_src]: packages/cadl-typescript
[cadl-typescript_chg]: packages/cadl-typescript/CHANGELOG.md
[typespec-ts_src]: packages/typespec-ts
[typespec-ts_chg]: packages/typespec-ts/CHANGELOG.md
[rlc-common_src]: packages/rlc-common
[rlc-common_chg]: packages/rlc-common/CHANGELOG.md

Expand All @@ -23,8 +23,8 @@ This repository is the code generator libraries for Azure TypeScript SDK and Azu

### General Introduction

Inside this project, we support both high level client generation and rest level client generation. In the rest level client generation, we support generate code from both rest api specs and cadl. In the high level client generation, we only support generation from rest api specs.
Inside this project, we support both high level client generation and rest level client generation. In the rest level client generation, we support generate code from both rest api specs and typespec. In the high level client generation, we only support generation from rest api specs.

- [**@autorest/typescript:**](https://github.com/Azure/autorest.typescript/tree/main/packages/autorest.typescript) contains the original high level client generation and the rest level client generation, mostly including the transformation from autorest code model into RLC model part.
- [**@azure-tools/cadl-typescript:**](https://github.com/Azure/autorest.typescript/tree/main/packages/cadl-typescript) is the typescript cadl emitter, which contains the transformation from cadl model into RLC model.
- [**@azure-tools/rlc-common:**](https://github.com/Azure/autorest.typescript/tree/main/packages/rlc-common) contains the common generation logic from RLC model into rest client libraries code part. Which is depended by both `@autorest/typescript` rlc generation part and `@azure-tools/cadl-typescript`.
- [**@azure-tools/typespec-ts:**](https://github.com/Azure/autorest.typescript/tree/main/packages/typespec-ts) is the typescript TypeSpec emitter, which contains the transformation from TypeSpec model into RLC model.
- [**@azure-tools/rlc-common:**](https://github.com/Azure/autorest.typescript/tree/main/packages/rlc-common) contains the common generation logic from RLC model into rest client libraries code part. Which is depended by both `@autorest/typescript` rlc generation part and `@azure-tools/typespec-ts`.
30 changes: 15 additions & 15 deletions docs/RLC-generation-Design.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@

## Overview

In the JavaScript/TypeScript RLC generator, we support generate RLC code from both CADL input and Swagger input. This document will mainly talk about how the high level design of JavaScript/TypeScript Rest Level Client (RLC) generator looks like. we will first show the whole workflow and then introduce the RLC generation from CADL and Swagger separately.
In the JavaScript/TypeScript RLC generator, we support generate RLC code from both TypeSpec input and Swagger input. This document will mainly talk about how the high level design of JavaScript/TypeScript Rest Level Client (RLC) generator looks like. we will first show the whole workflow and then introduce the RLC generation from TypeSpec and Swagger separately.

## Overall Workflow

Basically, the whole workflow is shown in the below flowchart.

```mermaid
graph TD
A[CADL Files] -->|CADL compiler| B(CADL Program)
B -->|CADL TypeScript emitter| C{RLCModel}
A[TypeSpec Files] -->|TypeSpec compiler| B(TypeSpec Program)
B -->|TypeSpec TypeScript emitter| C{RLCModel}
C --> |RLC common| D{RLC File Content}
D --> |CADL TypeScript emitter| E(JS RLC Code)
D --> |TypeSpec TypeScript emitter| E(JS RLC Code)
F(Swager Files) -->|Autorest core & m4 plugin| G(Modelerfour)
G -->|Autotest TypeScript Plugin| C
D --> |Autorest TypeScript Plugin| E
Expand All @@ -23,30 +23,30 @@ D --> |Autorest TypeScript Plugin| E

To better understand the above flowchart, we list some basic terms here.

- **Cadl Files**: A Service API that is defined using the [CADL](https://github.com/microsoft/cadl) language.
- **TypeSpec Files**: A Service API that is defined using the [TypeSpec](https://github.com/microsoft/typespec) language.
- **Swagger Files**: A REST api specification that follows the [OpenAPI](https://swagger.io/) definition.
- **CADL Program**: An internal model that is provided after CADL compiler has processed the CADL input.
- **TypeSpec Program**: An internal model that is provided after TypeSpec compiler has processed the TypeSpec input.
- **Modelerfour**: A code model that is provided after Autorest core and Modelerfour plugin have processed the swagger input.
- **RLCModel**: An internally defined model that contains all the necessary information required to build the RLC file content.
- **CADL compiler**: A module that compiles the CADL input and provides a set of APIs to the CADL emitter to retrieve the CADL program model.
- **TypeSpec compiler**: A module that compiles the TypeSpec input and provides a set of APIs to the TypeSpec emitter to retrieve the TypeSpec program model.
- **Autorest core & m4 plugin**: A module that takes swagger files as input and outputs the code model Modelerfour.
- **CADL TypeScript emitter**: A module that transforms the CADL program into RLCModel.
- **TypeSpec TypeScript emitter**: A module that transforms the TypeSpec program into RLCModel.
- **RLC common**: A common part that takes RLCModel as input and builds the RLC file content.
- **Autorest TypeScript plugin**: A module that transforms the Modelerfour into RLCModel.

## RLC generation from CADL
## RLC generation from TypeSpec

On a high level, the RLC generation process from CADL input involves the following steps:
1. The CADL input is processed by the CADL compiler to generate a CADL program model.
1. The CADL program model is transformed into an RLCModel.
On a high level, the RLC generation process from TypeSpec input involves the following steps:
1. The TypeSpec input is processed by the TypeSpec compiler to generate a TypeSpec program model.
1. The TypeSpec program model is transformed into an RLCModel.
1. The RLCModel is passed to the RLC Common library, which generates the code for the RLC file.

```mermaid
graph TD
A[CADL Files] -->|CADL compiler| B(CADL Program)
B -->|CADL TypeScript emitter| C{RLCModel}
A[TypeSpec Files] -->|TypeSpec compiler| B(TypeSpec Program)
B -->|TypeSpec TypeScript emitter| C{RLCModel}
C --> |RLC common| D(RLC File Content)
D --> |CADL TypeScript emitter| E(JS RLC Code)
D --> |TypeSpec TypeScript emitter| E(JS RLC Code)
```

## RLC generation from Swagger
Expand Down
2 changes: 1 addition & 1 deletion packages/rlc-common/CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Contributing

we have a [interfaces.ts](./src/interfaces.ts) to define the RLCModel And RLCOptions that is export for both Autorest RLC generator and Cadl emitters.
we have a [interfaces.ts](./src/interfaces.ts) to define the RLCModel And RLCOptions that is export for both Autorest RLC generator and TypeSpec emitters.

The generated Typescript/Javascript Rest Level Client has the following structure:

Expand Down
2 changes: 1 addition & 1 deletion packages/rlc-common/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# Introduction

In this library, we abstract the common generation logic for RLC generation from both swaggers and cadl. In this way, the autorest.typescript part will only need to transform the code model into RLCModel, and the cadl emitter will only need to transform the cadl program into RLCModel, then the rlc-common library will take the rest generation part to build the file content of from the RLCModel.
In this library, we abstract the common generation logic for RLC generation from both swaggers and typespec. In this way, the autorest.typescript part will only need to transform the code model into RLCModel, and the TypeSpec emitter will only need to transform the TypeSpec program into RLCModel, then the rlc-common library will take the rest generation part to build the file content of from the RLCModel.
4 changes: 2 additions & 2 deletions packages/rlc-common/src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,13 @@ export interface RLCOptions {
generateSample?: boolean;
azureSdkForJs?: boolean;
azureOutputDirectory?: string;
isCadlTest?: boolean;
isTypeSpecTest?: boolean;
title?: string;
dependencyInfo?: DependencyInfo;
productDocLink?: string;
serviceInfo?: ServiceInfo;
azureArm?: boolean;
sourceFrom?: "Cadl" | "Swagger";
sourceFrom?: "TypeSpec" | "Swagger";
isModularLibrary?: boolean;
enableOperationGroup?: boolean;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/rlc-common/src/metadata/buildPackageFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ function restLevelPackage(model: RLCModel, hasSamplesGenerated: boolean) {
generateSample,
azureOutputDirectory,
azureSdkForJs,
isCadlTest,
isTypeSpecTest,
sourceFrom,
multiClient,
batch
Expand Down Expand Up @@ -228,7 +228,7 @@ function restLevelPackage(model: RLCModel, hasSamplesGenerated: boolean) {
packageInfo.devDependencies["uglify-js"] = "^3.4.9";
}

if (isCadlTest) {
if (isTypeSpecTest) {
packageInfo["type"] = "module";
}

Expand Down
Loading

0 comments on commit 62cd637

Please sign in to comment.