github.com/shvar/terraform@v0.6.9-0.20151215234924-3365cd2231df/scripts/build.sh (about)

     1  #!/usr/bin/env bash
     2  #
     3  # This script builds the application from source for multiple platforms.
     4  
     5  # Get the parent directory of where this script is.
     6  SOURCE="${BASH_SOURCE[0]}"
     7  while [ -h "$SOURCE" ] ; do SOURCE="$(readlink "$SOURCE")"; done
     8  DIR="$( cd -P "$( dirname "$SOURCE" )/.." && pwd )"
     9  
    10  # Change into that directory
    11  cd "$DIR"
    12  
    13  # Get the git commit
    14  GIT_COMMIT=$(git rev-parse HEAD)
    15  GIT_DIRTY=$(test -n "`git status --porcelain`" && echo "+CHANGES" || true)
    16  
    17  # Determine the arch/os combos we're building for
    18  XC_ARCH=${XC_ARCH:-"386 amd64 arm"}
    19  XC_OS=${XC_OS:-linux darwin windows freebsd openbsd}
    20  
    21  # Get dependencies unless running in quick mode
    22  if [ "${TF_QUICKDEV}x" == "x" ]; then
    23      echo "==> Getting dependencies..."
    24      go get -d ./...
    25  fi
    26  
    27  # Delete the old dir
    28  echo "==> Removing old directory..."
    29  rm -f bin/*
    30  rm -rf pkg/*
    31  mkdir -p bin/
    32  
    33  # If its dev mode, only build for ourself
    34  if [ "${TF_DEV}x" != "x" ]; then
    35      XC_OS=$(go env GOOS)
    36      XC_ARCH=$(go env GOARCH)
    37  fi
    38  
    39  # Build!
    40  echo "==> Building..."
    41  gox \
    42      -os="${XC_OS}" \
    43      -arch="${XC_ARCH}" \
    44      -ldflags "-X main.GitCommit ${GIT_COMMIT}${GIT_DIRTY}" \
    45      -output "pkg/{{.OS}}_{{.Arch}}/terraform-{{.Dir}}" \
    46      ./...
    47  
    48  # Make sure "terraform-terraform" is renamed properly
    49  for PLATFORM in $(find ./pkg -mindepth 1 -maxdepth 1 -type d); do
    50      set +e
    51      mv ${PLATFORM}/terraform-terraform.exe ${PLATFORM}/terraform.exe 2>/dev/null
    52      mv ${PLATFORM}/terraform-terraform ${PLATFORM}/terraform 2>/dev/null
    53      set -e
    54  done
    55  
    56  # Move all the compiled things to the $GOPATH/bin
    57  GOPATH=${GOPATH:-$(go env GOPATH)}
    58  case $(uname) in
    59      CYGWIN*)
    60          GOPATH="$(cygpath $GOPATH)"
    61          ;;
    62  esac
    63  OLDIFS=$IFS
    64  IFS=: MAIN_GOPATH=($GOPATH)
    65  IFS=$OLDIFS
    66  
    67  # Create GOPATH/bin if it's doesn't exists
    68  if [ ! -d $MAIN_GOPATH/bin ]; then
    69      echo "==> Creating GOPATH/bin directory..."
    70      mkdir -p $MAIN_GOPATH/bin
    71  fi
    72  
    73  # Copy our OS/Arch to the bin/ directory
    74  DEV_PLATFORM="./pkg/$(go env GOOS)_$(go env GOARCH)"
    75  for F in $(find ${DEV_PLATFORM} -mindepth 1 -maxdepth 1 -type f); do
    76      cp ${F} bin/
    77      cp ${F} ${MAIN_GOPATH}/bin/
    78  done
    79  
    80  if [ "${TF_DEV}x" = "x" ]; then
    81      # Zip and copy to the dist dir
    82      echo "==> Packaging..."
    83      for PLATFORM in $(find ./pkg -mindepth 1 -maxdepth 1 -type d); do
    84          OSARCH=$(basename ${PLATFORM})
    85          echo "--> ${OSARCH}"
    86  
    87          pushd $PLATFORM >/dev/null 2>&1
    88          zip ../${OSARCH}.zip ./*
    89          popd >/dev/null 2>&1
    90      done
    91  fi
    92  
    93  # Done!
    94  echo
    95  echo "==> Results:"
    96  ls -hl bin/