build.sh 3.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
#!/usr/bin/env bash
# Copyright 2017 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Exit on error.
set -e

# Import config.
ROOT_DIR="$(cd $(dirname "${BASH_SOURCE}")/../.. && pwd -P)"
. "${ROOT_DIR}/aio/scripts/conf.sh"

# Declare variables.
CROSS=false
FRONTEND_ONLY=false

function clean {
  rm -rf ${DIST_DIR} ${TMP_DIR}
}

function build::frontend {
S
Sebastian Florek 已提交
32 33 34 35 36 37 38
  say "\nBuilding localized frontend"
  mkdir -p ${FRONTEND_DIR}
  ${NG_BIN} build \
            --aot \
            --prod \
            --localize \
            --outputPath=${FRONTEND_DIR}
A
Amir Ajorloo 已提交
39 40 41 42 43 44 45 46 47 48 49

  # Avoid locale caching due to the same output file naming
  # We'll add language code prefix to the generated main javascript file.
  languages=($(ls ${FRONTEND_DIR}))
  for language in "${languages[@]}"; do
    localeDir=${FRONTEND_DIR}/${language}
    filename=("$(find "${localeDir}" -name 'main.*.js' -exec basename {} \;)")

    mv "${localeDir}/${filename}" "${localeDir}/${language}.${filename}"
    sed -i "s/${filename}/${language}.${filename}/" "${localeDir}/index.html"
  done
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
}

function build::backend {
  say "\nBuilding backend"
  ${GULP_BIN} backend:prod
}

function build::backend::cross {
  say "\nBuilding backends for all supported architectures"
  ${GULP_BIN} backend:prod:cross
}

function copy::frontend {
  say "\nCopying frontend to backend dist dir"
  languages=($(ls ${FRONTEND_DIR}))
  architectures=($(ls ${DIST_DIR}))
  for arch in "${architectures[@]}"; do
    for language in "${languages[@]}"; do
      OUT_DIR=${DIST_DIR}/${arch}/public
      mkdir -p ${OUT_DIR}
      cp -r ${FRONTEND_DIR}/${language} ${OUT_DIR}
    done
  done
}

function copy::supported-locales {
76
  say "\nCopying locales file to backend dist dirs"
77 78 79 80 81 82 83
  architectures=($(ls ${DIST_DIR}))
  for arch in "${architectures[@]}"; do
    OUT_DIR=${DIST_DIR}/${arch}
    cp ${I18N_DIR}/locale_conf.json ${OUT_DIR}
  done
}

84 85 86 87 88 89 90 91 92
function copy::dockerfile {
  say "\nCopying Dockerfile to backend dist dirs"
  architectures=($(ls ${DIST_DIR}))
  for arch in "${architectures[@]}"; do
    OUT_DIR=${DIST_DIR}/${arch}
    cp ${AIO_DIR}/Dockerfile ${OUT_DIR}
  done
}

93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
function parse::args {
  POSITIONAL=()
  while [[ $# -gt 0 ]]; do
    key="$1"
    case ${key} in
      -c|--cross)
      CROSS=true
      shift
      ;;
      --frontend-only)
      FRONTEND_ONLY=true
      shift
      ;;
    esac
  done
  set -- "${POSITIONAL[@]}" # Restore positional parameters.
}

# Execute script.
S
Shu Muto 已提交
112
START=$(date +%s)
113 114 115 116

parse::args "$@"
clean

117 118
npm run postversion

119 120 121 122 123 124 125 126 127 128 129 130 131 132
if [ "${FRONTEND_ONLY}" = true ] ; then
  build::frontend
  exit
fi

if [ "${CROSS}" = true ] ; then
  build::backend::cross
else
  build::backend
fi

build::frontend
copy::frontend
copy::supported-locales
133
copy::dockerfile
134

S
Shu Muto 已提交
135
END=$(date +%s)
136
TOOK=$(echo "${END} - ${START}" | bc)
137
say "\nBuild finished successfully after ${TOOK}s"