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

Build d.ts for target #10192

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
91 changes: 84 additions & 7 deletions cli/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -2974,10 +2974,16 @@ 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);
}
return path.join(cwd, "node_modules", "pxt-core");
}

writeFile(module: pxt.Package, filename: string, contents: string) {
Expand Down Expand Up @@ -4068,6 +4074,7 @@ function compilesOK(opts: pxtc.CompileOptions, fn: string, content: string) {
return res.success
}


function getApiInfoAsync() {
return prepBuildOptionsAsync(BuildOption.GenDocs)
.then(opts => {
Expand Down Expand Up @@ -4874,7 +4881,7 @@ function buildCoreAsync(buildOpts: BuildCoreOptions): Promise<pxtc.CompileResult
console.log(compileResult.times)

switch (buildOpts.mode) {
case BuildOption.GenDocs:
case BuildOption.GenDocs: {
const apiInfo = pxtc.getApiInfo(res.ast, compileOptions.jres)
// keeps apis from this module only
for (const infok in apiInfo.byQName) {
Expand Down Expand Up @@ -4905,6 +4912,7 @@ function buildCoreAsync(buildOpts: BuildCoreOptions): Promise<pxtc.CompileResult
}
}
return null
}
case BuildOption.Deploy:
if (pxt.commands.hasDeployFn())
return pxt.commands.deployAsync(res)
Expand Down Expand Up @@ -5425,6 +5433,69 @@ export async function buildShareSimJsAsync(parsed: commandParser.ParsedCommand)
console.log(`saved prebuilt ${id} to ${outputLocation}`);
}

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", shareId ? `${shareId}dts` : "dts");
nodeutil.mkdirP(builtFolder);
process.chdir(cwd);

const host = shareId ? new Host() : new SnippetHost("decl-build", { "main.ts" : "" }, { "blocksprj": "*" });
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")
const opts = await mainPkg.getCompileOptionsAsync();
console.log("created compiler options")
opts.tsCompileOptions = opts.tsCompileOptions ?? {};
opts.tsCompileOptions.declaration = true;
await rimrafAsync(builtFolder, {});

const tsProg = pxtc.getTSProgram(opts);
console.log("compiled")

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(
/** targetSourceFile **/ undefined,
(fileName: string, data: string) => {
if (!data?.trim()) return;
writeDts(fileName, data);
},
/** cancellation token **/ undefined,
/** emitOnlyDtsFiles **/ true,
/** customTransformers -> I believe where we should apply culling of deprecated blocks */
);

console.log(`writing combined.d.ts`)
fs.writeFileSync(path.join(builtFolder, "combined.d.ts"), combined);
Copy link
Contributor

Choose a reason for hiding this comment

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

If this is a single file for all d.ts, maybe call it index.d.ts?

}

export function gendocsAsync(parsed: commandParser.ParsedCommand) {
const docs = !!parsed.flags["docs"];
const locs = !!parsed.flags["locs"];
Expand Down Expand Up @@ -6836,6 +6907,12 @@ ${pxt.crowdin.KEY_VARIABLE} - crowdin key
},
}, buildShareSimJsAsync)

p.defineCommand({
name: "buildcoredts",
help: "build d.ts files for core packages",
argString: "<share id>",
}, buildCoreDeclarationFiles)

simpleCmd("clean", "removes built folders", cleanAsync);
advancedCommand("cleangen", "remove generated files", cleanGenAsync);
simpleCmd("npminstallnative", "install native dependencies", npmInstallNativeAsync);
Expand Down
1 change: 1 addition & 0 deletions localtypings/pxtarget.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any>; // ts.CompilerOptions;

otherMultiVariants?: ExtensionTarget[];

Expand Down
6 changes: 6 additions & 0 deletions pxtcompiler/emitter/driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down