backend.js 2.8 KB
Newer Older
B
bryk 已提交
1
// Copyright 2015 Google Inc. All Rights Reserved.
2 3 4 5 6
//
// 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
//
B
bryk 已提交
7
//     http://www.apache.org/licenses/LICENSE-2.0
8 9 10 11 12 13 14 15
//
// 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.

/**
B
bryk 已提交
16
 * @fileoverview Gulp tasks for compiling backend application.
17 18 19 20 21 22
 */
import del from 'del';
import gulp from 'gulp';
import path from 'path';

import conf from './conf';
B
bryk 已提交
23
import goCommand from './gocommand';
24 25

/**
26
 * Compiles backend application in development mode and places the binary in the serve
27 28
 * directory.
 */
29
gulp.task('backend', ['package-backend-source'], function(doneFn) {
30 31 32 33 34 35 36 37 38 39
  goCommand(
      [
        'build',
        // Install dependencies to speed up subsequent compilations.
        '-i',
        '-o',
        path.join(conf.paths.serve, conf.backend.binaryName),
        conf.backend.packageName,
      ],
      doneFn);
40 41 42
});

/**
43
 * Compiles backend application in production mode and places the binary in the dist
44 45 46 47 48
 * directory.
 *
 * The production binary difference from development binary is only that it contains all
 * dependencies inside it and is targeted for Linux.
 */
49
gulp.task('backend:prod', ['package-backend-source'], function(doneFn) {
50 51
  let outputBinaryPath = path.join(conf.paths.dist, conf.backend.binaryName);

52 53
  // Delete output binary first. This is required because prod build does not override it.
  del(outputBinaryPath)
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
      .then(
          function() {
            goCommand(
                [
                  'build',
                  '-a',
                  '-installsuffix',
                  'cgo',
                  '-o',
                  outputBinaryPath,
                  conf.backend.packageName,
                ],
                doneFn, {
                  // Disable cgo package. Required to run on scratch docker image.
                  CGO_ENABLED: '0',
                  // Scratch docker image is linux.
                  GOOS: 'linux',
                });
          },
          function(error) { doneFn(error); });
74
});
75 76 77 78 79 80 81 82 83 84 85 86 87

/**
 * Moves all backend source files (app and tests) to a temporary package directory where it can be
 * applied go commands.
 *
 * This is required to consolidate test and app files into single directories and to make packaging
 * work.
 */
gulp.task('package-backend-source', function() {
  return gulp
      .src([path.join(conf.paths.backendSrc, '**/*'), path.join(conf.paths.backendTest, '**/*')])
      .pipe(gulp.dest(conf.paths.backendTmpSrc));
});