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