Merge remote-tracking branch 'midgard/master' into master
This commit is contained in:
commit
bd0889fa1c
1 changed files with 46 additions and 42 deletions
|
@ -1,66 +1,70 @@
|
|||
#!/bin/bash
|
||||
#!/bin/sh
|
||||
|
||||
# "Bash strict mode", see http://redsymbol.net/articles/unofficial-bash-strict-mode/
|
||||
set -euo pipefail
|
||||
IFS=$'\n\t'
|
||||
print() { printf '%s' "$1"; }
|
||||
println() { printf '%s\n' "$1"; }
|
||||
|
||||
# Go to the current working directory so things work if people are in a different one and e.g. use ../src/new_module.sh
|
||||
cd "$(dirname "$0")"
|
||||
cd -- "`dirname "$0"`"
|
||||
|
||||
# Make sure the template dir exists so we don't let people enter details unnecessarily
|
||||
if [ ! -d ./template_module ]; then
|
||||
echo "template_module doesn't exist" >&2
|
||||
println "template_module doesn't exist" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Ask for module name
|
||||
read -p "Name of module (e.g. Oil gauge): " module_name
|
||||
if [[ $module_name == *%* ]]; then
|
||||
echo "Module name must not contain %" >&2
|
||||
exit 1
|
||||
fi
|
||||
print "Name of module (e.g. Oil gauge): "
|
||||
read module_name
|
||||
|
||||
# Determine a "clean" module name: lowercase, no spaces
|
||||
module="$(tr [A-Z] [a-z] <<< "$module_name")"
|
||||
module="${module// /_}"
|
||||
module="${module//\'/}"
|
||||
# Determine a "clean" module name for paths: lowercase, no spaces
|
||||
module="`print "$module_name" | tr [A-Z] [a-z] | sed "s/ /_/g;s/'//g"`"
|
||||
|
||||
# Make sure `modules` directory exists and target directory doesn't
|
||||
mkdir -p modules
|
||||
module_dir="modules/$module"
|
||||
if [[ -e "$module_dir" ]]; then
|
||||
echo "$module_dir already exists" >&2
|
||||
if [ -e "$module_dir" ]; then
|
||||
println "$module_dir already exists" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Ask for author name
|
||||
read -p "How would you like to be credited? Your name: " author
|
||||
if [[ $author == *%* ]]; then
|
||||
echo "Author name must not contain %" >&2
|
||||
exit 1
|
||||
fi
|
||||
print "How would you like to be credited? Your name: "
|
||||
read author
|
||||
|
||||
# Copy the template directory
|
||||
cp -r template_module "$module_dir"
|
||||
cd "$module_dir"
|
||||
cp -r -- template_module "$module_dir"
|
||||
cd -- "$module_dir"
|
||||
|
||||
# Disallow % in fields that will be used in %-delimited ed substitution
|
||||
assert_no_percent() {
|
||||
case "$1" in
|
||||
*"%"*) println "$2 must not contain %" >&2; exit 1 ;;
|
||||
esac
|
||||
}
|
||||
assert_no_percent "$author" "Author name"
|
||||
assert_no_percent "$module_name" "Module name"
|
||||
assert_no_percent "$module" "Module path name"
|
||||
|
||||
# Fill in the blanks in the template
|
||||
if [ "$(uname)" == "Darwin" ]; then
|
||||
sed -i '' -e "
|
||||
s/{YEAR}/$(date +%Y)/
|
||||
s%{AUTHOR}%$author%
|
||||
s%{MODULE_NAME}%$module_name%
|
||||
s%{MODULE}%$module%
|
||||
" $(find . -type f)
|
||||
else
|
||||
sed -i "
|
||||
s/{YEAR}/$(date +%Y)/
|
||||
s%{AUTHOR}%$author%
|
||||
s%{MODULE_NAME}%$module_name%
|
||||
s%{MODULE}%$module%
|
||||
" $(find -type f)
|
||||
fi
|
||||
# Arduino IDE requires .ino sketches to have the same name as their directory
|
||||
mv main.ino "$module.ino"
|
||||
# `sed -i` is not portable so we create something like it ourselves
|
||||
reced() {
|
||||
for file in "$1"/*; do
|
||||
if [ -f "$file" ]; then
|
||||
ed "$file" <<HERE
|
||||
%s/{YEAR}/$(date +%Y)/
|
||||
%s%{AUTHOR}%$author%
|
||||
%s%{MODULE_NAME}%$module_name%
|
||||
%s%{MODULE}%$module%
|
||||
wq
|
||||
HERE
|
||||
elif [ -d "$file" ]; then
|
||||
reced "$file"
|
||||
fi
|
||||
done
|
||||
}
|
||||
reced .
|
||||
|
||||
echo "The basic structure for your module is now ready in $module_dir"
|
||||
# Arduino IDE requires .ino sketches to have the same name as their directory
|
||||
mv -- main.ino "$module.ino"
|
||||
|
||||
println "The basic structure for your module is now ready in $module_dir"
|
||||
|
|
Loading…
Reference in a new issue