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

Init logic bundle the compiler logic instead of shelling out to some random client #6086

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Changes from 1 commit
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
Prev Previous commit
Load templates
  • Loading branch information
timotheeguerin committed Feb 20, 2025
commit 9d36738adfdd4fc9009a3e33012b83bce107ca61
1 change: 0 additions & 1 deletion packages/compiler/src/internals/init.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export { NodeSystemHost } from "../core/node-system-host.js";
export { getTypeSpecCoreTemplates } from "../init/core-templates.js";
export { scaffoldNewProject } from "../init/scaffold.js";
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 also need to export validateTemplate method or template schema for vscode to do all the template validation.

1 change: 1 addition & 0 deletions packages/typespec-vscode/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
templates/
3 changes: 2 additions & 1 deletion packages/typespec-vscode/package.json
Original file line number Diff line number Diff line change
@@ -225,10 +225,11 @@
},
"scripts": {
"clean": "rimraf ./dist ./temp",
"build": "npm run compile && npm run copy-tmlanguage && npm run generate-language-configuration && npm run generate-third-party-notices && npm run package-vsix",
"build": "npm run compile && npm run copy-templates && npm run copy-tmlanguage && npm run generate-language-configuration && npm run generate-third-party-notices && npm run package-vsix",
"compile": "rollup --config rollup.config.ts --configPlugin typescript --failAfterWarnings 2>&1",
"watch": "rollup --config rollup.config.ts --configPlugin typescript --watch",
"dogfood": "node scripts/dogfood.js",
"copy-templates": "node scripts/copy-templates.js",
"copy-tmlanguage": "node scripts/copy-tmlanguage.js",
"generate-language-configuration": "node scripts/generate-language-configuration.js",
"generate-third-party-notices": "typespec-build-tool generate-third-party-notices",
5 changes: 5 additions & 0 deletions packages/typespec-vscode/scripts/copy-templates.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { cp } from "fs/promises";

await cp("node_modules/@typespec/compiler/templates", "../typespec-vscode/templates", {
recursive: true,
});
47 changes: 41 additions & 6 deletions packages/typespec-vscode/src/vscode-cmd/create-tsp-project.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
import {
SystemHost,
type InitProjectConfig,
type InitProjectTemplate,
type InitProjectTemplateEmitterTemplate,
type InitProjectTemplateLibrarySpec,
} from "@typespec/compiler";
import {
getTypeSpecCoreTemplates,
NodeSystemHost,
scaffoldNewProject,
} from "@typespec/compiler/internals/init";
import { NodeSystemHost, scaffoldNewProject } from "@typespec/compiler/internals/init";
import { readdir } from "fs/promises";
import * as semver from "semver";
import { fileURLToPath } from "url";
import vscode, { OpenDialogOptions, QuickPickItem } from "vscode";
import { ExtensionStateManager } from "../extension-state-manager.js";
import logger from "../log/logger.js";
import { getBaseFileName, getDirectoryPath, joinPaths, normalizePath } from "../path-utils.js";
import {
getBaseFileName,
getDirectoryPath,
joinPaths,
normalizePath,
resolvePath,
} from "../path-utils.js";
import { ResultCode, SettingName } from "../types.js";
import {
createPromiseWithCancelAndTimeout,
@@ -559,6 +563,37 @@ async function selectTemplate(
return selected?.info;
}

async function findProjectRoot(host: SystemHost) {
let current = getDirectoryPath(fileURLToPath(import.meta.url));
while (true) {
const pkgPath = joinPaths(current, "package.json");
try {
const stat = await host.stat(pkgPath);
if (stat?.isFile()) {
return current;
}
} catch (e) {}
const parent = getDirectoryPath(current);
if (parent === current) {
throw new Error("Unexpected: Failed to resolve project root.");
}
current = parent;
}
}

async function getTypeSpecCoreTemplates(
host: SystemHost,
): Promise<{ readonly baseUri: string; readonly templates: Record<string, any> }> {
const projectRoot = await findProjectRoot(host);
const templatesDir = resolvePath(projectRoot, "templates");
const file = await host.readFile(resolvePath(templatesDir, "scaffolding.json"));
const content = JSON.parse(file.text);
return {
baseUri: templatesDir,
templates: content,
};
}

async function loadInitTemplates(): Promise<Map<string, InitTemplateInfo[]>> {
logger.info("Loading init templates from compiler...");
const templateInfoMap: Map<string, InitTemplateInfo[]> = new Map();
Loading
Oops, something went wrong.