From 8761685fa609f8e9b9ef4674166da8a4ab2d1b95 Mon Sep 17 00:00:00 2001 From: Pieter Vander Vennet Date: Sat, 5 Jul 2025 02:03:39 +0200 Subject: [PATCH] Scripts: automatically reorder layers and themes into the fixed order --- scripts/lint.ts | 41 ++----------------- .../ThemeConfig/Conversion/PrepareLayer.ts | 18 +++++++- .../ThemeConfig/Conversion/PrepareTheme.ts | 24 ++++++++--- src/Utils.ts | 18 +++++++- 4 files changed, 55 insertions(+), 46 deletions(-) diff --git a/scripts/lint.ts b/scripts/lint.ts index 55ef491ea2..8e3d6fbb38 100644 --- a/scripts/lint.ts +++ b/scripts/lint.ts @@ -1,47 +1,14 @@ import ScriptUtils from "./ScriptUtils" import { writeFileSync } from "fs" -import { - FixLegacyTheme, - UpdateLegacyLayer, -} from "../src/Models/ThemeConfig/Conversion/LegacyJsonConvert" -import Translations from "../src/UI/i18n/Translations" -import { Translation } from "../src/UI/i18n/Translation" +import { FixLegacyTheme, UpdateLegacyLayer } from "../src/Models/ThemeConfig/Conversion/LegacyJsonConvert" import { LayerConfigJson } from "../src/Models/ThemeConfig/Json/LayerConfigJson" -import themeconfig from "../src/assets/schemas/layoutconfigmeta.json" -import layerconfig from "../src/assets/schemas/layerconfigmeta.json" - -import { Utils } from "../src/Utils" -import { ConfigMeta } from "../src/UI/Studio/configMeta" import { ConversionContext } from "../src/Models/ThemeConfig/Conversion/ConversionContext" /* * This script reads all theme and layer files and reformats them inplace * Use with caution, make a commit beforehand! */ -const themeAttributesOrder = Utils.Dedup( - (themeconfig).filter((c) => c.path.length === 1).map((c) => c.path[0]) -) -const layerAttributesOrder = Utils.Dedup( - (layerconfig).filter((c) => c.path.length === 1).map((c) => c.path[0]) -) -const t: Translation = Translations.t.general.add.addNew -t.OnEveryLanguage((txt, ln) => { - console.log(ln, txt) - return txt -}) -function reorder(object: object, order: string[]) { - const allKeys = new Set(Object.keys(object)) - const copy = {} - for (const key of order) { - copy[key] = object[key] - allKeys.delete(key) - } - for (const key of allKeys) { - copy[key] = object[key] - } - return copy -} const layerFiles = ScriptUtils.getLayerFiles() for (const layerFile of layerFiles) { @@ -52,8 +19,7 @@ for (const layerFile of layerFiles) { ConversionContext.construct([layerFile.path.split("/").at(-1)], ["update legacy"]) ) ) - const reordered = reorder(fixed, layerAttributesOrder) - writeFileSync(layerFile.path, JSON.stringify(reordered, null, " ") + "\n") + writeFileSync(layerFile.path, JSON.stringify(fixed, null, " ") + "\n") } catch (e) { console.error("COULD NOT LINT LAYER" + layerFile.path + ":\n\t" + e) } @@ -69,10 +35,9 @@ for (const themeFile of themeFiles) { // extractInlineLayer(fixed) const endsWithNewline = themeFile.raw.at(-1) === "\n" - const ordered = reorder(fixed, themeAttributesOrder) writeFileSync( themeFile.path, - JSON.stringify(ordered, null, " ") + (endsWithNewline ? "\n" : "") + JSON.stringify(fixed, null, " ") + (endsWithNewline ? "\n" : "") ) } catch (e) { console.error("COULD NOT LINT THEME" + themeFile.path + ":\n\t" + e) diff --git a/src/Models/ThemeConfig/Conversion/PrepareLayer.ts b/src/Models/ThemeConfig/Conversion/PrepareLayer.ts index 62256c392d..6d21e86bc0 100644 --- a/src/Models/ThemeConfig/Conversion/PrepareLayer.ts +++ b/src/Models/ThemeConfig/Conversion/PrepareLayer.ts @@ -32,6 +32,7 @@ import { ExpandRewrite } from "./ExpandRewrite" import { TagUtils } from "../../../Logic/Tags/TagUtils" import { ExpandFilter, PruneFilters } from "./ExpandFilter" import { ExpandTagRendering } from "./ExpandTagRendering" +import layerconfig from "../../../assets/schemas/layerconfigmeta.json" class AddFiltersFromTagRenderings extends DesugaringStep { constructor() { @@ -1077,6 +1078,20 @@ class DeriveSource extends DesugaringStep { } } +class OrderLayer extends DesugaringStep{ + + private static readonly layerAttributesOrder: ReadonlyArray = Utils.Dedup( + (layerconfig).filter((c) => c.path.length === 1).map((c) => c.path[0]) + ) + constructor() { + super("OrderLayer", "Reorders the layer to the default order") + } + + public convert(json: LayerConfigJson, context: ConversionContext): LayerConfigJson { + return Utils.reorder(json, OrderLayer.layerAttributesOrder) + } +} + export class PrepareLayer extends Fuse { constructor( state: DesugaringContext, @@ -1125,7 +1140,8 @@ export class PrepareLayer extends Fuse { new AddFiltersFromTagRenderings(), new ExpandFilter(state), new MoveUnitConfigs(), - new PruneFilters() + new PruneFilters(), + new OrderLayer() ) } diff --git a/src/Models/ThemeConfig/Conversion/PrepareTheme.ts b/src/Models/ThemeConfig/Conversion/PrepareTheme.ts index 68848a055d..7149cd5c72 100644 --- a/src/Models/ThemeConfig/Conversion/PrepareTheme.ts +++ b/src/Models/ThemeConfig/Conversion/PrepareTheme.ts @@ -20,6 +20,8 @@ import DependencyCalculator from "../DependencyCalculator" import { AddContextToTranslations } from "./AddContextToTranslations" import ValidationUtils from "./ValidationUtils" import { ConversionContext } from "./ConversionContext" +import { ConfigMeta } from "../../../UI/Studio/configMeta" +import themeconfig from "../../../assets/schemas/layoutconfigmeta.json" class SubstituteLayer extends Conversion { private readonly _state: DesugaringContext @@ -28,7 +30,6 @@ class SubstituteLayer extends Conversion 0 @@ -605,7 +603,20 @@ class PostvalidateTheme extends DesugaringStep { return json } } +class OrderTheme extends DesugaringStep{ + private static readonly themeAttributesOrder: ReadonlyArray = Utils.Dedup( + (themeconfig).filter((c) => c.path.length === 1).map((c) => c.path[0]) + ) + + constructor() { + super("OrderLayer", "Reorders the layer to the default order") + } + + public convert(json: ThemeConfigJson, context: ConversionContext): ThemeConfigJson { + return Utils.reorder(json, OrderTheme.themeAttributesOrder) + } +} export class PrepareTheme extends Fuse { private state: DesugaringContext @@ -646,7 +657,8 @@ export class PrepareTheme extends Fuse { : new AddDefaultLayers(state), new AddDependencyLayersToTheme(state), // new AddImportLayers(), - new PostvalidateTheme(state) + new PostvalidateTheme(state), + new OrderTheme() ) this.state = state } diff --git a/src/Utils.ts b/src/Utils.ts index bfe46f1f75..cd1138d1e1 100644 --- a/src/Utils.ts +++ b/src/Utils.ts @@ -1229,6 +1229,8 @@ In the case that MapComplete is pointed to the testing grounds, the edit will be * @param x * @constructor */ + static Clone(x: Readonly): T + static Clone(x: T): T static Clone(x: T): T { if (x === undefined) { return undefined @@ -1267,7 +1269,7 @@ In the case that MapComplete is pointed to the testing grounds, the edit will be reference: string, ts: ReadonlyArray, getName: (t: T) => string - ): string[] + ): T[] public static sortedByLevenshteinDistance( reference: string, ts: ReadonlyArray, @@ -1841,4 +1843,18 @@ In the case that MapComplete is pointed to the testing grounds, the edit will be public static concat(param: T[][]): T[] { return [].concat(...param) } + + + public static reorder(object: T, order: ReadonlyArray): T { + const allKeys = new Set(Object.keys(object)) + const copy = {} + for (const key of order) { + copy[key] = object[key] + allKeys.delete(key) + } + for (const key of allKeys) { + copy[key] = object[key] + } + return copy + } }