github.com/echohead/hub@v2.2.1+incompatible/script/build (about)

     1  #!/usr/bin/env bash
     2  # Usage: script/build [-o output] [test]
     3  #
     4  # Sets up GOPATH and compiles hub. With `test`, runs tests instead.
     5  
     6  set -e
     7  
     8  windows=
     9  [[ $OS == Windows* ]] && windows=1
    10  
    11  setup_gopath() {
    12    TMPDIR="${LOCALAPPDATA:-$TMPDIR}"
    13    TMP_GOPATH="${TMPDIR:-/tmp}/go"
    14    TMP_SELF="${TMP_GOPATH}/src/github.com/github/hub"
    15  
    16    if [ -n "$windows" ]; then
    17      export GOPATH="${TMP_GOPATH//\//\\}"
    18    else
    19      export GOPATH="$TMP_GOPATH"
    20    fi
    21  
    22    mkdir -p "${TMP_SELF%/*}"
    23    ln -snf "$PWD" "$TMP_SELF" 2>/dev/null || {
    24      rm -rf "$TMP_SELF"
    25      mkdir "$TMP_SELF"
    26      cp -R "$PWD"/* "${TMP_SELF}/"
    27    }
    28  }
    29  
    30  find_source_files() {
    31    find . -maxdepth 2 -name '*.go' -not -name '*_test.go' "$@"
    32  }
    33  
    34  count_changed_files() {
    35    printf %d $(find_source_files -newer "$1" | wc -l)
    36  }
    37  
    38  up_to_date() {
    39    [ -e "$1" ] && [ "$(count_changed_files "$1")" -eq 0 ]
    40  }
    41  
    42  build_hub() {
    43    setup_gopath
    44    [ -n "$1" ] && (up_to_date "$1" || go build -ldflags "-X github.com/github/hub/commands.Version `./script/version`" -o "$1")
    45  }
    46  
    47  test_hub() {
    48    setup_gopath
    49    go test ./...
    50  }
    51  
    52  case "$1" in
    53  "" )
    54    build_hub hub${windows:+.exe}
    55    ;;
    56  -o )
    57    shift
    58    build_hub $1
    59    ;;
    60  test )
    61    test_hub
    62    ;;
    63  -h | --help )
    64    sed -ne '/^#/!q;s/.\{1,2\}//;1d;p' < "$0"
    65    exit
    66    ;;
    67  * )
    68    "$0" --help >&2
    69    exit 1
    70  esac