From e76bdcd0e789cfe105ac9f5aff061bcfcf9823d7 Mon Sep 17 00:00:00 2001 From: Joey Wunderlich Date: Tue, 17 Sep 2024 11:56:25 -0700 Subject: [PATCH 1/6] start emitting dts --- cli/cli.ts | 68 +++++++++++++++++++++++++++++++---- localtypings/pxtarget.d.ts | 1 + pxtcompiler/emitter/driver.ts | 6 ++++ 3 files changed, 68 insertions(+), 7 deletions(-) diff --git a/cli/cli.ts b/cli/cli.ts index 5ba851aea3bf..d800a8fe1846 100644 --- a/cli/cli.ts +++ b/cli/cli.ts @@ -2954,17 +2954,17 @@ class SnippetHost implements pxt.Host { if (module.id === "this") { if (filename === "pxt-core.d.ts") { - const contents = fs.readFileSync(path.join(this.getRepoDir(), "libs", "pxt-common", "pxt-core.d.ts"), 'utf8'); + const contents = fs.readFileSync(path.join(this.getPxtRepoDir(), "libs", "pxt-common", "pxt-core.d.ts"), 'utf8'); this.writeFile(module, filename, contents); return contents; } else if (filename === "pxt-helpers.ts") { - const contents = fs.readFileSync(path.resolve(this.getRepoDir(), "libs", "pxt-common", "pxt-helpers.ts"), 'utf8'); + const contents = fs.readFileSync(path.resolve(this.getPxtRepoDir(), "libs", "pxt-common", "pxt-helpers.ts"), 'utf8'); this.writeFile(module, filename, contents); return contents; } else if (filename === "pxt-python.d.ts" || filename === "pxt-python-helpers.ts") { - const contents = fs.readFileSync(path.resolve(this.getRepoDir(), "libs", "pxt-python", filename), 'utf8'); + const contents = fs.readFileSync(path.resolve(this.getPxtRepoDir(), "libs", "pxt-python", filename), 'utf8'); this.writeFile(module, filename, contents); return contents; } @@ -2974,10 +2974,20 @@ class SnippetHost implements pxt.Host { return null; } - private getRepoDir() { + private getPxtRepoDir() { const cwd = process.cwd(); - const i = cwd.lastIndexOf(path.sep + "pxt" + path.sep); - return cwd.substr(0, i + 5); + let p = path.parse(cwd); + while (p.base) { + if (p.base === "pxt") { + return path.format(p); + } + p = path.parse(p.dir); + } + // const i = cwd.lastIndexOf(path.sep + "pxt" + path.sep); + // if (i !== -1) { + // return cwd.substr(0, i + 5); + // } + return path.join(cwd, "node_modules", "pxt-core"); } writeFile(module: pxt.Package, filename: string, contents: string) { @@ -4068,6 +4078,7 @@ function compilesOK(opts: pxtc.CompileOptions, fn: string, content: string) { return res.success } + function getApiInfoAsync() { return prepBuildOptionsAsync(BuildOption.GenDocs) .then(opts => { @@ -4874,7 +4885,7 @@ function buildCoreAsync(buildOpts: BuildCoreOptions): Promise { + // console.error(diag.messageText) + // }) + // const a: pxt.Map = {} + compileResult.emit( + undefined, + (fileName: string, data: string) => { + console.log(`writing ${fileName}`); + fs.writeFileSync(path.join(builtFolder, fileName), data); + }, + undefined, + true + ); + // fs.writeFileSync(path.join(builtFolder, 'justsavethis.json'), JSON.stringify(a)); +} + export function gendocsAsync(parsed: commandParser.ParsedCommand) { const docs = !!parsed.flags["docs"]; const locs = !!parsed.flags["locs"]; @@ -6836,6 +6885,11 @@ ${pxt.crowdin.KEY_VARIABLE} - crowdin key }, }, buildShareSimJsAsync) + p.defineCommand({ + name: "buildcoredts", + help: "build d.ts files for core packages", + }, buildCoreDeclarationFiles) + simpleCmd("clean", "removes built folders", cleanAsync); advancedCommand("cleangen", "remove generated files", cleanGenAsync); simpleCmd("npminstallnative", "install native dependencies", npmInstallNativeAsync); diff --git a/localtypings/pxtarget.d.ts b/localtypings/pxtarget.d.ts index aa6b88b1ca8e..0bc6e75ba8d1 100644 --- a/localtypings/pxtarget.d.ts +++ b/localtypings/pxtarget.d.ts @@ -1076,6 +1076,7 @@ declare namespace ts.pxtc { clearIncrBuildAndRetryOnError?: boolean; // on error when compiling in service, try again with a full recompile. errorOnGreyBlocks?: boolean; generateSourceMap?: boolean; + tsCompileOptions?: pxt.Map; // ts.CompilerOptions; otherMultiVariants?: ExtensionTarget[]; diff --git a/pxtcompiler/emitter/driver.ts b/pxtcompiler/emitter/driver.ts index b9c11e4e5f13..16f356aa8363 100644 --- a/pxtcompiler/emitter/driver.ts +++ b/pxtcompiler/emitter/driver.ts @@ -25,6 +25,12 @@ namespace ts.pxtc { options.noImplicitAny = true; options.noImplicitReturns = true; options.allowUnreachableCode = true; + options.declaration = true; + + for (const [key, value] of Object.entries(opts?.tsCompileOptions ?? {})) { + options[key] = value; + } + return options } From 6ee1919bbe25039a603ccec9c1e8271ddd2ea140 Mon Sep 17 00:00:00 2001 From: Joey Wunderlich Date: Tue, 17 Sep 2024 14:16:28 -0700 Subject: [PATCH 2/6] workin --- cli/cli.ts | 47 ++++++++++++++++++++++++++++++++--------------- 1 file changed, 32 insertions(+), 15 deletions(-) diff --git a/cli/cli.ts b/cli/cli.ts index d800a8fe1846..14cd2c4ff4da 100644 --- a/cli/cli.ts +++ b/cli/cli.ts @@ -5438,40 +5438,57 @@ export async function buildShareSimJsAsync(parsed: commandParser.ParsedCommand) } export async function buildCoreDeclarationFiles(parsed: commandParser.ParsedCommand) { - const rootPackage = parsed.args[0] const cwd = process.cwd(); - const builtFolder = path.join(cwd, "temp"); + const builtFolder = path.join(cwd, "temp", "dts"); nodeutil.mkdirP(builtFolder); - // process.chdir(builtFolder); + process.chdir(cwd); - // const host = new SnippetHost("decl-build", { "main.ts" : "" }, { "blocksprj": "*" }, true); const host = new SnippetHost("decl-build", { "main.ts" : "" }, { "blocksprj": "*" }, true); const mainPkg = new pxt.MainPackage(host); console.log("installing") - // await mainPkg.host().downloadPackageAsync(mainPkg); await mainPkg.installAllAsync(); console.log("installed") const opts = await mainPkg.getCompileOptionsAsync(); console.log("created compiler options") opts.tsCompileOptions = opts.tsCompileOptions ?? {}; opts.tsCompileOptions.declaration = true; - - const compileResult = pxtc.getTSProgram(opts); + await rimrafAsync(builtFolder, {}); + + const tsProg = pxtc.getTSProgram(opts); console.log("compiled") - // compileResult.diagnostics?.forEach(diag => { - // console.error(diag.messageText) - // }) - // const a: pxt.Map = {} - compileResult.emit( + + let combined = "" + const writeDts = (fileName: string, data: string) => { + console.log(`writing ${fileName}`); + const writePath = path.join(builtFolder, fileName); + nodeutil.mkdirP(path.parse(writePath).dir); + fs.writeFileSync(writePath, data); + combined += data + "\n\n"; + } + + // pre-created + for (const file of tsProg.getSourceFiles()) { + if (file.fileName.endsWith(".d.ts")) { + writeDts( + file.fileName, + file.getFullText() + ); + } + } + + // generated via build + tsProg.emit( undefined, (fileName: string, data: string) => { - console.log(`writing ${fileName}`); - fs.writeFileSync(path.join(builtFolder, fileName), data); + if (!data?.trim()) return; + writeDts(fileName, data); }, undefined, true ); - // fs.writeFileSync(path.join(builtFolder, 'justsavethis.json'), JSON.stringify(a)); + + console.log(`writing combined.d.ts`) + fs.writeFileSync(path.join(builtFolder, "combined.d.ts"), combined); } export function gendocsAsync(parsed: commandParser.ParsedCommand) { From 8ab9eb14db5aa1a96d9fff7d995059dbae9b811e Mon Sep 17 00:00:00 2001 From: Joey Wunderlich Date: Tue, 17 Sep 2024 14:17:40 -0700 Subject: [PATCH 3/6] remove commented out code --- cli/cli.ts | 4 ---- 1 file changed, 4 deletions(-) diff --git a/cli/cli.ts b/cli/cli.ts index 14cd2c4ff4da..48b97fbe43d8 100644 --- a/cli/cli.ts +++ b/cli/cli.ts @@ -2983,10 +2983,6 @@ class SnippetHost implements pxt.Host { } p = path.parse(p.dir); } - // const i = cwd.lastIndexOf(path.sep + "pxt" + path.sep); - // if (i !== -1) { - // return cwd.substr(0, i + 5); - // } return path.join(cwd, "node_modules", "pxt-core"); } From 3bf00fe9c8501754be6289d5a9485e7d214f5932 Mon Sep 17 00:00:00 2001 From: Joey Wunderlich Date: Tue, 17 Sep 2024 14:25:43 -0700 Subject: [PATCH 4/6] annotate emit params for clarity sake --- cli/cli.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/cli/cli.ts b/cli/cli.ts index 48b97fbe43d8..377eeb33d53e 100644 --- a/cli/cli.ts +++ b/cli/cli.ts @@ -5474,13 +5474,14 @@ export async function buildCoreDeclarationFiles(parsed: commandParser.ParsedComm // generated via build tsProg.emit( - undefined, + /** targetSourceFile **/ undefined, (fileName: string, data: string) => { if (!data?.trim()) return; writeDts(fileName, data); }, - undefined, - true + /** cancellation token **/ undefined, + /** emitOnlyDtsFiles **/ true, + /** customTransformers -> I believe where we should apply culling of deprecated blocks */ ); console.log(`writing combined.d.ts`) From 0d828da6cde68348f922abf5f53cdde5e9d61e9c Mon Sep 17 00:00:00 2001 From: Joey Wunderlich Date: Tue, 17 Sep 2024 14:42:10 -0700 Subject: [PATCH 5/6] add support for building from share link to cover extensions / etc --- cli/cli.ts | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/cli/cli.ts b/cli/cli.ts index 377eeb33d53e..3b109913b199 100644 --- a/cli/cli.ts +++ b/cli/cli.ts @@ -5434,13 +5434,21 @@ export async function buildShareSimJsAsync(parsed: commandParser.ParsedCommand) } export async function buildCoreDeclarationFiles(parsed: commandParser.ParsedCommand) { + const shareId = parsed.args[0]?.trim(); + console.log(shareId ? `Building .d.ts for ${shareId}` : `Building .d.ts for blocksprj`); const cwd = process.cwd(); - const builtFolder = path.join(cwd, "temp", "dts"); + const builtFolder = path.join(cwd, "temp", shareId ? `${shareId}dts` : "dts"); nodeutil.mkdirP(builtFolder); process.chdir(cwd); - const host = new SnippetHost("decl-build", { "main.ts" : "" }, { "blocksprj": "*" }, true); + const host = shareId ? new Host() : new SnippetHost("decl-build", { "main.ts" : "" }, { "blocksprj": "*" }, true); const mainPkg = new pxt.MainPackage(host); + + if (shareId) { + mainPkg._verspec = `pub:${shareId}`; + await mainPkg.host().downloadPackageAsync(mainPkg); + } + console.log("installing") await mainPkg.installAllAsync(); console.log("installed") @@ -6902,6 +6910,7 @@ ${pxt.crowdin.KEY_VARIABLE} - crowdin key p.defineCommand({ name: "buildcoredts", help: "build d.ts files for core packages", + argString: "", }, buildCoreDeclarationFiles) simpleCmd("clean", "removes built folders", cleanAsync); From 06e347f039f73cec1bdb32e6f360c38abd93f2bf Mon Sep 17 00:00:00 2001 From: Joey Wunderlich Date: Tue, 17 Sep 2024 14:49:41 -0700 Subject: [PATCH 6/6] base includes pxt-core / helpers --- cli/cli.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cli/cli.ts b/cli/cli.ts index 3b109913b199..048cd36f2517 100644 --- a/cli/cli.ts +++ b/cli/cli.ts @@ -5441,7 +5441,7 @@ export async function buildCoreDeclarationFiles(parsed: commandParser.ParsedComm nodeutil.mkdirP(builtFolder); process.chdir(cwd); - const host = shareId ? new Host() : new SnippetHost("decl-build", { "main.ts" : "" }, { "blocksprj": "*" }, true); + const host = shareId ? new Host() : new SnippetHost("decl-build", { "main.ts" : "" }, { "blocksprj": "*" }); const mainPkg = new pxt.MainPackage(host); if (shareId) {