github.com/ndarilek/terraform@v0.3.8-0.20150320140257-d3135c1b2bac/scripts/build.sh (about)

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