refactoring: make scripts use 'script'-interface'

This commit is contained in:
Pieter Vander Vennet 2023-03-15 13:53:53 +01:00
parent 99eb397d31
commit 57d5eac463
2 changed files with 337 additions and 307 deletions

View file

@ -22,15 +22,19 @@ import { PrepareTheme } from "../Models/ThemeConfig/Conversion/PrepareTheme"
import { DesugaringContext } from "../Models/ThemeConfig/Conversion/Conversion" import { DesugaringContext } from "../Models/ThemeConfig/Conversion/Conversion"
import { Utils } from "../Utils" import { Utils } from "../Utils"
import { AllKnownLayouts } from "../Customizations/AllKnownLayouts" import { AllKnownLayouts } from "../Customizations/AllKnownLayouts"
import { Script } from "vm" import Script from "./Script"
import { GenerateLicenseInfo } from "./generateLicenseInfo"
// This scripts scans 'assets/layers/*.json' for layer definition files and 'assets/themes/*.json' for theme definition files. // This scripts scans 'assets/layers/*.json' for layer definition files and 'assets/themes/*.json' for theme definition files.
// It spits out an overview of those to be used to load them // It spits out an overview of those to be used to load them
class LayerOverviewUtils { class LayerOverviewUtils extends Script {
public static readonly layerPath = "./assets/generated/layers/" public static readonly layerPath = "./assets/generated/layers/"
public static readonly themePath = "./assets/generated/themes/" public static readonly themePath = "./assets/generated/themes/"
constructor() {
super("Reviews and generates the compiled themes")
}
private static publicLayerIdsFrom(themefiles: LayoutConfigJson[]): Set<string> { private static publicLayerIdsFrom(themefiles: LayoutConfigJson[]): Set<string> {
const publicThemes = [].concat(...themefiles.filter((th) => !th.hideFromOverview)) const publicThemes = [].concat(...themefiles.filter((th) => !th.hideFromOverview))
@ -241,7 +245,7 @@ class LayerOverviewUtils {
} }
} }
main(args: string[]) { async main(args: string[]) {
const forceReload = args.some((a) => a == "--force") const forceReload = args.some((a) => a == "--force")
const licensePaths = new Set<string>() const licensePaths = new Set<string>()
@ -502,4 +506,4 @@ class LayerOverviewUtils {
} }
} }
new LayerOverviewUtils().main(process.argv) new LayerOverviewUtils().run()

View file

@ -1,10 +1,87 @@
import { existsSync, mkdirSync, readFileSync, unlinkSync, writeFileSync } from "fs" 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"
import Script from "./Script"
const prompt = require("prompt-sync")() const prompt = require("prompt-sync")()
function validateLicenseInfo(l: SmallLicense) { export class GenerateLicenseInfo extends Script {
constructor() {
super("Validates the licenses and compiles them into one single asset file")
}
static defaultLicenses() {
const knownLicenses = new Map<string, SmallLicense>()
knownLicenses.set("me", {
authors: ["Pieter Vander Vennet"],
path: undefined,
license: "CC0",
sources: [],
})
knownLicenses.set("streetcomplete", {
authors: ["Tobias Zwick (westnordost)"],
path: undefined,
license: "CC0",
sources: [
"https://github.com/streetcomplete/StreetComplete/tree/master/res/graphics",
"https://f-droid.org/packages/de.westnordost.streetcomplete/",
],
})
knownLicenses.set("temaki", {
authors: ["Temaki"],
path: undefined,
license: "CC0",
sources: [
"https://github.com/ideditor/temaki",
"https://ideditor.github.io/temaki/docs/",
],
})
knownLicenses.set("maki", {
authors: ["Maki"],
path: undefined,
license: "CC0",
sources: ["https://labs.mapbox.com/maki-icons/"],
})
knownLicenses.set("t", {
authors: [],
path: undefined,
license: "CC0; trivial",
sources: [],
})
knownLicenses.set("na", {
authors: [],
path: undefined,
license: "CC0",
sources: [],
})
knownLicenses.set("tv", {
authors: ["Toerisme Vlaanderen"],
path: undefined,
license: "CC0",
sources: [
"https://toerismevlaanderen.be/pinjepunt",
"https://mapcomplete.osm.be/toerisme_vlaanderenn",
],
})
knownLicenses.set("tvf", {
authors: ["Jo De Baerdemaeker "],
path: undefined,
license: "All rights reserved",
sources: ["https://www.studiotype.be/fonts/flandersart"],
})
knownLicenses.set("twemoji", {
authors: ["Twemoji"],
path: undefined,
license: "CC-BY 4.0",
sources: ["https://github.com/twitter/twemoji"],
})
return knownLicenses
}
validateLicenseInfo(l: SmallLicense) {
l.sources.map((s) => { l.sources.map((s) => {
try { try {
return new URL(s) return new URL(s)
@ -12,13 +89,14 @@ function validateLicenseInfo(l: SmallLicense) {
throw "Could not parse URL " + s + " for a license for " + l.path + " due to " + e throw "Could not parse URL " + s + " for a license for " + l.path + " due to " + e
} }
}) })
} }
/**
/**
* Sweeps the entire 'assets/' (except assets/generated) directory for image files and any 'license_info.json'-file. * Sweeps the entire 'assets/' (except assets/generated) directory for image files and any 'license_info.json'-file.
* Checks that the license info is included for each of them and generates a compiles license_info.json for those * Checks that the license info is included for each of them and generates a compiles license_info.json for those
*/ */
function generateLicenseInfos(paths: string[]): SmallLicense[] { generateLicenseInfos(paths: string[]): SmallLicense[] {
const licenses = [] const licenses = []
for (const path of paths) { for (const path of paths) {
try { try {
@ -33,7 +111,8 @@ function generateLicenseInfos(paths: string[]): SmallLicense[] {
licenses.push(...l) licenses.push(...l)
} else { } else {
const smallLicens: SmallLicense = parsed const smallLicens: SmallLicense = parsed
smallLicens.path = path.substring(0, 1 + path.lastIndexOf("/")) + smallLicens.path smallLicens.path =
path.substring(0, 1 + path.lastIndexOf("/")) + smallLicens.path
licenses.push(smallLicens) licenses.push(smallLicens)
} }
} catch (e) { } catch (e) {
@ -41,9 +120,9 @@ function generateLicenseInfos(paths: string[]): SmallLicense[] {
} }
} }
return licenses return licenses
} }
function missingLicenseInfos(licenseInfos: SmallLicense[], allIcons: string[]) { missingLicenseInfos(licenseInfos: SmallLicense[], allIcons: string[]) {
const missing = [] const missing = []
const knownPaths = new Set<string>() const knownPaths = new Set<string>()
@ -61,79 +140,17 @@ function missingLicenseInfos(licenseInfos: SmallLicense[], allIcons: string[]) {
missing.push(iconPath) missing.push(iconPath)
} }
return missing return missing
} }
const knownLicenses = new Map<string, SmallLicense>() promptLicenseFor(path): SmallLicense {
knownLicenses.set("me", { const knownLicenses = GenerateLicenseInfo.defaultLicenses()
authors: ["Pieter Vander Vennet"],
path: undefined,
license: "CC0",
sources: [],
})
knownLicenses.set("streetcomplete", {
authors: ["Tobias Zwick (westnordost)"],
path: undefined,
license: "CC0",
sources: [
"https://github.com/streetcomplete/StreetComplete/tree/master/res/graphics",
"https://f-droid.org/packages/de.westnordost.streetcomplete/",
],
})
knownLicenses.set("temaki", {
authors: ["Temaki"],
path: undefined,
license: "CC0",
sources: ["https://github.com/ideditor/temaki", "https://ideditor.github.io/temaki/docs/"],
})
knownLicenses.set("maki", {
authors: ["Maki"],
path: undefined,
license: "CC0",
sources: ["https://labs.mapbox.com/maki-icons/"],
})
knownLicenses.set("t", {
authors: [],
path: undefined,
license: "CC0; trivial",
sources: [],
})
knownLicenses.set("na", {
authors: [],
path: undefined,
license: "CC0",
sources: [],
})
knownLicenses.set("tv", {
authors: ["Toerisme Vlaanderen"],
path: undefined,
license: "CC0",
sources: [
"https://toerismevlaanderen.be/pinjepunt",
"https://mapcomplete.osm.be/toerisme_vlaanderenn",
],
})
knownLicenses.set("tvf", {
authors: ["Jo De Baerdemaeker "],
path: undefined,
license: "All rights reserved",
sources: ["https://www.studiotype.be/fonts/flandersart"],
})
knownLicenses.set("twemoji", {
authors: ["Twemoji"],
path: undefined,
license: "CC-BY 4.0",
sources: ["https://github.com/twitter/twemoji"],
})
function promptLicenseFor(path): SmallLicense {
console.log("License abbreviations:") console.log("License abbreviations:")
knownLicenses.forEach((value, key) => { knownLicenses.forEach((value, key) => {
console.log(key, " => ", value) console.log(key, " => ", value)
}) })
const author = prompt("What is the author for artwork " + path + "? (or: [Q]uit, [S]kip) > ") const author = prompt(
"What is the author for artwork " + path + "? (or: [Q]uit, [S]kip) > "
)
path = path.substring(path.lastIndexOf("/") + 1) path = path.substring(path.lastIndexOf("/") + 1)
if (knownLicenses.has(author)) { if (knownLicenses.has(author)) {
@ -158,17 +175,17 @@ function promptLicenseFor(path): SmallLicense {
license: prompt("What is the license for artwork " + path + "? > "), license: prompt("What is the license for artwork " + path + "? > "),
sources: prompt("Where was this artwork found? > ").split(";"), sources: prompt("Where was this artwork found? > ").split(";"),
} }
} }
function createLicenseInfoFor(path): void { createLicenseInfoFor(path): void {
const li = promptLicenseFor(path) const li = this.promptLicenseFor(path)
if (li == null) { if (li == null) {
return return
} }
writeFileSync(path + ".license_info.json", JSON.stringify(li, null, " ")) writeFileSync(path + ".license_info.json", JSON.stringify(li, null, " "))
} }
function cleanLicenseInfo(allPaths: string[], allLicenseInfos: SmallLicense[]) { 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 allPaths) { for (const licensePath of allPaths) {
@ -211,9 +228,9 @@ function cleanLicenseInfo(allPaths: string[], allLicenseInfos: SmallLicense[]) {
licenses.sort((a, b) => (a.path < b.path ? -1 : 1)) licenses.sort((a, b) => (a.path < b.path ? -1 : 1))
writeFileSync(dir + "/license_info.json", JSON.stringify(licenses, null, 2)) writeFileSync(dir + "/license_info.json", JSON.stringify(licenses, null, 2))
}) })
} }
function queryMissingLicenses(missingLicenses: string[]) { queryMissingLicenses(missingLicenses: string[]) {
process.on("SIGINT", function () { process.on("SIGINT", function () {
console.log("Aborting... Bye!") console.log("Aborting... Bye!")
process.exit() process.exit()
@ -226,35 +243,43 @@ function queryMissingLicenses(missingLicenses: string[]) {
if (i < missingLicenses.length - 5) { if (i < missingLicenses.length - 5) {
// continue // continue
} }
createLicenseInfoFor(missingLicens) this.createLicenseInfoFor(missingLicens)
} }
console.log("You're through!") console.log("You're through!")
} }
/** /**
* 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: string[]) { createFullLicenseOverview(licensePaths: string[]) {
const allLicenses: SmallLicense[] = [] const allLicenses: SmallLicense[] = []
for (const licensePath of licensePaths) { for (const licensePath of licensePaths) {
if (!existsSync(licensePath)) { if (!existsSync(licensePath)) {
continue continue
} }
const licenses = <SmallLicense[]>JSON.parse(readFileSync(licensePath, { encoding: "utf8" })) const licenses = <SmallLicense[]>(
JSON.parse(readFileSync(licensePath, { encoding: "utf8" }))
)
for (const license of licenses) { for (const license of licenses) {
validateLicenseInfo(license) this.validateLicenseInfo(license)
const dir = licensePath.substring(0, licensePath.length - "license_info.json".length) const dir = licensePath.substring(
0,
licensePath.length - "license_info.json".length
)
license.path = dir + license.path license.path = dir + license.path
allLicenses.push(license) allLicenses.push(license)
} }
} }
writeFileSync("./assets/generated/license_info.json", JSON.stringify(allLicenses, null, " ")) writeFileSync(
} "./assets/generated/license_info.json",
JSON.stringify(allLicenses, null, " ")
)
}
function main(args: string[]) { async main(args: string[]) {
console.log("Checking and compiling license info") console.log("Checking and compiling license info")
if (!existsSync("./assets/generated")) { if (!existsSync("./assets/generated")) {
@ -265,15 +290,15 @@ function main(args: string[]) {
.filter((p) => !p.startsWith("./assets/templates/")) .filter((p) => !p.startsWith("./assets/templates/"))
.filter((entry) => entry.indexOf("./assets/generated") != 0) .filter((entry) => entry.indexOf("./assets/generated") != 0)
let licensePaths = contents.filter((entry) => entry.indexOf("license_info.json") >= 0) let licensePaths = contents.filter((entry) => entry.indexOf("license_info.json") >= 0)
let licenseInfos = generateLicenseInfos(licensePaths) let licenseInfos = this.generateLicenseInfos(licensePaths)
const artwork = contents.filter( const artwork = contents.filter(
(pth) => pth.match(/(.svg|.png|.jpg|.ttf|.otf|.woff)$/i) != null (pth) => pth.match(/(.svg|.png|.jpg|.ttf|.otf|.woff)$/i) != null
) )
const missingLicenses = missingLicenseInfos(licenseInfos, artwork) const missingLicenses = this.missingLicenseInfos(licenseInfos, artwork)
if (args.indexOf("--prompt") >= 0 || args.indexOf("--query") >= 0) { if (args.indexOf("--prompt") >= 0 || args.indexOf("--query") >= 0) {
queryMissingLicenses(missingLicenses) this.queryMissingLicenses(missingLicenses)
return main([]) return this.main([])
} }
const invalidLicenses = licenseInfos const invalidLicenses = licenseInfos
@ -319,8 +344,9 @@ function main(args: string[]) {
} }
} }
cleanLicenseInfo(licensePaths, licenseInfos) this.cleanLicenseInfo(licensePaths, licenseInfos)
createFullLicenseOverview(licensePaths) this.createFullLicenseOverview(licensePaths)
}
} }
main(process.argv.slice(2)) new GenerateLicenseInfo().run()