aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/build-declarations.sh
blob: 44b68f4783cc2344273751405ba125a88b539206 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/env bash

main() {
    scriptfile="$1"
    is_tool=false
    if [[ "$(dirname "$scriptfile")" == tools ]]; then
        is_tool=true
    fi
    if [[ "$is_tool" == "true" ]]; then
        expr='[.]' 
    else
        expr='.subcommands' 
    fi
    argc --argc-export "$scriptfile" | \
    jq "$expr" | \
    build_declarations
}

build_declarations() {
    jq --arg is_tool "$is_tool" -r '
    def filter_declaration:
        (if $is_tool == "true" then
            .
        else
            select(.name | startswith("_") | not) 
        end) | select(.description != "");

    def parse_description(flag_option):
        if flag_option.describe == "" then
            {}
        else
            { "description": flag_option.describe }
        end;

    def parse_enum(flag_option):
        if flag_option.choice.type == "Values" then
            { "enum": flag_option.choice.data }
        else
            {}
        end;

    def parse_property(flag_option):
        [
            { condition: (flag_option.flag == true), result: { type: "boolean" } },
            { condition: (flag_option.multiple_occurs == true), result: { type: "array", items: { type: "string" } } },
            { condition: (flag_option.notations[0] == "INT"), result: { type: "integer" } },
            { condition: (flag_option.notations[0] == "NUM"), result: { type: "number" } },
            { condition: true, result: { type: "string" } } ]
        | map(select(.condition) | .result) | first 
        | (. + parse_description(flag_option))
        | (. + parse_enum(flag_option))
        ;


    def parse_parameter(flag_options):
        {
            type: "object",
            properties: (reduce flag_options[] as $item ({}; . + { ($item.id | sub("-"; "_"; "g")): parse_property($item) })),
            required: [flag_options[] | select(.required == true) | .id | sub("-"; "_"; "g")],
        };

    def parse_declaration:
        {
            name: (.name | sub("-"; "_"; "g")),
            description: .describe,
            parameters: parse_parameter([.flag_options[] | select(.id != "help" and .id != "version")])
        };
    [
        .[] | parse_declaration | filter_declaration
    ]'
}

main "$@"