github.com/supr/packer@v0.3.10-0.20131015195147-7b09e24ac3c1/scripts/build.sh (about)

     1  #!/bin/bash
     2  #
     3  # This script only builds the application from source.
     4  set -e
     5  
     6  NO_COLOR="\x1b[0m"
     7  OK_COLOR="\x1b[32;01m"
     8  ERROR_COLOR="\x1b[31;01m"
     9  WARN_COLOR="\x1b[33;01m"
    10  
    11  # Get the parent directory of where this script is.
    12  SOURCE="${BASH_SOURCE[0]}"
    13  while [ -h "$SOURCE" ] ; do SOURCE="$(readlink "$SOURCE")"; done
    14  DIR="$( cd -P "$( dirname "$SOURCE" )/.." && pwd )"
    15  
    16  # Change into that directory
    17  cd $DIR
    18  
    19  # Get the git commit
    20  GIT_COMMIT=$(git rev-parse HEAD)
    21  GIT_DIRTY=$(test -n "`git status --porcelain`" && echo "+CHANGES" || true)
    22  
    23  # If we're building on Windows, specify an extension
    24  EXTENSION=""
    25  if [ "$(go env GOOS)" = "windows" ]; then
    26      EXTENSION=".exe"
    27  fi
    28  
    29  # Make sure that if we're killed, we kill all our subprocseses
    30  trap "kill 0" SIGINT SIGTERM EXIT
    31  
    32  # If we're building a race-enabled build, then set that up.
    33  if [ ! -z $PACKER_RACE ]; then
    34      echo -e "${OK_COLOR}--> Building with race detection enabled${NO_COLOR}"
    35      PACKER_RACE="-race"
    36  fi
    37  
    38  echo -e "${OK_COLOR}--> Installing dependencies to speed up builds...${NO_COLOR}"
    39  go get ./...
    40  
    41  # This function waits for all background tasks to complete
    42  waitAll() {
    43      RESULT=0
    44      for job in `jobs -p`; do
    45          wait $job
    46          if [ $? -ne 0 ]; then
    47              RESULT=1
    48          fi
    49      done
    50  
    51      if [ $RESULT -ne 0 ]; then
    52          exit $RESULT
    53      fi
    54  }
    55  
    56  waitSingle() {
    57      if [ ! -z $PACKER_NO_BUILD_PARALLEL ]; then
    58          waitAll
    59      fi
    60  }
    61  
    62  if [ -z $PACKER_NO_BUILD_PARALLEL ]; then
    63      echo -e "${OK_COLOR}--> NOTE: Compilation of components " \
    64          "will be done in parallel.${NO_COLOR}"
    65  fi
    66  
    67  # Compile the main Packer app
    68  echo -e "${OK_COLOR}--> Compiling Packer${NO_COLOR}"
    69  (
    70  go build \
    71      ${PACKER_RACE} \
    72      -ldflags "-X github.com/mitchellh/packer/packer.GitCommit ${GIT_COMMIT}${GIT_DIRTY}" \
    73      -v \
    74      -o bin/packer${EXTENSION} .
    75  ) &
    76  
    77  waitSingle
    78  
    79  # Go over each plugin and build it
    80  for PLUGIN in $(find ./plugin -mindepth 1 -maxdepth 1 -type d); do
    81      PLUGIN_NAME=$(basename ${PLUGIN})
    82      echo -e "${OK_COLOR}--> Compiling Plugin: ${PLUGIN_NAME}${NO_COLOR}"
    83      (
    84      go build \
    85          ${PACKER_RACE} \
    86          -ldflags "-X github.com/mitchellh/packer/packer.GitCommit ${GIT_COMMIT}${GIT_DIRTY}" \
    87          -v \
    88          -o bin/packer-${PLUGIN_NAME}${EXTENSION} ${PLUGIN}
    89      ) &
    90  
    91      waitSingle
    92  done
    93  
    94  waitAll
    95  
    96  # Reset signal trapping to avoid "Terminated: 15" at the end
    97  trap - SIGINT SIGTERM EXIT