mapcomplete/scripts/generateLayerOverview.ts

891 lines
33 KiB
TypeScript
Raw Normal View History

2023-09-19 14:04:13 +02:00
import ScriptUtils from "./ScriptUtils"
import { existsSync, mkdirSync, readFileSync, statSync, writeFileSync } from "fs"
import licenses from "../src/assets/generated/license_info.json"
import { LayoutConfigJson } from "../src/Models/ThemeConfig/Json/LayoutConfigJson"
import { LayerConfigJson } from "../src/Models/ThemeConfig/Json/LayerConfigJson"
import Constants from "../src/Models/Constants"
2022-06-20 01:41:34 +02:00
import {
DetectDuplicateFilters,
2022-07-06 11:14:19 +02:00
DoesImageExist,
2022-06-20 01:41:34 +02:00
PrevalidateTheme,
ValidateLayer,
2023-09-19 14:04:13 +02:00
ValidateThemeAndLayers,
2024-06-20 04:21:29 +02:00
ValidateThemeEnsemble,
2023-09-19 14:04:13 +02:00
} from "../src/Models/ThemeConfig/Conversion/Validation"
import { Translation } from "../src/UI/i18n/Translation"
import { PrepareLayer } from "../src/Models/ThemeConfig/Conversion/PrepareLayer"
import { PrepareTheme } from "../src/Models/ThemeConfig/Conversion/PrepareTheme"
2024-06-20 04:21:29 +02:00
import {
Conversion,
DesugaringContext,
DesugaringStep,
} from "../src/Models/ThemeConfig/Conversion/Conversion"
2023-09-19 14:04:13 +02:00
import { Utils } from "../src/Utils"
import Script from "./Script"
import { AllSharedLayers } from "../src/Customizations/AllSharedLayers"
import { parse as parse_html } from "node-html-parser"
import { ExtraFunctions } from "../src/Logic/ExtraFunctions"
2023-10-01 13:13:07 +02:00
import { QuestionableTagRenderingConfigJson } from "../src/Models/ThemeConfig/Json/QuestionableTagRenderingConfigJson"
import LayerConfig from "../src/Models/ThemeConfig/LayerConfig"
import PointRenderingConfig from "../src/Models/ThemeConfig/PointRenderingConfig"
2023-11-02 04:35:32 +01:00
import { ConversionContext } from "../src/Models/ThemeConfig/Conversion/ConversionContext"
import { GenerateFavouritesLayer } from "./generateFavouritesLayer"
import LayoutConfig from "../src/Models/ThemeConfig/LayoutConfig"
2024-07-08 23:35:40 +02:00
import Translations from "../src/UI/i18n/Translations"
import { Translatable } from "../src/Models/ThemeConfig/Json/Translatable"
2023-10-30 18:08:49 +01:00
// This scripts scans 'src/assets/layers/*.json' for layer definition files and 'src/assets/themes/*.json' for theme definition files.
2021-04-10 03:18:32 +02:00
// It spits out an overview of those to be used to load them
class ParseLayer extends Conversion<
string,
{
parsed: LayerConfig
raw: LayerConfigJson
}
> {
private readonly _prepareLayer: PrepareLayer
private readonly _doesImageExist: DoesImageExist
constructor(prepareLayer: PrepareLayer, doesImageExist: DoesImageExist) {
super("Parsed a layer from file, validates it", [], "ParseLayer")
this._prepareLayer = prepareLayer
this._doesImageExist = doesImageExist
}
convert(
path: string,
2024-07-09 13:42:08 +02:00
context: ConversionContext
): {
parsed: LayerConfig
raw: LayerConfigJson
} {
let parsed
let fileContents
try {
fileContents = readFileSync(path, "utf8")
} catch (e) {
context.err("Could not read file " + path + " due to " + e)
return undefined
}
try {
parsed = JSON.parse(fileContents)
} catch (e) {
2024-01-30 18:33:30 +01:00
context.err("Could not parse file as JSON: " + e)
return undefined
}
if (parsed === undefined) {
context.err("yielded undefined")
return undefined
}
const fixed = this._prepareLayer.convert(parsed, context.inOperation("PrepareLayer"))
if (!fixed.source) {
context.enter("source").err("No source is configured")
return undefined
}
if (
typeof fixed.source !== "string" &&
fixed.source["osmTags"] &&
fixed.source["osmTags"]["and"] === undefined
) {
fixed.source["osmTags"] = { and: [fixed.source["osmTags"]] }
}
const validator = new ValidateLayer(path, true, this._doesImageExist)
return validator.convert(fixed, context.inOperation("ValidateLayer"))
}
}
class AddIconSummary extends DesugaringStep<{ raw: LayerConfigJson; parsed: LayerConfig }> {
static singleton = new AddIconSummary()
constructor() {
super("Adds an icon summary for quick reference", ["_layerIcon"], "AddIconSummary")
}
2024-06-18 03:33:11 +02:00
convert(json: { raw: LayerConfigJson; parsed: LayerConfig }) {
// Add a summary of the icon
const fixed = json.raw
const layerConfig = json.parsed
const pointRendering: PointRenderingConfig = layerConfig.mapRendering.find((pr) =>
2024-07-09 13:42:08 +02:00
pr.location.has("point")
)
const defaultTags = layerConfig.GetBaseTags()
fixed["_layerIcon"] = Utils.NoNull(
(pointRendering?.marker ?? []).map((i) => {
const icon = i.icon?.GetRenderValue(defaultTags)?.txt
if (!icon) {
return undefined
}
const result = { icon }
const c = i.color?.GetRenderValue(defaultTags)?.txt
if (c) {
result["color"] = c
}
return result
2024-07-09 13:42:08 +02:00
})
)
return { raw: fixed, parsed: layerConfig }
}
}
class LayerOverviewUtils extends Script {
public static readonly layerPath = "./src/assets/generated/layers/"
public static readonly themePath = "./src/assets/generated/themes/"
constructor() {
super("Reviews and generates the compiled themes")
}
private static publicLayerIdsFrom(themefiles: LayoutConfigJson[]): Set<string> {
2022-09-08 21:40:48 +02:00
const publicThemes = [].concat(...themefiles.filter((th) => !th.hideFromOverview))
2022-09-08 21:40:48 +02:00
return new Set([].concat(...publicThemes.map((th) => this.extractLayerIdsFrom(th))))
}
2022-09-08 21:40:48 +02:00
private static extractLayerIdsFrom(
themeFile: LayoutConfigJson,
2024-07-09 13:42:08 +02:00
includeInlineLayers = true
2022-09-08 21:40:48 +02:00
): string[] {
2024-06-18 03:33:11 +02:00
const publicLayerIds: string[] = []
2023-10-30 14:32:31 +01:00
if (!Array.isArray(themeFile.layers)) {
throw (
"Cannot iterate over 'layers' of " +
themeFile.id +
"; it is a " +
typeof themeFile.layers
)
}
for (const publicLayer of themeFile.layers) {
if (typeof publicLayer === "string") {
publicLayerIds.push(publicLayer)
continue
}
if (publicLayer["builtin"] !== undefined) {
2024-06-20 04:21:29 +02:00
const bi: string | string[] = publicLayer["builtin"]
if (typeof bi === "string") {
publicLayerIds.push(bi)
2024-06-18 03:33:11 +02:00
} else {
2024-06-20 04:21:29 +02:00
bi.forEach((id) => publicLayerIds.push(id))
}
continue
}
if (includeInlineLayers) {
publicLayerIds.push(publicLayer["id"])
}
}
return publicLayerIds
}
2024-07-08 23:35:40 +02:00
public static cleanTranslation(t: Record<string, string> | Translation): Translatable {
return Translations.T(t).OnEveryLanguage((s) => parse_html(s).textContent).translations
}
shouldBeUpdated(sourcefile: string | string[], targetfile: string): boolean {
if (!existsSync(targetfile)) {
2022-09-08 21:40:48 +02:00
return true
}
const targetModified = statSync(targetfile).mtime
if (typeof sourcefile === "string") {
sourcefile = [sourcefile]
}
2023-06-29 15:08:13 +02:00
for (const path of sourcefile) {
const hasChange = statSync(path).mtime > targetModified
if (hasChange) {
console.log("File ", targetfile, " should be updated as ", path, "has been changed")
return true
}
}
return false
}
2022-09-08 21:40:48 +02:00
writeSmallOverview(
themes: {
id: string
title: any
shortDescription: any
icon: string
hideFromOverview: boolean
mustHaveLanguage: boolean
layers: (
| LayerConfigJson
| string
| {
2024-07-09 13:42:08 +02:00
builtin
}
)[]
}[]
2022-09-08 21:40:48 +02:00
) {
const perId = new Map<string, any>()
2021-12-21 18:35:31 +01:00
for (const theme of themes) {
2022-06-20 01:41:34 +02:00
const keywords: {}[] = []
2022-09-08 21:40:48 +02:00
for (const layer of theme.layers ?? []) {
const l = <LayerConfigJson>layer
keywords.push({ "*": l.id })
keywords.push(l.title)
keywords.push(l.description)
}
2022-06-20 01:41:34 +02:00
2021-12-21 18:35:31 +01:00
const data = {
id: theme.id,
title: theme.title,
2024-07-08 23:35:40 +02:00
shortDescription: LayerOverviewUtils.cleanTranslation(theme.shortDescription),
2021-12-21 18:35:31 +01:00
icon: theme.icon,
2022-04-01 12:51:55 +02:00
hideFromOverview: theme.hideFromOverview,
mustHaveLanguage: theme.mustHaveLanguage,
2024-06-20 04:21:29 +02:00
keywords: Utils.NoNull(keywords),
2021-12-21 18:35:31 +01:00
}
2022-09-08 21:40:48 +02:00
perId.set(theme.id, data)
2021-12-21 18:35:31 +01:00
}
2022-09-08 21:40:48 +02:00
const sorted = Constants.themeOrder.map((id) => {
2021-12-21 18:35:31 +01:00
if (!perId.has(id)) {
throw "Ordered theme id " + id + " not found"
}
2022-09-08 21:40:48 +02:00
return perId.get(id)
})
2021-12-21 18:35:31 +01:00
perId.forEach((value) => {
if (Constants.themeOrder.indexOf(value.id) >= 0) {
2022-09-08 21:40:48 +02:00
return // actually a continue
2021-12-21 18:35:31 +01:00
}
sorted.push(value)
})
2022-09-08 21:40:48 +02:00
writeFileSync(
"./src/assets/generated/theme_overview.json",
2022-09-08 21:40:48 +02:00
JSON.stringify(sorted, null, " "),
2024-07-09 13:42:08 +02:00
{ encoding: "utf8" }
2022-09-08 21:40:48 +02:00
)
2021-04-10 03:18:32 +02:00
}
2021-04-10 03:50:44 +02:00
2021-12-21 18:35:31 +01:00
writeTheme(theme: LayoutConfigJson) {
if (!existsSync(LayerOverviewUtils.themePath)) {
2022-09-08 21:40:48 +02:00
mkdirSync(LayerOverviewUtils.themePath)
2021-05-19 20:47:41 +02:00
}
2023-10-30 18:08:49 +01:00
2022-09-08 21:40:48 +02:00
writeFileSync(
`${LayerOverviewUtils.themePath}${theme.id}.json`,
JSON.stringify(theme, null, " "),
2024-07-09 13:42:08 +02:00
{ encoding: "utf8" }
2022-09-08 21:40:48 +02:00
)
2021-12-21 18:35:31 +01:00
}
writeLayer(layer: LayerConfigJson) {
if (!existsSync(LayerOverviewUtils.layerPath)) {
2022-09-08 21:40:48 +02:00
mkdirSync(LayerOverviewUtils.layerPath)
}
2022-09-08 21:40:48 +02:00
writeFileSync(
`${LayerOverviewUtils.layerPath}${layer.id}.json`,
JSON.stringify(layer, null, " "),
2024-07-09 13:42:08 +02:00
{ encoding: "utf8" }
2022-09-08 21:40:48 +02:00
)
2021-12-21 18:35:31 +01:00
}
2024-06-20 04:21:29 +02:00
static asDict(
2024-07-09 13:42:08 +02:00
trs: QuestionableTagRenderingConfigJson[]
2024-06-20 04:21:29 +02:00
): Map<string, QuestionableTagRenderingConfigJson> {
2024-06-18 03:33:11 +02:00
const d = new Map<string, QuestionableTagRenderingConfigJson>()
for (const tr of trs) {
d.set(tr.id, tr)
}
return d
}
2024-06-20 04:21:29 +02:00
getSharedTagRenderings(doesImageExist: DoesImageExist): QuestionableTagRenderingConfigJson[]
2024-06-18 03:33:11 +02:00
getSharedTagRenderings(
doesImageExist: DoesImageExist,
bootstrapTagRenderings: Map<string, QuestionableTagRenderingConfigJson>,
2024-07-09 13:42:08 +02:00
bootstrapTagRenderingsOrder: string[]
2024-06-20 04:21:29 +02:00
): QuestionableTagRenderingConfigJson[]
getSharedTagRenderings(
doesImageExist: DoesImageExist,
2024-06-18 03:33:11 +02:00
bootstrapTagRenderings: Map<string, QuestionableTagRenderingConfigJson> = null,
2024-07-09 13:42:08 +02:00
bootstrapTagRenderingsOrder: string[] = []
2024-06-18 03:33:11 +02:00
): QuestionableTagRenderingConfigJson[] {
2024-06-16 16:06:26 +02:00
const prepareLayer = new PrepareLayer(
{
tagRenderings: bootstrapTagRenderings,
2024-06-18 03:33:11 +02:00
tagRenderingOrder: bootstrapTagRenderingsOrder,
2024-06-16 16:06:26 +02:00
sharedLayers: null,
2024-06-20 04:21:29 +02:00
publicLayers: null,
2024-06-16 16:06:26 +02:00
},
{
2024-06-20 04:21:29 +02:00
addTagRenderingsToContext: true,
2024-07-09 13:42:08 +02:00
}
2024-06-16 16:06:26 +02:00
)
const path = "assets/layers/questions/questions.json"
const sharedQuestions = this.parseLayer(doesImageExist, prepareLayer, path).raw
2023-10-01 13:13:07 +02:00
const dict = new Map<string, QuestionableTagRenderingConfigJson>()
for (const tr of sharedQuestions.tagRenderings) {
2023-10-01 13:13:07 +02:00
const tagRendering = <QuestionableTagRenderingConfigJson>tr
2023-07-28 00:29:21 +02:00
dict.set(tagRendering["id"], tagRendering)
2021-12-21 18:35:31 +01:00
}
2021-09-04 18:59:51 +02:00
if (dict.size === bootstrapTagRenderings?.size) {
2024-06-18 03:33:11 +02:00
return <QuestionableTagRenderingConfigJson[]>sharedQuestions.tagRenderings
}
2021-05-19 20:47:41 +02:00
2024-06-20 04:21:29 +02:00
return this.getSharedTagRenderings(
doesImageExist,
dict,
2024-07-09 13:42:08 +02:00
sharedQuestions.tagRenderings.map((tr) => tr["id"])
2024-06-20 04:21:29 +02:00
)
2021-12-21 18:35:31 +01:00
}
2021-05-19 20:47:41 +02:00
checkAllSvgs() {
const allSvgs = ScriptUtils.readDirRecSync("./src/assets")
2022-09-08 21:40:48 +02:00
.filter((path) => path.endsWith(".svg"))
.filter((path) => !path.startsWith("./src/assets/generated"))
2022-09-08 21:40:48 +02:00
let errCount = 0
const exempt = [
"src/assets/SocialImageTemplate.svg",
"src/assets/SocialImageTemplateWide.svg",
"src/assets/SocialImageBanner.svg",
"src/assets/SocialImageRepo.svg",
"src/assets/svg/osm-logo.svg",
2024-06-20 04:21:29 +02:00
"src/assets/templates/*",
2022-09-08 21:40:48 +02:00
]
for (const path of allSvgs) {
2022-10-27 01:50:01 +02:00
if (
exempt.some((p) => {
if (p.endsWith("*") && path.startsWith("./" + p.substring(0, p.length - 1))) {
return true
}
return "./" + p === path
})
) {
2022-03-08 04:09:03 +01:00
continue
}
2023-01-15 23:28:02 +01:00
const contents = readFileSync(path, { encoding: "utf8" })
2022-05-07 21:41:58 +02:00
if (contents.indexOf("data:image/png;") >= 0) {
console.warn("The SVG at " + path + " is a fake SVG: it contains PNG data!")
2022-09-08 21:40:48 +02:00
errCount++
if (path.startsWith("./src/assets/svg")) {
2022-05-07 21:41:58 +02:00
throw "A core SVG is actually a PNG. Don't do this!"
}
}
2022-06-20 01:41:34 +02:00
if (contents.indexOf("<text") > 0) {
2022-09-08 21:40:48 +02:00
console.warn(
"The SVG at " +
2024-07-09 13:42:08 +02:00
path +
" contains a `text`-tag. This is highly discouraged. Every machine viewing your theme has their own font libary, and the font you choose might not be present, resulting in a different font being rendered. Solution: open your .svg in inkscape (or another program), select the text and convert it to a path"
2022-09-08 21:40:48 +02:00
)
errCount++
}
}
if (errCount > 0) {
2022-05-07 21:41:58 +02:00
throw `There are ${errCount} invalid svgs`
}
}
async main(args: string[]) {
2023-10-31 11:50:03 +01:00
console.log("Generating layer overview...")
2024-01-30 18:33:30 +01:00
const themeWhitelist = new Set(
args
.find((a) => a.startsWith("--themes="))
?.substring("--themes=".length)
2024-07-09 13:42:08 +02:00
?.split(",") ?? []
2024-01-30 18:33:30 +01:00
)
2024-01-30 18:33:30 +01:00
const layerWhitelist = new Set(
args
.find((a) => a.startsWith("--layers="))
?.substring("--layers=".length)
2024-07-09 13:42:08 +02:00
?.split(",") ?? []
2024-01-30 18:33:30 +01:00
)
2022-09-08 21:40:48 +02:00
const forceReload = args.some((a) => a == "--force")
2022-01-16 01:59:06 +01:00
const licensePaths = new Set<string>()
for (const i in licenses) {
licensePaths.add(licenses[i].path)
}
const doesImageExist = new DoesImageExist(licensePaths, existsSync)
const sharedLayers = this.buildLayerIndex(doesImageExist, forceReload, layerWhitelist)
const priviliged = new Set<string>(Constants.priviliged_layers)
sharedLayers.forEach((_, key) => {
priviliged.delete(key)
})
2024-08-08 22:54:49 +02:00
// These two get a free pass
priviliged.delete("summary")
priviliged.delete("last_click")
if (priviliged.size > 0) {
throw (
"Priviliged layer " +
Array.from(priviliged).join(", ") +
" has no definition file, create it at `src/assets/layers/<layername>/<layername.json>"
)
}
2022-07-08 15:37:31 +02:00
const recompiledThemes: string[] = []
2022-09-08 21:40:48 +02:00
const sharedThemes = this.buildThemeIndex(
licensePaths,
2022-09-08 21:40:48 +02:00
sharedLayers,
recompiledThemes,
forceReload,
2024-07-09 13:42:08 +02:00
themeWhitelist
2022-09-08 21:40:48 +02:00
)
new ValidateThemeEnsemble().convertStrict(
2024-07-09 13:42:08 +02:00
Array.from(sharedThemes.values()).map((th) => new LayoutConfig(th, true))
2024-02-15 17:48:26 +01:00
)
if (recompiledThemes.length > 0) {
writeFileSync(
"./src/assets/generated/known_layers.json",
JSON.stringify({
2024-06-20 04:21:29 +02:00
layers: Array.from(sharedLayers.values()).filter((l) => l.id !== "favourite"),
2024-07-09 13:42:08 +02:00
})
)
}
2022-09-08 21:40:48 +02:00
2023-11-13 04:33:25 +01:00
const mcChangesPath = "./assets/themes/mapcomplete-changes/mapcomplete-changes.json"
2022-09-08 21:40:48 +02:00
if (
2023-11-13 12:08:55 +01:00
(recompiledThemes.length > 0 &&
2023-12-06 03:07:36 +01:00
!(
recompiledThemes.length === 1 && recompiledThemes[0] === "mapcomplete-changes"
)) ||
args.indexOf("--generate-change-map") >= 0 ||
2023-11-13 12:08:55 +01:00
!existsSync(mcChangesPath)
2022-09-08 21:40:48 +02:00
) {
// mapcomplete-changes shows an icon for each corresponding mapcomplete-theme
2022-09-08 21:40:48 +02:00
const iconsPerTheme = Array.from(sharedThemes.values()).map((th) => ({
if: "theme=" + th.id,
2024-06-20 04:21:29 +02:00
then: th.icon,
2022-09-08 21:40:48 +02:00
}))
const proto: LayoutConfigJson = JSON.parse(
readFileSync("./assets/themes/mapcomplete-changes/mapcomplete-changes.proto.json", {
2024-06-20 04:21:29 +02:00
encoding: "utf8",
2024-07-09 13:42:08 +02:00
})
2022-09-08 21:40:48 +02:00
)
const protolayer = <LayerConfigJson>(
proto.layers.filter((l) => l["id"] === "mapcomplete-changes")[0]
)
2023-09-19 14:04:13 +02:00
const rendering = protolayer.pointRendering[0]
rendering.marker[0].icon["mappings"] = iconsPerTheme
2023-11-13 04:33:25 +01:00
writeFileSync(mcChangesPath, JSON.stringify(proto, null, " "))
}
this.checkAllSvgs()
2022-09-08 21:40:48 +02:00
2022-10-27 01:50:01 +02:00
new DetectDuplicateFilters().convertStrict(
{
layers: ScriptUtils.getLayerFiles().map((f) => f.parsed),
2024-06-20 04:21:29 +02:00
themes: ScriptUtils.getThemeFiles().map((f) => f.parsed),
2022-10-27 01:50:01 +02:00
},
2024-07-09 13:42:08 +02:00
ConversionContext.construct([], [])
2022-10-27 01:50:01 +02:00
)
for (const [_, theme] of sharedThemes) {
theme.layers = theme.layers.filter(
2024-07-09 13:42:08 +02:00
(l) => Constants.added_by_default.indexOf(l["id"]) < 0
)
}
if (recompiledThemes.length > 0) {
writeFileSync(
"./src/assets/generated/known_themes.json",
JSON.stringify({
2024-06-20 04:21:29 +02:00
themes: Array.from(sharedThemes.values()),
2024-07-09 13:42:08 +02:00
})
)
}
if (AllSharedLayers.getSharedLayersConfigs().size == 0) {
2024-02-15 17:48:26 +01:00
console.error("This was a bootstrapping-run. Run generate layeroverview again!")
2022-07-11 09:14:26 +02:00
}
2022-01-16 01:59:06 +01:00
}
2021-12-21 18:35:31 +01:00
private parseLayer(
doesImageExist: DoesImageExist,
prepLayer: PrepareLayer,
2024-07-09 13:42:08 +02:00
sharedLayerPath: string
): {
raw: LayerConfigJson
parsed: LayerConfig
context: ConversionContext
} {
const parser = new ParseLayer(prepLayer, doesImageExist)
const context = ConversionContext.construct([sharedLayerPath], ["ParseLayer"])
const parsed = parser.convertStrict(sharedLayerPath, context)
const result = AddIconSummary.singleton.convertStrict(
parsed,
2024-07-09 13:42:08 +02:00
context.inOperation("AddIconSummary")
)
return { ...result, context }
}
2022-09-08 21:40:48 +02:00
private buildLayerIndex(
doesImageExist: DoesImageExist,
forceReload: boolean,
2024-07-09 13:42:08 +02:00
whitelist: Set<string>
2022-09-08 21:40:48 +02:00
): Map<string, LayerConfigJson> {
// First, we expand and validate all builtin layers. These are written to src/assets/generated/layers
2021-12-21 18:35:31 +01:00
// At the same time, an index of available layers is built.
console.log("------------- VALIDATING THE BUILTIN QUESTIONS ---------------")
2022-09-08 21:40:48 +02:00
const sharedTagRenderings = this.getSharedTagRenderings(doesImageExist)
console.log(" ---------- VALIDATING BUILTIN LAYERS ---------")
2021-12-21 18:35:31 +01:00
const state: DesugaringContext = {
2024-06-18 03:33:11 +02:00
tagRenderings: LayerOverviewUtils.asDict(sharedTagRenderings),
2024-06-20 04:21:29 +02:00
tagRenderingOrder: sharedTagRenderings.map((tr) => tr.id),
sharedLayers: AllSharedLayers.getSharedLayersConfigs(),
2021-12-21 18:35:31 +01:00
}
2022-07-11 09:14:26 +02:00
const sharedLayers = new Map<string, LayerConfigJson>()
2022-09-08 21:40:48 +02:00
const prepLayer = new PrepareLayer(state)
const skippedLayers: string[] = []
const recompiledLayers: string[] = []
let warningCount = 0
for (const sharedLayerPath of ScriptUtils.getLayerPaths()) {
if (whitelist.size > 0) {
const idByPath = sharedLayerPath.split("/").at(-1).split(".")[0]
2024-01-30 18:33:30 +01:00
if (
Constants.priviliged_layers.indexOf(<any>idByPath) < 0 &&
!whitelist.has(idByPath)
) {
continue
}
}
{
2022-09-08 21:40:48 +02:00
const targetPath =
LayerOverviewUtils.layerPath +
sharedLayerPath.substring(sharedLayerPath.lastIndexOf("/"))
if (!forceReload && !this.shouldBeUpdated(sharedLayerPath, targetPath)) {
const sharedLayer = JSON.parse(readFileSync(targetPath, "utf8"))
sharedLayers.set(sharedLayer.id, sharedLayer)
skippedLayers.push(sharedLayer.id)
ScriptUtils.erasableLog("Loaded " + sharedLayer.id)
2022-09-08 21:40:48 +02:00
continue
}
2022-07-08 15:37:31 +02:00
}
const parsed = this.parseLayer(doesImageExist, prepLayer, sharedLayerPath)
warningCount += parsed.context.getAll("warning").length
const fixed = parsed.raw
2021-12-21 18:35:31 +01:00
if (sharedLayers.has(fixed.id)) {
throw "There are multiple layers with the id " + fixed.id + ", " + sharedLayerPath
2021-05-19 20:47:41 +02:00
}
2021-12-21 18:35:31 +01:00
sharedLayers.set(fixed.id, fixed)
recompiledLayers.push(fixed.id)
2021-12-21 18:35:31 +01:00
this.writeLayer(fixed)
}
2022-06-20 01:41:34 +02:00
2022-09-08 21:40:48 +02:00
console.log(
"Recompiled layers " +
2024-07-09 13:42:08 +02:00
recompiledLayers.join(", ") +
" and skipped " +
skippedLayers.length +
" layers. Detected " +
warningCount +
" warnings"
2022-09-08 21:40:48 +02:00
)
// We always need the calculated tags of 'usersettings', so we export them separately
this.extractJavascriptCodeForLayer(
state.sharedLayers.get("usersettings"),
2024-07-09 13:42:08 +02:00
"./src/Logic/State/UserSettingsMetaTagging.ts"
)
2022-09-08 21:40:48 +02:00
return sharedLayers
}
2021-04-10 03:18:32 +02:00
/**
* Given: a fully expanded themeConfigJson
*
* Will extract a dictionary of the special code and write it into a javascript file which can be imported.
* This removes the need for _eval_, allowing for a correct CSP
* @param themeFile
* @private
*/
private extractJavascriptCode(themeFile: LayoutConfigJson) {
const allCode = [
"import {Feature} from 'geojson'",
2024-07-09 13:42:08 +02:00
'import { ExtraFuncType } from "../../../Logic/ExtraFunctions";',
'import { Utils } from "../../../Utils"',
"export class ThemeMetaTagging {",
" public static readonly themeName = " + JSON.stringify(themeFile.id),
2024-06-20 04:21:29 +02:00
"",
]
for (const layer of themeFile.layers) {
const l = <LayerConfigJson>layer
2023-09-22 12:42:09 +02:00
const id = l.id.replace(/[^a-zA-Z0-9_]/g, "_")
const code = l.calculatedTags ?? []
allCode.push(
" public metaTaggging_for_" +
2024-07-09 13:42:08 +02:00
id +
"(feat: Feature, helperFunctions: Record<ExtraFuncType, (feature: Feature) => Function>) {"
)
allCode.push(" const {" + ExtraFunctions.types.join(", ") + "} = helperFunctions")
for (const line of code) {
const firstEq = line.indexOf("=")
let attributeName = line.substring(0, firstEq).trim()
const expression = line.substring(firstEq + 1)
const isStrict = attributeName.endsWith(":")
if (!isStrict) {
allCode.push(
" Utils.AddLazyProperty(feat.properties, '" +
2024-07-09 13:42:08 +02:00
attributeName +
"', () => " +
expression +
" ) "
)
} else {
2023-09-22 12:42:09 +02:00
attributeName = attributeName.substring(0, attributeName.length - 1).trim()
allCode.push(" feat.properties['" + attributeName + "'] = " + expression)
}
}
allCode.push(" }")
}
const targetDir = "./src/assets/generated/metatagging/"
if (!existsSync(targetDir)) {
mkdirSync(targetDir)
}
allCode.push("}")
writeFileSync(targetDir + themeFile.id + ".ts", allCode.join("\n"))
}
private extractJavascriptCodeForLayer(l: LayerConfigJson, targetPath?: string) {
2023-09-22 12:42:09 +02:00
if (!l) {
return // Probably a bootstrapping run
}
let importPath = "../../../"
if (targetPath) {
const l = targetPath.split("/")
if (l.length == 1) {
importPath = "./"
} else {
importPath = ""
for (let i = 0; i < l.length - 3; i++) {
importPath += "../"
}
}
}
const allCode = [
`import { Utils } from "${importPath}Utils"`,
2023-09-22 12:42:09 +02:00
`/** This code is autogenerated - do not edit. Edit ./assets/layers/${l?.id}/${l?.id}.json instead */`,
"export class ThemeMetaTagging {",
" public static readonly themeName = " + JSON.stringify(l.id),
2024-06-20 04:21:29 +02:00
"",
]
const code = l.calculatedTags ?? []
allCode.push(
2024-07-09 13:42:08 +02:00
" public metaTaggging_for_" + l.id + "(feat: {properties: Record<string, string>}) {"
)
for (const line of code) {
const firstEq = line.indexOf("=")
let attributeName = line.substring(0, firstEq).trim()
const expression = line.substring(firstEq + 1)
const isStrict = attributeName.endsWith(":")
if (!isStrict) {
allCode.push(
" Utils.AddLazyProperty(feat.properties, '" +
2024-07-09 13:42:08 +02:00
attributeName +
"', () => " +
expression +
" ) "
)
} else {
attributeName = attributeName.substring(0, attributeName.length - 2).trim()
allCode.push(" feat.properties['" + attributeName + "'] = " + expression)
}
}
allCode.push(" }")
allCode.push("}")
const targetDir = "./src/assets/generated/metatagging/"
if (!targetPath) {
if (!existsSync(targetDir)) {
mkdirSync(targetDir)
}
}
writeFileSync(targetPath ?? targetDir + "layer_" + l.id + ".ts", allCode.join("\n"))
}
2022-09-08 21:40:48 +02:00
private buildThemeIndex(
licensePaths: Set<string>,
2022-09-08 21:40:48 +02:00
sharedLayers: Map<string, LayerConfigJson>,
recompiledThemes: string[],
forceReload: boolean,
2024-07-09 13:42:08 +02:00
whitelist: Set<string>
2022-09-08 21:40:48 +02:00
): Map<string, LayoutConfigJson> {
2021-12-21 18:35:31 +01:00
console.log(" ---------- VALIDATING BUILTIN THEMES ---------")
2022-09-08 21:40:48 +02:00
const themeFiles = ScriptUtils.getThemeFiles()
const fixed = new Map<string, LayoutConfigJson>()
2022-09-08 21:40:48 +02:00
const publicLayers = LayerOverviewUtils.publicLayerIdsFrom(
2024-07-09 13:42:08 +02:00
themeFiles.map((th) => th.parsed)
2022-09-08 21:40:48 +02:00
)
2024-06-20 04:21:29 +02:00
const trs = this.getSharedTagRenderings(new DoesImageExist(licensePaths, existsSync))
2024-06-18 03:33:11 +02:00
2021-12-21 18:35:31 +01:00
const convertState: DesugaringContext = {
sharedLayers,
2024-06-18 03:33:11 +02:00
tagRenderings: LayerOverviewUtils.asDict(trs),
2024-06-20 04:21:29 +02:00
tagRenderingOrder: trs.map((tr) => tr.id),
publicLayers,
2021-05-19 20:47:41 +02:00
}
const knownTagRenderings = new Set<string>()
convertState.tagRenderings.forEach((_, key) => knownTagRenderings.add(key))
sharedLayers.forEach((layer) => {
for (const tagRendering of layer.tagRenderings ?? []) {
if (tagRendering["id"]) {
knownTagRenderings.add(layer.id + "." + tagRendering["id"])
}
if (tagRendering["labels"]) {
for (const label of tagRendering["labels"]) {
knownTagRenderings.add(layer.id + "." + label)
}
}
}
})
const skippedThemes: string[] = []
for (let i = 0; i < themeFiles.length; i++) {
const themeInfo = themeFiles[i]
2022-09-08 21:40:48 +02:00
const themePath = themeInfo.path
2021-12-21 18:35:31 +01:00
let themeFile = themeInfo.parsed
if (whitelist.size > 0 && !whitelist.has(themeFile.id)) {
continue
}
const targetPath =
LayerOverviewUtils.themePath + "/" + themePath.substring(themePath.lastIndexOf("/"))
const usedLayers = Array.from(
2024-07-09 13:42:08 +02:00
LayerOverviewUtils.extractLayerIdsFrom(themeFile, false)
).map((id) => LayerOverviewUtils.layerPath + id + ".json")
if (!forceReload && !this.shouldBeUpdated([themePath, ...usedLayers], targetPath)) {
fixed.set(
themeFile.id,
JSON.parse(
2024-07-09 13:42:08 +02:00
readFileSync(LayerOverviewUtils.themePath + themeFile.id + ".json", "utf8")
)
)
ScriptUtils.erasableLog("Skipping", themeFile.id)
skippedThemes.push(themeFile.id)
continue
}
recompiledThemes.push(themeFile.id)
2022-01-16 01:59:06 +01:00
new PrevalidateTheme().convertStrict(
themeFile,
2024-07-09 13:42:08 +02:00
ConversionContext.construct([themePath], ["PrepareLayer"])
)
2022-06-20 01:41:34 +02:00
try {
2023-12-06 12:12:53 +01:00
themeFile = new PrepareTheme(convertState, {
2024-06-20 04:21:29 +02:00
skipDefaultLayers: true,
2023-12-06 12:12:53 +01:00
}).convertStrict(
themeFile,
2024-07-09 13:42:08 +02:00
ConversionContext.construct([themePath], ["PrepareLayer"])
)
2022-09-08 21:40:48 +02:00
new ValidateThemeAndLayers(
new DoesImageExist(licensePaths, existsSync, knownTagRenderings),
2022-09-08 21:40:48 +02:00
themePath,
true,
2024-07-09 13:42:08 +02:00
knownTagRenderings
).convertStrict(
themeFile,
2024-07-09 13:42:08 +02:00
ConversionContext.construct([themePath], ["PrepareLayer"])
)
2022-06-20 01:41:34 +02:00
if (themeFile.icon.endsWith(".svg")) {
try {
2023-01-17 01:53:50 +01:00
ScriptUtils.ReadSvgSync(themeFile.icon, (svg) => {
2024-06-18 03:33:11 +02:00
const width: string = svg["$"].width
if (width === undefined) {
2024-07-21 10:52:51 +02:00
throw (
"The logo at " +
themeFile.icon +
" does not have a defined width"
)
}
2024-06-18 03:33:11 +02:00
const height: string = svg["$"].height
2023-01-17 01:53:50 +01:00
const err = themeFile.hideFromOverview ? console.warn : console.error
if (width !== height) {
const e =
2023-01-17 01:53:50 +01:00
`the icon for theme ${themeFile.id} is not square. Please square the icon at ${themeFile.icon}` +
` Width = ${width} height = ${height}`
2023-01-17 01:53:50 +01:00
err(e)
}
if (width?.endsWith("%")) {
2024-07-21 10:52:51 +02:00
throw (
"The logo at " +
themeFile.icon +
" has a relative width; this is not supported"
)
}
const w = parseInt(width)
const h = parseInt(height)
if (w < 370 || h < 370) {
const e: string = [
2023-01-17 01:53:50 +01:00
`the icon for theme ${themeFile.id} is too small. Please rescale the icon at ${themeFile.icon}`,
`Even though an SVG is 'infinitely scaleable', the icon should be dimensioned bigger. One of the build steps of the theme does convert the image to a PNG (to serve as PWA-icon) and having a small dimension will cause blurry images.`,
2024-06-20 04:21:29 +02:00
` Width = ${width} height = ${height}; we recommend a size of at least 500px * 500px and to use a square aspect ratio.`,
].join("\n")
2023-01-17 01:53:50 +01:00
err(e)
}
})
} catch (e) {
2023-01-17 01:53:50 +01:00
console.error("Could not read " + themeFile.icon + " due to " + e)
}
}
2022-04-22 03:17:40 +02:00
this.writeTheme(themeFile)
fixed.set(themeFile.id, themeFile)
this.extractJavascriptCode(themeFile)
2022-06-20 01:41:34 +02:00
} catch (e) {
console.error("ERROR: could not prepare theme " + themePath + " due to " + e)
2022-09-08 21:40:48 +02:00
throw e
}
}
if (whitelist.size == 0) {
this.writeSmallOverview(
Array.from(fixed.values()).map((t) => {
return {
...t,
hideFromOverview: t.hideFromOverview ?? false,
shortDescription:
2024-07-08 23:35:40 +02:00
t.shortDescription ?? new Translation(t.description).FirstSentence(),
2024-06-20 04:21:29 +02:00
mustHaveLanguage: t.mustHaveLanguage?.length > 0,
}
2024-07-09 13:42:08 +02:00
})
)
}
2022-09-08 21:40:48 +02:00
console.log(
"Recompiled themes " +
2024-07-09 13:42:08 +02:00
recompiledThemes.join(", ") +
" and skipped " +
skippedThemes.length +
" themes"
2022-09-08 21:40:48 +02:00
)
return fixed
2021-12-21 18:35:31 +01:00
}
2021-04-10 14:25:06 +02:00
}
new GenerateFavouritesLayer().run()
new LayerOverviewUtils().run()