Improve license script

This commit is contained in:
pietervdvn 2022-06-20 01:41:48 +02:00
parent 7a68e83afa
commit 2a6a09da76

View file

@ -2,6 +2,7 @@ import {existsSync, mkdirSync, readFileSync, unlinkSync, writeFileSync} from "fs
import SmallLicense from "../Models/smallLicense"; import SmallLicense from "../Models/smallLicense";
import ScriptUtils from "./ScriptUtils"; import ScriptUtils from "./ScriptUtils";
const prompt = require('prompt-sync')();
function validateLicenseInfo(l : SmallLicense){ function validateLicenseInfo(l : SmallLicense){
l.sources.map(s => new URL(s)) l.sources.map(s => new URL(s))
@ -55,7 +56,6 @@ function missingLicenseInfos(licenseInfos: SmallLicense[], allIcons: string[]) {
return missing; return missing;
} }
const prompt = require('prompt-sync')();
const knownLicenses = new Map<string, SmallLicense>() const knownLicenses = new Map<string, SmallLicense>()
knownLicenses.set("me", { knownLicenses.set("me", {
@ -64,45 +64,36 @@ knownLicenses.set("me", {
license: "CC0", license: "CC0",
sources: [] sources: []
}) })
knownLicenses.set("streetcomplete", { knownLicenses.set("streetcomplete", {
authors: ["Tobias Zwick (westnordost)"], authors: ["Tobias Zwick (westnordost)"],
path: undefined, path: undefined,
license: "CC0", license: "CC0",
sources: ["https://github.com/streetcomplete/StreetComplete/tree/master/res/graphics", "https://f-droid.org/packages/de.westnordost.streetcomplete/"] sources: ["https://github.com/streetcomplete/StreetComplete/tree/master/res/graphics", "https://f-droid.org/packages/de.westnordost.streetcomplete/"]
}) })
knownLicenses.set("t", { knownLicenses.set("t", {
authors: [], authors: [],
path: undefined, path: undefined,
license: "CC0; trivial", license: "CC0; trivial",
sources: [] sources: []
}) })
knownLicenses.set("na", { knownLicenses.set("na", {
authors: [], authors: [],
path: undefined, path: undefined,
license: "CC0", license: "CC0",
sources: [] sources: []
}) })
knownLicenses.set("tv", { knownLicenses.set("tv", {
authors: ["Toerisme Vlaanderen"], authors: ["Toerisme Vlaanderen"],
path: undefined, path: undefined,
license: "CC0", license: "CC0",
sources: ["https://toerismevlaanderen.be/pinjepunt","https://mapcomplete.osm.be/toerisme_vlaanderenn"] sources: ["https://toerismevlaanderen.be/pinjepunt","https://mapcomplete.osm.be/toerisme_vlaanderenn"]
}) })
knownLicenses.set("tvf", { knownLicenses.set("tvf", {
authors: ["Jo De Baerdemaeker "], authors: ["Jo De Baerdemaeker "],
path: undefined, path: undefined,
license: "All rights reserved", license: "All rights reserved",
sources: ["https://www.studiotype.be/fonts/flandersart"] sources: ["https://www.studiotype.be/fonts/flandersart"]
}) })
knownLicenses.set("twemoji", { knownLicenses.set("twemoji", {
authors: ["Twemoji"], authors: ["Twemoji"],
path: undefined, path: undefined,
@ -154,7 +145,7 @@ function createLicenseInfoFor(path): void {
function cleanLicenseInfo(allPaths: string[], allLicenseInfos: SmallLicense[]) { function cleanLicenseInfo(allPaths: string[], allLicenseInfos: SmallLicense[]) {
// Read the license info file from the generated assets, creates a compiled license info in every directory // Read the license info file from the generated assets, creates a compiled license info in every directory
// Note: this removes all the old license infos // Note: this removes all the old license infos
for (const licensePath of licensePaths) { for (const licensePath of allPaths) {
unlinkSync(licensePath) unlinkSync(licensePath)
} }
@ -219,10 +210,13 @@ function queryMissingLicenses(missingLicenses: string[]) {
* Creates the humongous license_info in the generated assets, containing all licenses with a path relative to the root * Creates the humongous license_info in the generated assets, containing all licenses with a path relative to the root
* @param licensePaths * @param licensePaths
*/ */
function createFullLicenseOverview(licensePaths) { function createFullLicenseOverview(licensePaths: string[]) {
const allLicenses: SmallLicense[] = [] const allLicenses: SmallLicense[] = []
for (const licensePath of licensePaths) { for (const licensePath of licensePaths) {
if(!existsSync(licensePath)){
continue
}
const licenses = <SmallLicense[]>JSON.parse(readFileSync(licensePath, "UTF-8")) const licenses = <SmallLicense[]>JSON.parse(readFileSync(licensePath, "UTF-8"))
for (const license of licenses) { for (const license of licenses) {
validateLicenseInfo(license) validateLicenseInfo(license)
@ -235,52 +229,54 @@ function createFullLicenseOverview(licensePaths) {
writeFileSync("./assets/generated/license_info.json", JSON.stringify(allLicenses, null, " ")) writeFileSync("./assets/generated/license_info.json", JSON.stringify(allLicenses, null, " "))
} }
console.log("Checking and compiling license info") function main(args: string[]){
if (!existsSync("./assets/generated")) { console.log("Checking and compiling license info")
mkdirSync("./assets/generated")
} if (!existsSync("./assets/generated")) {
mkdirSync("./assets/generated")
}
let contents = ScriptUtils.readDirRecSync("./assets")
.filter(entry => entry.indexOf("./assets/generated") != 0)
let licensePaths = contents.filter(entry => entry.indexOf("license_info.json") >= 0) let contents = ScriptUtils.readDirRecSync("./assets")
let licenseInfos = generateLicenseInfos(licensePaths);
const artwork = contents.filter(pth => pth.match(/(.svg|.png|.jpg|.ttf|.otf|.woff)$/i) != null)
const missingLicenses = missingLicenseInfos(licenseInfos, artwork)
if (process.argv.indexOf("--prompt") >= 0 || process.argv.indexOf("--query") >= 0) {
queryMissingLicenses(missingLicenses)
contents = ScriptUtils.readDirRecSync("./assets")
.filter(entry => entry.indexOf("./assets/generated") != 0) .filter(entry => entry.indexOf("./assets/generated") != 0)
licensePaths = contents.filter(entry => entry.indexOf("license_info.json") >= 0) let licensePaths = contents.filter(entry => entry.indexOf("license_info.json") >= 0)
licenseInfos = generateLicenseInfos(licensePaths); let licenseInfos = generateLicenseInfos(licensePaths);
}
const invalidLicenses = licenseInfos.filter(l => (l.license ?? "") === "").map(l => `License for artwork ${l.path} is empty string or undefined`)
for (const licenseInfo of licenseInfos) { const artwork = contents.filter(pth => pth.match(/(.svg|.png|.jpg|.ttf|.otf|.woff)$/i) != null)
for (const source of licenseInfo.sources) { const missingLicenses = missingLicenseInfos(licenseInfos, artwork)
if (source == "") { if (args.indexOf("--prompt") >= 0 || args.indexOf("--query") >= 0) {
invalidLicenses.push("Invalid license: empty string in " + JSON.stringify(licenseInfo)) queryMissingLicenses(missingLicenses)
} return main([])
try { }
new URL(source);
} catch { const invalidLicenses = licenseInfos.filter(l => (l.license ?? "") === "").map(l => `License for artwork ${l.path} is empty string or undefined`)
invalidLicenses.push("Not a valid URL: " + source) for (const licenseInfo of licenseInfos) {
for (const source of licenseInfo.sources) {
if (source == "") {
invalidLicenses.push("Invalid license: empty string in " + JSON.stringify(licenseInfo))
}
try {
new URL(source);
} catch {
invalidLicenses.push("Not a valid URL: " + source)
}
} }
} }
}
if (missingLicenses.length > 0) {
if (missingLicenses.length > 0) { const msg = `There are ${missingLicenses.length} licenses missing and ${invalidLicenses.length} invalid licenses.`
const msg = `There are ${missingLicenses.length} licenses missing and ${invalidLicenses.length} invalid licenses.` console.log(missingLicenses.concat(invalidLicenses).join("\n"))
console.log(missingLicenses.concat(invalidLicenses).join("\n")) console.error(msg)
console.error(msg) if (args.indexOf("--no-fail") < 0) {
if (process.argv.indexOf("--no-fail") < 0) { throw msg
throw msg }
} }
cleanLicenseInfo(licensePaths, licenseInfos)
createFullLicenseOverview(licensePaths)
} }
cleanLicenseInfo(licensePaths, licenseInfos) main(process.argv.slice(2))
createFullLicenseOverview(licensePaths)