forked from MapComplete/MapComplete
refactoring: make scripts use 'script'-interface'
This commit is contained in:
parent
99eb397d31
commit
57d5eac463
2 changed files with 337 additions and 307 deletions
|
@ -22,15 +22,19 @@ import { PrepareTheme } from "../Models/ThemeConfig/Conversion/PrepareTheme"
|
|||
import { DesugaringContext } from "../Models/ThemeConfig/Conversion/Conversion"
|
||||
import { Utils } from "../Utils"
|
||||
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.
|
||||
// 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 themePath = "./assets/generated/themes/"
|
||||
|
||||
constructor() {
|
||||
super("Reviews and generates the compiled themes")
|
||||
}
|
||||
private static publicLayerIdsFrom(themefiles: LayoutConfigJson[]): Set<string> {
|
||||
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 licensePaths = new Set<string>()
|
||||
|
@ -502,4 +506,4 @@ class LayerOverviewUtils {
|
|||
}
|
||||
}
|
||||
|
||||
new LayerOverviewUtils().main(process.argv)
|
||||
new LayerOverviewUtils().run()
|
||||
|
|
|
@ -1,68 +1,16 @@
|
|||
import { existsSync, mkdirSync, readFileSync, unlinkSync, writeFileSync } from "fs"
|
||||
import SmallLicense from "../Models/smallLicense"
|
||||
import ScriptUtils from "./ScriptUtils"
|
||||
import Script from "./Script"
|
||||
|
||||
const prompt = require("prompt-sync")()
|
||||
|
||||
function 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
|
||||
*/
|
||||
|
||||
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
|
||||
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"],
|
||||
|
@ -84,7 +32,10 @@ knownLicenses.set("temaki", {
|
|||
authors: ["Temaki"],
|
||||
path: undefined,
|
||||
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", {
|
||||
|
@ -127,13 +78,79 @@ knownLicenses.set("twemoji", {
|
|||
license: "CC-BY 4.0",
|
||||
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:")
|
||||
knownLicenses.forEach((value, key) => {
|
||||
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)
|
||||
|
||||
if (knownLicenses.has(author)) {
|
||||
|
@ -160,15 +177,15 @@ function promptLicenseFor(path): SmallLicense {
|
|||
}
|
||||
}
|
||||
|
||||
function createLicenseInfoFor(path): void {
|
||||
const li = promptLicenseFor(path)
|
||||
createLicenseInfoFor(path): void {
|
||||
const li = this.promptLicenseFor(path)
|
||||
if (li == null) {
|
||||
return
|
||||
}
|
||||
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
|
||||
// Note: this removes all the old license infos
|
||||
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 () {
|
||||
console.log("Aborting... Bye!")
|
||||
process.exit()
|
||||
|
@ -226,7 +243,7 @@ function queryMissingLicenses(missingLicenses: string[]) {
|
|||
if (i < missingLicenses.length - 5) {
|
||||
// continue
|
||||
}
|
||||
createLicenseInfoFor(missingLicens)
|
||||
this.createLicenseInfoFor(missingLicens)
|
||||
}
|
||||
|
||||
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
|
||||
* @param licensePaths
|
||||
*/
|
||||
function createFullLicenseOverview(licensePaths: string[]) {
|
||||
createFullLicenseOverview(licensePaths: string[]) {
|
||||
const allLicenses: SmallLicense[] = []
|
||||
for (const licensePath of licensePaths) {
|
||||
if (!existsSync(licensePath)) {
|
||||
continue
|
||||
}
|
||||
const licenses = <SmallLicense[]>JSON.parse(readFileSync(licensePath, { encoding: "utf8" }))
|
||||
const licenses = <SmallLicense[]>(
|
||||
JSON.parse(readFileSync(licensePath, { encoding: "utf8" }))
|
||||
)
|
||||
for (const license of licenses) {
|
||||
validateLicenseInfo(license)
|
||||
const dir = licensePath.substring(0, licensePath.length - "license_info.json".length)
|
||||
this.validateLicenseInfo(license)
|
||||
const dir = licensePath.substring(
|
||||
0,
|
||||
licensePath.length - "license_info.json".length
|
||||
)
|
||||
license.path = dir + license.path
|
||||
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")
|
||||
|
||||
if (!existsSync("./assets/generated")) {
|
||||
|
@ -265,15 +290,15 @@ function main(args: string[]) {
|
|||
.filter((p) => !p.startsWith("./assets/templates/"))
|
||||
.filter((entry) => entry.indexOf("./assets/generated") != 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(
|
||||
(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) {
|
||||
queryMissingLicenses(missingLicenses)
|
||||
return main([])
|
||||
this.queryMissingLicenses(missingLicenses)
|
||||
return this.main([])
|
||||
}
|
||||
|
||||
const invalidLicenses = licenseInfos
|
||||
|
@ -319,8 +344,9 @@ function main(args: string[]) {
|
|||
}
|
||||
}
|
||||
|
||||
cleanLicenseInfo(licensePaths, licenseInfos)
|
||||
createFullLicenseOverview(licensePaths)
|
||||
this.cleanLicenseInfo(licensePaths, licenseInfos)
|
||||
this.createFullLicenseOverview(licensePaths)
|
||||
}
|
||||
}
|
||||
|
||||
main(process.argv.slice(2))
|
||||
new GenerateLicenseInfo().run()
|
||||
|
|
Loading…
Reference in a new issue