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,68 +1,16 @@
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 {
l.sources.map((s) => { constructor() {
try { super("Validates the licenses and compiles them into one single asset file")
return new URL(s)
} catch (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.
* 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[] {
const licenses = []
for (const path of paths) {
try {
const parsed = JSON.parse(readFileSync(path, { encoding: "utf8" }))
if (Array.isArray(parsed)) {
const l: SmallLicense[] = parsed
for (const smallLicens of l) {
smallLicens.path =
path.substring(0, path.length - "license_info.json".length) +
smallLicens.path
}
licenses.push(...l)
} else {
const smallLicens: SmallLicense = parsed
smallLicens.path = path.substring(0, 1 + path.lastIndexOf("/")) + smallLicens.path
licenses.push(smallLicens)
}
} catch (e) {
console.error("Error: ", e, "while handling", path)
}
}
return licenses
}
function missingLicenseInfos(licenseInfos: SmallLicense[], allIcons: string[]) {
const missing = []
const knownPaths = new Set<string>()
for (const licenseInfo of licenseInfos) {
knownPaths.add(licenseInfo.path)
}
for (const iconPath of allIcons) {
if (iconPath.indexOf("license_info.json") >= 0) {
continue
}
if (knownPaths.has(iconPath)) {
continue
}
missing.push(iconPath)
}
return missing
} }
static defaultLicenses() {
const knownLicenses = new Map<string, SmallLicense>() const knownLicenses = new Map<string, SmallLicense>()
knownLicenses.set("me", { knownLicenses.set("me", {
authors: ["Pieter Vander Vennet"], authors: ["Pieter Vander Vennet"],
@ -84,7 +32,10 @@ knownLicenses.set("temaki", {
authors: ["Temaki"], authors: ["Temaki"],
path: undefined, path: undefined,
license: "CC0", license: "CC0",
sources: ["https://github.com/ideditor/temaki", "https://ideditor.github.io/temaki/docs/"], sources: [
"https://github.com/ideditor/temaki",
"https://ideditor.github.io/temaki/docs/",
],
}) })
knownLicenses.set("maki", { knownLicenses.set("maki", {
@ -127,13 +78,79 @@ knownLicenses.set("twemoji", {
license: "CC-BY 4.0", license: "CC-BY 4.0",
sources: ["https://github.com/twitter/twemoji"], sources: ["https://github.com/twitter/twemoji"],
}) })
return knownLicenses
}
function promptLicenseFor(path): SmallLicense { validateLicenseInfo(l: SmallLicense) {
l.sources.map((s) => {
try {
return new URL(s)
} catch (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.
* Checks that the license info is included for each of them and generates a compiles license_info.json for those
*/
generateLicenseInfos(paths: string[]): SmallLicense[] {
const licenses = []
for (const path of paths) {
try {
const parsed = JSON.parse(readFileSync(path, { encoding: "utf8" }))
if (Array.isArray(parsed)) {
const l: SmallLicense[] = parsed
for (const smallLicens of l) {
smallLicens.path =
path.substring(0, path.length - "license_info.json".length) +
smallLicens.path
}
licenses.push(...l)
} else {
const smallLicens: SmallLicense = parsed
smallLicens.path =
path.substring(0, 1 + path.lastIndexOf("/")) + smallLicens.path
licenses.push(smallLicens)
}
} catch (e) {
console.error("Error: ", e, "while handling", path)
}
}
return licenses
}
missingLicenseInfos(licenseInfos: SmallLicense[], allIcons: string[]) {
const missing = []
const knownPaths = new Set<string>()
for (const licenseInfo of licenseInfos) {
knownPaths.add(licenseInfo.path)
}
for (const iconPath of allIcons) {
if (iconPath.indexOf("license_info.json") >= 0) {
continue
}
if (knownPaths.has(iconPath)) {
continue
}
missing.push(iconPath)
}
return missing
}
promptLicenseFor(path): SmallLicense {
const knownLicenses = GenerateLicenseInfo.defaultLicenses()
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)) {
@ -160,15 +177,15 @@ function promptLicenseFor(path): SmallLicense {
} }
} }
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) {
@ -213,7 +230,7 @@ function cleanLicenseInfo(allPaths: string[], allLicenseInfos: SmallLicense[]) {
}) })
} }
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,7 +243,7 @@ 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!")
@ -236,25 +253,33 @@ 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: 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()