13920e296Sopenharmony_ci/*
23920e296Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
33920e296Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
43920e296Sopenharmony_ci * you may not use this file except in compliance with the License.
53920e296Sopenharmony_ci * You may obtain a copy of the License at
63920e296Sopenharmony_ci *
73920e296Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
83920e296Sopenharmony_ci *
93920e296Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
103920e296Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
113920e296Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
123920e296Sopenharmony_ci * See the License for the specific language governing permissions and
133920e296Sopenharmony_ci * limitations under the License.
143920e296Sopenharmony_ci */
153920e296Sopenharmony_ci
163920e296Sopenharmony_ci#ifndef OHOS_RESTOOL_CMD_PARSER_H
173920e296Sopenharmony_ci#define OHOS_RESTOOL_CMD_PARSER_H
183920e296Sopenharmony_ci
193920e296Sopenharmony_ci#include <getopt.h>
203920e296Sopenharmony_ci#include <iostream>
213920e296Sopenharmony_ci#include <functional>
223920e296Sopenharmony_ci#include <vector>
233920e296Sopenharmony_ci#include "singleton.h"
243920e296Sopenharmony_ci#include "resource_data.h"
253920e296Sopenharmony_ci#include "restool_errors.h"
263920e296Sopenharmony_ci
273920e296Sopenharmony_cinamespace OHOS {
283920e296Sopenharmony_cinamespace Global {
293920e296Sopenharmony_cinamespace Restool {
303920e296Sopenharmony_ciclass ICmdParser {
313920e296Sopenharmony_cipublic:
323920e296Sopenharmony_ci    virtual uint32_t Parse(int argc, char *argv[]) = 0;
333920e296Sopenharmony_ci};
343920e296Sopenharmony_ci
353920e296Sopenharmony_ciclass PackageParser : public ICmdParser {
363920e296Sopenharmony_cipublic:
373920e296Sopenharmony_ci    PackageParser() {};
383920e296Sopenharmony_ci    virtual ~PackageParser() = default;
393920e296Sopenharmony_ci    uint32_t Parse(int argc, char *argv[]) override;
403920e296Sopenharmony_ci    const std::vector<std::string> &GetInputs() const;
413920e296Sopenharmony_ci    const std::string &GetPackageName() const;
423920e296Sopenharmony_ci    const std::string &GetOutput() const;
433920e296Sopenharmony_ci    const std::vector<std::string> &GetResourceHeaders() const;
443920e296Sopenharmony_ci    bool GetForceWrite() const;
453920e296Sopenharmony_ci    const std::vector<std::string> &GetModuleNames() const;
463920e296Sopenharmony_ci    const std::string &GetConfig() const;
473920e296Sopenharmony_ci    const std::string &GetRestoolPath() const;
483920e296Sopenharmony_ci    uint32_t GetStartId() const;
493920e296Sopenharmony_ci    bool IsFileList() const;
503920e296Sopenharmony_ci    const std::vector<std::string> &GetAppend() const;
513920e296Sopenharmony_ci    bool GetCombine() const;
523920e296Sopenharmony_ci    const std::string &GetDependEntry() const;
533920e296Sopenharmony_ci    const std::string &GetIdDefinedOutput() const;
543920e296Sopenharmony_ci    const std::string &GetIdDefinedInputPath() const;
553920e296Sopenharmony_ci    bool GetIconCheck() const;
563920e296Sopenharmony_ci    const TargetConfig &GetTargetConfigValues() const;
573920e296Sopenharmony_ci    bool IsTargetConfig() const;
583920e296Sopenharmony_ci    const std::vector<std::string> &GetSysIdDefinedPaths() const;
593920e296Sopenharmony_ci    const std::string &GetCompressionPath() const;
603920e296Sopenharmony_ci
613920e296Sopenharmony_ciprivate:
623920e296Sopenharmony_ci    void InitCommand();
633920e296Sopenharmony_ci    uint32_t ParseCommand(int argc, char *argv[]);
643920e296Sopenharmony_ci    uint32_t CheckError(int argc, char *argv[], int c, int optIndex);
653920e296Sopenharmony_ci    uint32_t AddInput(const std::string& argValue);
663920e296Sopenharmony_ci    uint32_t AddPackageName(const std::string& argValue);
673920e296Sopenharmony_ci    uint32_t AddOutput(const std::string& argValue);
683920e296Sopenharmony_ci    uint32_t AddResourceHeader(const std::string& argValue);
693920e296Sopenharmony_ci    uint32_t ForceWrite();
703920e296Sopenharmony_ci    uint32_t PrintVersion();
713920e296Sopenharmony_ci    uint32_t AddMoudleNames(const std::string& argValue);
723920e296Sopenharmony_ci    uint32_t AddConfig(const std::string& argValue);
733920e296Sopenharmony_ci    uint32_t AddStartId(const std::string& argValue);
743920e296Sopenharmony_ci    void AdaptResourcesDirForInput();
753920e296Sopenharmony_ci    uint32_t CheckParam() const;
763920e296Sopenharmony_ci    uint32_t HandleProcess(int c, const std::string& argValue);
773920e296Sopenharmony_ci    uint32_t ParseFileList(const std::string& fileListPath);
783920e296Sopenharmony_ci    uint32_t AddAppend(const std::string& argValue);
793920e296Sopenharmony_ci    uint32_t SetCombine();
803920e296Sopenharmony_ci    uint32_t AddDependEntry(const std::string& argValue);
813920e296Sopenharmony_ci    uint32_t ShowHelp() const;
823920e296Sopenharmony_ci    uint32_t SetIdDefinedOutput(const std::string& argValue);
833920e296Sopenharmony_ci    uint32_t SetIdDefinedInputPath(const std::string& argValue);
843920e296Sopenharmony_ci    uint32_t AddSysIdDefined(const std::string& argValue);
853920e296Sopenharmony_ci    bool IsAscii(const std::string& argValue) const;
863920e296Sopenharmony_ci    bool IsLongOpt(int argc, char *argv[]) const;
873920e296Sopenharmony_ci    uint32_t IconCheck();
883920e296Sopenharmony_ci    uint32_t ParseTargetConfig(const std::string& argValue);
893920e296Sopenharmony_ci    uint32_t AddCompressionPath(const std::string& argValue);
903920e296Sopenharmony_ci
913920e296Sopenharmony_ci    static const struct option CMD_OPTS[];
923920e296Sopenharmony_ci    static const std::string CMD_PARAMS;
933920e296Sopenharmony_ci    using HandleArgValue = std::function<uint32_t(const std::string&)>;
943920e296Sopenharmony_ci    std::map<int32_t, HandleArgValue> handles_;
953920e296Sopenharmony_ci    std::vector<std::string> inputs_;
963920e296Sopenharmony_ci    std::string packageName_;
973920e296Sopenharmony_ci    std::string output_;
983920e296Sopenharmony_ci    std::vector<std::string> resourceHeaderPaths_;
993920e296Sopenharmony_ci    bool forceWrite_ = false;
1003920e296Sopenharmony_ci    std::vector<std::string> moduleNames_;
1013920e296Sopenharmony_ci    std::string configPath_;
1023920e296Sopenharmony_ci    std::string restoolPath_;
1033920e296Sopenharmony_ci    uint32_t startId_ = 0;
1043920e296Sopenharmony_ci    bool isFileList_ = false;
1053920e296Sopenharmony_ci    std::vector<std::string> append_;
1063920e296Sopenharmony_ci    bool combine_ = false;
1073920e296Sopenharmony_ci    std::string dependEntry_;
1083920e296Sopenharmony_ci    std::string idDefinedOutput_;
1093920e296Sopenharmony_ci    std::string idDefinedInputPath_;
1103920e296Sopenharmony_ci    bool isIconCheck_ = false;
1113920e296Sopenharmony_ci    TargetConfig targetConfig_;
1123920e296Sopenharmony_ci    bool isTtargetConfig_;
1133920e296Sopenharmony_ci    std::vector<std::string> sysIdDefinedPaths_;
1143920e296Sopenharmony_ci    std::string compressionPath_;
1153920e296Sopenharmony_ci};
1163920e296Sopenharmony_ci
1173920e296Sopenharmony_citemplate<class T>
1183920e296Sopenharmony_ciclass CmdParser : public Singleton<CmdParser<T>> {
1193920e296Sopenharmony_cipublic:
1203920e296Sopenharmony_ci    T &GetCmdParser();
1213920e296Sopenharmony_ci    uint32_t Parse(int argc, char *argv[]);
1223920e296Sopenharmony_ci    static void ShowUseage();
1233920e296Sopenharmony_ci
1243920e296Sopenharmony_ciprivate:
1253920e296Sopenharmony_ci    T cmdParser_;
1263920e296Sopenharmony_ci};
1273920e296Sopenharmony_ci
1283920e296Sopenharmony_citemplate<class T>
1293920e296Sopenharmony_civoid CmdParser<T>::ShowUseage()
1303920e296Sopenharmony_ci{
1313920e296Sopenharmony_ci    std::cout << "This is an OHOS Packaging Tool.\n";
1323920e296Sopenharmony_ci    std::cout << "Usage:\n";
1333920e296Sopenharmony_ci    std::cout << TOOL_NAME << " [arguments] Package the OHOS resources.\n";
1343920e296Sopenharmony_ci    std::cout << "[arguments]:\n";
1353920e296Sopenharmony_ci    std::cout << "    -i/--inputPath      input resource path, can add more.\n";
1363920e296Sopenharmony_ci    std::cout << "    -p/--packageName    package name.\n";
1373920e296Sopenharmony_ci    std::cout << "    -o/--outputPath     output path.\n";
1383920e296Sopenharmony_ci    std::cout << "    -r/--resHeader      resource header file path(like ./ResourceTable.js, ./ResrouceTable.h).\n";
1393920e296Sopenharmony_ci    std::cout << "    -f/--forceWrite     if output path exists,force delete it.\n";
1403920e296Sopenharmony_ci    std::cout << "    -v/--version        print tool version.\n";
1413920e296Sopenharmony_ci    std::cout << "    -m/--modules        module name, can add more, split by ','(like entry1,entry2,...).\n";
1423920e296Sopenharmony_ci    std::cout << "    -j/--json           config.json path.\n";
1433920e296Sopenharmony_ci    std::cout << "    -e/--startId        start id mask, e.g 0x01000000,";
1443920e296Sopenharmony_ci    std::cout << " in [0x01000000, 0x06FFFFFF),[0x08000000, 0xFFFFFFFF)\n";
1453920e296Sopenharmony_ci    std::cout << "    -x/--append         resources folder path\n";
1463920e296Sopenharmony_ci    std::cout << "    -z/--combine        flag for incremental compilation\n";
1473920e296Sopenharmony_ci    std::cout << "    -h/--help           Displays this help menu\n";
1483920e296Sopenharmony_ci    std::cout << "    -l/--fileList       input json file of the option set, e.g resConfig.json.";
1493920e296Sopenharmony_ci    std::cout << " For details, see the developer documentation.\n";
1503920e296Sopenharmony_ci    std::cout << "    --ids               save id_defined.json direcory\n";
1513920e296Sopenharmony_ci    std::cout << "    --defined-ids       input id_defined.json path\n";
1523920e296Sopenharmony_ci    std::cout << "    --dependEntry       Build result directory of the specified entry module when the feature";
1533920e296Sopenharmony_ci    std::cout << " module resources are independently built in the FA model.\n";
1543920e296Sopenharmony_ci    std::cout << "    --icon-check        Enable the PNG image verification function for icons and startwindows.\n";
1553920e296Sopenharmony_ci    std::cout << "    --target-config     When used with '-i', selective compilation is supported.\n";
1563920e296Sopenharmony_ci    std::cout << "    --compressed-config opt-compression.json path.\n";
1573920e296Sopenharmony_ci}
1583920e296Sopenharmony_ci
1593920e296Sopenharmony_citemplate<class T>
1603920e296Sopenharmony_ciT &CmdParser<T>::GetCmdParser()
1613920e296Sopenharmony_ci{
1623920e296Sopenharmony_ci    return cmdParser_;
1633920e296Sopenharmony_ci}
1643920e296Sopenharmony_ci
1653920e296Sopenharmony_citemplate<class T>
1663920e296Sopenharmony_ciuint32_t CmdParser<T>::Parse(int argc, char *argv[])
1673920e296Sopenharmony_ci{
1683920e296Sopenharmony_ci    if (cmdParser_.Parse(argc, argv) != RESTOOL_SUCCESS) {
1693920e296Sopenharmony_ci        return RESTOOL_ERROR;
1703920e296Sopenharmony_ci    }
1713920e296Sopenharmony_ci    return RESTOOL_SUCCESS;
1723920e296Sopenharmony_ci}
1733920e296Sopenharmony_ci}
1743920e296Sopenharmony_ci}
1753920e296Sopenharmony_ci}
1763920e296Sopenharmony_ci#endif
177