Fix autoapply for GRB theme

This commit is contained in:
Pieter Vander Vennet 2022-02-10 23:16:14 +01:00
parent db770f2c35
commit 30be86668e
16 changed files with 392 additions and 209 deletions

View file

@ -18,11 +18,15 @@ export abstract class Conversion<TIn, TOut> {
this.name = name ?? this.constructor.name
}
public static strict<T>(fixed: { errors?: string[], warnings?: string[], result?: T }): T {
public static strict<T>(fixed: { errors?: string[], warnings?: string[], information?: string[], result?: T }): T {
if (fixed?.errors !== undefined && fixed?.errors?.length > 0) {
throw fixed.errors.join("\n\n");
}
fixed.warnings?.forEach(w => console.warn(w))
fixed.information?.forEach(i => console.log(" ", i))
const yellow = (s) => "\x1b[33m"+s+"\x1b[0m"
const red = s => '\x1b[31m'+s+'\x1b[0m'
fixed.warnings?.forEach(w => console.warn(red(`<!> `), yellow (w)))
return fixed.result;
}
@ -31,26 +35,29 @@ export abstract class Conversion<TIn, TOut> {
return DesugaringStep.strict(fixed)
}
abstract convert(json: TIn, context: string): { result: TOut, errors?: string[], warnings?: string[] }
abstract convert(json: TIn, context: string): { result: TOut, errors?: string[], warnings?: string[], information?: string[] }
public convertAll(jsons: TIn[], context: string): { result: TOut[], errors: string[], warnings: string[] } {
public convertAll(jsons: TIn[], context: string): { result: TOut[], errors: string[], warnings: string[], information?: string[] } {
if(jsons === undefined){
throw "convertAll received undefined - don't do this (at "+context+")"
}
const result = []
const errors = []
const warnings = []
const information = []
for (let i = 0; i < jsons.length; i++) {
const json = jsons[i];
const r = this.convert(json, context + "[" + i + "]")
result.push(r.result)
errors.push(...r.errors ?? [])
warnings.push(...r.warnings ?? [])
information.push(...r.information ?? [])
}
return {
result,
errors,
warnings
warnings,
information
}
}
@ -69,16 +76,15 @@ export class OnEvery<X, T> extends DesugaringStep<T> {
this.key = key;
}
convert(json: T, context: string): { result: T; errors?: string[]; warnings?: string[] } {
convert(json: T, context: string): { result: T; errors?: string[]; warnings?: string[], information?: string[] } {
json = {...json}
const step = this.step
const key = this.key;
const r = step.convertAll((<X[]>json[key]), context + "." + key)
json[key] = r.result
return {
...r,
result: json,
errors: r.errors,
warnings: r.warnings
};
}
}
@ -94,7 +100,7 @@ export class OnEveryConcat<X, T> extends DesugaringStep<T> {
this.key = key;
}
convert(json: T, context: string): { result: T; errors: string[]; warnings: string[] } {
convert(json: T, context: string): { result: T; errors?: string[]; warnings?: string[], information?: string[] } {
json = {...json}
const step = this.step
const key = this.key;
@ -103,17 +109,14 @@ export class OnEveryConcat<X, T> extends DesugaringStep<T> {
// Move on - nothing to see here!
return {
result: json,
errors: [],
warnings: []
}
}
const r = step.convertAll((<X[]>values), context + "." + key)
const vals: X[][] = r.result
json[key] = [].concat(...vals)
return {
...r,
result: json,
errors: r.errors,
warnings: r.warnings
};
}
@ -129,14 +132,16 @@ export class Fuse<T> extends DesugaringStep<T> {
this.steps = steps;
}
convert(json: T, context: string): { result: T; errors: string[]; warnings: string[] } {
convert(json: T, context: string): { result: T; errors: string[]; warnings: string[], information: string[] } {
const errors = []
const warnings = []
const information = []
for (let i = 0; i < this.steps.length; i++) {
const step = this.steps[i];
let r = step.convert(json, "While running step " +step.name + ": " + context)
errors.push(...r.errors ?? [])
warnings.push(...r.warnings ?? [])
information.push(...r.information ?? [])
json = r.result
if (errors.length > 0) {
break;
@ -145,7 +150,8 @@ export class Fuse<T> extends DesugaringStep<T> {
return {
result: json,
errors,
warnings
warnings,
information
};
}
@ -163,14 +169,13 @@ export class SetDefault<T> extends DesugaringStep<T> {
this._overrideEmptyString = overrideEmptyString;
}
convert(json: T, context: string): { result: T; errors: string[]; warnings: string[] } {
convert(json: T, context: string): { result: T } {
if (json[this.key] === undefined || (json[this.key] === "" && this._overrideEmptyString)) {
json = {...json}
json[this.key] = this.value
}
return {
errors: [], warnings: [],
result: json
};
}

View file

@ -20,9 +20,7 @@ export default class CreateNoteImportLayer extends Conversion<LayerConfigJson, L
this._includeClosedNotesDays = includeClosedNotesDays;
}
convert(layerJson: LayerConfigJson, context: string): { result: LayerConfigJson; errors: string[]; warnings: string[] } {
const errors = []
const warnings = []
convert(layerJson: LayerConfigJson, context: string): { result: LayerConfigJson } {
const t = Translations.t.importLayer;
/**
@ -182,8 +180,7 @@ export default class CreateNoteImportLayer extends Conversion<LayerConfigJson, L
return {
result,
errors, warnings
result
};
}

View file

@ -59,7 +59,7 @@ export class FixImages extends DesugaringStep<LayoutConfigJson> {
this._knownImages = knownImages;
}
convert(json: LayoutConfigJson, context: string): { result: LayoutConfigJson; errors?: string[]; warnings?: string[] } {
convert(json: LayoutConfigJson, context: string): { result: LayoutConfigJson } {
let url: URL;
try {
url = new URL(json.id)

View file

@ -213,7 +213,6 @@ class ExpandGroupRewrite extends Conversion<{
for (let i = 0; i < sourceStrings.length; i++) {
const source = sourceStrings[i]
const target = targets[i] // This is a string OR a translation
console.log("Replacing every "+source+" with "+JSON.stringify(target))
rewritten = this.prepConfig(source, target, rewritten)
}
rewritten.group = rewritten.group ?? groupName

View file

@ -20,8 +20,9 @@ class SubstituteLayer extends Conversion<(string | LayerConfigJson), LayerConfig
this._state = state;
}
convert(json: string | LayerConfigJson, context: string): { result: LayerConfigJson[]; errors: string[], warnings?: string[] } {
convert(json: string | LayerConfigJson, context: string): { result: LayerConfigJson[]; errors: string[], information?: string[] } {
const errors = []
const information = []
const state= this._state
function reportNotFound(name: string){
const knownLayers = Array.from(state.sharedLayers.keys())
@ -55,7 +56,6 @@ class SubstituteLayer extends Conversion<(string | LayerConfigJson), LayerConfig
names = [names]
}
const layers = []
const warnings = []
for (const name of names) {
const found = Utils.Clone(state.sharedLayers.get(name))
@ -84,20 +84,20 @@ class SubstituteLayer extends Conversion<(string | LayerConfigJson), LayerConfig
const forbiddenLabel = labels.findIndex(l => hideLabels.has(l))
if(forbiddenLabel >= 0){
usedLabels.add(labels[forbiddenLabel])
warnings.push(context+": Dropping tagRendering "+tr["id"]+" as it has a forbidden label: "+labels[forbiddenLabel])
information.push(context+": Dropping tagRendering "+tr["id"]+" as it has a forbidden label: "+labels[forbiddenLabel])
continue
}
}
if(hideLabels.has(tr["id"])){
usedLabels.add(tr["id"])
warnings.push(context+": Dropping tagRendering "+tr["id"]+" as its id is a forbidden label")
information.push(context+": Dropping tagRendering "+tr["id"]+" as its id is a forbidden label")
continue
}
if(hideLabels.has(tr["group"])){
usedLabels.add(tr["group"])
warnings.push(context+": Dropping tagRendering "+tr["id"]+" as its group `"+tr["group"]+"` is a forbidden label")
information.push(context+": Dropping tagRendering "+tr["id"]+" as its group `"+tr["group"]+"` is a forbidden label")
continue
}
@ -113,7 +113,7 @@ class SubstituteLayer extends Conversion<(string | LayerConfigJson), LayerConfig
return {
result: layers,
errors,
warnings
information
}
}
@ -185,9 +185,8 @@ class AddImportLayers extends DesugaringStep<LayoutConfigJson> {
super("For every layer in the 'layers'-list, create a new layer which'll import notes. (Note that priviliged layers and layers which have a geojson-source set are ignored)", ["layers"]);
}
convert(json: LayoutConfigJson, context: string): { result: LayoutConfigJson; errors: string[]; warnings: string[] } {
convert(json: LayoutConfigJson, context: string): { result: LayoutConfigJson; errors: string[] } {
const errors = []
const warnings = []
json = {...json}
const allLayers: LayerConfigJson[] = <LayerConfigJson[]>json.layers;
@ -221,8 +220,6 @@ class AddImportLayers extends DesugaringStep<LayoutConfigJson> {
try {
const importLayerResult = creator.convert(layer, context + ".(noteimportlayer)[" + i1 + "]")
errors.push(...importLayerResult.errors)
warnings.push(...importLayerResult.warnings)
if (importLayerResult.result !== undefined) {
json.layers.push(importLayerResult.result)
}
@ -234,7 +231,6 @@ class AddImportLayers extends DesugaringStep<LayoutConfigJson> {
return {
errors,
warnings,
result: json
};
}
@ -274,7 +270,7 @@ export class AddMiniMap extends DesugaringStep<LayerConfigJson> {
return false;
}
convert(layerConfig: LayerConfigJson, context: string): { result: LayerConfigJson; errors: string[]; warnings: string[] } {
convert(layerConfig: LayerConfigJson, context: string): { result: LayerConfigJson } {
const state = this._state;
const hasMinimap = layerConfig.tagRenderings?.some(tr => AddMiniMap.hasMinimap(<TagRenderingConfigJson>tr)) ?? true
@ -286,8 +282,6 @@ export class AddMiniMap extends DesugaringStep<LayerConfigJson> {
}
return {
errors: [],
warnings: [],
result: layerConfig
};
}
@ -384,12 +378,11 @@ class AddDependencyLayersToTheme extends DesugaringStep<LayoutConfigJson> {
});
}
convert(theme: LayoutConfigJson, context: string): { result: LayoutConfigJson; errors: string[]; warnings: string[] } {
convert(theme: LayoutConfigJson, context: string): { result: LayoutConfigJson; information: string[] } {
const state = this._state
const allKnownLayers: Map<string, LayerConfigJson> = state.sharedLayers;
const knownTagRenderings: Map<string, TagRenderingConfigJson> = state.tagRenderings;
const errors = [];
const warnings = [];
const information = [];
const layers: LayerConfigJson[] = <LayerConfigJson[]>theme.layers; // Layers should be expanded at this point
knownTagRenderings.forEach((value, key) => {
@ -399,7 +392,7 @@ class AddDependencyLayersToTheme extends DesugaringStep<LayoutConfigJson> {
const dependencies = AddDependencyLayersToTheme.CalculateDependencies(layers, allKnownLayers, theme.id);
if (dependencies.length > 0) {
warnings.push(context + ": added " + dependencies.map(d => d.id).join(", ") + " to the theme as they are needed")
information.push(context + ": added " + dependencies.map(d => d.id).join(", ") + " to the theme as they are needed")
}
layers.unshift(...dependencies);
@ -408,8 +401,7 @@ class AddDependencyLayersToTheme extends DesugaringStep<LayoutConfigJson> {
...theme,
layers: layers
},
errors,
warnings
information
};
}
}

View file

@ -9,6 +9,7 @@ import LayoutConfig from "../LayoutConfig";
import {TagRenderingConfigJson} from "../Json/TagRenderingConfigJson";
import {TagUtils} from "../../../Logic/Tags/TagUtils";
import {ExtractImages} from "./FixImages";
import ScriptUtils from "../../../scripts/ScriptUtils";
class ValidateLanguageCompleteness extends DesugaringStep<any> {
@ -55,9 +56,10 @@ class ValidateTheme extends DesugaringStep<LayoutConfigJson> {
this._isBuiltin = isBuiltin;
}
convert(json: LayoutConfigJson, context: string): { result: LayoutConfigJson; errors: string[], warnings: string[] } {
convert(json: LayoutConfigJson, context: string): { result: LayoutConfigJson; errors: string[], warnings: string[], information: string[] } {
const errors = []
const warnings = []
const information = []
{
// Legacy format checks
if (this._isBuiltin) {
@ -70,7 +72,7 @@ class ValidateTheme extends DesugaringStep<LayoutConfigJson> {
}
}
{
// Check for remote images
// Check images: are they local, are the licenses there, is the theme icon square, ...
const images = new ExtractImages().convertStrict(json, "validation")
const remoteImages = images.filter(img => img.indexOf("http") == 0)
for (const remoteImage of remoteImages) {
@ -78,14 +80,14 @@ class ValidateTheme extends DesugaringStep<LayoutConfigJson> {
}
for (const image of images) {
if (image.indexOf("{") >= 0) {
warnings.push("Ignoring image with { in the path: ", image)
information.push("Ignoring image with { in the path: " + image)
continue
}
if(image === "assets/SocialImage.png"){
if (image === "assets/SocialImage.png") {
continue
}
if(image.match(/[a-z]*/)){
if (image.match(/[a-z]*/)) {
// This is a builtin img, e.g. 'checkmark' or 'crosshair'
continue;
}
@ -96,6 +98,22 @@ class ValidateTheme extends DesugaringStep<LayoutConfigJson> {
}
}
if (json.icon.endsWith(".svg")) {
try {
ScriptUtils.ReadSvgSync(json.icon, svg => {
const width: string = svg.$.width;
const height: string = svg.$.height;
if (width !== height) {
const e = `the icon for theme ${json.id} is not square. Please square the icon at ${json.icon}` +
` Width = ${width} height = ${height}`;
(json.hideFromOverview ? warnings : errors).push(e)
}
})
} catch (e) {
console.error("Could not read " + json.icon + " due to " + e)
}
}
}
try {
const theme = new LayoutConfig(json, true, "test")
@ -127,7 +145,8 @@ class ValidateTheme extends DesugaringStep<LayoutConfigJson> {
return {
result: json,
errors,
warnings
warnings,
information
};
}
}
@ -142,60 +161,60 @@ export class ValidateThemeAndLayers extends Fuse<LayoutConfigJson> {
}
class OverrideShadowingCheck extends DesugaringStep<LayoutConfigJson>{
class OverrideShadowingCheck extends DesugaringStep<LayoutConfigJson> {
constructor() {
super("Checks that an 'overrideAll' does not override a single override");
}
convert(json: LayoutConfigJson, context: string): { result: LayoutConfigJson; errors?: string[]; warnings?: string[] } {
const overrideAll = json.overrideAll;
if(overrideAll === undefined){
if (overrideAll === undefined) {
return {result: json}
}
const errors = []
const withOverride = json.layers.filter(l => l["override"] !== undefined)
const withOverride = json.layers.filter(l => l["override"] !== undefined)
for (const layer of withOverride) {
for (const key in overrideAll) {
if(layer["override"][key] !== undefined || layer["override"]["="+key] !== undefined){
const w = "The override of layer "+JSON.stringify(layer["builtin"])+" has a shadowed property: "+key+" is overriden by overrideAll of the theme";
errors.push(w)
}
if (layer["override"][key] !== undefined || layer["override"]["=" + key] !== undefined) {
const w = "The override of layer " + JSON.stringify(layer["builtin"]) + " has a shadowed property: " + key + " is overriden by overrideAll of the theme";
errors.push(w)
}
}
}
return {result: json, errors}
return {result: json, errors}
}
}
export class PrevalidateTheme extends Fuse<LayoutConfigJson>{
export class PrevalidateTheme extends Fuse<LayoutConfigJson> {
constructor() {
super("Various consistency checks on the raw JSON",
new OverrideShadowingCheck()
);
);
}
}
export class DetectShadowedMappings extends DesugaringStep<TagRenderingConfigJson>{
export class DetectShadowedMappings extends DesugaringStep<TagRenderingConfigJson> {
constructor() {
super("Checks that the mappings don't shadow each other");
}
convert(json: TagRenderingConfigJson, context: string): { result: TagRenderingConfigJson; errors?: string[]; warnings?: string[] } {
const errors = []
if(json.mappings === undefined || json.mappings.length === 0){
if (json.mappings === undefined || json.mappings.length === 0) {
return {result: json}
}
const parsedConditions = json.mappings.map(m => TagUtils.Tag(m.if))
for (let i = 0; i < json.mappings.length; i++){
if(!parsedConditions[i].isUsableAsAnswer()){
for (let i = 0; i < json.mappings.length; i++) {
if (!parsedConditions[i].isUsableAsAnswer()) {
continue
}
const keyValues = parsedConditions[i].asChange({});
@ -203,12 +222,12 @@ export class DetectShadowedMappings extends DesugaringStep<TagRenderingConfigJso
keyValues.forEach(({k, v}) => {
properties[k] = v
})
for (let j = 0; j < i; j++){
for (let j = 0; j < i; j++) {
const doesMatch = parsedConditions[j].matchesProperties(properties)
if(doesMatch){
if (doesMatch) {
// The current mapping is shadowed!
errors.push(`Mapping ${i} is shadowed by mapping ${j} and will thus never be shown:
The mapping ${parsedConditions[i].asHumanString(false,false, {})} is fully matched by a previous mapping, which matches:
The mapping ${parsedConditions[i].asHumanString(false, false, {})} is fully matched by a previous mapping, which matches:
${parsedConditions[j].asHumanString(false, false, {})}.
Move the mapping up to fix this problem
@ -217,7 +236,7 @@ export class DetectShadowedMappings extends DesugaringStep<TagRenderingConfigJso
}
}
return {
errors,
result: json
@ -316,17 +335,15 @@ export class ValidateLayer extends DesugaringStep<LayerConfigJson> {
}
}
}
if(json.tagRenderings !== undefined){
new DetectShadowedMappings().convertAll(<TagRenderingConfigJson[]> json.tagRenderings, context+".tagRenderings")
if (json.tagRenderings !== undefined) {
new DetectShadowedMappings().convertAll(<TagRenderingConfigJson[]>json.tagRenderings, context + ".tagRenderings")
}
} catch (e) {
errors.push(e)
}
return {
result: json,
errors,