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

[tsp-client] Print example command #9217

Merged
merged 7 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions tools/tsp-client/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Release

## 2024-11-08 - 0.14.1

- Print an example `tsp compile` call when the `--debug` flag is passed to a `tsp-client` command.

## 2024-11-07 - 0.14.0

- Fix `init` command when using a local spec:
Expand Down
4 changes: 2 additions & 2 deletions tools/tsp-client/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tools/tsp-client/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@azure-tools/typespec-client-generator-cli",
"version": "0.14.0",
"version": "0.14.1",
"description": "A tool to generate Azure SDKs from TypeSpec",
"main": "dist/index.js",
"homepage": "https://github.com/Azure/azure-sdk-tools/tree/main/tools/tsp-client#readme",
Expand Down
9 changes: 8 additions & 1 deletion tools/tsp-client/src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,14 +276,21 @@ export async function generateCommand(argv: any) {
args.push("--force");
}
await npmCommand(srcDir, args);
const success = await compileTsp({

const [success, exampleCmd] = await compileTsp({
emitterPackage: emitter,
outputPath: outputDir,
resolvedMainFilePath,
saveInputs: saveInputs,
additionalEmitterOptions: emitterOptions,
});

if (argv["debug"]) {
Logger.warn(`Example of how to compile using the tsp commandline. NOTE: tsp-client does NOT directly run this command, results may vary:
${exampleCmd}
`);
}

if (saveInputs) {
Logger.debug(`Skipping cleanup of temp directory: ${tempRoot}`);
} else {
Expand Down
20 changes: 16 additions & 4 deletions tools/tsp-client/src/typespec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export async function compileTsp({
resolvedMainFilePath: string;
additionalEmitterOptions?: string;
saveInputs?: boolean;
}): Promise<boolean> {
}): Promise<[boolean, string]> {
const parsedEntrypoint = getDirectoryPath(resolvedMainFilePath);
const { compile, NodeHost, resolveCompilerOptions, formatDiagnostic } =
await importTsp(parsedEntrypoint);
Expand Down Expand Up @@ -103,6 +103,18 @@ export async function compileTsp({
overrides,
});
Logger.debug(`Compiler options: ${JSON.stringify(options)}`);

const cliOptions = Object.entries(options.options?.[emitterPackage] ?? {})
.map(([key, value]) => {
if (typeof value === "object") {
value = JSON.stringify(value);
}
return `--option ${key}=${value}`;
})
.join(" ");

const exampleCmd = `npx tsp compile ${resolvedMainFilePath} --emit ${emitterPackage} ${cliOptions}`;

if (diagnostics.length > 0) {
let errorDiagnostic = false;
// This should not happen, but if it does, we should log it.
Expand All @@ -118,7 +130,7 @@ export async function compileTsp({
}
}
if (errorDiagnostic) {
return false;
return [false, exampleCmd];
}
}

Expand All @@ -138,11 +150,11 @@ export async function compileTsp({
}
}
if (errorDiagnostic) {
return false;
return [false, exampleCmd];
}
}
Logger.success("generation complete");
return true;
return [true, exampleCmd];
}

export async function importTsp(baseDir: string): Promise<typeof import("@typespec/compiler")> {
Expand Down
2 changes: 1 addition & 1 deletion tools/tsp-client/test/typespec.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe("Check diagnostic reporting", function () {
resolvePath(process.cwd(), "test", "examples", "specification", "diagnostics"),
);
try {
const succeeded = await compileTsp({
const [succeeded, _] = await compileTsp({
emitterPackage: "@azure-tools/typespec-ts",
outputPath: joinPaths(process.cwd(), "examples"),
resolvedMainFilePath: mainFile,
Expand Down