aboutsummaryrefslogtreecommitdiff
path: root/run.sh
blob: 13ee95306dc3dfae00bca5d26259a2840244a336 (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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#!/bin/bash

cd "$(dirname "$0")" || exit 1

declare -A tools

for file in .run/src/bin/*.cs; do
    if [ -f "$file" ]; then
        name=$(basename "$file" .cs)
        tools["$name"]="cs"
    fi
done

for file in .run/src/bin/*.rs; do
    if [ -f "$file" ]; then
        name=$(basename "$file" .rs)
        tools["$name"]="rs"
    fi
done

for file in .run/src/bin/*.py; do
    if [ -f "$file" ]; then
        name=$(basename "$file" .py)
        tools["$name"]="py"
    fi
done

for file in .run/src/bin/*.sh; do
    if [ -f "$file" ]; then
        name=$(basename "$file" .sh)
        tools["$name"]="sh"
    fi
done

for file in .run/src/bin/*.rb; do
    if [ -f "$file" ]; then
        name=$(basename "$file" .rb)
        tools["$name"]="rb"
    fi
done

for file in .run/src/bin/*.pl; do
    if [ -f "$file" ]; then
        name=$(basename "$file" .pl)
        tools["$name"]="pl"
    fi
done

for file in .run/src/bin/*.go; do
    if [ -f "$file" ]; then
        name=$(basename "$file" .go)
        tools["$name"]="go"
    fi
done

for file in .run/src/bin/*.zig; do
    if [ -f "$file" ]; then
        name=$(basename "$file" .zig)
        tools["$name"]="zig"
    fi
done

for file in .run/src/bin/*.nim; do
    if [ -f "$file" ]; then
        name=$(basename "$file" .nim)
        tools["$name"]="nim"
    fi
done

for file in .run/src/bin/*; do
    if [ -f "$file" ]; then
        name=$(basename "$file")
        if [[ ! "$name" == *.* ]]; then
            tools["$name"]="binary"
        fi
    fi
done

if [ $# -eq 0 ]; then
    sorted_names=($(echo "${!tools[@]}" | tr ' ' '\n' | sort))
    total=${#sorted_names[@]}
    num_w=${#total}

    max_name=0
    for name in "${sorted_names[@]}"; do
        len=${#name}
        ((len > max_name)) && max_name=$len
    done

    inner_w=$((2 + num_w + 1 + 1 + max_name + 2 + 6 + 1 + 2))
    ((inner_w < 38)) && inner_w=38

    title="./run.sh <NUMBER/NAME> [ARGS...]"
    title_len=${#title}
    dash_total=$((inner_w - 2 - title_len))
    dash_left=$((dash_total / 2))
    dash_right=$((dash_total - dash_left))

    echo "┌$(printf '─%.0s' $(seq 1 $dash_left)) $title $(printf '─%.0s' $(seq 1 $dash_right))┐"
    printf "│%*s│\n" $inner_w ""

    i=1
    for name in "${sorted_names[@]}"; do
        type="${tools[$name]}"
        case "$type" in
            cs) lang="C#";;
            py) lang="Python";;
            rs) lang="Rust";;
            sh) lang="Shell";;
            rb) lang="Ruby";;
            pl) lang="Perl";;
            go) lang="Go";;
            zig) lang="Zig";;
            nim) lang="Nim";;
            binary) lang="Binary";;
        esac
        entry=$(printf "  %-*d) %-*s [%s]" $num_w $i $max_name $name $lang)
        printf "│%-*s│\n" $inner_w "$entry"
        i=$((i + 1))
    done

    printf "│%*s│\n" $inner_w ""
    echo "└$(printf '─%.0s' $(seq 1 $inner_w))┘"
    exit 1
fi

target_name="$1"
shift

if [[ "$target_name" =~ ^[0-9]+$ ]]; then
    sorted=($(echo "${!tools[@]}" | tr ' ' '\n' | sort))
    idx=$((target_name - 1))
    if [ "$idx" -ge 0 ] && [ "$idx" -lt "${#sorted[@]}" ]; then
        target_name="${sorted[$idx]}"
    else
        echo "Error: invalid number '$target_name', valid range is 1-${#sorted[@]}"
        exit 1
    fi
fi

if [ -z "${tools[$target_name]}" ]; then
    echo "Error: target '$target_name' does not exist"
    exit 1
fi

type="${tools[$target_name]}"

case "$type" in
    sh)
        chmod +x ".run/src/bin/$target_name.sh"
        ".run/src/bin/$target_name.sh" "$@"
        ;;
    py)
        python ".run/src/bin/$target_name.py" "$@"
        ;;
    rs)
        if [ ! -f ".run/Cargo.toml" ]; then
            cat > ".run/Cargo.toml" <<'EOF'
[package]
name = "run_rust"
version = "0.1.0"
edition = "2024"

[workspace]

[dependencies]
EOF
        fi
        cargo build --manifest-path ".run/Cargo.toml" --target-dir ".run/target" --bin "$target_name" --quiet
        ".run/target/debug/$target_name" "$@"
        ;;
    cs)
        temp_dir=".run/target/csproj/$target_name"
        mkdir -p "$temp_dir"
        cat > "$temp_dir/Directory.Build.props" <<'PROPS'
<Project>
  <PropertyGroup>
    <BaseOutputPath>$(MSBuildThisFileDirectory)../../csharp/bin</BaseOutputPath>
    <BaseIntermediateOutputPath>$(MSBuildThisFileDirectory)../../csharp/obj</BaseIntermediateOutputPath>
  </PropertyGroup>
</Project>
PROPS
        cat > "$temp_dir/$target_name.csproj" <<'CSPROJ'
<Project Sdk="Microsoft.NET.Sdk">

    <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>net8.0</TargetFramework>
        <ImplicitUsings>enable</ImplicitUsings>
        <Nullable>enable</Nullable>
    </PropertyGroup>

</Project>
CSPROJ
        cp ".run/src/bin/$target_name.cs" "$temp_dir/Program.cs"
        dotnet run --project "$temp_dir/$target_name.csproj" -- "$@"
        ;;
    rb)
        ruby ".run/src/bin/$target_name.rb" "$@"
        ;;
    pl)
        perl ".run/src/bin/$target_name.pl" "$@"
        ;;
    go)
        go run ".run/src/bin/$target_name.go" "$@"
        ;;
    zig)
        zig run ".run/src/bin/$target_name.zig" "$@"
        ;;
    nim)
        nim r --hints:off ".run/src/bin/$target_name.nim" "$@"
        ;;
    binary)
        chmod +x ".run/src/bin/$target_name"
        ".run/src/bin/$target_name" "$@"
        ;;
esac