2
0
mirror of https://github.com/bpkg/bpkg synced 2024-11-19 15:25:36 +00:00
bpkg/lib/install/install.sh

274 lines
5.5 KiB
Bash
Raw Normal View History

2014-05-22 19:52:55 +00:00
#!/bin/bash
2014-05-23 00:07:51 +00:00
BPKG_GIT_REMOTE="${BPKG_GIT_REMOTE:-"https://github.com"}"
2014-05-22 19:52:55 +00:00
BPKG_REMOTE="${BPKG_REMOTE:-"https://raw.githubusercontent.com"}"
BPKG_USER="${BPKG_USER:-"bpkg"}"
## outut usage
usage () {
echo "usage: bpkg-install [-h|--help]"
2014-05-22 20:34:28 +00:00
echo " or: bpkg-install [-g|--global] <package>"
echo " or: bpkg-install [-g|--global] <user>/<package>"
2014-05-22 19:52:55 +00:00
}
2014-05-24 19:29:07 +00:00
message () {
if type -f bpkg-term > /dev/null 2>&1; then
2014-05-26 14:16:06 +00:00
bpkg-term color "${1}"
2014-05-24 19:29:07 +00:00
fi
shift
printf " ${1}"
shift
if type -f bpkg-term > /dev/null 2>&1; then
2014-05-26 14:16:06 +00:00
bpkg-term reset
2014-05-24 19:29:07 +00:00
fi
printf ": "
if type -f bpkg-term > /dev/null 2>&1; then
2014-05-26 14:16:06 +00:00
bpkg-term reset
bpkg-term bright
2014-05-24 19:29:07 +00:00
fi
printf "%s\n" "${@}"
if type -f bpkg-term > /dev/null 2>&1; then
2014-05-26 14:16:06 +00:00
bpkg-term reset
2014-05-24 19:29:07 +00:00
fi
}
## output error
error () {
{
message "red" "error" "${@}"
} >&2
}
## output warning
warn () {
{
message "yellow" "warn" "${@}"
} >&2
}
## output info
info () {
local title="info"
if (( "${#}" > 1 )); then
title="${1}"
shift
fi
message "cyan" "${title}" "${@}"
}
2014-05-22 19:52:55 +00:00
## Install a bash package
bpkg_install () {
local pkg=""
2014-05-22 19:52:55 +00:00
local cwd="`pwd`"
local user=""
local name=""
local url=""
local uri=""
local version=""
local status=""
local json=""
2014-05-22 20:34:28 +00:00
local let needs_global=0
2014-05-22 19:52:55 +00:00
declare -a local parts=()
declare -a local scripts=()
declare -a args=( "${@}" )
2014-05-22 19:52:55 +00:00
for opt in "${@}"; do
if [ "-" = "${opt:0:1}" ]; then
continue
fi
pkg="${opt}"
break
done
for opt in "${@}"; do
case "${opt}" in
-h|--help)
usage
return 0
;;
-g|--global)
shift
needs_global=1
;;
*)
if [ "-" = "${opt:0:1}" ]; then
echo 2>&1 "error: Unknwon argument \`${1}'"
usage
return 1
fi
;;
esac
done
2014-05-22 19:52:55 +00:00
## ensure there is a package to install
if [ -z "${pkg}" ]; then
2014-05-23 00:07:51 +00:00
usage
2014-05-22 19:52:55 +00:00
return 1
fi
2014-05-24 19:29:07 +00:00
echo
2014-05-22 19:52:55 +00:00
## ensure remote is reachable
{
curl -s "${BPKG_REMOTE}"
if [ "0" != "$?" ]; then
2014-05-24 19:29:07 +00:00
error "Remote unreachable"
2014-05-22 19:52:55 +00:00
return 1
fi
}
## get version if available
{
OLDIFS="${IFS}"
IFS="@"
parts=(${pkg})
IFS="${OLDIFS}"
}
if [ "1" = "${#parts[@]}" ]; then
version="master"
2014-05-24 19:29:07 +00:00
info "Using latest (master)"
2014-05-22 19:52:55 +00:00
elif [ "2" = "${#parts[@]}" ]; then
name="${parts[0]}"
version="${parts[1]}"
else
2014-05-24 19:29:07 +00:00
error "Error parsing package version"
2014-05-22 19:52:55 +00:00
return 1
fi
## split by user name and repo
{
OLDIFS="${IFS}"
IFS='/'
parts=(${pkg})
IFS="${OLDIFS}"
}
if [ "1" = "${#parts[@]}" ]; then
user="${BPKG_USER}"
name="${parts[0]}"
elif [ "2" = "${#parts[@]}" ]; then
user="${parts[0]}"
name="${parts[1]}"
else
2014-05-24 19:29:07 +00:00
error "Unable to determine package name"
2014-05-22 19:52:55 +00:00
return 1
fi
2014-05-23 00:07:51 +00:00
## clean up name of weird trailing
## versions and slashes
2014-05-22 19:52:55 +00:00
name=${name/@*//}
2014-05-23 00:07:51 +00:00
name=${name////}
2014-05-22 19:52:55 +00:00
## build uri portion
uri="/${user}/${name}/${version}"
## clean up extra slashes in uri
uri=${uri/\/\///}
## build url
url="${BPKG_REMOTE}${uri}"
## determine if `package.json' exists at url
{
2014-05-25 20:32:46 +00:00
status=$(curl -sL "${url}/package.json?`date +%s`" -w '%{http_code}' -o /dev/null)
2014-05-22 19:52:55 +00:00
if [ "0" != "$?" ] || (( status >= 400 )); then
2014-05-24 19:29:07 +00:00
error "Package doesn't exist"
2014-05-22 19:52:55 +00:00
return 1
fi
}
## read package.json
2014-05-25 20:32:46 +00:00
json=$(curl -sL "${url}/package.json?`date +%s`")
2014-05-22 19:52:55 +00:00
2014-05-24 19:33:24 +00:00
## check if forced global
if [ ! -z $(echo -n $json | bpkg-json -b | grep 'global' | awk '{ print $2 }' | tr -d '"') ]; then
2014-05-24 19:33:24 +00:00
needs_global=1
fi
2014-05-22 19:52:55 +00:00
## construct scripts array
{
2014-05-25 23:22:12 +00:00
scripts=$(echo -n $json | bpkg-json -b | grep 'scripts' | awk '{$1=""; print $0 }' | tr -d '"')
2014-05-22 19:52:55 +00:00
OLDIFS="${IFS}"
2014-05-25 23:22:12 +00:00
## comma to space
2014-05-22 19:52:55 +00:00
IFS=','
2014-05-25 23:22:12 +00:00
scripts=($(echo ${scripts[@]}))
2014-05-22 19:52:55 +00:00
IFS="${OLDIFS}"
2014-05-25 23:22:12 +00:00
## account for existing space
scripts=($(echo ${scripts[@]}))
2014-05-22 19:52:55 +00:00
}
2014-05-24 19:29:07 +00:00
## build global if needed
2014-05-22 20:34:28 +00:00
if [ "1" = "${needs_global}" ]; then
## install bin if needed
2014-05-24 19:29:07 +00:00
build="$(echo -n ${json} | bpkg-json -b | grep 'install' | awk '{$1=""; print $0 }' | tr -d '\"')"
build="$(echo -n ${build} | sed -e 's/^ *//' -e 's/ *$//')"
2014-05-22 20:49:04 +00:00
if [ ! -z "${build}" ]; then
2014-05-24 19:29:07 +00:00
info "install: \`${build}'"
2014-05-23 00:07:51 +00:00
{(
2014-05-22 20:49:04 +00:00
## go to tmp dir
2014-05-22 20:52:20 +00:00
cd $( [ ! -z $TMPDIR ] && echo $TMPDIR || echo /tmp) &&
2014-05-22 20:49:04 +00:00
## prune existing
rm -rf ${name}-${version} &&
## shallow clone
2014-05-24 17:05:32 +00:00
git clone --depth=1 ${BPKG_GIT_REMOTE}/${user}/${name}.git ${name}-${version} > /dev/null 2>&1 &&
2014-05-24 19:29:07 +00:00
(
## move into directory
cd ${name}-${version} &&
## build
eval "${build}"
) &&
2014-05-22 20:49:04 +00:00
## clean up
rm -rf ${name}-${version}
2014-05-23 00:07:51 +00:00
)}
2014-05-24 19:29:07 +00:00
else
warn "Mssing build script"
2014-05-22 20:49:04 +00:00
fi
2014-05-22 20:34:28 +00:00
elif [ "${#scripts[@]}" -gt "0" ]; then
2014-05-22 20:49:04 +00:00
## get package name from `package.json'
2014-05-25 23:22:12 +00:00
name="$(
echo -n ${json} |
bpkg-json -b |
grep 'name' |
awk '{ $1=""; print $0 }' |
tr -d '\"' |
tr -d ' '
)"
2014-05-22 20:49:04 +00:00
2014-05-22 19:52:55 +00:00
## make `deps/' directory if possible
mkdir -p "${cwd}/deps/${name}"
2014-05-22 20:49:04 +00:00
2014-05-22 20:34:28 +00:00
## copy package.json over
curl -sL "${url}/package.json" -o "${cwd}/deps/${name}/package.json"
2014-05-22 20:49:04 +00:00
2014-05-22 19:52:55 +00:00
## grab each script and place in deps directory
for (( i = 0; i < ${#scripts[@]} ; ++i )); do
(
2014-05-26 04:36:49 +00:00
local script="$(echo ${scripts[$i]} | xargs basename )"
info "fetch" "${url}/${script}"
info "write" "${cwd}/deps/${name}/${script}"
2014-05-22 19:52:55 +00:00
curl -sL "${url}/${script}" -o "${cwd}/deps/${name}/${script}"
)
done
fi
return 0
}
if [[ ${BASH_SOURCE[0]} != $0 ]]; then
export -f bpkg_install
else
bpkg_install "${@}"
exit $?
fi