#!/bin/bash . $GVM_ROOT/scripts/functions show_usage() { echo "Usage: gvm pkg install Package [options]" echo " -v, --version=VERSION Set the package version to install." echo " -s, --source=SORUCE Specify the source for this package." echo " --strict Only use installed packages." echo " -a, --all Update all dependencies." echo " -h, --help Display this message." } function read_command_line() { [[ "$1" == "" ]] && display_error "Please specify package name" # Get package name package_name=$1 source="git@github.com:moovweb/$1" shift for i in $*; do case $i in -v=*|--version=*) version=`echo $i | sed 's/[-a-zA-Z0-9]*=//'` ;; -s=*|--source=*) source=`echo $i | sed 's/[-a-zA-Z0-9]*=//'` ;; -a|--all) all_deps="true" ;; -h|--help*) show_usage exit 0 ;; --strict) strict="true" ;; *) echo "Invalid option $i" show_usage exit 65 # Bad arguments ;; esac done gvm_go_path=$GVM_ROOT/pkgsets/$gvm_go_name/$gvm_pkgset_name source_dir=$GVM_ROOT/tmp/$package_name/src build_dir=$GVM_ROOT/tmp/$package_name/build } function download_package() { rm -rf $source_dir && mkdir -p $source_dir if [[ "$source" != "." ]]; then echo "Downloading $package_name..." git clone $source $source_dir > /dev/null 2>&1 if [[ $? -ne 0 ]]; then rm -rf $source_dir display_error "Could not find $package_name in any sources" fi fi } function select_version() { if [ "$version" != "" ]; then echo "Checking out $version" cd $source_dir > /dev/null 2>&1 || display_error "Source directory doesn't exist" git checkout $version > /dev/null 2>&1 || display_error "Invalid version $version" fi } function setup_build_env() { rm -rf $build_dir && mkdir -p $build_dir select_version if [[ -f $source_dir/VERSION ]]; then SOURCE_VERSION=`cat $source_dir/VERSION` else SOURCE_VERSION="0.0" fi if [[ -n $BUILD_NUMBER ]]; then BUILD_NUMBER="$SOURCE_VERSION.$BUILD_NUMBER" else BUILD_NUMBER="$SOURCE_VERSION.dev" fi } function install_dep() { [[ "$strict" == "true" ]] && display_error "Missing dependancy ($1) and strict mode is set" [[ "$source" == "." ]] && passthrough_options="--source=. "$passthrough_options [[ "$all_deps" == "true" ]] && passthrough_options="--all "$passthrough_options gvm pkg install $1 $passthrough_options || display_error "Failed to install dependency $1" } function load_dep() { if [[ "$all_deps" == "true" ]]; then install_dep $1 fi CUR_PKG=$gvm_go_path/pkg.gvm/$1/current if [ -e "$CUR_PKG/BUILD_VERSION" ]; then echo "pkg $1 `cat $CUR_PKG/BUILD_VERSION`" >> $build_dir/manifest else install_dep $1 fi cp -rf $CUR_PKG/pkg $build_dir } function check_deps() { export GOPATH=$build_dir echo ":source $source" >> $build_dir/manifest if [ -f "$source_dir/Package.gvm" ]; then for line in `cat $source_dir/Package.gvm | grep ^pkg | awk '{ print $2 }'`; do load_dep $line done fi echo ":complete" >> $build_dir/manifest } function copy_source() { mkdir -p $gvm_go_path/pkg.gvm/$package_name/$BUILD_NUMBER cp -rf $source_dir $gvm_go_path/pkg.gvm/$package_name/$BUILD_NUMBER/ } function build_package() { echo "Installing $package_name..." echo "$BUILD_NUMBER" > $build_dir/BUILD_VERSION cd $source_dir && gvmake > $build_dir/build.log 2>&1 if [ $? -ne 0 ]; then echo "ERROR: Failed to build" cat "$build_dir/build.log" exit 1 fi } function install_package() { mkdir -p $gvm_go_path/pkg.gvm/$package_name/$BUILD_NUMBER && cp -r $build_dir/* $gvm_go_path/pkg.gvm/$package_name/$BUILD_NUMBER && rm -f $gvm_go_path/pkg.gvm/$package_name/current && ln -s $gvm_go_path/pkg.gvm/$package_name/$BUILD_NUMBER $gvm_go_path/pkg.gvm/$package_name/current && if [ $? -ne 0 ]; then echo "ERROR: Failed to install" exit 1 fi if [ -d $build_dir/bin ]; then echo " * Installing binaries" mkdir -p $gvm_go_path/bin && cp -f $build_dir/bin/* $gvm_go_path/bin/ fi } function cleanup() { rm -rf $build_dir rm -rf $source_dir } read_command_line $@ download_package setup_build_env check_deps copy_source build_package install_package cleanup