From 92d67590a458801926b9c9dafe59fb63daead1ce Mon Sep 17 00:00:00 2001 From: "FAREAST\\chunyu" Date: Thu, 19 Dec 2024 13:19:25 +0800 Subject: [PATCH] resolve comments --- packages/typespec-vscode/src/emit/emit.ts | 48 +++++-------------- packages/typespec-vscode/src/emit/emitter.ts | 3 +- packages/typespec-vscode/src/extension.ts | 2 +- .../typespec-vscode/src/typespec-utils.ts | 13 ++--- packages/typespec-vscode/src/utils.ts | 5 -- 5 files changed, 21 insertions(+), 50 deletions(-) diff --git a/packages/typespec-vscode/src/emit/emit.ts b/packages/typespec-vscode/src/emit/emit.ts index a01f7aba89..b8a4886b78 100644 --- a/packages/typespec-vscode/src/emit/emit.ts +++ b/packages/typespec-vscode/src/emit/emit.ts @@ -1,4 +1,3 @@ -// import { TypeSpecConfig } from "@typespec/compiler"; import path, { dirname } from "path"; import vscode, { Uri } from "vscode"; import { Executable } from "vscode-languageclient/node.js"; @@ -6,10 +5,10 @@ import { StartFileName } from "../const.js"; import logger from "../log/logger.js"; import { InstallationAction, npmDependencyType, NpmUtil } from "../npm-utils.js"; import { - getMainTspFile, + getEntrypointTspFile, + onStdioError, + onStdioOut, resolveTypeSpecCli, - toError, - toOutput, TraverseMainTspFileInWorkspace, } from "../typespec-utils.js"; import { ExecOutput, isFile, spawnExecution } from "../utils.js"; @@ -20,13 +19,12 @@ export async function doEmit( context: vscode.ExtensionContext, mainTspFile: string, kind: EmitterKind, - overallProgress: vscode.Progress<{ message?: string; increment?: number }>, ) { if (!mainTspFile || !(await isFile(mainTspFile))) { logger.info( "Invalid typespec project. There is no main tsp file in the project. Generating Cancelled.", [], - { showOutput: false, showPopup: true, progress: overallProgress }, + { showOutput: false, showPopup: true }, ); return; } @@ -60,7 +58,6 @@ export async function doEmit( logger.info("No emitter selected. Generating Cancelled.", [], { showOutput: false, showPopup: false, - progress: overallProgress, }); return; } @@ -79,8 +76,7 @@ export async function doEmit( if (selection === "OK") { logger.info("Generating Cancelled.", [], { showOutput: false, - showPopup: true, - progress: overallProgress, + showPopup: false, }); } }); @@ -90,7 +86,6 @@ export async function doEmit( logger.info("npm install...", [], { showOutput: false, showPopup: false, - progress: overallProgress, }); const npmUtil = new NpmUtil(baseDir); @@ -122,7 +117,6 @@ export async function doEmit( logger.info(`Ignore upgrading emitter ${selectedEmitter.package} for generating`, [], { showOutput: false, showPopup: false, - progress: overallProgress, }); } else { logger.info( @@ -131,7 +125,6 @@ export async function doEmit( { showOutput: false, showPopup: true, - progress: overallProgress, }, ); return; @@ -164,18 +157,16 @@ export async function doEmit( logger.info(`Installing ${packagesToInstall.join("\n\n")} under ${baseDir}`, [], { showOutput: true, showPopup: true, - progress: overallProgress, }); try { const npmInstallResult = await npmUtil.npmInstallPackages(packagesToInstall, undefined, { - onStdioOut: toOutput, - onStdioError: toError, + onStdioOut: onStdioOut, + onStdioError: onStdioError, }); if (npmInstallResult.exitCode !== 0) { logger.error(`Error occurred when installing packages.`, [`${npmInstallResult.stderr}`], { showOutput: true, showPopup: true, - progress: overallProgress, }); return; } @@ -184,7 +175,6 @@ export async function doEmit( logger.error(`Exception occurred when installing packages.`, [err], { showOutput: true, showPopup: true, - progress: overallProgress, }); return; } @@ -194,7 +184,6 @@ export async function doEmit( logger.info("Generating ...", [], { showOutput: false, showPopup: false, - progress: overallProgress, }); const cli = await resolveTypeSpecCli(baseDir); @@ -205,7 +194,6 @@ export async function doEmit( { showOutput: true, showPopup: true, - progress: overallProgress, }, ); return; @@ -220,7 +208,6 @@ export async function doEmit( { showOutput: true, showPopup: false, - progress: overallProgress, }, ); try { @@ -232,7 +219,6 @@ export async function doEmit( { showOutput: true, showPopup: true, - progress: overallProgress, }, ); } else { @@ -242,7 +228,6 @@ export async function doEmit( { showOutput: true, showPopup: true, - progress: overallProgress, }, ); } @@ -253,7 +238,6 @@ export async function doEmit( { showOutput: true, showPopup: true, - progress: overallProgress, }, ); } @@ -261,11 +245,7 @@ export async function doEmit( /*TODO: build sdk. */ } -export async function emitCode( - context: vscode.ExtensionContext, - uri: vscode.Uri, - overallProgress: vscode.Progress<{ message?: string; increment?: number }>, -) { +export async function emitCode(context: vscode.ExtensionContext, uri: vscode.Uri) { let tspProjectFile: string = ""; if (!uri) { const targetPathes = await TraverseMainTspFileInWorkspace(); @@ -274,7 +254,6 @@ export async function emitCode( logger.info("No main tsp file found. Generating Cancelled.", [], { showOutput: false, showPopup: true, - progress: overallProgress, }); return; } @@ -301,18 +280,16 @@ export async function emitCode( logger.info("No project selected. Generating Cancelled.", [], { showOutput: false, showPopup: true, - progress: overallProgress, }); return; } tspProjectFile = selectedProjectFile.path; } else { - const tspStartFile = await getMainTspFile(uri.fsPath); + const tspStartFile = await getEntrypointTspFile(uri.fsPath); if (!tspStartFile) { logger.info("No main file. Invalid typespec project.", [], { showOutput: false, showPopup: true, - progress: overallProgress, }); return; } @@ -353,11 +330,10 @@ export async function emitCode( logger.info("No emitter Type selected. Generating Cancelled.", [], { showOutput: false, showPopup: false, - progress: overallProgress, }); return; } - await doEmit(context, tspProjectFile, codeType.emitterKind, overallProgress); + await doEmit(context, tspProjectFile, codeType.emitterKind); } export async function compile( @@ -378,8 +354,8 @@ export async function compile( } return await spawnExecution(cli.command, args, dirname(startFile), { - onStdioOut: toOutput, - onStdioError: toError, + onStdioOut: onStdioOut, + onStdioError: onStdioError, }); } diff --git a/packages/typespec-vscode/src/emit/emitter.ts b/packages/typespec-vscode/src/emit/emitter.ts index bb6a760327..b720c071ca 100644 --- a/packages/typespec-vscode/src/emit/emitter.ts +++ b/packages/typespec-vscode/src/emit/emitter.ts @@ -14,8 +14,6 @@ export interface Emitter { kind: EmitterKind; } -const extensionConfig = vscode.workspace.getConfiguration(); - function getEmitter(kind: EmitterKind, emitter: Emitter): Emitter { const packageFullName: string = emitter.package ?? ""; const index = packageFullName.lastIndexOf("@"); @@ -35,6 +33,7 @@ function getEmitter(kind: EmitterKind, emitter: Emitter): Emitter { } export function getRegisterEmitters(kind: EmitterKind): ReadonlyArray { + const extensionConfig = vscode.workspace.getConfiguration(); const emitters: ReadonlyArray = extensionConfig.get(SettingName.Emitters) ?? []; return emitters .filter((emitter) => emitter.kind === kind) diff --git a/packages/typespec-vscode/src/extension.ts b/packages/typespec-vscode/src/extension.ts index f5882c3324..b2935f225d 100644 --- a/packages/typespec-vscode/src/extension.ts +++ b/packages/typespec-vscode/src/extension.ts @@ -54,7 +54,7 @@ export async function activate(context: ExtensionContext) { title: "Generate from TypeSpec...", cancellable: false, }, - async (progress) => await emitCode(context, uri, progress), + async (progress) => await emitCode(context, uri), ); }), ); diff --git a/packages/typespec-vscode/src/typespec-utils.ts b/packages/typespec-vscode/src/typespec-utils.ts index f111f8b912..2b6ec58d4e 100644 --- a/packages/typespec-vscode/src/typespec-utils.ts +++ b/packages/typespec-vscode/src/typespec-utils.ts @@ -4,15 +4,16 @@ import vscode from "vscode"; import { Executable } from "vscode-languageclient/node.js"; import { StartFileName } from "./const.js"; import logger from "./log/logger.js"; -import { isFile, loadModule, normalizeSlash } from "./utils.js"; +import { normalizeSlashes } from "./path-utils.js"; +import { isFile, loadModule } from "./utils.js"; -export const toOutput = (str: string) => { +export const onStdioOut = (str: string) => { str .trim() .split("\n") .forEach((line) => logger.info(line)); }; -export const toError = (str: string) => { +export const onStdioError = (str: string) => { str .trim() .split("\n") @@ -39,7 +40,7 @@ export async function resolveTypeSpecCli(absolutePath: string): Promise { +export async function getEntrypointTspFile(tspPath: string): Promise { const isFilePath = await isFile(tspPath); const baseDir = isFilePath ? dirname(tspPath) : tspPath; const mainTspFile = path.resolve(baseDir, StartFileName); @@ -72,11 +73,11 @@ export async function getMainTspFile(tspPath: string): Promise uris .filter((uri) => uri.scheme === "file" && !uri.fsPath.includes("node_modules")) - .map((uri) => normalizeSlash(uri.fsPath)), + .map((uri) => normalizeSlashes(uri.fsPath)), ); } diff --git a/packages/typespec-vscode/src/utils.ts b/packages/typespec-vscode/src/utils.ts index ea70042f7d..f5b72bae7d 100644 --- a/packages/typespec-vscode/src/utils.ts +++ b/packages/typespec-vscode/src/utils.ts @@ -7,11 +7,6 @@ import { Executable } from "vscode-languageclient/node.js"; import logger from "./log/logger.js"; import { isUrl } from "./path-utils.js"; -/** normalize / and \\ to / */ -export function normalizeSlash(str: string): string { - return str.replaceAll(/\\/g, "/"); -} - export async function isFile(path: string) { try { const stats = await stat(path);