Skip to content

Commit

Permalink
Reimplement vendor_build.py and vendor_license.py in JS.
Browse files Browse the repository at this point in the history
  • Loading branch information
jchv committed Aug 27, 2019
1 parent 1f66574 commit 1813f9e
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 104 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ node_js: 10
install:
- pip install --user -r requirements.txt
- npm install
- 'if [[ "$TRAVIS_BRANCH" = stable ]]; then npm install kaitai-struct-compiler@latest && npm install kaitai-struct@latest && python vendor_build.py; fi'
- 'if [[ "$TRAVIS_BRANCH" = stable ]]; then npm install kaitai-struct-compiler@latest && npm install kaitai-struct@latest && node vendor_build.js; fi'

script:
- '[ "$TRAVIS_BRANCH" = stable ] && export GA_ID=UA-76299550-2 || export GA_ID=UA-76299550-3'
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"install": "python vendor_build.py"
"install": "node vendor_build.js"
},
"repository": {
"type": "git",
Expand Down
59 changes: 59 additions & 0 deletions vendor_build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
const YAML = require("yamljs");
const { copyFileSync, readdirSync, statSync, mkdirSync } = require("fs");
const { join, basename, dirname } = require("path");
const firstBy = require("thenby");

function isDirectory(file) {
return statSync(file).isDirectory();
}

function copyOverwrite(src, dst) {
if (isDirectory(src)) {
readdirSync(src).forEach(file =>
copyOverwrite(join(src, file), join(dst, file))
);
} else {
let dstFile, dstDir;
if (dst.endsWith("/")) {
dstDir = dst;
dstFile = join(dst, basename(src));
} else {
dstDir = dirname(dst);
dstFile = dst;
}
console.log(" copying", src, "to", dstFile);
mkdirSync(dstDir, { recursive: true });
copyFileSync(src, dstFile);
}
}

function main() {
const vendor = YAML.load("vendor.yaml");
const sortedLibs = Object.entries(vendor["libs"]).sort(
firstBy(([, lib]) => lib["priority"])
);
for (const [libName, lib] of sortedLibs) {
if (!lib["npmDir"] || !lib["files"]) {
continue;
}
console.log("Processing:", libName);
const distDir = `./lib/_npm/${lib["distDir"] || lib["npmDir"]}/`;
for (let file of lib["files"]) {
const allFilesInDir = file.endsWith("/*");
if (allFilesInDir) {
file = file.replace("/*", "");
}

srcPattern = `./node_modules/${lib["npmDir"]}/${file}`;
if (isDirectory(srcPattern) && !allFilesInDir) {
copyOverwrite(srcPattern, join(distDir, file));
} else {
copyOverwrite(srcPattern, distDir);
}
}
}

require("./vendor_license");
}

main();
53 changes: 0 additions & 53 deletions vendor_build.py

This file was deleted.

68 changes: 68 additions & 0 deletions vendor_license.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
const YAML = require("yamljs");
const { readdirSync, readFileSync, writeFileSync, statSync } = require("fs");
const { join, basename } = require("path");
const firstBy = require("thenby");

function isLicenseFilename(name) {
const fn = basename(name);
return fn.startsWith("LICENSE") || fn.startsWith("license");
}

function findLicenses(dst) {
return readdirSync(dst)
.map(file => join(dst, file))
.filter(isLicenseFilename);
}

function main() {
const vendor = YAML.load("vendor.yaml");
let licResult = "";
let wikiResult = "# 3rd-party libraries\n\n";
const sortedLibs = Object.entries(vendor["libs"]).sort(
firstBy(([libName]) => libName)
);
for (const [libName, lib] of sortedLibs) {
console.log("Processing", libName);
const distDir = `lib/${lib["npmDir"] ? "_npm/" : ""}${lib["distDir"] || lib["npmDir"]}/`;
const licFns = findLicenses(distDir);

if (licFns.length != 1) {
console.log(`License not found: ${distDir}:`, licFns);
continue;
}

licResult += "=".repeat(80) + "\n";
licResult += " ".repeat(((80 - libName.length) / 2) | 0) + libName + "\n";
licResult += "\n";
licResult += `License name: ${lib["licenseName"]}\n`;
licResult += ` License URL: ${lib["licenseUrl"]}\n`;
licResult += ` License applies to files under the folder ${distDir}\n`;
licResult += "\n";

wikiResult += `## ${libName}\n`;

if (lib["website"]) {
licResult += `Website: ${lib["website"]}\n`;
wikiResult += `Website: ${lib["website"]}\n\n`;
}

if (lib["source"]) {
licResult += `Source: ${lib["source"]}\n`;
wikiResult += `Source: ${lib["source"]}\n\n`;
}

wikiResult += `License: ${lib["licenseName"]} (${lib["licenseUrl"]})\n\n`;

licResult += "=".repeat(80) + "\n";
licResult +=
readFileSync(licFns[0], { encoding: "utf-8" })
.trim()
.replace(/\r\n/g, "\n") + "\n";
licResult += "=".repeat(80) + "\n\n";
}

writeFileSync("LICENSE-3RD-PARTY.txt", licResult.trim());
writeFileSync("docs/wiki/3rd-party-libraries.md", wikiResult.trim());
}

main();
49 changes: 0 additions & 49 deletions vendor_license.py

This file was deleted.

0 comments on commit 1813f9e

Please sign in to comment.