UNPKG

3.48 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.IndependentPackageBuilder = void 0;
4const path = require("path");
5const logging = require("./logging");
6const util_1 = require("./util");
7/**
8 * Base implementation, building the package targets for the given language independently of each other
9 *
10 * Some languages can gain substantial speedup in preparing an "uber project" for all packages
11 * and compiling them all in one go (Those will be implementing a custom Builder).
12 *
13 * For languages where it doesn't matter--or where we haven't figured out how to
14 * do that yet--this class can serve as a base class: it will build each package
15 * independently, taking care to build them in the right order.
16 */
17class IndependentPackageBuilder {
18 constructor(targetName, targetConstructor, modules, options) {
19 this.targetName = targetName;
20 this.targetConstructor = targetConstructor;
21 this.modules = modules;
22 this.options = options;
23 }
24 async buildModules() {
25 if (this.options.codeOnly) {
26 await Promise.all((0, util_1.flatten)(this.modules).map((module) => this.generateModuleCode(module, this.options)));
27 return;
28 }
29 for (const modules of this.modules) {
30 // eslint-disable-next-line no-await-in-loop
31 await Promise.all(modules.map((module) => this.buildModule(module, this.options)));
32 }
33 }
34 async generateModuleCode(module, options) {
35 const outputDir = this.finalOutputDir(module, options);
36 logging.debug(`Generating ${this.targetName} code into ${outputDir}`);
37 await this.makeTarget(module, options).generateCode(outputDir, module.tarball);
38 }
39 async buildModule(module, options) {
40 const target = this.makeTarget(module, options);
41 const outputDir = this.finalOutputDir(module, options);
42 const src = await util_1.Scratch.make((tmpdir) => {
43 logging.debug(`Generating ${this.targetName} code into ${tmpdir}`);
44 return target.generateCode(tmpdir, module.tarball);
45 });
46 try {
47 logging.debug(`Building ${src.directory} into ${outputDir}`);
48 return await target.build(src.directory, outputDir);
49 }
50 catch (err) {
51 logging.warn(`Failed building ${this.targetName}`);
52 return await Promise.reject(err);
53 }
54 finally {
55 if (options.clean) {
56 logging.debug(`Cleaning ${src.directory}`);
57 await src.cleanup();
58 }
59 else {
60 logging.info(`Generated code for ${this.targetName} retained at ${src.directory}`);
61 }
62 }
63 }
64 makeTarget(module, options) {
65 return new this.targetConstructor({
66 arguments: options.arguments,
67 assembly: module.assembly,
68 fingerprint: options.fingerprint,
69 force: options.force,
70 packageDir: module.moduleDirectory,
71 rosetta: options.rosetta,
72 runtimeTypeChecking: options.runtimeTypeChecking,
73 targetName: this.targetName,
74 });
75 }
76 finalOutputDir(module, options) {
77 if (options.languageSubdirectory) {
78 return path.join(module.outputDirectory, this.targetName);
79 }
80 return module.outputDirectory;
81 }
82}
83exports.IndependentPackageBuilder = IndependentPackageBuilder;
84//# sourceMappingURL=builder.js.map
\No newline at end of file