UNPKG

4.32 kBJavaScriptView Raw
1"use strict";
2var __importDefault = (this && this.__importDefault) || function (mod) {
3 return (mod && mod.__esModule) ? mod : { "default": mod };
4};
5Object.defineProperty(exports, "__esModule", { value: true });
6const Generator_1 = __importDefault(require("./Generator"));
7const generators_1 = __importDefault(require("./generators"));
8const PREFERRED_UBUNTU_VERSION = '18.04';
9/**
10 * Compare semantic version numbers
11 *
12 * @param versionOne
13 * @param versionTwo
14 */
15function versionCompare(versionOne, versionTwo) {
16 if (versionOne === versionTwo) {
17 return 0; // shortcut
18 }
19 let splitV1 = versionOne.split('.');
20 let splitV2 = versionTwo.split('.');
21 while (splitV1.length < splitV2.length) {
22 splitV1.push('0');
23 }
24 while (splitV2.length < splitV1.length) {
25 splitV2.push('0');
26 }
27 for (let i = 0; i < splitV1.length; i) {
28 let component1 = parseInt(splitV1[i], 10);
29 let component2 = parseInt(splitV2[i], 10);
30 if (component1 < component2) {
31 return -1;
32 }
33 else if (component1 > component2) {
34 return 1;
35 }
36 }
37 return 0; // all components equal
38}
39/**
40 * A Dockerfile generator that collects instructions from
41 * all the other generators to allow for images that support
42 * multiple languages.
43 */
44class DockerGenerator extends Generator_1.default {
45 constructor(urlFetcher, environ, folder) {
46 super(urlFetcher, folder);
47 this.environ = environ;
48 // Each of the environment's `softwareRequirements` is
49 // matched to one of the language specific generators
50 // (the first that says that it `applies`)
51 this.generators = [];
52 for (let pkg of this.environ.softwareRequirements) {
53 for (let GeneratorClass of generators_1.default) {
54 // @ts-ignore
55 const generator = new GeneratorClass(urlFetcher, pkg, folder);
56 if (generator.applies()) {
57 this.generators.push(generator);
58 break;
59 }
60 }
61 }
62 }
63 /**
64 * Collect arrays of string from each child generator
65 * and flatten them into an array of strings.
66 * Used below for method overrides.
67 *
68 * @param func The child generator method to call
69 */
70 collect(func) {
71 // @ts-ignore
72 return this.generators.map(func).reduce((memo, items) => (memo.concat(items)), []);
73 }
74 /**
75 * Join strings from each child generator
76 *
77 * @param func The child generator method to call
78 */
79 join(func, sep = ' \\\n && ') {
80 // @ts-ignore
81 return this.generators.map(func).filter(cmd => cmd).join(sep);
82 }
83 // Methods that override those in `Generator`
84 applies() {
85 return true;
86 }
87 baseVersion() {
88 return [PREFERRED_UBUNTU_VERSION].concat(this.generators.filter(generator => generator.baseName() === this.baseName()) // filter to generators with matching base name
89 .map(generator => generator.baseVersion()))
90 .sort(versionCompare)[0];
91 }
92 envVars(sysVersion) {
93 return this.collect((generator) => generator.envVars(sysVersion));
94 }
95 aptKeysCommand(sysVersion) {
96 return this.join((generator) => generator.aptKeysCommand(sysVersion));
97 }
98 aptRepos(sysVersion) {
99 return this.collect((generator) => generator.aptRepos(sysVersion));
100 }
101 aptPackages(sysVersion) {
102 // Get the set of unique apt packages requested by each child generator
103 const pkgs = this.collect((generator) => generator.aptPackages(sysVersion)).sort();
104 return Array.from(new Set(pkgs));
105 }
106 stencilaInstall(sysVersion) {
107 return this.join((generator) => generator.stencilaInstall(sysVersion));
108 }
109 installFiles(sysVersion) {
110 return this.collect((generator) => generator.installFiles(sysVersion));
111 }
112 installCommand(sysVersion) {
113 return this.join((generator) => generator.installCommand(sysVersion));
114 }
115 projectFiles(sysVersion) {
116 return this.collect((generator) => generator.projectFiles(sysVersion));
117 }
118 runCommand(sysVersion) {
119 return this.join((generator) => generator.runCommand(sysVersion));
120 }
121}
122exports.default = DockerGenerator;