From f5f390089d25573e27ffb0f8d4dce37bb731c1ee Mon Sep 17 00:00:00 2001 From: Klaus Aehlig Date: Tue, 17 Jun 2025 15:37:15 +0200 Subject: Add auto-formatting of json file Given the just added json-file formatter, we can generate a diff formatting properly all json files, i.e., all target, rules, and expression files, by applying that formatter to every target file and combining the patches. A target description doing precisely this can easily be obtained as computed root. In this way, we also make good use of caching. To avoid doing recomputing the target description unnecessarily, we factor through the tree structure of the repository tree; the latter, we obtain from the to-git view of the top-level directory, whereas for computing the diff we use the actual (not necessarily committed) files. --- format-json/EXPRESSIONS | 46 ++++++++++++++++ format-json/RULES | 126 ++++++++++++++++++++++++++++++++++++++++++++ format-json/TARGETS | 1 + format-json/TARGETS.tasks | 16 ++++++ format-json/generate-fmt.py | 24 +++++++++ 5 files changed, 213 insertions(+) create mode 100644 format-json/EXPRESSIONS create mode 100644 format-json/RULES create mode 100644 format-json/TARGETS create mode 100644 format-json/TARGETS.tasks create mode 100644 format-json/generate-fmt.py (limited to 'format-json') diff --git a/format-json/EXPRESSIONS b/format-json/EXPRESSIONS new file mode 100644 index 00000000..f1ca6f4a --- /dev/null +++ b/format-json/EXPRESSIONS @@ -0,0 +1,46 @@ +{ "stage_singleton_field": + { "vars": ["fieldname", "transition", "location"] + , "expression": + { "type": "assert_non_empty" + , "msg": + ["No artifact specified in field", {"type": "var", "name": "fieldname"}] + , "$1": + { "type": "disjoint_map_union" + , "msg": + [ "Expecting (essentially) a single artifact in field" + , {"type": "var", "name": "fieldname"} + ] + , "$1": + { "type": "foreach" + , "var": "src" + , "range": + {"type": "FIELD", "name": {"type": "var", "name": "fieldname"}} + , "body": + { "type": "disjoint_map_union" + , "$1": + { "type": "foreach" + , "var": "artifact" + , "range": + { "type": "values" + , "$1": + { "type": "DEP_ARTIFACTS" + , "dep": {"type": "var", "name": "src"} + , "transition": + { "type": "var" + , "name": "transition" + , "default": {"type": "empty_map"} + } + } + } + , "body": + { "type": "singleton_map" + , "key": {"type": "var", "name": "location"} + , "value": {"type": "var", "name": "artifact"} + } + } + } + } + } + } + } +} diff --git a/format-json/RULES b/format-json/RULES new file mode 100644 index 00000000..1bfb36cb --- /dev/null +++ b/format-json/RULES @@ -0,0 +1,126 @@ +{ "fmt": + { "target_fields": ["files"] + , "implicit": {"formatter": [["@", "bin", "", "json-format.py"]]} + , "imports": {"stage": "stage_singleton_field"} + , "tainted": ["lint"] + , "expression": + { "type": "let*" + , "bindings": + [ ["fieldname", "formatter"] + , ["location", "format"] + , ["format", {"type": "CALL_EXPRESSION", "name": "stage"}] + , [ "diffs" + , { "type": "disjoint_map_union" + , "$1": + { "type": "foreach" + , "var": "dep" + , "range": {"type": "FIELD", "name": "files"} + , "body": + { "type": "disjoint_map_union" + , "$1": + { "type": "foreach_map" + , "range": + { "type": "DEP_ARTIFACTS" + , "dep": {"type": "var", "name": "dep"} + } + , "body": + { "type": "ACTION" + , "outs": + [ { "type": "join" + , "$1": [{"type": "var", "name": "_"}, ".diff"] + } + ] + , "inputs": + { "type": "disjoint_map_union" + , "$1": + [ {"type": "var", "name": "format"} + , { "type": "singleton_map" + , "key": + { "type": "join" + , "$1": [{"type": "var", "name": "_"}, ".orig"] + } + , "value": {"type": "var", "name": "$_"} + } + ] + } + , "cmd": + [ "sh" + , "-c" + , { "type": "join" + , "separator": " " + , "$1": + [ "./format -s" + , { "type": "join_cmd" + , "$1": + [ { "type": "join" + , "$1": [{"type": "var", "name": "_"}, ".orig"] + } + ] + } + , ">" + , { "type": "join_cmd" + , "$1": [{"type": "var", "name": "_"}] + } + , "&& diff -u" + , { "type": "join_cmd" + , "$1": + [ { "type": "join" + , "$1": [{"type": "var", "name": "_"}, ".orig"] + } + ] + } + , { "type": "join_cmd" + , "$1": [{"type": "var", "name": "_"}] + } + , ">" + , { "type": "join_cmd" + , "$1": + [ { "type": "join" + , "$1": [{"type": "var", "name": "_"}, ".diff"] + } + ] + } + ] + } + ] + , "may_fail": ["lint"] + , "fail_message": + { "type": "join" + , "$1": + [ "Target file " + , {"type": "var", "name": "_"} + , " not formatted correctly." + ] + } + } + } + } + } + } + ] + , [ "diff" + , { "type": "ACTION" + , "inputs": {"type": "var", "name": "diffs"} + , "outs": ["targets.diff"] + , "cmd": + [ "sh" + , "-c" + , { "type": "join" + , "separator": " " + , "$1": + [ "cat" + , { "type": "join_cmd" + , "$1": + {"type": "keys", "$1": {"type": "var", "name": "diffs"}} + } + , "> targets.diff" + ] + } + ] + } + ] + ] + , "body": {"type": "RESULT", "artifacts": {"type": "var", "name": "diff"}} + } + } +} diff --git a/format-json/TARGETS b/format-json/TARGETS new file mode 100644 index 00000000..0967ef42 --- /dev/null +++ b/format-json/TARGETS @@ -0,0 +1 @@ +{} diff --git a/format-json/TARGETS.tasks b/format-json/TARGETS.tasks new file mode 100644 index 00000000..2eb54fb8 --- /dev/null +++ b/format-json/TARGETS.tasks @@ -0,0 +1,16 @@ +{ "ls": + { "type": "generic" + , "outs": ["json-files"] + , "cmds": + [ "find . '(' -name 'TARGETS*' -o -name RULES -o -name EXPRESSIONS ')' -type f > json-files" + ] + , "deps": [["TREE", null, "."]] + } +, "targets": + { "type": "generic" + , "outs": ["TARGETS"] + , "cmds": ["python3 generate-fmt.py"] + , "deps": ["ls", ["@", "format", "", "generate-fmt.py"]] + } +, "": {"type": "export", "target": "targets"} +} diff --git a/format-json/generate-fmt.py b/format-json/generate-fmt.py new file mode 100644 index 00000000..de2c5391 --- /dev/null +++ b/format-json/generate-fmt.py @@ -0,0 +1,24 @@ +#!/usr/bin/env python3 +# Copyright 2025 Huawei Cloud Computing Technology Co., Ltd. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import json + +with open("json-files") as f: + tfiles = f.read().splitlines() + +targets = {"": {"type": "fmt", "files": [t.removeprefix("./") for t in tfiles]}} + +with open("TARGETS", "w") as f: + json.dump(targets, f, indent=2) -- cgit v1.2.3