Scripts: automatically reorder layers and themes into the fixed order

This commit is contained in:
Pieter Vander Vennet 2025-07-05 02:03:39 +02:00
parent 40678a2eb3
commit 8761685fa6
4 changed files with 55 additions and 46 deletions

View file

@ -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<LayerConfigJson> {
constructor() {
@ -1077,6 +1078,20 @@ class DeriveSource extends DesugaringStep<LayerConfigJson> {
}
}
class OrderLayer extends DesugaringStep<LayerConfigJson>{
private static readonly layerAttributesOrder: ReadonlyArray<string> = Utils.Dedup(
(<ConfigMeta[]>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<LayerConfigJson> {
constructor(
state: DesugaringContext,
@ -1125,7 +1140,8 @@ export class PrepareLayer extends Fuse<LayerConfigJson> {
new AddFiltersFromTagRenderings(),
new ExpandFilter(state),
new MoveUnitConfigs(),
new PruneFilters()
new PruneFilters(),
new OrderLayer()
)
}

View file

@ -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<string | LayerConfigJson, LayerConfigJson[]> {
private readonly _state: DesugaringContext
@ -28,7 +30,6 @@ class SubstituteLayer extends Conversion<string | LayerConfigJson, LayerConfigJs
super(
"SubstituteLayer",
"Converts the identifier of a builtin layer into the actual layer, or converts a 'builtin' syntax with override in the fully expanded form. Note that 'tagRenderings+' will be inserted before 'leftover-questions'",
[]
)
this._state = state
}
@ -73,13 +74,10 @@ class SubstituteLayer extends Conversion<string | LayerConfigJson, LayerConfigJs
if (found === undefined) {
const nearbyNames = Utils.sortedByLevenshteinDistance(name, Array.from(state.sharedLayers.keys()))
context.err("Layer with name " + name + " not found. Dit you mean one of "+nearbyNames.slice(0, 3))
continue
}
found["_basedOn"] = name
if (found === undefined) {
reportNotFound(name)
continue
}
found["_basedOn"] = name
if (
json["override"]["tagRenderings"] !== undefined &&
(found["tagRenderings"] ?? []).length > 0
@ -605,7 +603,20 @@ class PostvalidateTheme extends DesugaringStep<ThemeConfigJson> {
return json
}
}
class OrderTheme extends DesugaringStep<ThemeConfigJson>{
private static readonly themeAttributesOrder: ReadonlyArray<string> = Utils.Dedup(
(<ConfigMeta[]>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<ThemeConfigJson> {
private state: DesugaringContext
@ -646,7 +657,8 @@ export class PrepareTheme extends Fuse<ThemeConfigJson> {
: new AddDefaultLayers(state),
new AddDependencyLayersToTheme(state),
// new AddImportLayers(),
new PostvalidateTheme(state)
new PostvalidateTheme(state),
new OrderTheme()
)
this.state = state
}