github.com/mikesimons/terraform@v0.6.13-0.20160304043642-f11448c69214/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 solaris}
    20  
    21  # Delete the old dir
    22  echo "==> Removing old directory..."
    23  rm -f bin/*
    24  rm -rf pkg/*
    25  mkdir -p bin/
    26  
    27  # If its dev mode, only build for ourself
    28  if [ "${TF_DEV}x" != "x" ]; then
    29      XC_OS=$(go env GOOS)
    30      XC_ARCH=$(go env GOARCH)
    31  fi
    32  
    33  if ! which gox > /dev/null; then
    34      echo "==> Installing gox..."
    35      go get -u github.com/mitchellh/gox
    36  fi
    37  
    38  # Build!
    39  echo "==> Building..."
    40  gox \
    41      -os="${XC_OS}" \
    42      -arch="${XC_ARCH}" \
    43      -ldflags "-X main.GitCommit=${GIT_COMMIT}${GIT_DIRTY}" \
    44      -output "pkg/{{.OS}}_{{.Arch}}/terraform-{{.Dir}}" \
    45      $(go list ./... | grep -v /vendor/)
    46  
    47  # Make sure "terraform-terraform" is renamed properly
    48  for PLATFORM in $(find ./pkg -mindepth 1 -maxdepth 1 -type d); do
    49      set +e
    50      mv ${PLATFORM}/terraform-terraform.exe ${PLATFORM}/terraform.exe 2>/dev/null
    51      mv ${PLATFORM}/terraform-terraform ${PLATFORM}/terraform 2>/dev/null
    52      set -e
    53  done
    54  
    55  # Move all the compiled things to the $GOPATH/bin
    56  GOPATH=${GOPATH:-$(go env GOPATH)}
    57  case $(uname) in
    58      CYGWIN*)
    59          GOPATH="$(cygpath $GOPATH)"
    60          ;;
    61  esac
    62  OLDIFS=$IFS
    63  IFS=: MAIN_GOPATH=($GOPATH)
    64  IFS=$OLDIFS
    65  
    66  # Create GOPATH/bin if it's doesn't exists
    67  if [ ! -d $MAIN_GOPATH/bin ]; then
    68      echo "==> Creating GOPATH/bin directory..."
    69      mkdir -p $MAIN_GOPATH/bin
    70  fi
    71  
    72  # Copy our OS/Arch to the bin/ directory
    73  DEV_PLATFORM="./pkg/$(go env GOOS)_$(go env GOARCH)"
    74  for F in $(find ${DEV_PLATFORM} -mindepth 1 -maxdepth 1 -type f); do
    75      cp ${F} bin/
    76      cp ${F} ${MAIN_GOPATH}/bin/
    77  done
    78  
    79  if [ "${TF_DEV}x" = "x" ]; then
    80      # Zip and copy to the dist dir
    81      echo "==> Packaging..."
    82      for PLATFORM in $(find ./pkg -mindepth 1 -maxdepth 1 -type d); do
    83          OSARCH=$(basename ${PLATFORM})
    84          echo "--> ${OSARCH}"
    85  
    86          pushd $PLATFORM >/dev/null 2>&1
    87          zip ../${OSARCH}.zip ./*
    88          popd >/dev/null 2>&1
    89      done
    90  fi
    91  
    92  # Done!
    93  echo
    94  echo "==> Results:"
    95  ls -hl bin/