From 8d72f68679ff6599469329c7d847ca4792f56f75 Mon Sep 17 00:00:00 2001 From: "FAREAST\\chunyu" Date: Wed, 18 Dec 2024 16:19:48 +0800 Subject: [PATCH] refine text --- packages/typespec-vscode/package.json | 104 +++++++------------ packages/typespec-vscode/src/const.ts | 8 -- packages/typespec-vscode/src/emit/emit.ts | 78 +++++++------- packages/typespec-vscode/src/emit/emitter.ts | 10 +- packages/typespec-vscode/src/extension.ts | 4 +- packages/typespec-vscode/src/types.ts | 6 +- 6 files changed, 85 insertions(+), 125 deletions(-) diff --git a/packages/typespec-vscode/package.json b/packages/typespec-vscode/package.json index d1c8f91131..d789c2cca2 100644 --- a/packages/typespec-vscode/package.json +++ b/packages/typespec-vscode/package.json @@ -109,7 +109,7 @@ "default": "off", "description": "Define whether/how the TypeSpec language server should send traces to client. For the traces to show properly in vscode Output, make sure 'Log Level' is also set to 'Trace' so that they won't be filtered at client side, which can be set through 'Developer: Set Log Level...' command." }, - "typespec.client.emitter": { + "typespec.emitters": { "scope": "window", "type": "array", "items": { @@ -122,99 +122,65 @@ "Java", "JavaScript", "Python", - "Go" + "Go", + "OpenAPI3", + "ProtoBuf", + "JsonSchema" ], "description": "Define the language the emitter will emit." }, "package": { "type": "string", "description": "Define the emitter package.\n\nExample (with version): @typespec/http-client-csharp@1.0.0\n\nExample (without version): @typespec/http-client-csharp" + }, + "kind": { + "type": "string", + "enum": [ + "client", + "server", + "schema" + ], + "description": "Define the emitter kind." } } }, "default": [ { "language": "DotNet", - "package": "@typespec/http-client-csharp" + "package": "@typespec/http-client-csharp", + "kind": "client" }, { "language": "Java", - "package": "@typespec/http-client-java" + "package": "@typespec/http-client-java", + "kind": "client" }, { "language": "JavaScript", - "package": "@azure-tools/typespec-ts" + "package": "@azure-tools/typespec-ts", + "kind": "client" }, { "language": "Python", - "package": "@typespec/http-client-python" - } - ], - "description": "Define the emitter for a language client sdk generation." - }, - "typespec.server.emitter": { - "scope": "window", - "type": "array", - "items": { - "type": "object", - "properties": { - "language": { - "type": "string", - "enum": [ - "DotNet", - "Java", - "JavaScript", - "Python", - "Go" - ], - "description": "Define the language for the emitter." - }, - "package": { - "type": "string", - "description": "Define the emitter package.\n\nExample (with version): @typespec/http-server-csharp@1.0.0\n\nExample (without version): @typespec/http-server-csharp" - } - } - }, - "default": [ + "package": "@typespec/http-client-python", + "kind": "client" + }, { "language": "DotNet", - "package": "@typespec/http-server-csharp" + "package": "@typespec/http-server-csharp", + "kind": "server" }, { "language": "JavaScript", - "package": "@typespec/http-server-javascript" - } - ], - "description": "Define the emitter for server code generation." - }, - "typespec.schema.emitter": { - "scope": "window", - "type": "array", - "items": { - "type": "object", - "properties": { - "language": { - "type": "string", - "enum": [ - "OpenAPI3", - "ProtoBuf", - "JsonSchema" - ], - "description": "Define the language for the emitter." - }, - "package": { - "type": "string", - "description": "Define the emitter package.\n\nExample (with version): @typespec/openapi3@1.0.0\n\nExample (without version): @typespec/openapi3" - } - } - }, - "default": [ + "package": "@typespec/http-server-javascript", + "kind": "server" + }, { "language": "OpenAPI3", - "package": "@typespec/openapi3" + "package": "@typespec/openapi3", + "kind": "schema" } - ], - "description": "Define the emitter for a schema." + ] } } } @@ -249,8 +215,8 @@ "category": "TypeSpec" }, { - "command": "typespec.emit", - "title": "TypeSpec: Emit Code", + "command": "typespec.generate", + "title": "Generate from TypeSpec", "category": "TypeSpec" }, { @@ -267,14 +233,14 @@ "menus": { "explorer/context": [ { - "command": "typespec.emit", + "command": "typespec.generate", "when": "explorerResourceIsFolder || resourceLangId == typespec", "group": "code_generation" } ], "editor/context": [ { - "command": "typespec.emit", + "command": "typespec.generate", "when": "resourceLangId == typespec", "group": "code_generation" } diff --git a/packages/typespec-vscode/src/const.ts b/packages/typespec-vscode/src/const.ts index 9216ab4e34..e7b898f945 100644 --- a/packages/typespec-vscode/src/const.ts +++ b/packages/typespec-vscode/src/const.ts @@ -1,9 +1 @@ -import { SettingName } from "./types.js"; - -export const EmitterSettingName: Record = { - client: SettingName.ClientEmitter, - server: SettingName.ServerEmitter, - schema: SettingName.SchemaEmitter, -}; - export const StartFileName = "main.tsp"; diff --git a/packages/typespec-vscode/src/emit/emit.ts b/packages/typespec-vscode/src/emit/emit.ts index f55776b28e..937417871f 100644 --- a/packages/typespec-vscode/src/emit/emit.ts +++ b/packages/typespec-vscode/src/emit/emit.ts @@ -24,7 +24,7 @@ export async function doEmit( ) { if (!mainTspFile || !(await isFile(mainTspFile))) { logger.info( - "Invalid typespec project. There is no main tsp file in the project. Emit canceled.", + "Invalid typespec project. There is no main tsp file in the project. Generating Cancelled.", [], { showOutput: false, showPopup: true, progress: overallProgress }, ); @@ -50,16 +50,16 @@ export async function doEmit( const all = [...registerEmitters].map((e) => toQuickPickItem(e)); const selectedEmitter = await vscode.window.showQuickPick(all, { - title: "Select the Language", + title: "Select a Language", canPickMany: false, placeHolder: "Pick a Language", ignoreFocusOut: true, }); if (!selectedEmitter) { - logger.info("No emitters selected. Emit canceled.", [], { + logger.info("No emitter selected. Generating Cancelled.", [], { showOutput: false, - showPopup: true, + showPopup: false, progress: overallProgress, }); return; @@ -77,7 +77,7 @@ export async function doEmit( ) .then((selection) => { if (selection === "OK") { - logger.info("Emit canceled.", [], { + logger.info("Generating Cancelled.", [], { showOutput: false, showPopup: true, progress: overallProgress, @@ -113,20 +113,20 @@ export async function doEmit( const selected = await vscode.window.showQuickPick(Object.values(options), { canPickMany: false, ignoreFocusOut: true, - placeHolder: `Package '${selectedEmitter.package}' needs to be upgraded for emitting`, - title: `TypeSpec Emit...`, + placeHolder: `Package '${selectedEmitter.package}' needs to be upgraded for generating`, + title: `TypeSpec Generating...`, }); if (selected === options.ok) { packagesToInstall.push(`${selectedEmitter.package}@${version}`); } else if (selected === options.ignore) { - logger.info(`Ignore upgrading emitter ${selectedEmitter.package} for emitting`, [], { + logger.info(`Ignore upgrading emitter ${selectedEmitter.package} for generating`, [], { showOutput: false, showPopup: false, progress: overallProgress, }); } else { logger.info( - `Need to manually install the package ${selectedEmitter.package}@${version}. Emit canceled.`, + `Need to manually install the package ${selectedEmitter.package}@${version}. Generating Cancelled.`, [], { showOutput: false, @@ -157,14 +157,14 @@ export async function doEmit( const selected = await vscode.window.showQuickPick(Object.values(options), { canPickMany: false, ignoreFocusOut: true, - placeHolder: `Package '${dependency}' needs to be upgraded for emitting`, - title: `TypeSpec Emit...`, + placeHolder: `Package '${dependency}' needs to be upgraded for generating`, + title: `TypeSpec Generate...`, }); if (selected === options.ok) { packagesToInstall.push(dependency); } else { logger.info( - `Need to manually install the dependency package ${dependency}@latest. Emit canceled.`, + `Need to manually install the dependency package ${dependency}@latest. Generating Cancelled.`, [], { showOutput: false, @@ -210,7 +210,7 @@ export async function doEmit( } /* emit */ - logger.info("Emit code ...", [], { + logger.info("Generating ...", [], { showOutput: false, showPopup: false, progress: overallProgress, @@ -218,11 +218,15 @@ export async function doEmit( const cli = await resolveTypeSpecCli(baseDir); if (!cli) { - logger.error("Cannot find TypeSpec CLI. Please install @typespec/compiler. Cancel emit.", [], { - showOutput: true, - showPopup: true, - progress: overallProgress, - }); + logger.error( + "Cannot find TypeSpec CLI. Please install @typespec/compiler. Generating Cancelled.", + [], + { + showOutput: true, + showPopup: true, + progress: overallProgress, + }, + ); return; } const outputDir = path.resolve(baseDir, selectedEmitter.emitterKind, selectedEmitter.language); @@ -242,7 +246,7 @@ export async function doEmit( const compileResult = await compile(cli, mainTspFile, selectedEmitter.package, options); if (compileResult.exitCode !== 0) { logger.error( - `Failed to generate ${selectedEmitter.language} ${selectedEmitter.emitterKind} code.`, + `Failed to generate ${selectedEmitter.emitterKind} code for ${selectedEmitter.language}.`, [], { showOutput: true, @@ -252,7 +256,7 @@ export async function doEmit( ); } else { logger.info( - `complete generating ${selectedEmitter.language} ${selectedEmitter.emitterKind} code.`, + `Generating ${selectedEmitter.emitterKind} code for ${selectedEmitter.language}...Succeeded`, [], { showOutput: true, @@ -263,7 +267,7 @@ export async function doEmit( } } catch (err) { logger.error( - `Exception occurred when generating ${selectedEmitter.language} ${selectedEmitter.emitterKind} code.`, + `Exception occurred when generating ${selectedEmitter.emitterKind} code for ${selectedEmitter.language}.`, [err], { showOutput: true, @@ -286,7 +290,7 @@ export async function emitCode( const targetPathes = await TraverseMainTspFileInWorkspace(); logger.info(`Found ${targetPathes.length} ${StartFileName} files`); if (targetPathes.length === 0) { - logger.info("No main tsp file found. Emit canceled.", [], { + logger.info("No main tsp file found. Generating Cancelled.", [], { showOutput: false, showPopup: true, progress: overallProgress, @@ -307,13 +311,13 @@ export async function emitCode( toProjectPickItem(filePath), ); const selectedProjectFile = await vscode.window.showQuickPick(typespecProjectQuickPickItems, { - title: "Select TypeSpec Project", + title: "Select a TypeSpec Project", canPickMany: false, placeHolder: "Pick a project", ignoreFocusOut: true, }); if (!selectedProjectFile) { - logger.info("No project selected. Emit canceled.", [], { + logger.info("No project selected. Generating Cancelled.", [], { showOutput: false, showPopup: true, progress: overallProgress, @@ -340,34 +344,34 @@ export async function emitCode( const codesToEmit = [ { - label: "Client SDK", - detail: "Generate client SDK library from typespec.", + label: "Protocol Schema", + detail: "Generating Protocol schema (OpenAPI for example) from TypeSpec", + iconPath: Uri.file(context.asAbsolutePath(`./icons/schema.svg`)), + emitterKind: EmitterKind.Schema, + }, + { + label: "Client Code", + detail: "Generating Client Code from TypeSpec.", iconPath: Uri.file(context.asAbsolutePath(`./icons/sdk.svg`)), emitterKind: EmitterKind.Client, }, { - label: "Server Stub", - detail: "Generate server codes from typespec", + label: " Server Stub", + detail: "Generating Server Stub from TypeSpec", iconPath: Uri.file(context.asAbsolutePath(`./icons/serverstub.svg`)), emitterKind: EmitterKind.Server, }, - { - label: "Protocol Schema", - detail: "Generate protocol schema (e.g. OpenAPI, Protobuf) from typespec", - iconPath: Uri.file(context.asAbsolutePath(`./icons/schema.svg`)), - emitterKind: EmitterKind.Schema, - }, ]; const codeType = await vscode.window.showQuickPick(codesToEmit, { - title: "Emit Code", + title: "Select an Emitter Type", canPickMany: false, placeHolder: "Select an option", ignoreFocusOut: true, }); if (!codeType) { - logger.info("No emitters selected. Emit canceled.", [], { + logger.info("No emitters selected. Generating Cancelled.", [], { showOutput: false, - showPopup: true, + showPopup: false, progress: overallProgress, }); return; diff --git a/packages/typespec-vscode/src/emit/emitter.ts b/packages/typespec-vscode/src/emit/emitter.ts index 555bd03157..bb6a760327 100644 --- a/packages/typespec-vscode/src/emit/emitter.ts +++ b/packages/typespec-vscode/src/emit/emitter.ts @@ -1,5 +1,5 @@ import vscode from "vscode"; -import { EmitterSettingName } from "../const.js"; +import { SettingName } from "../types.js"; export enum EmitterKind { Schema = "schema", @@ -35,8 +35,8 @@ function getEmitter(kind: EmitterKind, emitter: Emitter): Emitter { } export function getRegisterEmitters(kind: EmitterKind): ReadonlyArray { - const emitters: ReadonlyArray = - extensionConfig.get(EmitterSettingName[kind] ?? "") ?? []; - - return emitters.map((emitter) => getEmitter(kind, emitter)); + const emitters: ReadonlyArray = extensionConfig.get(SettingName.Emitters) ?? []; + return emitters + .filter((emitter) => emitter.kind === kind) + .map((emitter) => getEmitter(kind, emitter)); } diff --git a/packages/typespec-vscode/src/extension.ts b/packages/typespec-vscode/src/extension.ts index d14b3a02fe..f5882c3324 100644 --- a/packages/typespec-vscode/src/extension.ts +++ b/packages/typespec-vscode/src/extension.ts @@ -47,11 +47,11 @@ export async function activate(context: ExtensionContext) { /* emit command. */ context.subscriptions.push( - commands.registerCommand(CommandName.EmitCode, async (uri: vscode.Uri) => { + commands.registerCommand(CommandName.Generate, async (uri: vscode.Uri) => { await vscode.window.withProgress( { location: vscode.ProgressLocation.Window, - title: "Emit Code...", + title: "Generate from TypeSpec...", cancellable: false, }, async (progress) => await emitCode(context, uri, progress), diff --git a/packages/typespec-vscode/src/types.ts b/packages/typespec-vscode/src/types.ts index 3300121ff0..6c748d6be8 100644 --- a/packages/typespec-vscode/src/types.ts +++ b/packages/typespec-vscode/src/types.ts @@ -1,9 +1,7 @@ export const enum SettingName { TspServerPath = "typespec.tsp-server.path", InitTemplatesUrls = "typespec.initTemplatesUrls", - ClientEmitter = "typespec.client.emitter", - ServerEmitter = "typespec.server.emitter", - SchemaEmitter = "typespec.schema.emitter", + Emitters = "typespec.emitters", } export const enum CommandName { @@ -12,7 +10,7 @@ export const enum CommandName { InstallGlobalCompilerCli = "typespec.installGlobalCompilerCli", CreateProject = "typespec.createProject", OpenUrl = "typespec.openUrl", - EmitCode = "typespec.emit", + Generate = "typespec.generate", } export interface InstallGlobalCliCommandArgs {