Skip to content

Commit

Permalink
refine text
Browse files Browse the repository at this point in the history
  • Loading branch information
chunyu3 committed Dec 27, 2024
1 parent db5d908 commit 77452a0
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 49 deletions.
7 changes: 7 additions & 0 deletions packages/typespec-vscode/icons/openapi.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
29 changes: 0 additions & 29 deletions packages/typespec-vscode/icons/schema.svg

This file was deleted.

21 changes: 16 additions & 5 deletions packages/typespec-vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@
"language": {
"type": "string",
"enum": [
"DotNet",
".NET",
"Java",
"JavaScript",
"Python",
Expand All @@ -133,52 +133,63 @@
"type": "string",
"description": "Define the emitter package.\n\nExample (with version): @typespec/[email protected]\n\nExample (without version): @typespec/http-client-csharp"
},
"sourceRepo": {
"type": "string",
"description": "Define the source repository of the emitter package."
},
"kind": {
"type": "string",
"enum": [
"client",
"server",
"schema"
"openapi"
],
"description": "Define the emitter kind."
}
}
},
"default": [
{
"language": "DotNet",
"language": ".NET",
"package": "@typespec/http-client-csharp",
"sourceRepo": "https://github.com/microsoft/typespec/tree/main/packages/http-client-csharp",
"kind": "client"
},
{
"language": "Java",
"package": "@typespec/http-client-java",
"sourceRepo": "https://github.com/microsoft/typespec/tree/main/packages/http-client-java ",
"kind": "client"
},
{
"language": "JavaScript",
"package": "@azure-tools/typespec-ts",
"sourceRepo": "https://github.com/Azure/autorest.typescript/tree/main/packages/typespec-ts",
"kind": "client"
},
{
"language": "Python",
"package": "@typespec/http-client-python",
"sourceRepo": "https://github.com/microsoft/typespec/tree/main/packages/http-client-python",
"kind": "client"
},
{
"language": "DotNet",
"language": ".NET",
"package": "@typespec/http-server-csharp",
"sourceRepo": "https://github.com/microsoft/typespec/tree/main/packages/http-server-csharp",
"kind": "server"
},
{
"language": "JavaScript",
"package": "@typespec/http-server-javascript",
"sourceRepo": "https://github.com/microsoft/typespec/tree/main/packages/http-server-javascript",
"kind": "server"
},
{
"language": "OpenAPI3",
"package": "@typespec/openapi3",
"kind": "schema"
"sourceRepo": "https://github.com/microsoft/typespec/tree/main/packages/openapi3",
"kind": "openapi"
}
]
}
Expand Down
30 changes: 21 additions & 9 deletions packages/typespec-vscode/src/vscode-cmd/emit-code/emit-code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { EmitQuickPickItem } from "./emit-quick-pick-item.js";
import {
Emitter,
EmitterKind,
getLanguageAlias,
getRegisterEmitters,
getRegisterEmitterTypes,
PreDefinedEmitterPickItems,
Expand All @@ -35,30 +36,33 @@ async function doEmit(context: vscode.ExtensionContext, mainTspFile: string, kin
const baseDir = getDirectoryPath(mainTspFile);

const toQuickPickItem = (e: Emitter): EmitQuickPickItem => {
const moreDetail = e.sourceRepo ? `[**More Detail**](${e.sourceRepo})` : "";
return {
language: e.language,
package: e.package,
emitterKind: e.kind,
label: e.language,
detail: `Generate ${e.language} ${e.kind} code from ${e.package}`,
detail: `Generate ${e.kind} code for ${e.language} by TypeSpec library ${e.package}.${moreDetail}`,
picked: false,
fromConfig: false,
iconPath: Uri.file(context.asAbsolutePath(`./icons/${e.language.toLowerCase()}.svg`)),
iconPath: Uri.file(
context.asAbsolutePath(`./icons/${getLanguageAlias(e.language).toLowerCase()}.svg`),
),
};
};

const registerEmitters = getRegisterEmitters(kind);
const all = [...registerEmitters].map((e) => toQuickPickItem(e));

const selectedEmitter = await vscode.window.showQuickPick<EmitQuickPickItem>(all, {
title: "Select a Language",
title: `Select a Language for ${kind} code generation`,
canPickMany: false,
placeHolder: "Pick a Language",
placeHolder: `Select a Language for ${kind} code generation`,
ignoreFocusOut: true,
});

if (!selectedEmitter) {
logger.info("No emitter selected. Generating Cancelled.");
logger.info("No language selected. Generating Cancelled.");
return;
}

Expand Down Expand Up @@ -173,7 +177,11 @@ async function doEmit(context: vscode.ExtensionContext, mainTspFile: string, kin
);
return;
}
const outputDir = path.join(baseDir, selectedEmitter.emitterKind, selectedEmitter.language);
const outputDir = path.join(
baseDir,
selectedEmitter.emitterKind,
getLanguageAlias(selectedEmitter.language),
);

const options: Record<string, string> = {};
options["emitter-output-dir"] = outputDir;
Expand Down Expand Up @@ -238,7 +246,7 @@ export async function emitCode(context: vscode.ExtensionContext, uri: vscode.Uri
} else {
const toProjectPickItem = (filePath: string): any => {
return {
label: filePath,
label: `Project: ${filePath}`,
path: filePath,
iconPath: {
light: Uri.file(context.asAbsolutePath(`./icons/tsp-file.light.svg`)),
Expand All @@ -250,7 +258,7 @@ export async function emitCode(context: vscode.ExtensionContext, uri: vscode.Uri
toProjectPickItem(filePath),
);
const selectedProjectFile = await vscode.window.showQuickPick(typespecProjectQuickPickItems, {
title: "Select a TypeSpec Project",
title: "Select a Project",
canPickMany: false,
placeHolder: "Pick a project",
ignoreFocusOut: true,
Expand Down Expand Up @@ -280,9 +288,13 @@ export async function emitCode(context: vscode.ExtensionContext, uri: vscode.Uri

const emitterKinds = getRegisterEmitterTypes();
const toEmitterTypeQuickPickItem = (kind: EmitterKind): any => {
const registerEmitters = getRegisterEmitters(kind);
const supportedLanguages = registerEmitters.map((e) => e.language).join(", ");
return {
label: PreDefinedEmitterPickItems[kind]?.label ?? kind,
detail: PreDefinedEmitterPickItems[kind]?.detail ?? `Generate ${kind} code from TypeSpec`,
detail:
PreDefinedEmitterPickItems[kind]?.detail ??
`Generate ${kind} code from TypeSpec files. Supported languages are ${supportedLanguages}.`,
emitterKind: kind,
iconPath: Uri.file(context.asAbsolutePath(`./icons/${kind.toLowerCase()}.svg`)),
};
Expand Down
24 changes: 18 additions & 6 deletions packages/typespec-vscode/src/vscode-cmd/emit-code/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import logger from "../../log/logger.js";
import { SettingName } from "../../types.js";

export enum EmitterKind {
Schema = "schema",
Schema = "openapi",
Client = "client",
Server = "server",
}
Expand All @@ -12,21 +12,23 @@ export interface Emitter {
language: string;
package: string;
version?: string;
sourceRepo?: string;
kind: EmitterKind;
}

export const PreDefinedEmitterPickItems: Record<string, vscode.QuickPickItem> = {
schema: {
label: "OpenAPI",
detail: "Generating OpenAPI from TypeSpec",
openapi: {
label: "OpenAPI Document",
detail: "Generating OpenAPI3 Document from TypeSpec files.",
},
client: {
label: "Client Code",
detail: "Generating Client Code from TypeSpec.",
detail:
"Generating Client Code from TypeSpec files. Supported languages are .NET, Python, Java, JavaScript.",
},
server: {
label: "<PREVIEW> Server Stub",
detail: "Generating Server Stub from TypeSpec",
detail: "Generating Server Stub from TypeSpec files. Supported languages are .NET, JavaScript.",
},
};

Expand All @@ -49,6 +51,7 @@ function getEmitter(kind: EmitterKind, emitter: Emitter): Emitter | undefined {
language: emitter.language,
package: packageName,
version: version,
sourceRepo: emitter.sourceRepo,
kind: kind,
};
}
Expand All @@ -69,3 +72,12 @@ export function getRegisterEmitterTypes(): ReadonlyArray<EmitterKind> {
extensionConfig.get(SettingName.GenerateCodeEmitters) ?? [];
return Array.from(new Set(emitters.map((emitter) => emitter.kind)));
}

const languageAlias: Record<string, string> = {
".net": "dotnet",
};

/*return the alias of the language if it exists, otherwise return the original language. */
export function getLanguageAlias(language: string): string {
return languageAlias[language] ?? language;
}

0 comments on commit 77452a0

Please sign in to comment.