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 45 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

@chunyu3 chunyu3 force-pushed the generateSDK branch 4 times, most recently from 2f0fc43 to b592194 Compare December 19, 2024 09:20
Copy link
Member

Choose a reason for hiding this comment

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

Can this wait until I'm back before merging so we can discuss a little more what we want. I do not think this is a good feature to add in this form. The argument for not requiring to learn commands to generate is I feel invalid as you still need to know the language and install anything to get the rest working.

If this instead was just providing a way to add some known emitters and configure them to the tspconfig I think this would be much much better.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks @timotheeguerin .
Emitters will be registered in the VSCode extension settings. We pre-defined default emtters (both client emitters, server emitters for all languages, and the schema emitter also). We will provide a way for customer to register their customized emitter in future.

We will hold this PR merge and we discuss more after you back. Thanks

Copy link
Contributor

Choose a reason for hiding this comment

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

@timotheeguerin , thanks for looking at the PR in holiday and Merry Christmas!
Below is the spec for the feature (Those user cases with name "Generate XXX from TypeSpec") which should be able to give you more context and the user experience we want to provide. Any feedback is welcome, and we can discuss more when you are back. thanks.
https://microsoft.sharepoint.com/:w:/t/AzureDeveloperExperience/EXfInUWxaOpNpXVtcQOAt4cBoVwvu3tZe3Od__r9vdc8qQ?e=cmyNb0

@chunyu3 chunyu3 force-pushed the generateSDK branch 3 times, most recently from 6a30951 to 1722c43 Compare December 20, 2024 03:11
@chunyu3 chunyu3 force-pushed the generateSDK branch 2 times, most recently from b2e150b to d1b7723 Compare December 20, 2024 09:19
PreDefinedEmitterPickItems,
} from "./emitter.js";

export async function doEmit(
Copy link
Contributor

Choose a reason for hiding this comment

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

seems no need to export

if (!selectedEmitter) {
logger.info("No emitter selected. Generating Cancelled.", [], {
showOutput: false,
showPopup: false,
Copy link
Contributor

Choose a reason for hiding this comment

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

false is the default value, you can just logger.info("...") to make your code shorter.

return;
}

logger.info("npm install...", [], {
Copy link
Contributor

Choose a reason for hiding this comment

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

npm install? People (us or end users) will use the log to do troubleshooting, please help to make these logs more detail and understandable.

logger.info(`${dependenciesToInstall}`);
if (dependenciesToInstall.length > 0) {
vscode.window.showInformationMessage(
`Need to manually upgrade following dependency packages: ${dependenciesToInstall.join("\\n")}. \nGenerating Cancelled`,
Copy link
Contributor

Choose a reason for hiding this comment

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

no option to install them for user?

);
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?

});

const emitterKinds = getRegisterEmitterTypes();
const toEitTypeQuickPickItem = (kind: EmitterKind): any => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Eit? typo?

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

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


if (dependenciesResult.exitCode === 0) {
// Remove all newline characters
let dependenciesJsonStr = dependenciesResult.stdout.trim();
Copy link
Contributor

Choose a reason for hiding this comment

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

i remember there is a --json arg, doesn't it help?

.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?

);
};

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

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

await doEmit(context, tspProjectFile, codeType.emitterKind);
}

export async function compile(
Copy link
Contributor

Choose a reason for hiding this comment

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

you dont need to export this

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.

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