Skip to content

Commit

Permalink
[tcgc] add example model in tcgc (#1076)
Browse files Browse the repository at this point in the history
  • Loading branch information
tadelesh authored Jul 24, 2024
1 parent a242706 commit 374c373
Show file tree
Hide file tree
Showing 57 changed files with 4,421 additions and 3,014 deletions.
7 changes: 7 additions & 0 deletions .chronus/changes/tcgc_sample-2024-5-24-18-19-39.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: feature
packages:
- "@azure-tools/typespec-client-generator-core"
---

add example types support
7 changes: 7 additions & 0 deletions .chronus/changes/tcgc_sample-2024-5-25-18-26-42.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: internal
packages:
- "@azure-tools/typespec-autorest"
---

create tcgc context change to async
7 changes: 7 additions & 0 deletions .chronus/changes/tcgc_sample-2024-5-26-10-23-7.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: internal
packages:
- "@azure-tools/typespec-autorest-canonical"
---

adopt tcgc breaking
5 changes: 4 additions & 1 deletion packages/typespec-autorest-canonical/src/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ interface ResolvedAutorestCanonicalEmitterOptions extends AutorestDocumentEmitte

export async function $onEmit(context: EmitContext<AutorestCanonicalEmitterOptions>) {
const resolvedOptions = { ...defaultOptions, ...context.options };
const tcgcSdkContext = createSdkContext(context, "@azure-tools/typespec-autorest-canonical");
const tcgcSdkContext = await createSdkContext(
context,
"@azure-tools/typespec-autorest-canonical"
);
const armTypesDir = interpolatePath(
resolvedOptions["arm-types-dir"] ?? "{project-root}/../../common-types/resource-management",
{
Expand Down
2 changes: 1 addition & 1 deletion packages/typespec-autorest/src/emit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export async function getAllServicesAtAllVersions(
program: Program,
options: ResolvedAutorestEmitterOptions
): Promise<AutorestServiceRecord[]> {
const tcgcSdkContext = createSdkContext(
const tcgcSdkContext = await createSdkContext(
{ program, options: {} } as any,
"@azure-tools/typespec-autorest",
{
Expand Down
3 changes: 2 additions & 1 deletion packages/typespec-client-generator-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
"@azure-tools/typespec-azure-core": "workspace:~",
"@typespec/compiler": "workspace:~",
"@typespec/http": "workspace:~",
"@typespec/openapi": "workspace:~",
"@typespec/rest": "workspace:~",
"@typespec/versioning": "workspace:~"
},
Expand All @@ -71,10 +72,10 @@
"@typespec/compiler": "workspace:~",
"@typespec/http": "workspace:~",
"@typespec/library-linter": "workspace:~",
"@typespec/openapi": "workspace:~",
"@typespec/prettier-plugin-typespec": "workspace:~",
"@typespec/rest": "workspace:~",
"@typespec/tspd": "workspace:~",
"@typespec/versioning": "workspace:~",
"@typespec/xml": "workspace:~",
"@vitest/coverage-v8": "^1.6.0",
"@vitest/ui": "^1.6.0",
Expand Down
27 changes: 16 additions & 11 deletions packages/typespec-client-generator-core/src/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
import { isHeader } from "@typespec/http";
import { buildVersionProjections, getVersions } from "@typespec/versioning";
import { defaultDecoratorsAllowList } from "./configs.js";
import { handleClientExamples } from "./example.js";
import {
AccessFlags,
LanguageScopes,
Expand All @@ -41,7 +42,13 @@ import {
SdkServiceOperation,
UsageFlags,
} from "./interfaces.js";
import { AllScopes, TCGCContext, clientNameKey, parseEmitterName } from "./internal-utils.js";
import {
AllScopes,
TCGCContext,
clientNameKey,
getValidApiVersion,
parseEmitterName,
} from "./internal-utils.js";
import { createStateSymbol, reportDiagnostic } from "./lib.js";
import { getSdkPackage } from "./package.js";
import { getLibraryName } from "./public-utils.js";
Expand Down Expand Up @@ -230,14 +237,7 @@ function serviceVersioningProjection(context: TCGCContext, client: SdkClient) {
?.getVersions()
.map((x) => x.value);
if (!allApiVersions) return;
let apiVersion = context.apiVersion;
if (
apiVersion === "latest" ||
apiVersion === undefined ||
!allApiVersions.includes(apiVersion)
) {
apiVersion = allApiVersions[allApiVersions.length - 1];
}
const apiVersion = getValidApiVersion(context, allApiVersions);
if (apiVersion === undefined) return;
const versionProjections = buildVersionProjections(context.program, client.service).filter(
(v) => apiVersion === v.version
Expand Down Expand Up @@ -599,14 +599,14 @@ export interface CreateSdkContextOptions {
additionalDecorators?: string[];
}

export function createSdkContext<
export async function createSdkContext<
TOptions extends Record<string, any> = SdkEmitterOptions,
TServiceOperation extends SdkServiceOperation = SdkHttpOperation,
>(
context: EmitContext<TOptions>,
emitterName?: string,
options?: CreateSdkContextOptions
): SdkContext<TOptions, TServiceOperation> {
): Promise<SdkContext<TOptions, TServiceOperation>> {
const diagnostics = createDiagnosticCollector();
const protocolOptions = true; // context.program.getLibraryOptions("generate-protocol-methods");
const convenienceOptions = true; // context.program.getLibraryOptions("generate-convenience-methods");
Expand All @@ -631,10 +631,15 @@ export function createSdkContext<
__namespaceToApiVersionParameter: new Map(),
__tspTypeToApiVersions: new Map(),
__namespaceToApiVersionClientDefaultValue: new Map(),
examplesDirectory: context.options["examples-directory"],
decoratorsAllowList: [...defaultDecoratorsAllowList, ...(options?.additionalDecorators ?? [])],
previewStringRegex: options?.versioning?.previewStringRegex || /-preview$/,
};
sdkContext.sdkPackage = getSdkPackage(sdkContext);
for (const client of sdkContext.sdkPackage.clients) {
diagnostics.pipe(await handleClientExamples(sdkContext, client));
}

if (sdkContext.diagnostics) {
sdkContext.diagnostics = sdkContext.diagnostics.concat(
sdkContext.sdkPackage.diagnostics // eslint-disable-line deprecation/deprecation
Expand Down
Loading

0 comments on commit 374c373

Please sign in to comment.