Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate Code from TypeSpec in VSCode #5312

Open
wants to merge 53 commits into
base: main
Choose a base branch
from

Conversation

chunyu3
Copy link
Contributor

@chunyu3 chunyu3 commented Dec 10, 2024

Fix #5025

Integrate Code Generation in VSCode extension

@chunyu3 chunyu3 marked this pull request as draft December 10, 2024 01:48
@microsoft-github-policy-service microsoft-github-policy-service bot added the ide Issues for VS, VSCode, Monaco, etc. label Dec 10, 2024
@azure-sdk
Copy link
Collaborator

azure-sdk commented Dec 10, 2024

All changed packages have been documented.

  • typespec-vscode
Show changes

typespec-vscode - feature ✏️

integrate client SDK generation

);
return;
}
const outputDir = path.resolve(baseDir, selectedEmitter.emitterKind, selectedEmitter.language);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

path.resolve? shouldn't be joinPath?

devDependencies = "devDependencies",
}

export interface NpmPackageInfo {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is a NodePackage type in compiler you can use directly

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

VSCode Extension may not library dependent on @typespec/compiler. So we may not import type from compiler.

import logger from "./log/logger.js";
import { ExecOutput, loadModule, spawnExecution, spawnExecutionEvents } from "./utils.js";

export enum InstallationAction {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

usually we prefer to use union to enum. like: InstallationAction = "install" | "upgrade" | "skip" | "cancel". same for below

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I want to use the enum to define the supported action, user don't need to pass in the value. Union defined is to define an type or string literal, it may not well control/guide.

.split("\n")
.forEach((line) => logger.info(line));
};
export const onStdioError = (str: string) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

People can't figure out what these two methods for just by name which looks more like an event name... Why not just use the spawnExecutionAndLogToOutput which would log the stdio/stderr which is just what you want to do here, isn't it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

spawnExecutionAndLogToOutput will log out all the logs in one string. I want to split it one line by one line which will be more clear.

);
};

export async function getEntrypointTspFile(tspPath: string): Promise<string | undefined> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think you need to search parent folder for the main.tsp too. you can refer to getMainFileForDocument in core\server\compiler-service.ts

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need to search parent folder. This function is to retrieve out all the tsp project (main.tsp) under a specified folder.

try {
const files = await readdir(baseDir);
if (files && files.length === 1 && files[0].endsWith(".tsp")) {
return path.resolve(baseDir, files[0]);
Copy link
Contributor

@RodgeFu RodgeFu Dec 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's better to report error than pick a random file which wouldn't right in most case

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case, there only one file, and no main.tsp, it can be the entrypoint file.

import vscode from "vscode";
import { EmitterKind } from "./emitter.js";

export interface EmitQuickPickItem extends vscode.QuickPickItem {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

feels you dont need to define this type explicitly either.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The EmitQuickPickItem has several extra properties, it is not simple one, so I think we can define an explicit type here to clear understanding and good readability in future maintenance.

const dependenciesToInstall = await npmUtil.ensureNpmPackageDependencyToUpgrade(
selectedEmitter.package,
version,
npmDependencyType.peerDependencies,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dont we need to check dep and devdep?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Current all the emitters has peer dependency, current we only need check peerDependencies. In future, when we support customized emitter which may not follow our rule to define peerDependencies, we will add more dependency check.

@azure-sdk
Copy link
Collaborator

You can try these changes here

🛝 Playground 🌐 Website 📚 Next docs 🛝 VSCode Extension

@microsoft microsoft deleted a comment from azure-sdk Dec 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ide Issues for VS, VSCode, Monaco, etc.
Projects
None yet
5 participants