|
| 1 | +// Copyright 2015 Google Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +/** |
| 16 | + * Gulp tasks for compiling backend application. |
| 17 | + */ |
| 18 | +import child from 'child_process'; |
| 19 | +import del from 'del'; |
| 20 | +import gulp from 'gulp'; |
| 21 | +import lodash from 'lodash'; |
| 22 | +import path from 'path'; |
| 23 | + |
| 24 | +import conf from './conf'; |
| 25 | + |
| 26 | + |
| 27 | +/** |
| 28 | + * External dependencies of the Go backend application. |
| 29 | + * |
| 30 | + * @type {!Array<string>} |
| 31 | + */ |
| 32 | +const goBackendDependencies = [ |
| 33 | + 'github.com/golang/glog', |
| 34 | + 'github.com/spf13/pflag' |
| 35 | +]; |
| 36 | + |
| 37 | + |
| 38 | +/** |
| 39 | + * Spawns Go process with GOPATH placed in the backend tmp folder. |
| 40 | + * |
| 41 | + * @param {!Array<string>} args |
| 42 | + * @param {function(?Error=)} doneFn |
| 43 | + * @param {!Object<string, string>=} opt_env Optional environment variables to be concatenated with |
| 44 | + * default ones. |
| 45 | + */ |
| 46 | +function spawnGoProcess(args, doneFn, opt_env) { |
| 47 | + var goTask = child.spawn('go', args, { |
| 48 | + env: lodash.merge(process.env, {GOPATH: conf.paths.backendTmp}, opt_env || {}) |
| 49 | + }); |
| 50 | + |
| 51 | + // Call Gulp callback on task exit. This has to be done to make Gulp dependency management |
| 52 | + // work. |
| 53 | + goTask.on('exit', function(code) { |
| 54 | + if (code === 0) { |
| 55 | + doneFn(); |
| 56 | + } else { |
| 57 | + doneFn(new Error('Go command error, code:' + code)); |
| 58 | + } |
| 59 | + }); |
| 60 | + |
| 61 | + goTask.stdout.on('data', function (data) { |
| 62 | + console.log('' + data); |
| 63 | + }); |
| 64 | + |
| 65 | + goTask.stderr.on('data', function (data) { |
| 66 | + console.error('' + data); |
| 67 | + }); |
| 68 | +} |
| 69 | + |
| 70 | + |
| 71 | +/** |
| 72 | + * Compiles backend application in development mode and places 'console' binary in the serve |
| 73 | + * directory. |
| 74 | + */ |
| 75 | +gulp.task('backend', ['backend-dependencies'], function(doneFn) { |
| 76 | + spawnGoProcess([ |
| 77 | + 'build', |
| 78 | + '-o', path.join(conf.paths.serve, 'console'), |
| 79 | + path.join(conf.paths.backendSrc, 'console.go') |
| 80 | + ], doneFn); |
| 81 | +}); |
| 82 | + |
| 83 | + |
| 84 | +/** |
| 85 | + * Compiles backend application in production mode and places 'console' binary in the dist |
| 86 | + * directory. |
| 87 | + * |
| 88 | + * The production binary difference from development binary is only that it contains all |
| 89 | + * dependencies inside it and is targeted for Linux. |
| 90 | + */ |
| 91 | +gulp.task('backend:prod', ['backend-dependencies'], function(doneFn) { |
| 92 | + let outputBinaryPath = path.join(conf.paths.dist, 'console'); |
| 93 | + // Delete output binary first. This is required because prod build does not override it. |
| 94 | + del(outputBinaryPath) |
| 95 | + .then(function() { |
| 96 | + spawnGoProcess([ |
| 97 | + 'build', |
| 98 | + '-a', |
| 99 | + '-installsuffix', 'cgo', |
| 100 | + '-o', outputBinaryPath, |
| 101 | + path.join(conf.paths.backendSrc, 'console.go') |
| 102 | + ], doneFn, { |
| 103 | + // Disable cgo package. Required to run on scratch docker image. |
| 104 | + CGO_ENABLED: '0', |
| 105 | + // Scratch docker image is linux. |
| 106 | + GOOS: 'linux' |
| 107 | + }); |
| 108 | + }, function(error) { |
| 109 | + doneFn(error); |
| 110 | + }); |
| 111 | +}); |
| 112 | + |
| 113 | + |
| 114 | +/** |
| 115 | + * Gets backend dependencies and places them in the backend tmp directory. |
| 116 | + * |
| 117 | + * TODO(bryk): Investigate switching to Godep: https://github.com/tools/godep |
| 118 | + */ |
| 119 | +gulp.task('backend-dependencies', [], function(doneFn) { |
| 120 | + let args = ['get'].concat(goBackendDependencies); |
| 121 | + spawnGoProcess(args, doneFn); |
| 122 | +}); |
0 commit comments