2
0
mirror of https://github.com/bpkg/bpkg synced 2024-11-16 00:12:52 +00:00
bpkg/lib/init/init.sh

345 lines
6.0 KiB
Bash
Raw Normal View History

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
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
}
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
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"
echo "\`bpkg.json' file."
2014-05-29 15:42:41 +00:00
echo
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)")"
opt VERSION "0.1.0"
2014-05-29 15:42:41 +00:00
opt DESCRIPTION ""
opt GLOBAL ""
opt INSTALL "install -b ${NAME}.sh \${PREFIX:-/usr/local}/bin/${NAME}"
2014-05-29 15:42:41 +00:00
opt SCRIPTS "${NAME}.sh"
}
set_global () {
2021-01-10 10:06:23 +00:00
# shellcheck disable=SC2034
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: "
prompt INSTALL "install: (${INSTALL})"
2014-05-29 15:42:41 +00:00
prompt SCRIPTS "scripts: (${SCRIPTS}) "
prompt USER "Github username: (${USER}) "
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
[ -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
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 () {
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
prompt_if "A 'bpkg.json' already exists. Would you like to replace it?" rm -f "${file}"
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
}
create_readme () {
if [ ! -f "README.md" ]; then
{
echo "# $NAME"
echo
echo "$DESCRIPTION"
echo
echo "# Install"
echo
echo "Available as a [bpkg](http://www.bpkg.sh/)"
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
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
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}"
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