-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmix
99 lines (96 loc) · 3 KB
/
mix
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
#!/bin/bash
RED=$(tput setaf 1)
GREEN=$(tput setaf 2)
YELLOW=$(tput setaf 3)
NC='\033[0m' # No Color
# Usage:
# m → mix help
# m a → mix archive
# m h → mix hex
# m a.b → mix archive.build
# m d.co → mix deps.compile
# m p.n → mix phoenix.new
function m {
if [ "$1" == "refresh" ]; then
mix help | grep ^mix | awk '{ print $2 }' | sed "s/#//" | grep -v help | grep . | awk '{ print length, $0 }' | sort -n -s | cut -d" " -f2- > ~/.mix_tasks
elif [ ! -f ~/.mix_tasks ]; then
m refresh
m $@
printf "\nCreated ${YELLOW}~/.mix_tasks${NC}...\n\n"
printf "Use shortcuts:\n"
printf " ${YELLOW}m a${NC} → ${GREEN}mix archive${NC}\n"
printf " ${YELLOW}m d.co${NC} → ${GREEN}mix deps.compile${NC}\n"
elif [ -z "$1" ] || [ "$1" == "help" ]; then
mix help | sed "s/^/\\${GREEN}/" | sed "s/#/\\${NC}/" | xargs -0 printf
else
local commands=`cat ~/.mix_tasks`
if [[ $1 =~ \..+\. ]]; then
local shortcut=(${1//./ })
while read -r line; do
if [[ $line =~ \..+\. ]] ; then
local cmd=(${line//./ })
if [[ ${cmd[0]} == ${shortcut[0]}* ]] && [[ ${cmd[1]} == ${shortcut[1]}* ]] && [[ ${cmd[2]} == ${shortcut[2]}* ]]; then
shift
printf "${YELLOW}mix $line $@${NC}\n"
mix $line $@
return
fi
fi
done <<< "$commands"
elif [[ $1 =~ \. ]]; then
local shortcut=(${1//./ })
while read -r line; do
if [[ $line =~ \. ]] ; then
local cmd=(${line//./ })
if [[ ${cmd[0]} == ${shortcut[0]}* ]] && [[ ${cmd[1]} == ${shortcut[1]}* ]]; then
shift
printf "${YELLOW}mix $line $@${NC}\n"
mix $line $@
return
fi
fi
done <<< "$commands"
else
while read -r line; do
if [[ $line == $1* ]]; then
shift
printf "${YELLOW}mix $line $@${NC}\n"
mix $line $@
return
fi
done <<< "$commands"
fi
local new_commands=`mix help | grep ^mix | awk '{ print $2 }' | sed "s/#//" | grep -v help | grep .`
local added=false
while read -r line; do
if ! printf '%s\n\0' "${commands[@]}" | grep -Fqx $line
then
if ! $added ; then
printf "Adding to ${YELLOW}~/.mix_tasks${NC} for shortcut resolution:\n"
fi
added=true
printf "Added ${GREEN}$line${NC}\n"
echo $line >> ~/.mix_tasks
fi
done <<< "$new_commands"
if [ $added == true ]; then
cat ~/.mix_tasks | awk '{ print length, $0 }' | sort -n -s | cut -d" " -f2- > ~/._mix_tasks
mv ~/._mix_tasks ~/.mix_tasks
m $@
else
printf "${RED}Task not found${NC}\n"
fi
fi
}
_mix()
{
local cur prev opts
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
opts=`mix help | grep ^mix | awk '{ print $2 }' | sed "s/#//" | grep -v help | grep .`
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
return 0
}
complete -F _mix m
complete -F _mix mix