| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  | import { existsSync, mkdirSync, readFileSync, unlinkSync, writeFileSync } from "fs" | 
					
						
							| 
									
										
										
										
											2023-07-15 18:04:30 +02:00
										 |  |  | import SmallLicense from "../src/Models/smallLicense" | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  | import ScriptUtils from "./ScriptUtils" | 
					
						
							| 
									
										
										
										
											2023-03-15 13:53:53 +01:00
										 |  |  | import Script from "./Script" | 
					
						
							| 
									
										
										
										
											2023-07-27 03:32:49 +02:00
										 |  |  | import { Utils } from "../src/Utils" | 
					
						
							| 
									
										
										
										
											2024-07-23 17:59:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-21 01:19:31 +02:00
										 |  |  | const prompt = require("prompt-sync")() | 
					
						
							| 
									
										
										
										
											2024-07-23 17:59:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-15 13:53:53 +01:00
										 |  |  | export class GenerateLicenseInfo extends Script { | 
					
						
							| 
									
										
										
										
											2023-07-27 14:34:05 +02:00
										 |  |  |     private static readonly needsLicenseRef = new Set( | 
					
						
							|  |  |  |         ScriptUtils.readDirRecSync("./LICENSES") | 
					
						
							|  |  |  |             .map((p) => p.substring(p.lastIndexOf("/") + 1)) | 
					
						
							|  |  |  |             .filter((p) => p.startsWith("LicenseRef-")) | 
					
						
							|  |  |  |             .map((p) => p.substring("LicenseRef-".length)) | 
					
						
							|  |  |  |             .map((p) => p.substring(0, p.lastIndexOf("."))) | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-15 13:53:53 +01:00
										 |  |  |     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", | 
					
						
							| 
									
										
										
										
											2024-08-09 16:55:08 +02:00
										 |  |  |             sources: [], | 
					
						
							| 
									
										
										
										
											2023-03-15 13:53:53 +01:00
										 |  |  |         }) | 
					
						
							|  |  |  |         knownLicenses.set("streetcomplete", { | 
					
						
							|  |  |  |             authors: ["Tobias Zwick (westnordost)"], | 
					
						
							|  |  |  |             path: undefined, | 
					
						
							|  |  |  |             license: "CC0", | 
					
						
							|  |  |  |             sources: [ | 
					
						
							|  |  |  |                 "https://github.com/streetcomplete/StreetComplete/tree/master/res/graphics", | 
					
						
							| 
									
										
										
										
											2024-08-09 16:55:08 +02:00
										 |  |  |                 "https://f-droid.org/packages/de.westnordost.streetcomplete/", | 
					
						
							|  |  |  |             ], | 
					
						
							| 
									
										
										
										
											2023-03-15 13:53:53 +01:00
										 |  |  |         }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         knownLicenses.set("temaki", { | 
					
						
							|  |  |  |             authors: ["Temaki"], | 
					
						
							|  |  |  |             path: undefined, | 
					
						
							|  |  |  |             license: "CC0", | 
					
						
							|  |  |  |             sources: [ | 
					
						
							|  |  |  |                 "https://github.com/ideditor/temaki", | 
					
						
							| 
									
										
										
										
											2024-08-09 16:55:08 +02:00
										 |  |  |                 "https://ideditor.github.io/temaki/docs/", | 
					
						
							|  |  |  |             ], | 
					
						
							| 
									
										
										
										
											2023-03-15 13:53:53 +01:00
										 |  |  |         }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         knownLicenses.set("maki", { | 
					
						
							|  |  |  |             authors: ["Maki"], | 
					
						
							|  |  |  |             path: undefined, | 
					
						
							|  |  |  |             license: "CC0", | 
					
						
							| 
									
										
										
										
											2024-08-09 16:55:08 +02:00
										 |  |  |             sources: ["https://labs.mapbox.com/maki-icons/"], | 
					
						
							| 
									
										
										
										
											2023-03-15 13:53:53 +01:00
										 |  |  |         }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         knownLicenses.set("t", { | 
					
						
							|  |  |  |             authors: [], | 
					
						
							|  |  |  |             path: undefined, | 
					
						
							|  |  |  |             license: "CC0; trivial", | 
					
						
							| 
									
										
										
										
											2024-08-09 16:55:08 +02:00
										 |  |  |             sources: [], | 
					
						
							| 
									
										
										
										
											2023-03-15 13:53:53 +01:00
										 |  |  |         }) | 
					
						
							|  |  |  |         knownLicenses.set("na", { | 
					
						
							|  |  |  |             authors: [], | 
					
						
							|  |  |  |             path: undefined, | 
					
						
							|  |  |  |             license: "CC0", | 
					
						
							| 
									
										
										
										
											2024-08-09 16:55:08 +02:00
										 |  |  |             sources: [], | 
					
						
							| 
									
										
										
										
											2023-03-15 13:53:53 +01:00
										 |  |  |         }) | 
					
						
							| 
									
										
										
										
											2023-10-30 14:32:31 +01:00
										 |  |  |         knownLicenses.set("carto", { | 
					
						
							|  |  |  |             authors: ["OSM-Carto"], | 
					
						
							|  |  |  |             path: undefined, | 
					
						
							|  |  |  |             license: "CC0", | 
					
						
							| 
									
										
										
										
											2024-08-09 16:55:08 +02:00
										 |  |  |             sources: [""], | 
					
						
							| 
									
										
										
										
											2023-10-30 14:32:31 +01:00
										 |  |  |         }) | 
					
						
							| 
									
										
										
										
											2023-03-15 13:53:53 +01:00
										 |  |  |         knownLicenses.set("tv", { | 
					
						
							|  |  |  |             authors: ["Toerisme Vlaanderen"], | 
					
						
							|  |  |  |             path: undefined, | 
					
						
							|  |  |  |             license: "CC0", | 
					
						
							|  |  |  |             sources: [ | 
					
						
							|  |  |  |                 "https://toerismevlaanderen.be/pinjepunt", | 
					
						
							| 
									
										
										
										
											2024-08-09 16:55:08 +02:00
										 |  |  |                 "https://mapcomplete.org/toerisme_vlaanderenn", | 
					
						
							|  |  |  |             ], | 
					
						
							| 
									
										
										
										
											2023-03-15 13:53:53 +01:00
										 |  |  |         }) | 
					
						
							|  |  |  |         knownLicenses.set("tvf", { | 
					
						
							|  |  |  |             authors: ["Jo De Baerdemaeker "], | 
					
						
							|  |  |  |             path: undefined, | 
					
						
							|  |  |  |             license: "All rights reserved", | 
					
						
							| 
									
										
										
										
											2024-08-09 16:55:08 +02:00
										 |  |  |             sources: ["https://www.studiotype.be/fonts/flandersart"], | 
					
						
							| 
									
										
										
										
											2023-03-15 13:53:53 +01:00
										 |  |  |         }) | 
					
						
							|  |  |  |         knownLicenses.set("twemoji", { | 
					
						
							|  |  |  |             authors: ["Twemoji"], | 
					
						
							|  |  |  |             path: undefined, | 
					
						
							|  |  |  |             license: "CC-BY 4.0", | 
					
						
							| 
									
										
										
										
											2024-08-09 16:55:08 +02:00
										 |  |  |             sources: ["https://github.com/twitter/twemoji"], | 
					
						
							| 
									
										
										
										
											2023-03-15 13:53:53 +01:00
										 |  |  |         }) | 
					
						
							|  |  |  |         return knownLicenses | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     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 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                     smallLicens.path = | 
					
						
							| 
									
										
										
										
											2023-03-15 13:53:53 +01:00
										 |  |  |                         path.substring(0, 1 + path.lastIndexOf("/")) + smallLicens.path | 
					
						
							|  |  |  |                     licenses.push(smallLicens) | 
					
						
							| 
									
										
										
										
											2021-05-07 02:06:08 +02:00
										 |  |  |                 } | 
					
						
							| 
									
										
										
										
											2023-03-15 13:53:53 +01:00
										 |  |  |             } catch (e) { | 
					
						
							|  |  |  |                 console.error("Error: ", e, "while handling", path) | 
					
						
							| 
									
										
										
										
											2021-04-07 01:32:39 +02:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-03-15 13:53:53 +01:00
										 |  |  |         return licenses | 
					
						
							| 
									
										
										
										
											2021-04-07 01:32:39 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-23 17:59:06 +02:00
										 |  |  |     async mostlyWhite(allIcons: string[]) { | 
					
						
							|  |  |  |         const whitePaths = new Set<string>() | 
					
						
							|  |  |  |         for (const icon of allIcons) { | 
					
						
							|  |  |  |             if (!icon.endsWith(".svg")) { | 
					
						
							|  |  |  |                 continue | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             const svg = await ScriptUtils.ReadSvg(icon) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             const colours = new Set<string>() | 
					
						
							| 
									
										
										
										
											2024-08-09 16:55:08 +02:00
										 |  |  |             Utils.WalkObject( | 
					
						
							|  |  |  |                 svg, | 
					
						
							|  |  |  |                 (leaf) => { | 
					
						
							|  |  |  |                     const style = leaf["style"].split(";") | 
					
						
							|  |  |  |                     for (const styleElement of style) { | 
					
						
							|  |  |  |                         const [key, value] = styleElement.split(":").map((x) => x.trim()) | 
					
						
							|  |  |  |                         if (value === "none") { | 
					
						
							|  |  |  |                             continue | 
					
						
							|  |  |  |                         } | 
					
						
							|  |  |  |                         if (key === "fill" || key === "stroke") { | 
					
						
							|  |  |  |                             colours.add(value) | 
					
						
							|  |  |  |                         } | 
					
						
							|  |  |  |                         return colours | 
					
						
							| 
									
										
										
										
											2024-07-23 17:59:06 +02:00
										 |  |  |                     } | 
					
						
							| 
									
										
										
										
											2024-08-09 16:55:08 +02:00
										 |  |  |                 }, | 
					
						
							|  |  |  |                 (leaf) => typeof leaf["style"] === "string" | 
					
						
							|  |  |  |             ) | 
					
						
							|  |  |  |             if (colours.size === 0) { | 
					
						
							| 
									
										
										
										
											2024-07-23 17:59:06 +02:00
										 |  |  |                 continue | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2024-08-09 16:55:08 +02:00
										 |  |  |             const whiteColours = Array.from(colours).map((c) => { | 
					
						
							| 
									
										
										
										
											2024-07-23 17:59:06 +02:00
										 |  |  |                 const rgb = Utils.color(c) | 
					
						
							| 
									
										
										
										
											2024-08-09 16:55:08 +02:00
										 |  |  |                 if (!rgb) { | 
					
						
							| 
									
										
										
										
											2024-07-23 17:59:06 +02:00
										 |  |  |                     return false | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2024-08-09 16:55:08 +02:00
										 |  |  |                 const { r, g, b } = rgb | 
					
						
							|  |  |  |                 return r > 245 && g > 245 && b > 245 | 
					
						
							| 
									
										
										
										
											2024-07-23 17:59:06 +02:00
										 |  |  |             }) | 
					
						
							| 
									
										
										
										
											2024-08-09 16:55:08 +02:00
										 |  |  |             const hasDark = whiteColours.some((isWhite) => !isWhite) | 
					
						
							|  |  |  |             if (!hasDark) { | 
					
						
							| 
									
										
										
										
											2024-07-23 17:59:06 +02:00
										 |  |  |                 whitePaths.add(icon) | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return whitePaths | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-15 13:53:53 +01:00
										 |  |  |     missingLicenseInfos(licenseInfos: SmallLicense[], allIcons: string[]) { | 
					
						
							|  |  |  |         const missing = [] | 
					
						
							| 
									
										
										
										
											2021-04-07 01:32:39 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-15 13:53:53 +01:00
										 |  |  |         const knownPaths = new Set<string>() | 
					
						
							|  |  |  |         for (const licenseInfo of licenseInfos) { | 
					
						
							|  |  |  |             knownPaths.add(licenseInfo.path) | 
					
						
							| 
									
										
										
										
											2021-04-07 01:32:39 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-15 13:53:53 +01:00
										 |  |  |         for (const iconPath of allIcons) { | 
					
						
							|  |  |  |             if (iconPath.indexOf("license_info.json") >= 0) { | 
					
						
							|  |  |  |                 continue | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             if (knownPaths.has(iconPath)) { | 
					
						
							|  |  |  |                 continue | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             missing.push(iconPath) | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return missing | 
					
						
							| 
									
										
										
										
											2021-04-07 01:32:39 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-04-07 21:58:51 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-15 13:53:53 +01:00
										 |  |  |     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)  > " | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |         path = path.substring(path.lastIndexOf("/") + 1) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (knownLicenses.has(author)) { | 
					
						
							|  |  |  |             const license = knownLicenses.get(author) | 
					
						
							|  |  |  |             license.path = path | 
					
						
							|  |  |  |             return license | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-04-07 01:32:39 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-15 13:53:53 +01:00
										 |  |  |         if (author == "s") { | 
					
						
							|  |  |  |             return null | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (author == "Q" || author == "q" || author == "") { | 
					
						
							|  |  |  |             throw "Quitting now!" | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return { | 
					
						
							|  |  |  |             authors: author.split(";"), | 
					
						
							|  |  |  |             path: path, | 
					
						
							|  |  |  |             license: prompt("What is the license for artwork " + path + "?  > "), | 
					
						
							| 
									
										
										
										
											2024-08-09 16:55:08 +02:00
										 |  |  |             sources: prompt("Where was this artwork found?  > ").split(";"), | 
					
						
							| 
									
										
										
										
											2023-03-15 13:53:53 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-04-07 01:32:39 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-15 13:53:53 +01:00
										 |  |  |     createLicenseInfoFor(path): void { | 
					
						
							|  |  |  |         const li = this.promptLicenseFor(path) | 
					
						
							|  |  |  |         if (li == null) { | 
					
						
							|  |  |  |             return | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         writeFileSync(path + ".license_info.json", JSON.stringify(li, null, "  ")) | 
					
						
							| 
									
										
										
										
											2021-04-10 13:57:16 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-04-11 01:58:51 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-27 03:32:49 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Rewrites a license into a SPDX-valid-ID. | 
					
						
							|  |  |  |      * Might involve some guesswork (e.g. 'CC-BY-SA' --> 'CC-BY-SA 4.0" | 
					
						
							|  |  |  |      * @param licenseId | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     toSPDXCompliantLicense(licenseId: string): string { | 
					
						
							| 
									
										
										
										
											2023-07-27 14:10:08 +02:00
										 |  |  |         licenseId = licenseId.trim() | 
					
						
							| 
									
										
										
										
											2023-07-27 03:32:49 +02:00
										 |  |  |         // https://spdx.org/licenses/
 | 
					
						
							|  |  |  |         const mappings: Record<string, string> = { | 
					
						
							|  |  |  |             "CC-0": "CC0-1.0", | 
					
						
							|  |  |  |             CC0: "CC0-1.0", | 
					
						
							|  |  |  |             "CC-BY-4.0-INTERNATIONAL": "CC-BY-4.0", | 
					
						
							|  |  |  |             "CC-4.0": "CC-BY-4.0", | 
					
						
							|  |  |  |             "CC-BY": "CC-BY-4.0", | 
					
						
							|  |  |  |             "CC-BY-SA-4.0-INTERNATIONAL": "CC-BY-SA-4.0", | 
					
						
							|  |  |  |             "CC-BY-SA": "CC-BY-SA-4.0", | 
					
						
							|  |  |  |             "CREATIVE-COMMONS-4.0-BY-NC": "CC-BY-NC-4.0", | 
					
						
							|  |  |  |             "CC-BY-SA-3.0-UNPORTED": "CC-BY-SA-3.0", | 
					
						
							|  |  |  |             "ISC-LICENSE": "ISC", | 
					
						
							| 
									
										
										
										
											2023-07-27 13:04:27 +02:00
										 |  |  |             "LOGO-BY-THE-GOVERNMENT": "LOGO", | 
					
						
							|  |  |  |             PD: "PUBLIC-DOMAIN", | 
					
						
							| 
									
										
										
										
											2024-08-09 16:55:08 +02:00
										 |  |  |             "LOGO-(ALL-RIGHTS-RESERVED)": "LOGO", | 
					
						
							| 
									
										
										
										
											2023-07-27 03:32:49 +02:00
										 |  |  |             /*  ALL-RIGHTS-RESERVED: | 
					
						
							|  |  |  |             PD: | 
					
						
							|  |  |  |                 PUBLIC-DOMAIN: | 
					
						
							|  |  |  |         TRIVIAL: //*/
 | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return mappings[licenseId] ?? licenseId | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-07-27 13:04:27 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-15 13:53:53 +01:00
										 |  |  |     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) { | 
					
						
							|  |  |  |             unlinkSync(licensePath) | 
					
						
							| 
									
										
										
										
											2021-04-10 13:57:16 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-04-11 01:58:51 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-15 13:53:53 +01:00
										 |  |  |         const perDirectory = new Map<string, SmallLicense[]>() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         for (const license of allLicenseInfos) { | 
					
						
							|  |  |  |             const p = license.path | 
					
						
							|  |  |  |             const dir = p.substring(0, p.lastIndexOf("/")) | 
					
						
							|  |  |  |             license.path = p.substring(dir.length + 1) | 
					
						
							|  |  |  |             if (!perDirectory.has(dir)) { | 
					
						
							|  |  |  |                 perDirectory.set(dir, []) | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             const cloned: SmallLicense = { | 
					
						
							|  |  |  |                 // We make a clone to force the order of the keys
 | 
					
						
							|  |  |  |                 path: license.path, | 
					
						
							|  |  |  |                 license: license.license, | 
					
						
							|  |  |  |                 authors: license.authors, | 
					
						
							| 
									
										
										
										
											2024-08-09 16:55:08 +02:00
										 |  |  |                 sources: license.sources, | 
					
						
							| 
									
										
										
										
											2021-11-07 15:03:03 +01:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2023-07-27 03:32:49 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-27 14:10:08 +02:00
										 |  |  |             cloned.license = Utils.Dedup( | 
					
						
							| 
									
										
										
										
											2023-07-27 03:32:49 +02:00
										 |  |  |                 cloned.license.split(";").map((l) => this.toSPDXCompliantLicense(l)) | 
					
						
							| 
									
										
										
										
											2023-07-27 14:10:08 +02:00
										 |  |  |             ).join("; ") | 
					
						
							|  |  |  |             if (cloned.license === "CC0-1.0; TRIVIAL") { | 
					
						
							|  |  |  |                 cloned.license = "TRIVIAL" | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             if (cloned.license === "LOGO; ALL-RIGHTS-RESERVED") { | 
					
						
							|  |  |  |                 cloned.license = "LOGO" | 
					
						
							| 
									
										
										
										
											2023-07-27 13:04:27 +02:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2023-07-27 14:10:08 +02:00
										 |  |  |             cloned.license = cloned.license.split("; ").join(" AND ") | 
					
						
							| 
									
										
										
										
											2023-07-27 03:32:49 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-15 13:53:53 +01:00
										 |  |  |             perDirectory.get(dir).push(cloned) | 
					
						
							| 
									
										
										
										
											2021-11-07 15:03:03 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-11-07 15:16:28 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-15 13:53:53 +01:00
										 |  |  |         perDirectory.forEach((licenses, dir) => { | 
					
						
							|  |  |  |             for (let i = licenses.length - 1; i >= 0; i--) { | 
					
						
							|  |  |  |                 const license = licenses[i] | 
					
						
							|  |  |  |                 const path = dir + "/" + license.path | 
					
						
							|  |  |  |                 if (!existsSync(path)) { | 
					
						
							|  |  |  |                     console.log( | 
					
						
							|  |  |  |                         "Found license for now missing file: ", | 
					
						
							|  |  |  |                         path, | 
					
						
							|  |  |  |                         " - removing this license" | 
					
						
							|  |  |  |                     ) | 
					
						
							|  |  |  |                     licenses.splice(i, 1) | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2021-04-10 13:57:16 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-15 13:53:53 +01:00
										 |  |  |             licenses.sort((a, b) => (a.path < b.path ? -1 : 1)) | 
					
						
							| 
									
										
										
										
											2025-07-10 18:26:31 +02:00
										 |  |  |             licenses = Utils.DedupOnId(licenses, (l) => l.path) | 
					
						
							| 
									
										
										
										
											2023-06-14 01:47:39 +02:00
										 |  |  |             const path = dir + "/license_info.json" | 
					
						
							| 
									
										
										
										
											2023-06-14 20:39:36 +02:00
										 |  |  |             if (licenses.length === 0) { | 
					
						
							|  |  |  |                 console.log("Removing", path, "as it is empty") | 
					
						
							| 
									
										
										
										
											2023-06-14 01:47:39 +02:00
										 |  |  |                 // No need to _actually_ unlik, this is done above
 | 
					
						
							| 
									
										
										
										
											2023-06-14 20:39:36 +02:00
										 |  |  |             } else { | 
					
						
							| 
									
										
										
										
											2023-06-14 01:47:39 +02:00
										 |  |  |                 writeFileSync(path, JSON.stringify(licenses, null, 2)) | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2023-03-15 13:53:53 +01:00
										 |  |  |         }) | 
					
						
							| 
									
										
										
										
											2021-04-10 14:25:06 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-15 13:53:53 +01:00
										 |  |  |     queryMissingLicenses(missingLicenses: string[]) { | 
					
						
							| 
									
										
										
										
											2024-08-09 16:55:08 +02:00
										 |  |  |         process.on("SIGINT", function () { | 
					
						
							| 
									
										
										
										
											2023-03-15 13:53:53 +01:00
										 |  |  |             console.log("Aborting... Bye!") | 
					
						
							|  |  |  |             process.exit() | 
					
						
							|  |  |  |         }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         let i = 1 | 
					
						
							|  |  |  |         for (const missingLicens of missingLicenses) { | 
					
						
							|  |  |  |             console.log(i + " / " + missingLicenses.length) | 
					
						
							|  |  |  |             i++ | 
					
						
							|  |  |  |             if (i < missingLicenses.length - 5) { | 
					
						
							|  |  |  |                 //    continue
 | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             this.createLicenseInfoFor(missingLicens) | 
					
						
							| 
									
										
										
										
											2021-11-07 15:16:28 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-15 13:53:53 +01:00
										 |  |  |         console.log("You're through!") | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-11-07 15:16:28 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-15 13:53:53 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Creates the humongous license_info in the generated assets, containing all licenses with a path relative to the root | 
					
						
							|  |  |  |      * @param licensePaths | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2024-07-23 17:59:06 +02:00
										 |  |  |     createFullLicenseOverview(licensePaths: string[], mostlyWhite: string[]) { | 
					
						
							| 
									
										
										
										
											2023-03-15 13:53:53 +01:00
										 |  |  |         const allLicenses: SmallLicense[] = [] | 
					
						
							|  |  |  |         for (const licensePath of licensePaths) { | 
					
						
							|  |  |  |             if (!existsSync(licensePath)) { | 
					
						
							|  |  |  |                 continue | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             const licenses = <SmallLicense[]>( | 
					
						
							|  |  |  |                 JSON.parse(readFileSync(licensePath, { encoding: "utf8" })) | 
					
						
							|  |  |  |             ) | 
					
						
							|  |  |  |             for (const license of licenses) { | 
					
						
							|  |  |  |                 this.validateLicenseInfo(license) | 
					
						
							|  |  |  |                 const dir = licensePath.substring( | 
					
						
							|  |  |  |                     0, | 
					
						
							|  |  |  |                     licensePath.length - "license_info.json".length | 
					
						
							|  |  |  |                 ) | 
					
						
							|  |  |  |                 license.path = dir + license.path | 
					
						
							| 
									
										
										
										
											2024-08-09 16:55:08 +02:00
										 |  |  |                 if (mostlyWhite.some((l) => license.path === l)) { | 
					
						
							| 
									
										
										
										
											2024-07-23 17:59:06 +02:00
										 |  |  |                     license["mostly_white"] = true | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2023-03-15 13:53:53 +01:00
										 |  |  |                 allLicenses.push(license) | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-15 13:53:53 +01:00
										 |  |  |         writeFileSync( | 
					
						
							| 
									
										
										
										
											2023-07-15 18:04:30 +02:00
										 |  |  |             "./src/assets/generated/license_info.json", | 
					
						
							| 
									
										
										
										
											2023-03-15 13:53:53 +01:00
										 |  |  |             JSON.stringify(allLicenses, null, "  ") | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2022-06-20 01:41:48 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-15 13:53:53 +01:00
										 |  |  |     async main(args: string[]) { | 
					
						
							|  |  |  |         console.log("Checking and compiling license info") | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-15 18:04:30 +02:00
										 |  |  |         if (!existsSync("./src/assets/generated")) { | 
					
						
							|  |  |  |             mkdirSync("./src/assets/generated") | 
					
						
							| 
									
										
										
										
											2023-03-15 13:53:53 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-23 12:33:24 +02:00
										 |  |  |         const contents = ScriptUtils.readDirRecSync("./assets").filter( | 
					
						
							| 
									
										
										
										
											2023-07-15 18:04:30 +02:00
										 |  |  |             (entry) => entry.indexOf("./assets/generated") != 0 | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2024-07-23 12:33:24 +02:00
										 |  |  |         const licensePaths = contents.filter((entry) => entry.indexOf("license_info.json") >= 0) | 
					
						
							|  |  |  |         const licenseInfos = this.generateLicenseInfos(licensePaths) | 
					
						
							| 
									
										
										
										
											2023-03-15 13:53:53 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         const artwork = contents.filter( | 
					
						
							| 
									
										
										
										
											2023-07-27 14:10:08 +02:00
										 |  |  |             (pth) => pth.match(/(.svg|.png|.jpg|.ttf|.otf|.woff|.jpeg)$/i) != null | 
					
						
							| 
									
										
										
										
											2023-03-15 13:53:53 +01:00
										 |  |  |         ) | 
					
						
							|  |  |  |         const missingLicenses = this.missingLicenseInfos(licenseInfos, artwork) | 
					
						
							| 
									
										
										
										
											2024-07-23 17:59:06 +02:00
										 |  |  |         const mostlyWhite: Set<string> = await this.mostlyWhite(artwork) | 
					
						
							| 
									
										
										
										
											2023-03-15 13:53:53 +01:00
										 |  |  |         if (args.indexOf("--prompt") >= 0 || args.indexOf("--query") >= 0) { | 
					
						
							|  |  |  |             this.queryMissingLicenses(missingLicenses) | 
					
						
							|  |  |  |             return this.main([]) | 
					
						
							| 
									
										
										
										
											2022-11-14 02:03:23 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-15 13:53:53 +01:00
										 |  |  |         const invalidLicenses = licenseInfos | 
					
						
							|  |  |  |             .filter((l) => (l.license ?? "") === "") | 
					
						
							|  |  |  |             .map((l) => `License for artwork ${l.path} is empty string or undefined`) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         for (const licenseInfo of licenseInfos) { | 
					
						
							| 
									
										
										
										
											2023-07-27 14:34:05 +02:00
										 |  |  |             const isTrivial = licenseInfo.license | 
					
						
							|  |  |  |                 .split(";") | 
					
						
							|  |  |  |                 .map((l) => l.trim().toLowerCase()) | 
					
						
							|  |  |  |                 .some((s) => s.endsWith("trivial")) | 
					
						
							| 
									
										
										
										
											2023-03-15 13:53:53 +01:00
										 |  |  |             if (licenseInfo.sources.length + licenseInfo.authors.length == 0 && !isTrivial) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                 invalidLicenses.push( | 
					
						
							| 
									
										
										
										
											2023-03-15 13:53:53 +01:00
										 |  |  |                     "Invalid license: No sources nor authors given in the license for " + | 
					
						
							| 
									
										
										
										
											2024-08-09 16:55:08 +02:00
										 |  |  |                         JSON.stringify(licenseInfo) | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                 ) | 
					
						
							| 
									
										
										
										
											2023-03-15 13:53:53 +01:00
										 |  |  |                 continue | 
					
						
							| 
									
										
										
										
											2022-06-20 01:41:48 +02:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2023-03-15 13:53:53 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |             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) | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2022-06-20 01:41:48 +02:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2023-07-27 03:32:49 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             const spdxPath = licenseInfo.path + ".license" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             const spdxContent = [ | 
					
						
							|  |  |  |                 "SPDX-FileCopyrightText: " + licenseInfo.authors.join("; "), | 
					
						
							| 
									
										
										
										
											2023-07-27 14:34:05 +02:00
										 |  |  |                 "SPDX-License-Identifier: " + | 
					
						
							| 
									
										
										
										
											2024-08-09 16:55:08 +02:00
										 |  |  |                     licenseInfo.license | 
					
						
							|  |  |  |                         .split(" AND ") | 
					
						
							|  |  |  |                         .map((s) => this.addLicenseRef(s)) | 
					
						
							|  |  |  |                         .join(" AND "), | 
					
						
							| 
									
										
										
										
											2023-07-27 03:32:49 +02:00
										 |  |  |             ] | 
					
						
							|  |  |  |             writeFileSync(spdxPath, spdxContent.join("\n")) | 
					
						
							| 
									
										
										
										
											2021-04-23 16:52:20 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-15 13:53:53 +01:00
										 |  |  |         if (missingLicenses.length > 0 || invalidLicenses.length) { | 
					
						
							|  |  |  |             const msg = `There are ${missingLicenses.length} licenses missing and ${invalidLicenses.length} invalid licenses.` | 
					
						
							|  |  |  |             console.log(missingLicenses.concat(invalidLicenses).join("\n")) | 
					
						
							|  |  |  |             console.error(msg) | 
					
						
							|  |  |  |             if (args.indexOf("--no-fail") < 0) { | 
					
						
							|  |  |  |                 throw msg | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2022-06-20 01:41:48 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-15 13:53:53 +01:00
										 |  |  |         this.cleanLicenseInfo(licensePaths, licenseInfos) | 
					
						
							| 
									
										
										
										
											2024-07-23 17:59:06 +02:00
										 |  |  |         this.createFullLicenseOverview(licensePaths, Array.from(mostlyWhite)) | 
					
						
							| 
									
										
										
										
											2023-03-15 13:53:53 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-07-27 14:34:05 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Some licenses need "LicenseRef-" to be added to make reuse lint work | 
					
						
							|  |  |  |      * @param s | 
					
						
							|  |  |  |      * @private | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     private addLicenseRef(s: string): string { | 
					
						
							|  |  |  |         if (GenerateLicenseInfo.needsLicenseRef.has(s)) { | 
					
						
							|  |  |  |             return "LicenseRef-" + s | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return s | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-04-10 01:18:17 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-15 13:53:53 +01:00
										 |  |  | new GenerateLicenseInfo().run() |