161847f8eSopenharmony_ci/*
261847f8eSopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
361847f8eSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
461847f8eSopenharmony_ci * you may not use this file except in compliance with the License.
561847f8eSopenharmony_ci * You may obtain a copy of the License at
661847f8eSopenharmony_ci *
761847f8eSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
861847f8eSopenharmony_ci *
961847f8eSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1061847f8eSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1161847f8eSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1261847f8eSopenharmony_ci * See the License for the specific language governing permissions and
1361847f8eSopenharmony_ci * limitations under the License.
1461847f8eSopenharmony_ci */
1561847f8eSopenharmony_ci
1661847f8eSopenharmony_ciimport commander from 'commander';
1761847f8eSopenharmony_ciimport envConfig from './config/env';
1861847f8eSopenharmony_ciimport { getToolConfiguration, ToolConfigType } from './bin/index';
1961847f8eSopenharmony_ciimport { CommandType, OptionObjType, PluginType, PluginOptionsType, toolNameType, toolNameSet } from './bin/config';
2061847f8eSopenharmony_ciimport { LogUtil } from './utils/logUtil';
2161847f8eSopenharmony_ciimport { FileUtils } from './utils/FileUtils';
2261847f8eSopenharmony_ci
2361847f8eSopenharmony_ciclass ToolBoxCommander {
2461847f8eSopenharmony_ci  program: commander.Command = new commander.Command();
2561847f8eSopenharmony_ci  constructor() { }
2661847f8eSopenharmony_ci  addPluginCommand(plugin: PluginType): void {
2761847f8eSopenharmony_ci    const pluginOption: PluginOptionsType = plugin.pluginOptions;
2861847f8eSopenharmony_ci    if (!pluginOption) {
2961847f8eSopenharmony_ci      return;
3061847f8eSopenharmony_ci    }
3161847f8eSopenharmony_ci    const pluginCommand: commander.Command = this.program
3261847f8eSopenharmony_ci      .name(pluginOption.name)
3361847f8eSopenharmony_ci      .description(pluginOption.description)
3461847f8eSopenharmony_ci      .version(pluginOption.version)
3561847f8eSopenharmony_ci      .action((opts: OptionObjType) => {
3661847f8eSopenharmony_ci        this.judgeOpts(opts);
3761847f8eSopenharmony_ci        plugin.start(opts);
3861847f8eSopenharmony_ci        plugin.stop();
3961847f8eSopenharmony_ci      });
4061847f8eSopenharmony_ci    pluginOption.commands.forEach((command: CommandType) => {
4161847f8eSopenharmony_ci      if (command.isRequiredOption) {
4261847f8eSopenharmony_ci        pluginCommand.requiredOption(...command.options);
4361847f8eSopenharmony_ci      } else {
4461847f8eSopenharmony_ci        pluginCommand.option(...command.options);
4561847f8eSopenharmony_ci      }
4661847f8eSopenharmony_ci    });
4761847f8eSopenharmony_ci  }
4861847f8eSopenharmony_ci  buildCommands(): void {
4961847f8eSopenharmony_ci    this.program.parse();
5061847f8eSopenharmony_ci  }
5161847f8eSopenharmony_ci  /**
5261847f8eSopenharmony_ci   * 判断传入命令是否满足工具允许条件,满足正常允许,不满足的时候通过stopRun停止命令行执行
5361847f8eSopenharmony_ci   *
5461847f8eSopenharmony_ci   * @param {OptionObjType} opts
5561847f8eSopenharmony_ci   */
5661847f8eSopenharmony_ci  judgeOpts(opts: OptionObjType): void {
5761847f8eSopenharmony_ci    const toolName: string = opts.toolName;
5861847f8eSopenharmony_ci    if (!toolNameSet.has(toolName)) {
5961847f8eSopenharmony_ci      this.stopRun(`error toolName "${toolName}",toolName not in \[${[...toolNameSet]}\] `);
6061847f8eSopenharmony_ci    }
6161847f8eSopenharmony_ci    switch (toolName) {
6261847f8eSopenharmony_ci      case toolNameType.COLLECT:
6361847f8eSopenharmony_ci        const collectPath: string = opts.collectPath;
6461847f8eSopenharmony_ci        if (collectPath === '' || !FileUtils.isExists(collectPath)) {
6561847f8eSopenharmony_ci          this.stopRun(`error collectPath "${collectPath}",collectPath need a exist file path`);
6661847f8eSopenharmony_ci        }
6761847f8eSopenharmony_ci        break;
6861847f8eSopenharmony_ci      case toolNameType.LABELDETECTION:
6961847f8eSopenharmony_ci        const checkLabels: string = opts.checkLabels;
7061847f8eSopenharmony_ci        if (checkLabels === '') {
7161847f8eSopenharmony_ci          this.stopRun(`error checkLabels "${checkLabels}",detection tools need checkLabels`);
7261847f8eSopenharmony_ci        }
7361847f8eSopenharmony_ci    }
7461847f8eSopenharmony_ci  }
7561847f8eSopenharmony_ci  /**
7661847f8eSopenharmony_ci   * 停止命令行执行,输出错误信息
7761847f8eSopenharmony_ci   *
7861847f8eSopenharmony_ci   * @param {string} text
7961847f8eSopenharmony_ci   */
8061847f8eSopenharmony_ci  stopRun(text: string): void {
8161847f8eSopenharmony_ci    LogUtil.e('commander', text);
8261847f8eSopenharmony_ci    this.program.help({ error: true });
8361847f8eSopenharmony_ci  }
8461847f8eSopenharmony_ci}
8561847f8eSopenharmony_ciclass ToolboxEntry {
8661847f8eSopenharmony_ci  commandBuilder: ToolBoxCommander;
8761847f8eSopenharmony_ci  constructor() {
8861847f8eSopenharmony_ci    this.commandBuilder = new ToolBoxCommander();
8961847f8eSopenharmony_ci  }
9061847f8eSopenharmony_ci  runPlugins(): void {
9161847f8eSopenharmony_ci    const configuration: ToolConfigType = getToolConfiguration();
9261847f8eSopenharmony_ci    configuration.plugins.forEach((plugin: PluginType) => {
9361847f8eSopenharmony_ci      this.commandBuilder.addPluginCommand(plugin);
9461847f8eSopenharmony_ci    });
9561847f8eSopenharmony_ci    this.commandBuilder.buildCommands();
9661847f8eSopenharmony_ci  }
9761847f8eSopenharmony_ci}
9861847f8eSopenharmony_ci
9961847f8eSopenharmony_cifunction main(): void {
10061847f8eSopenharmony_ci  Object.assign(process.env, envConfig);
10161847f8eSopenharmony_ci  const entry = new ToolboxEntry();
10261847f8eSopenharmony_ci  entry.runPlugins();
10361847f8eSopenharmony_ci}
10461847f8eSopenharmony_ci
10561847f8eSopenharmony_cimain();
106