Skip to content

Commit

Permalink
resolve comments
Browse files Browse the repository at this point in the history
  • Loading branch information
chunyu3 committed Dec 19, 2024
1 parent bae77bb commit 92d6759
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 50 deletions.
48 changes: 12 additions & 36 deletions packages/typespec-vscode/src/emit/emit.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
// import { TypeSpecConfig } from "@typespec/compiler";
import path, { dirname } from "path";
import vscode, { Uri } from "vscode";
import { Executable } from "vscode-languageclient/node.js";
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";
Expand All @@ -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;
}
Expand Down Expand Up @@ -60,7 +58,6 @@ export async function doEmit(
logger.info("No emitter selected. Generating Cancelled.", [], {
showOutput: false,
showPopup: false,
progress: overallProgress,
});
return;
}
Expand All @@ -79,8 +76,7 @@ export async function doEmit(
if (selection === "OK") {
logger.info("Generating Cancelled.", [], {
showOutput: false,
showPopup: true,
progress: overallProgress,
showPopup: false,
});
}
});
Expand All @@ -90,7 +86,6 @@ export async function doEmit(
logger.info("npm install...", [], {
showOutput: false,
showPopup: false,
progress: overallProgress,
});

const npmUtil = new NpmUtil(baseDir);
Expand Down Expand Up @@ -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(
Expand All @@ -131,7 +125,6 @@ export async function doEmit(
{
showOutput: false,
showPopup: true,
progress: overallProgress,
},
);
return;
Expand Down Expand Up @@ -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;
}
Expand All @@ -184,7 +175,6 @@ export async function doEmit(
logger.error(`Exception occurred when installing packages.`, [err], {
showOutput: true,
showPopup: true,
progress: overallProgress,
});
return;
}
Expand All @@ -194,7 +184,6 @@ export async function doEmit(
logger.info("Generating ...", [], {
showOutput: false,
showPopup: false,
progress: overallProgress,
});

const cli = await resolveTypeSpecCli(baseDir);
Expand All @@ -205,7 +194,6 @@ export async function doEmit(
{
showOutput: true,
showPopup: true,
progress: overallProgress,
},
);
return;
Expand All @@ -220,7 +208,6 @@ export async function doEmit(
{
showOutput: true,
showPopup: false,
progress: overallProgress,
},
);
try {
Expand All @@ -232,7 +219,6 @@ export async function doEmit(
{
showOutput: true,
showPopup: true,
progress: overallProgress,
},
);
} else {
Expand All @@ -242,7 +228,6 @@ export async function doEmit(
{
showOutput: true,
showPopup: true,
progress: overallProgress,
},
);
}
Expand All @@ -253,19 +238,14 @@ export async function doEmit(
{
showOutput: true,
showPopup: true,
progress: overallProgress,
},
);
}

/*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();
Expand All @@ -274,7 +254,6 @@ export async function emitCode(
logger.info("No main tsp file found. Generating Cancelled.", [], {
showOutput: false,
showPopup: true,
progress: overallProgress,
});
return;
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -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(
Expand All @@ -378,8 +354,8 @@ export async function compile(
}

return await spawnExecution(cli.command, args, dirname(startFile), {
onStdioOut: toOutput,
onStdioError: toError,
onStdioOut: onStdioOut,
onStdioError: onStdioError,
});
}

Expand Down
3 changes: 1 addition & 2 deletions packages/typespec-vscode/src/emit/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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("@");
Expand All @@ -35,6 +33,7 @@ function getEmitter(kind: EmitterKind, emitter: Emitter): Emitter {
}

export function getRegisterEmitters(kind: EmitterKind): ReadonlyArray<Emitter> {
const extensionConfig = vscode.workspace.getConfiguration();
const emitters: ReadonlyArray<Emitter> = extensionConfig.get(SettingName.Emitters) ?? [];
return emitters
.filter((emitter) => emitter.kind === kind)
Expand Down
2 changes: 1 addition & 1 deletion packages/typespec-vscode/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
);
}),
);
Expand Down
13 changes: 7 additions & 6 deletions packages/typespec-vscode/src/typespec-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -39,7 +40,7 @@ export async function resolveTypeSpecCli(absolutePath: string): Promise<Executab
return undefined;
}

export async function getMainTspFile(tspPath: string): Promise<string | undefined> {
export async function getEntrypointTspFile(tspPath: string): Promise<string | undefined> {
const isFilePath = await isFile(tspPath);
const baseDir = isFilePath ? dirname(tspPath) : tspPath;
const mainTspFile = path.resolve(baseDir, StartFileName);
Expand Down Expand Up @@ -72,11 +73,11 @@ export async function getMainTspFile(tspPath: string): Promise<string | undefine
}

export async function TraverseMainTspFileInWorkspace() {
return await vscode.workspace
return vscode.workspace
.findFiles(`**/${StartFileName}`, "**/node_modules/**")
.then((uris) =>
uris
.filter((uri) => uri.scheme === "file" && !uri.fsPath.includes("node_modules"))
.map((uri) => normalizeSlash(uri.fsPath)),
.map((uri) => normalizeSlashes(uri.fsPath)),
);
}
5 changes: 0 additions & 5 deletions packages/typespec-vscode/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 92d6759

Please sign in to comment.