#!/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 sources=`cat $GVM_ROOT/config/sources` shift for i in $*; do case $i in -v=*|--version=*) version=`echo $i | sed 's/[-a-zA-Z0-9]*=//'` ;; -s=*|--source=*) sources=`echo $i | sed 's/[-a-zA-Z0-9]*=//' && echo $sources` ;; -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 cache_dir=$GVM_ROOT/archive/package/$package_name tmp_dir=$GVM_ROOT/tmp/$$ source_dir=$tmp_dir/$package_name/src/$package_name build_dir=$tmp_dir/$package_name/build } function download_package() { [ -d $cache_dir ] && return #echo "Downloading $package_name..." for source in $sources; do git clone $source/$package_name $cache_dir > /dev/null 2>&1 if [ "$?" == "0" ]; then echo $source > $cache_dir/GVM_SOURCE return fi done display_error "Could not find $package_name in any sources" } function update_package_cache() { cd $cache_dir && git pull > /dev/null 2>&1 || display_error "Failed to update source" } function copy_tmp_source() { rm -rf $source_dir if [ "$version" != "" ]; then git clone $cache_dir $source_dir > /dev/null 2>&1 cd $source_dir && git checkout $version > /dev/null 2>&1 || return 1 else git clone $cache_dir $source_dir > /dev/null 2>&1 cd $source_dir git describe --exact-match > /dev/null 2>&1 if [ "$?" == "0" ]; then git_tag=`git describe --exact-match` git_version=`echo $git_tag | awk '{ n=split($1,path,"_"); print path[n] }'` if [ "$BUILD_NUMBER" == "" ]; then BUILD_NUMBER=$git_version fi fi fi } function setup_build_env() { rm -rf $build_dir && mkdir -p $build_dir copy_tmp_source if [ "$?" != "0" ]; then update_package_cache copy_tmp_source || display_error "Invalid version" fi if [[ -f $source_dir/VERSION ]]; then SOURCE_VERSION=`cat $source_dir/VERSION` fi if [[ -n $BUILD_NUMBER ]]; then BUILD_NUMBER="$SOURCE_VERSION.$BUILD_NUMBER" else if [[ "$SOURCE_VERSION" != "" ]]; then BUILD_NUMBER="$SOURCE_VERSION.src" else BUILD_NUMBER="src" fi 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 `cat $cache_dir/GVM_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-$BUILD_NUMBER..." 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 $tmp_dir } read_command_line $@ download_package setup_build_env check_deps copy_source build_package install_package cleanup