-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup
115 lines (102 loc) · 2.27 KB
/
setup
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
#! /bin/bash
# source me!
# source ~/exercism/setup
#
#
# Helpful commands:
# - init;edit => adds a vimrc/template and opens the src/test in your editor
# - submit => exercism submit "$(src)"
# - etest => language specific testing
#
shopt -s extglob
unset TMOUT
gitac () {
if [[ $PWD != */exercism ]]; then
echo "Not in the root" >&2
return 1
fi
git status -s | while IFS=' /' read _ l p; do
git add $l/$p
git commit -m "${l^}: Solve ${p^}"
done
}
lang () {
if [[ $PWD != */exercism/* ]]; then
echo "Not in exercism" >&2
return 1
fi
l="${PWD#*/exercism/}"
echo "${l%/*}"
}
exercise () {
if [[ $PWD != */exercism/*/* ]]; then
echo "Not in exercism exercise" >&2
return 1
fi
e="${PWD##*/}"
echo "${e//-/_}"
}
camel () {
local f="${1//_/ }" w x
read -r -a w <<< "$f"
for x in "${w[@]}"; do printf '%s' "${x^}"; done
}
src () {
case "$(lang)" in
python) echo "$(exercise).py" ;;
bash) echo "$(exercise).sh" ;;
go) echo "$(exercise).go" ;;
haskell) f=$(exercise); echo "src/$(camel "$f").hs" ;;
perl5) f=$(exercise); echo "$(camel "$f").pm" ;;
esac
}
tst () {
case "$(lang)" in
python) echo "$(exercise)_test.py" ;;
bash) echo "$(exercise)_test.sh" ;;
go) echo "$(exercise)_test.go" ;;
haskell) f=$(exercise); echo "test/Tests.hs" ;;
perl5) echo "$(exercise).t" ;;
esac
}
comment () {
case "$(lang)" in
python) echo "#" ;;
bash) echo "#" ;;
go) echo "//" ;;
haskell) echo "--" ;;
perl5) echo "#" ;;
esac
}
init () {
[[ -n "$(exercise)" ]] || return 1
[[ $(lang) = "go" ]] && return
ll=$(tail -n1 "$(src)" 2>/dev/null)
if [[ $ll = *vim:* ]]; then
echo "Already initialized" >&2
return 1
fi
printf '\n\n%s vim:ts=2:sw=2:expandtab\n' "$(comment)" >> "$(src)"
if [[ $(lang) == bash ]]; then
cat ../tmpl > "$(src)"
fi
}
edit () {
[[ -n "$(exercise)" ]] || return 1
vim -p "$(src)" "$(tst)"
}
etest () {
[[ -n "$(exercise)" ]] || return 1
case "$(lang)" in
python) pytest "$(tst)";;
bash) BATS_RUN_SKIPPED=true bats "$(tst)";;
go) golint && go test -v --bench . --benchmem;;
haskell) stack test;;
perl5) prove .;;
esac
}
submit () {
[[ -n "$(exercise)" ]] || return 1
exercism submit "$(src)"
}
# vim:ts=2:sw=2:expandtab