• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (C) 2022 Huawei Device Co., Ltd.
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
16const path = require('path');
17const fs = require('fs');
18const childProcess = require('child_process');
19const os = require('os');
20const log4js = require('log4js');
21
22const compileServer = true;
23const outDir = 'dist';
24
25const sdkWams = [
26  'trace_streamer_sdk_builtin.js',
27  'trace_streamer_sdk_builtin.wasm',
28  'trace_streamer_dubai_builtin.js',
29  'trace_streamer_dubai_builtin.wasm',
30];
31
32const staticPath = ['/src/img', '/server/cert', '/src/doc', '/src/figures'];
33
34const staticFiles = [
35  '/server/version.txt',
36  '/src/index.html',
37  '/src/base-ui/icon.svg',
38  '/server/wasm.json',
39  '/server/server-config.txt',
40];
41
42const thirdParty = [
43  {
44    srcFilePath: '/third-party/sql-wasm.wasm',
45    distFilePath: '/trace/database/sql-wasm.wasm',
46  },
47  {
48    srcFilePath: '/third-party/sql-wasm.js',
49    distFilePath: '/trace/database/sql-wasm.js',
50  },
51  {
52    srcFilePath: '/third-party/worker.sql-wasm.js',
53    distFilePath: '/trace/database/worker.sql-wasm.js',
54  },
55];
56
57let log;
58
59function cpFile(from, to) {
60  if (fs.existsSync(from)) {
61    fs.writeFileSync(to, fs.readFileSync(from));
62    log.info('cp file %s  to  %s', from, to);
63  } else {
64    log.warn('file %s  is not exists', from, to);
65  }
66}
67
68function checkEnvironment() {
69  let goVersion = childProcess.execSync('go version', {
70    encoding: 'utf-8',
71  });
72  log.info('go is', goVersion);
73  let nodeVersion = childProcess.execSync('node -v', {
74    encoding: 'utf-8',
75  });
76  log.info('node version is', nodeVersion);
77  let tscVersion = childProcess.execSync('tsc -v', {
78    encoding: 'utf-8',
79  });
80  log.info('tsc version is', tscVersion);
81  if (goVersion == '' || nodeVersion == '' || tscVersion == '') {
82    return false;
83  }
84  let traceStreamer = path.normalize(path.join(__dirname, '/bin'));
85  if (!checkDirExist(traceStreamer + '/trace_streamer_builtin.js')) {
86    log.error(traceStreamer + '/trace_streamer_builtin.js' + ' Must exist');
87    return false;
88  }
89  if (!checkDirExist(traceStreamer + '/trace_streamer_builtin.wasm')) {
90    log.error(traceStreamer + '/trace_streamer_builtin.wasm' + ' Must exist');
91    return false;
92  }
93  return true;
94}
95
96function initLog() {
97  log4js.configure({
98    appenders: {
99      out: { type: 'stdout' },
100    },
101    categories: {
102      default: { appenders: ['out'], level: 'debug' },
103    },
104  });
105  return log4js.getLogger('smartPerf');
106}
107
108function main() {
109  log = initLog();
110  if (!checkEnvironment()) {
111    return;
112  }
113  // clean outDir
114  let outPath = path.normalize(path.join(__dirname, '/', outDir));
115  if (checkDirExist(outPath)) {
116    log.info('delete the last compilation result');
117    removeDir(outPath);
118    log.info('delete the last compilation success');
119  }
120  // run tsc compile
121  log.info('start compiling typeScript code');
122  let rootPath = path.join(__dirname, '/');
123  childProcess.execSync('tsc -p ' + rootPath, {
124    encoding: 'utf-8',
125  });
126  log.info('compiling typeScript code success');
127  // run cp to mv all staticFile
128  staticFiles.forEach((value) => {
129    let filePath = path.join(__dirname, value);
130    let distFile;
131    if (value.startsWith('/src')) {
132      distFile = path.join(__dirname, outDir, value.substring(4, value.length + 1));
133    } else if (value.startsWith('/server')) {
134      distFile = path.join(__dirname, outDir, value.substring(7, value.length + 1));
135    }
136    cpFile(filePath, distFile);
137  });
138  staticPath.forEach((value) => {
139    let pa = path.join(__dirname, value);
140    let distPath;
141    if (value.startsWith('/src')) {
142      distPath = path.join(__dirname, outDir, value.substring(4, value.length + 1));
143    } else if (value.startsWith('/server')) {
144      distPath = path.join(__dirname, outDir, value.substring(7, value.length + 1));
145    }
146    copyDirectory(pa, distPath);
147  });
148  thirdParty.forEach((value) => {
149    let thirdFile = path.join(__dirname, value.srcFilePath);
150    let thirdDistFile = path.join(__dirname, outDir, value.distFilePath);
151    cpFile(thirdFile, thirdDistFile);
152  });
153  let traceStreamer = path.normalize(path.join(__dirname, '/bin'));
154  if (checkDirExist(traceStreamer)) {
155    let dest = path.normalize(path.join(__dirname, outDir, '/bin'));
156    copyDirectory(traceStreamer, dest);
157    // to mv traceStream Wasm and js
158    cpFile(
159      traceStreamer + '/trace_streamer_builtin.js',
160      rootPath + outDir + '/trace/database/trace_streamer_builtin.js'
161    );
162    cpFile(
163      traceStreamer + '/trace_streamer_builtin.wasm',
164      rootPath + outDir + '/trace/database/trace_streamer_builtin.wasm'
165    );
166    if (sdkWams.length > 0) {
167      sdkWams.forEach((fileName) => {
168        cpFile(traceStreamer + '/' + fileName, rootPath + outDir + '/trace/database/' + fileName);
169      });
170    }
171  } else {
172    log.error('traceStreamer dir is not Exits');
173    return;
174  }
175  // compile server
176  if (compileServer) {
177    log.log('start compile server');
178    let serverSrc = path.normalize(path.join(__dirname, '/server/main.go'));
179    let rs;
180    if (os.type() === 'Windows_NT') {
181      rs = childProcess.spawnSync('go', ['build', '-o', outPath, serverSrc], {
182        encoding: 'utf-8',
183      });
184    } else {
185      rs = childProcess.spawnSync('go', ['build', '-o', outPath + '/main', serverSrc], {
186        encoding: 'utf-8',
187      });
188    }
189    if (rs.status == 0) {
190      log.log('compile server success');
191    } else {
192      log.error('compile server failed', rs);
193    }
194  } else {
195    log.warn('skip compile server');
196  }
197  log.log('smartPerf compile success');
198}
199
200function copyDirectory(src, dest) {
201  if (checkDirExist(dest) == false) {
202    fs.mkdirSync(dest);
203  }
204  if (checkDirExist(src) == false) {
205    return false;
206  }
207  let directories = fs.readdirSync(src);
208  directories.forEach((value) => {
209    let filePath = path.join(src, value);
210    let fileSys = fs.statSync(filePath);
211    if (fileSys.isFile()) {
212      let destPath = path.join(dest, value);
213      log.info('cp file %s  to  %s', filePath, destPath);
214      fs.copyFileSync(filePath, destPath);
215    } else if (fileSys.isDirectory()) {
216      copyDirectory(filePath, path.join(dest, value));
217    }
218  });
219}
220
221function checkDirExist(dirPath) {
222  return fs.existsSync(dirPath);
223}
224
225function removeDir(outPath) {
226  let files = [];
227  if (fs.existsSync(outPath)) {
228    files = fs.readdirSync(outPath);
229    files.forEach((file, index) => {
230      let curPath = outPath + '/' + file;
231      if (fs.statSync(curPath).isDirectory()) {
232        removeDir(curPath);
233      } else {
234        fs.unlinkSync(curPath);
235      }
236    });
237    fs.rmdirSync(outPath);
238  }
239}
240
241main();
242