Improvements to bpkg init

- Generate README.md file with description and install directions pre-filled
- Generate $NAME.sh file with boilerplate, including source-check, usage,
-   argument processing, and version print.
- Nice default for package.json "install" field
- Initialize git repo if one does not exist
- Fix issue with prompt where delete key erased prompt, by using `read -p`
pull/27/head
Chris Carpita 10 years ago
parent cc6d9dd6fe
commit 5ad5fc9318

@ -1,7 +1,7 @@
#!/bin/bash #!/bin/bash
## sets optional variable from environment ## sets optional variable from environment
opt () { eval "if [ -z "\${$1}" ]; then ${1}=${2}; fi"; } opt () { eval "if [ -z "\${$1}" ]; then ${1}='${2}'; fi"; }
## output usage ## output usage
usage () { usage () {
@ -15,11 +15,11 @@ prompt () {
local var="$1" local var="$1"
local q="$2" local q="$2"
local value="" local value=""
printf "%s" "${q}"
{ {
trap "exit -1" SIGINT SIGTERM trap "exit -1" SIGINT SIGTERM
read -re value; read -p "$q" -r -e value;
value="${value//\"/\'}"; value="${value//\"/\'}";
} 2>&1 } 2>&1
if [ ! -z "${value}" ]; then if [ ! -z "${value}" ]; then
@ -27,6 +27,20 @@ prompt () {
fi fi
} }
prompt_if () {
local mesg="$1"
local func="$2"
prompt ANSWER "$mesg [y/n]: "
case "$ANSWER" in
y|Y|yes|YES|Yes)
shift
shift
$func $@
return 0
esac
return 1
}
## alert user of hint ## alert user of hint
hint () { hint () {
{ {
@ -64,7 +78,7 @@ wrap () {
intro () { intro () {
echo echo
echo "This will walk you through initialzing the bpkg \`package.json' file." echo "This will walk you through initializing the bpkg \`package.json' file."
echo "It will prompt you for the bare minimum that is needed and provide" echo "It will prompt you for the bare minimum that is needed and provide"
echo "defaults." echo "defaults."
echo echo
@ -81,17 +95,22 @@ options () {
opt VERSION "0.0.1" opt VERSION "0.0.1"
opt DESCRIPTION "" opt DESCRIPTION ""
opt GLOBAL "" opt GLOBAL ""
opt INSTALL "" opt INSTALL "install -b ${NAME}.sh \${PREFIX:-/usr/local}/bin/${NAME}"
opt SCRIPTS "${NAME}.sh" opt SCRIPTS "${NAME}.sh"
} }
set_global () {
GLOBAL=1
}
prompts () { prompts () {
prompt NAME "name: (${NAME}) " prompt NAME "name: (${NAME}) "
prompt VERSION "version: (${VERSION}) " prompt VERSION "version: (${VERSION}) "
prompt DESCRIPTION "description: " prompt DESCRIPTION "description: "
prompt GLOBAL "global: " prompt INSTALL "install: (${INSTALL})"
prompt INSTALL "install: "
prompt SCRIPTS "scripts: (${SCRIPTS}) " prompt SCRIPTS "scripts: (${SCRIPTS}) "
prompt USER "username: (${USER}) "
prompt_if "Force global install?" set_global
} }
## handle required fields ## handle required fields
@ -108,6 +127,7 @@ required () {
## convert scripts to quoted csv ## convert scripts to quoted csv
csv () { csv () {
if [ ! -z "${SCRIPTS}" ]; then if [ ! -z "${SCRIPTS}" ]; then
RAW_SCRIPTS=${SCRIPTS}
{ {
local TMP="" local TMP=""
SCRIPTS="${SCRIPTS//,/ }" SCRIPTS="${SCRIPTS//,/ }"
@ -165,7 +185,7 @@ delimit () {
## validate completed contents with user ## validate completed contents with user
validate () { validate () {
prompt ANSWER "${buf}(yes) ? " prompt ANSWER "${buf} Does this look OK? (type 'n' to cancel) "
if [ "n" = "${ANSWER:0:1}" ]; then if [ "n" = "${ANSWER:0:1}" ]; then
exit 1 exit 1
fi fi
@ -174,15 +194,79 @@ validate () {
## if package file already exists, ensure user wants to clobber ## if package file already exists, ensure user wants to clobber
clobber () { clobber () {
if test -f "${file}"; then if test -f "${file}"; then
prompt ANSWER "A \`package.json' already exists. Would you like to replace it? (yes): " prompt_if "A \`package.json' already exists. Would you like to replace it?" rm -f "${file}"
if [ "n" = "${ANSWER:0:1}" ]; then fi
exit 1 }
else
rm -f "${file}" create_shell_file () {
fi if [ "${NAME}.sh" == "${RAW_SCRIPTS}" ] && [ ! -f "${NAME}.sh" ]; then
{
echo "#!/bin/bash"
echo
echo "VERSION=$VERSION"
echo
echo "usage () {"
echo " echo \"$NAME [-hV]\""
echo " echo"
echo " echo \"Options:\""
echo " echo \" -h|--help Print this help dialogue and exit\""
echo " echo \" -V|--version Print the current version and exit\""
echo '}'
echo
echo "${NAME} () {"
echo " for opt in \"\${@}\"; do"
echo " case \"\$opt\" in"
echo " -h|--help)"
echo " usage"
echo " return 0"
echo " ;;"
echo " -V|--version)"
echo " echo \"\$VERSION\""
echo " return 0"
echo " ;;"
echo " esac"
echo " done"
echo
echo " ## your code here"
echo "}"
echo
echo 'if [[ ${BASH_SOURCE[0]} != $0 ]]; then'
echo " export -f $NAME"
echo 'else'
echo " $NAME "'"${@}"'
echo " exit $?"
echo 'fi'
} > "${NAME}.sh"
chmod 755 "{$NAME}.sh"
fi fi
} }
create_readme () {
if [ ! -f "README.md" ]; then
{
echo "# $NAME"
echo
echo "$DESCRIPTION"
echo
echo "# Install"
echo
echo "Available as a [bpkg](http://www.bpkg.io/)"
echo '```sh'
echo "bpkg install [-g] ${USER:-bpkg}/$NAME"
echo '```'
} > "README.md"
fi
}
create_repo () {
if git status &>/dev/null; then
echo "Repo already exists"
else
git init
fi
}
## main ## main
bpkg_init () { bpkg_init () {
local version="0.0.1" local version="0.0.1"
@ -227,6 +311,15 @@ bpkg_init () {
## create and write package file ## create and write package file
touch "${file}" touch "${file}"
echo "${buf}" > "${file}" echo "${buf}" > "${file}"
create_shell_file
create_readme
# initialize a git repo if one does not exist
if [ ! -d '.git' ]; then
git init
fi
return 0 return 0
} }

Loading…
Cancel
Save