forked from MycroftAI/mycroft-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathskiller.sh
executable file
·189 lines (146 loc) · 5.94 KB
/
skiller.sh
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
#!/usr/bin/env bash
# Copyright 2017 Mycroft AI Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
to_lower() {
echo ${@,,}
}
user_input() {
local var_name=$1
local prompt=$2
local regex=${3-^.+$}
local error_msg=${4-Invalid input.}
local format_input=${5-to_lower}
while true; do
read -p "$prompt " input
input=$($format_input $input) # To lowercase
if [[ $input =~ $regex ]]; then
break
fi
echo $error_msg
done
printf -v $var_name "$input" # $var_name=$input
}
user_input_raw() {
user_input "$1" "$2" "$3" "$4" 'echo'
}
input_lines() {
local var_name=$1
local prompt=$2
read -p "$prompt " line
lines=$line
while [ -n "${line//[\n ]}" ]; do
read -p "> " line
lines="$(printf "$lines\n$line")"
done
printf -v $var_name "$lines" # $var_name=$lines
}
skills_dir=/opt/mycroft/skills
if [ "$#" -gt "2" ] || [ "$1" = "-h" ]; then
echo "Usage: $0"
exit 0
fi
while true; do
user_input skill_desc 'Enter short description (ie. "siren alarm" or "pizza orderer"):' "^[a-z ]+$" "Please use only letter and spaces."
lang=${2-en-us}
capital_desc=$(echo $skill_desc | sed -e "s/\b./\u\0/g") # pizza orderer -> Pizza Orderer
camel_case_desc=${capital_desc// } # pizza orderer -> PizzaOrderer
class_name=${camel_case_desc}Skill # PizzaOrderer -> PizzaOrdererSkill
folder_name=skill-${skill_desc// /-} # pizza orderer -> skill-pizza-orderer
handler_name=${skill_desc// /_} # pizza orderer -> pizza_orderer
dialog_name=${skill_desc// /.} # pizza change -> pizza.orderer
skill_dir=$skills_dir/$folder_name
if [ -d "$skill_dir" ]; then
echo "The corresponding skill folder, $skill_dir already exists."
continue
fi
echo "Class name: $class_name"
echo "Folder name: $folder_name"
echo
user_input status "Looks good? (y/n)" "^[yn]$"
if [ "$status" = "y" ]; then
break
fi
done
user_input lang "Locale (default = en-us):" "^([a-z]+\-[a-z]+|)$" "Please leave empty or write locale in the format of xx-xx"
if [ -z "$lang" ]; then lang=en-us; fi
user_input_raw short_desc "Enter one line description (ie. Orders fresh pizzas from the store):"
input_lines long_desc "Enter a long description:"
input_lines examples "Enter example phrases (ie. Order a pizza.):"
user_input_raw author "Enter author:"
examples=$(echo "$examples" | sed -e 's/^/ * "/' | sed -e 's/$/"/')
mkdir -p "$skill_dir"
echo """## $capital_desc
$short_desc
## Description
$long_desc
## Examples
$examples
## Credits
$author
""" > "$skill_dir/README.md"
echo "Generated README.md"
keyword=$camel_case_desc
echo """from adapt.intent import IntentBuilder
from mycroft import MycroftSkill, intent_handler
class $class_name(MycroftSkill):
def __init__(self):
MycroftSkill.__init__(self)
@intent_handler(IntentBuilder().require('$keyword'))
def handle_${handler_name}(self, message):
self.speak_dialog('$dialog_name')
def create_skill():
return $class_name()
""" > "$skill_dir/__init__.py"
echo -e "*.pyc\nsettings.json\n" > $skill_dir/.gitignore
for i in "dialog" "vocab"; do mkdir -p "$skill_dir/$i/$lang"; done
echo "$skill_desc" > "$skill_dir/vocab/$lang/$keyword.voc"
echo "$skill_desc" > "$skill_dir/dialog/$lang/$dialog_name.dialog"
year=$(date +"%Y")
echo """Copyright $year $author
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
""" > "$skill_dir/LICENSE"
echo "Generated MIT license file. If you would like to choose a different license, please replace LICENSE."
echo
function on_ctrl_c() {
echo
echo "Removing partially created skill..."
rm -rf "$skill_dir"
exit $?
}
trap on_ctrl_c SIGINT
user_input status 'Would you like to initialize a git repo? (y/n)' "^[yn]$"
if [ "$status" = "y" ]; then
cd "$skill_dir"
git init
git add .
git commit -am "Initial commit"
user_input github_name "Please enter your GitHub username (leave empty to skip):" "^([0-9a-z\-]{2,}|)$" "Please only use letters, numbers, and hyphens."
if [ -n "$github_name" ]; then
github_url="https://github.com/$github_name/$folder_name.git"
git remote add origin "$github_url"
echo 'To add to GitHub, create the repo "'$folder_name'" and run "git push -u origin master"'
fi
cd $OLDPWD
fi
echo
echo "Finished!"
echo "Wrote to $skill_dir"
if command -v tree >/dev/null; then
cd "$skills_dir"
echo
tree --dirsfirst "$folder_name"
cd $OLDPWD
fi