forked from kubernetes/dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackend.js
141 lines (128 loc) · 4.33 KB
/
backend.js
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Copyright 2015 Google Inc. All Rights Reserved.
//
// 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.
/**
* @fileoverview Gulp tasks for compiling backend application.
*/
import del from 'del';
import fs from 'fs';
import gulp from 'gulp';
import lodash from 'lodash';
import path from 'path';
import conf from './conf';
import goCommand from './gocommand';
/**
* Compiles backend application in development mode and places the binary in the serve
* directory.
*/
gulp.task('backend', ['package-backend'], function(doneFn) {
goCommand(
[
'build',
// Install dependencies to speed up subsequent compilations.
'-i',
'-o',
path.join(conf.paths.serve, conf.backend.binaryName),
conf.backend.mainPackageName,
],
doneFn);
});
/**
* Compiles backend application in production mode for the current architecture and places the
* binary in the dist directory.
*
* The production binary difference from development binary is only that it contains all
* dependencies inside it and is targeted for a specific architecture.
*/
gulp.task('backend:prod', ['package-backend', 'clean-dist'], function() {
let outputBinaryPath = path.join(conf.paths.dist, conf.backend.binaryName);
return backendProd([[outputBinaryPath, conf.arch.default]]);
});
/**
* Compiles backend application in production mode for all architectures and places the
* binary in the dist directory.
*
* The production binary difference from development binary is only that it contains all
* dependencies inside it and is targeted specific architecture.
*/
gulp.task('backend:prod:cross', ['package-backend', 'clean-dist'], function() {
let outputBinaryPaths =
conf.paths.distCross.map((dir) => path.join(dir, conf.backend.binaryName));
return backendProd(lodash.zip(outputBinaryPaths, conf.arch.list));
});
/**
* Packages backend code to be ready for tests and compilation.
*/
gulp.task('package-backend', ['package-backend-source', 'link-vendor']);
/**
* Moves all backend source files (app and tests) to a temporary package directory where it can be
* applied go commands.
*/
gulp.task('package-backend-source', ['clean-packaged-backend-source'], function() {
return gulp
.src([path.join(conf.paths.backendSrc, '**/*'), path.join(conf.paths.backendTest, '**/*')])
.pipe(gulp.dest(conf.paths.backendTmpSrc));
});
/**
* Cleans packaged backend source to remove any leftovers from there.
*/
gulp.task('clean-packaged-backend-source', function() { return del([conf.paths.backendTmpSrc]); });
/**
* Links vendor folder to the packaged backend source.
*/
gulp.task('link-vendor', ['package-backend-source'], function(doneFn) {
fs.symlink(conf.paths.backendVendor, conf.paths.backendTmpSrcVendor, 'dir', (err) => {
if (err && err.code === 'EEXIST') {
// Skip errors if the link already exists.
doneFn();
} else {
doneFn(err);
}
});
});
/**
* @param {!Array<!Array<string>>} outputBinaryPathsAndArchs array of
* (output binary path, architecture) pairs
* @return {!Promise}
*/
function backendProd(outputBinaryPathsAndArchs) {
let promiseFn = (path, arch) => {
return (resolve, reject) => {
goCommand(
[
'build',
'-a',
'-installsuffix',
'cgo',
'-o',
path,
conf.backend.mainPackageName,
],
(err) => {
if (err) {
reject(err);
} else {
resolve();
}
},
{
// Disable cgo package. Required to run on scratch docker image.
CGO_ENABLED: '0',
GOARCH: arch,
});
};
};
let goCommandPromises = outputBinaryPathsAndArchs.map(
(pathAndArch) => new Promise(promiseFn(pathAndArch[0], pathAndArch[1])));
return Promise.all(goCommandPromises);
}