2014-05-29 15:42:41 +00:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
## sets optional variable from environment
|
2021-01-09 20:51:01 +00:00
|
|
|
opt () { eval "if [ -z \"\${$1}\" ]; then ${1}='${2}'; fi"; }
|
2014-05-29 15:42:41 +00:00
|
|
|
|
|
|
|
## output usage
|
|
|
|
usage () {
|
|
|
|
echo ""
|
|
|
|
echo " usage: bpkg-init [-hV]"
|
|
|
|
echo ""
|
|
|
|
}
|
|
|
|
|
|
|
|
## prompt with question and store result in variable
|
|
|
|
prompt () {
|
|
|
|
local var="$1"
|
|
|
|
local q="$2"
|
|
|
|
local value=""
|
|
|
|
|
|
|
|
{
|
|
|
|
trap "exit -1" SIGINT SIGTERM
|
2014-10-10 17:39:05 +00:00
|
|
|
read -p "$q" -r -e value;
|
|
|
|
|
2014-05-29 15:42:41 +00:00
|
|
|
value="${value//\"/\'}";
|
2014-07-16 21:18:22 +00:00
|
|
|
} 2>&1
|
2021-01-09 20:53:07 +00:00
|
|
|
if [ -n "${value}" ]; then
|
2021-01-09 20:51:01 +00:00
|
|
|
eval "${var}=\"${value}\""
|
2014-05-29 15:42:41 +00:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2014-10-10 17:39:05 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2014-05-29 15:42:41 +00:00
|
|
|
## alert user of hint
|
|
|
|
hint () {
|
|
|
|
{
|
|
|
|
echo
|
|
|
|
printf " hint: %s\n" "$@"
|
|
|
|
echo
|
|
|
|
} >&2
|
|
|
|
}
|
|
|
|
|
|
|
|
## output error
|
|
|
|
error () {
|
|
|
|
{
|
|
|
|
printf "error: %s\n" "${@}"
|
|
|
|
} >&2
|
|
|
|
}
|
|
|
|
|
|
|
|
## append line to buffer
|
|
|
|
append () {
|
|
|
|
appendf '%s' "${@}"
|
|
|
|
buf+=$'\n'
|
|
|
|
}
|
|
|
|
|
|
|
|
## append formatted string to buffer
|
|
|
|
appendf () {
|
|
|
|
local fmt="$1"
|
|
|
|
shift
|
2021-01-09 20:24:34 +00:00
|
|
|
# shellcheck disable=SC2059
|
|
|
|
buf+="$(printf "${fmt}" "${@}")"
|
2014-05-29 15:42:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
## wraps each argument in quotes
|
|
|
|
wrap () {
|
|
|
|
printf '"%s" ' "${@}";
|
|
|
|
echo "";
|
|
|
|
}
|
|
|
|
|
|
|
|
intro () {
|
|
|
|
echo
|
2021-01-03 20:24:17 +00:00
|
|
|
echo "This will walk you through initializing the 'bpkg.json' file."
|
2014-05-29 15:42:41 +00:00
|
|
|
echo "It will prompt you for the bare minimum that is needed and provide"
|
|
|
|
echo "defaults."
|
|
|
|
echo
|
|
|
|
echo "See github.com/bpkg/bpkg for more information on defining the bpkg"
|
2021-01-03 20:24:17 +00:00
|
|
|
echo "\`bpkg.json' file."
|
2014-05-29 15:42:41 +00:00
|
|
|
echo
|
2021-01-03 20:24:17 +00:00
|
|
|
echo "You can press ^C anytime to quit this prompt. The 'bpkg.json' file"
|
2014-05-29 15:42:41 +00:00
|
|
|
echo "will only be written upon completion."
|
|
|
|
echo
|
|
|
|
}
|
|
|
|
|
|
|
|
options () {
|
2021-01-10 13:10:44 +00:00
|
|
|
opt NAME "$(basename "$(pwd)")"
|
2021-01-03 20:24:17 +00:00
|
|
|
opt VERSION "0.1.0"
|
2014-05-29 15:42:41 +00:00
|
|
|
opt DESCRIPTION ""
|
|
|
|
opt GLOBAL ""
|
2014-10-10 17:39:05 +00:00
|
|
|
opt INSTALL "install -b ${NAME}.sh \${PREFIX:-/usr/local}/bin/${NAME}"
|
2014-05-29 15:42:41 +00:00
|
|
|
opt SCRIPTS "${NAME}.sh"
|
|
|
|
}
|
|
|
|
|
2014-10-10 17:39:05 +00:00
|
|
|
set_global () {
|
2021-01-10 10:06:23 +00:00
|
|
|
# shellcheck disable=SC2034
|
2014-10-10 17:39:05 +00:00
|
|
|
GLOBAL=1
|
|
|
|
}
|
|
|
|
|
2014-05-29 15:42:41 +00:00
|
|
|
prompts () {
|
|
|
|
prompt NAME "name: (${NAME}) "
|
2021-01-10 13:10:44 +00:00
|
|
|
# shellcheck disable=SC2153
|
2014-05-29 15:42:41 +00:00
|
|
|
prompt VERSION "version: (${VERSION}) "
|
|
|
|
prompt DESCRIPTION "description: "
|
2014-10-10 17:39:05 +00:00
|
|
|
prompt INSTALL "install: (${INSTALL})"
|
2014-05-29 15:42:41 +00:00
|
|
|
prompt SCRIPTS "scripts: (${SCRIPTS}) "
|
2016-08-05 03:09:04 +00:00
|
|
|
prompt USER "Github username: (${USER}) "
|
2014-10-10 17:39:05 +00:00
|
|
|
prompt_if "Force global install?" set_global
|
2014-05-29 15:42:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
## handle required fields
|
|
|
|
required () {
|
|
|
|
for key in \
|
|
|
|
"NAME" \
|
|
|
|
"SCRIPTS"
|
|
|
|
do
|
|
|
|
eval local val="\${${key}}"
|
2021-01-10 13:10:44 +00:00
|
|
|
# shellcheck disable=SC2154
|
2017-02-06 15:28:58 +00:00
|
|
|
[ -z "${val}" ] && error "Missing \`
|
|
|
|
${key}' property"
|
2014-05-29 15:42:41 +00:00
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
## convert scripts to quoted csv
|
|
|
|
csv () {
|
2021-01-09 20:53:07 +00:00
|
|
|
if [ -n "${SCRIPTS}" ]; then
|
2014-10-10 17:39:05 +00:00
|
|
|
RAW_SCRIPTS=${SCRIPTS}
|
2014-05-29 15:42:41 +00:00
|
|
|
{
|
|
|
|
local TMP=""
|
|
|
|
SCRIPTS="${SCRIPTS//,/ }"
|
|
|
|
SCRIPTS="${SCRIPTS//\"/}"
|
|
|
|
SCRIPTS="${SCRIPTS//\'/}"
|
2021-01-09 20:51:01 +00:00
|
|
|
# shellcheck disable=SC2086
|
2014-05-29 15:42:41 +00:00
|
|
|
SCRIPTS=($(wrap ${SCRIPTS}))
|
2021-01-09 20:55:23 +00:00
|
|
|
(( len=${#SCRIPTS[@]} ))
|
2014-05-29 15:42:41 +00:00
|
|
|
for (( i = 0; i < len; i++ )); do
|
|
|
|
word=${SCRIPTS[$i]}
|
|
|
|
if (( i + 1 != len )); then
|
|
|
|
TMP+="${word}, "
|
|
|
|
else
|
|
|
|
TMP+="${word}"
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
SCRIPTS="${TMP}"
|
|
|
|
}
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
## delimit object and key-value pairs
|
|
|
|
delimit () {
|
2021-01-10 09:47:12 +00:00
|
|
|
local lowercase
|
|
|
|
|
2014-05-29 15:42:41 +00:00
|
|
|
append "{"
|
|
|
|
|
|
|
|
for key in \
|
|
|
|
"NAME" \
|
|
|
|
"VERSION" \
|
|
|
|
"DESCRIPTION" \
|
|
|
|
"GLOBAL" \
|
|
|
|
"INSTALL" \
|
|
|
|
"SCRIPTS"
|
|
|
|
do
|
2021-01-10 09:47:12 +00:00
|
|
|
lowercase="$(echo ${key} | tr '[:upper:]' '[:lower:]')"
|
2014-05-29 15:42:41 +00:00
|
|
|
|
|
|
|
eval local val="\${${key}}"
|
2021-01-09 20:53:07 +00:00
|
|
|
if [ -n "${val}" ]; then
|
2014-05-29 15:42:41 +00:00
|
|
|
|
|
|
|
## swap leading/trailing quotes for brackets in arrays
|
|
|
|
local before="\""
|
|
|
|
local after="\""
|
|
|
|
[ "$key" == "SCRIPTS" ] && before="[ " && after=" ]"
|
|
|
|
|
|
|
|
appendf " \"${lowercase}\": ${before}%s${after}" "${val}"
|
|
|
|
append ","
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
## remove final trailing newline and comma
|
|
|
|
buf="${buf%?}"
|
|
|
|
buf="${buf%?}"
|
|
|
|
|
|
|
|
append ""
|
|
|
|
append "}"
|
|
|
|
}
|
|
|
|
|
|
|
|
## validate completed contents with user
|
|
|
|
validate () {
|
2014-10-10 17:39:05 +00:00
|
|
|
prompt ANSWER "${buf} Does this look OK? (type 'n' to cancel) "
|
2014-05-29 15:42:41 +00:00
|
|
|
if [ "n" = "${ANSWER:0:1}" ]; then
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
## if package file already exists, ensure user wants to clobber
|
|
|
|
clobber () {
|
|
|
|
if test -f "${file}"; then
|
2021-01-03 20:24:17 +00:00
|
|
|
prompt_if "A 'bpkg.json' already exists. Would you like to replace it?" rm -f "${file}"
|
2014-10-10 17:39:05 +00:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
create_shell_file () {
|
|
|
|
if [ "${NAME}.sh" == "${RAW_SCRIPTS}" ] && [ ! -f "${NAME}.sh" ]; then
|
2021-01-10 13:10:44 +00:00
|
|
|
cat << EOF > "${NAME}.sh"
|
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
VERSION=${VERSION}
|
|
|
|
|
|
|
|
usage () {
|
|
|
|
echo "${NAME} [-hV]"
|
|
|
|
echo
|
|
|
|
echo "Options:"
|
|
|
|
echo " -h|--help Print this help dialogue and exit"
|
|
|
|
echo " -V|--version Print the current version and exit"
|
|
|
|
}
|
|
|
|
|
|
|
|
${NAME} () {
|
|
|
|
for opt in "\${@}"; do
|
|
|
|
case "\${opt}" in
|
|
|
|
-h|--help)
|
|
|
|
usage
|
|
|
|
return 0
|
|
|
|
;;
|
|
|
|
-V|--version)
|
|
|
|
echo "\${VERSION}"
|
|
|
|
return 0
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
|
|
|
## your code here
|
|
|
|
}
|
|
|
|
|
|
|
|
if [[ \${BASH_SOURCE[0]} != "\$0" ]]; then
|
|
|
|
export -f ${NAME}
|
|
|
|
else
|
|
|
|
${NAME} "\${@}"
|
|
|
|
exit $?
|
|
|
|
fi
|
|
|
|
|
|
|
|
EOF
|
2015-12-22 13:30:23 +00:00
|
|
|
chmod 755 "${NAME}.sh"
|
2014-05-29 15:42:41 +00:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2014-10-10 17:39:05 +00:00
|
|
|
create_readme () {
|
|
|
|
if [ ! -f "README.md" ]; then
|
|
|
|
{
|
|
|
|
echo "# $NAME"
|
|
|
|
echo
|
|
|
|
echo "$DESCRIPTION"
|
|
|
|
echo
|
|
|
|
echo "# Install"
|
|
|
|
echo
|
2017-07-26 15:01:50 +00:00
|
|
|
echo "Available as a [bpkg](http://www.bpkg.sh/)"
|
2014-10-10 17:39:05 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-29 15:42:41 +00:00
|
|
|
## main
|
|
|
|
bpkg_init () {
|
2021-01-10 09:47:12 +00:00
|
|
|
local cwd
|
|
|
|
|
2021-01-03 20:24:17 +00:00
|
|
|
local version="0.1.0"
|
2021-01-10 09:47:12 +00:00
|
|
|
cwd="$(pwd)"
|
2014-05-29 15:42:41 +00:00
|
|
|
local buf="" ## output buffer
|
2021-01-03 20:24:17 +00:00
|
|
|
local file="${cwd}/bpkg.json" ## output file
|
2014-05-29 15:42:41 +00:00
|
|
|
local arg="$1"
|
|
|
|
shift
|
|
|
|
|
|
|
|
case "${arg}" in
|
|
|
|
|
|
|
|
## flags
|
|
|
|
-V|--version)
|
|
|
|
echo "${version}"
|
|
|
|
exit 0
|
|
|
|
;;
|
|
|
|
|
|
|
|
-h|--help)
|
|
|
|
usage
|
|
|
|
exit 0
|
|
|
|
;;
|
|
|
|
|
|
|
|
*)
|
2021-01-09 20:53:07 +00:00
|
|
|
if [ -n "${arg}" ]; then
|
2014-05-29 15:42:41 +00:00
|
|
|
error "Unknown option: \`${arg}'"
|
|
|
|
usage
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
|
|
|
## set up package file
|
|
|
|
intro
|
|
|
|
options
|
|
|
|
prompts
|
|
|
|
required
|
|
|
|
csv
|
|
|
|
delimit
|
|
|
|
validate
|
|
|
|
clobber
|
|
|
|
|
|
|
|
## create and write package file
|
|
|
|
touch "${file}"
|
|
|
|
echo "${buf}" > "${file}"
|
2014-10-10 17:39:05 +00:00
|
|
|
|
|
|
|
create_shell_file
|
|
|
|
create_readme
|
|
|
|
|
|
|
|
# initialize a git repo if one does not exist
|
|
|
|
if [ ! -d '.git' ]; then
|
|
|
|
git init
|
|
|
|
fi
|
|
|
|
|
2014-05-29 15:42:41 +00:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
## export or run
|
2021-01-09 19:15:19 +00:00
|
|
|
if [[ ${BASH_SOURCE[0]} != "$0" ]]; then
|
2014-05-29 15:42:41 +00:00
|
|
|
export -f bpkg-init
|
|
|
|
else
|
|
|
|
bpkg_init "${@}"
|
|
|
|
exit $?
|
|
|
|
fi
|
|
|
|
|