aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/create-tool.sh
blob: 40d2ef3d8f0728c64cadcbc43ec7b3dc09516d52 (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/env bash
set -e

# @describe Create a boilplate tool script file.
# It automatically generate declaration json for `*.py` and `*.js` and generate `@option` tags for `.sh`.
# Examples:
#   argc create abc.sh foo bar! baz+ qux*
#   ./scripts/create-tool.sh test.py foo bar! baz+ qux*
# @arg name! The script file name.
# @arg params* The script parameters

main() {
    output="tools/$argc_name"
    if [[ -f "$output" ]]; then
        _die "$output already exists"
    fi
    ext="${argc_name##*.}"
    support_exts=('.sh' '.js' '.py')
    if [[ "$ext" == "$argc_name" ]]; then
        _die "No extension name, pelease add one of ${support_exts[*]}" 
    fi
    case $ext in
    sh) create_sh ;;
    js) create_js ;;
    py) create_py ;;
    *) _die "Invalid extension name: $ext, must be one of ${support_exts[*]}" ;; 
    esac
}

create_sh() {
    cat <<-'EOF' | sed 's/__DESCRIBE_TAG__/# @describe/g' > "$output"
#!/usr/bin/env bash
set -e

__DESCRIBE_TAG__
EOF
    for param in "${argc_params[@]}"; do
        echo "# @option --$(echo $param | sed 's/-/_/g')" >> "$output"
    done
    cat <<-'EOF' >> "$output"

main() {
    ( set -o posix ; set ) | grep ^argc_ # inspect all argc variables
}

eval "$(argc --argc-eval "$0" "$@")"
EOF
    chmod +x "$output"
}

create_js() {
    cat <<EOF > "$output"
exports.declarate = function declarate() {
  return $(build_schema)
}

exports.execute = function execute(data) {
  console.log(data)
}
EOF
}

create_py() {
    cat <<EOF > "$output"
def declarate():
  return $(build_schema) 


def execute(data):
  print(data)
EOF
}

build_schema() {
    echo '{
        "name": "'"${argc_name%%.*}"'",
        "description": "",
        "parameters": '"$(build_properties)"'
    }' | jq '.' | sed '2,$s/^/  /g'
}

build_properties() {
    required_params=()
    properties=''
    for param in "${argc_params[@]}"; do
        if [[ "$param" == *'!' ]]; then
            param="${param:0:$((${#param}-1))}"
            required_params+=("$param")
            property='{"'"$param"'":{"type":"string","description":""}}'
        elif [[ "$param" == *'+' ]]; then
            param="${param:0:$((${#param}-1))}"
            required_params+=("$param")
            property='{"'"$param"'":{"type":"array","description":"","items": {"type":"string"}}}'
        elif [[ "$param" == *'*' ]]; then
            param="${param:0:$((${#param}-1))}"
            property='{"'"$param"'":{"type":"array","description":"","items": {"type":"string"}}}'
        else
            property='{"'"$param"'":{"type":"string","description":""}}'
        fi
        properties+="$property"
    done
    required=''
    for param in "${required_params[@]}"; do
        if [[ -z "$required" ]]; then
            required=',"required":['
        fi
        required+="\"$param\","
    done
    if [[ -n "$required" ]]; then
        required="${required:0:$((${#required}-1))}"
        required+="]"
    fi
    echo '{
        "type": "object",
        "properties": '"$(echo "$properties" | jq -s 'add')$required"'
    }' | jq '.'
}

_die() {
    echo "$*"
    exit 1
}

# See more details at https://github.com/sigoden/argc
eval "$(argc --argc-eval "$0" "$@")"