build.sh 2.6 KB
Newer Older
A
Aaron 已提交
1
#!/bin/bash
H
HFO4 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15

REPO=$(cd $(dirname $0); pwd)
COMMIT_SHA=$(git rev-parse --short HEAD)
VERSION=$(git describe --tags)
ASSETS="false"
BINARY="false"
RELEASE="false"

debugInfo () {
  echo "Repo:           $REPO"
  echo "Build assets:   $ASSETS"
  echo "Build binary:   $BINARY"
  echo "Release:        $RELEASE"
  echo "Version:        $VERSION"
H
HFO4 已提交
16
  echo "Commit:        $COMMIT_SHA"
H
HFO4 已提交
17 18 19 20 21 22 23
}

buildAssets () {
  cd $REPO
  rm -rf assets/build
  rm -f statik/statik.go

H
HFO4 已提交
24 25
  export CI=false

H
HFO4 已提交
26 27 28 29 30 31
  cd $REPO/assets

  yarn install
  yarn run build

  if ! [ -x "$(command -v statik)" ]; then
A
Aaron 已提交
32
    export CGO_ENABLED=0
H
HFO4 已提交
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
    go get github.com/rakyll/statik
  fi

  cd $REPO
  statik -src=assets/build/  -include=*.html,*.js,*.json,*.css,*.png,*.svg,*.ico -f
}

buildBinary () {
  cd $REPO
  go build -a -o cloudreve -ldflags " -X 'github.com/HFO4/cloudreve/pkg/conf.BackendVersion=$VERSION' -X 'github.com/HFO4/cloudreve/pkg/conf.LastCommit=$COMMIT_SHA'"
}

_build() {
    local osarch=$1
    IFS=/ read -r -a arr <<<"$osarch"
    os="${arr[0]}"
    arch="${arr[1]}"
A
Aaron 已提交
50
    gcc="${arr[2]}"
H
HFO4 已提交
51 52 53 54

    # Go build to build the binary.
    export GOOS=$os
    export GOARCH=$arch
A
Aaron 已提交
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
    export CC=$gcc
    export CGO_ENABLED=1

    out="release/cloudreve_${VERSION}_${os}_${arch}"
    go build -a -o "${out}" -ldflags " -X 'github.com/HFO4/cloudreve/pkg/conf.BackendVersion=$VERSION' -X 'github.com/HFO4/cloudreve/pkg/conf.LastCommit=$COMMIT_SHA'"

    if [ "$os" = "windows" ]; then
      mv $out release/cloudreve.exe
      zip -j -q "${out}.zip" release/cloudreve.exe
      rm -f "release/cloudreve.exe"
    else
      mv $out release/cloudreve
      tar -zcvf "${out}.tar.gz" -C release cloudreve
      rm -f "release/cloudreve"
    fi
H
HFO4 已提交
70 71 72 73 74
}

release(){
  cd $REPO
  ## List of architectures and OS to test coss compilation.
H
HFO4 已提交
75
  SUPPORTED_OSARCH="linux/amd64/gcc linux/arm/arm-linux-gnueabihf-gcc windows/amd64/x86_64-w64-mingw32-gcc"
H
HFO4 已提交
76

A
Aaron 已提交
77
  echo "Release builds for OS/Arch/CC: ${SUPPORTED_OSARCH}"
H
HFO4 已提交
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
  for each_osarch in ${SUPPORTED_OSARCH}; do
      _build "${each_osarch}"
  done
}

usage() {
  echo "Usage: $0 [-a] [-c] [-b] [-r]" 1>&2;
  exit 1;
}

while getopts "bacr:d" o; do
  case "${o}" in
    b)
      ASSETS="true"
      BINARY="true"
      ;;
    a)
      ASSETS="true"
      ;;
    c)
      BINARY="true"
      ;;
    r)
H
HFO4 已提交
101
      ASSETS="true"
H
HFO4 已提交
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
      RELEASE="true"
      ;;
    d)
      DEBUG="true"
      ;;
    *)
      usage
      ;;
  esac
done
shift $((OPTIND-1))

if [ "$DEBUG" = "true" ]; then
  debugInfo
fi

if [ "$ASSETS" = "true" ]; then
  buildAssets
fi

if [ "$BINARY" = "true" ]; then
  buildBinary
fi

if [ "$RELEASE" = "true" ]; then
  release
A
Aaron 已提交
128
fi