From 6a28defe99bc2372546fcd1523bdf8cb35410e90 Mon Sep 17 00:00:00 2001 From: fangyunzhong Date: Fri, 24 Mar 2023 03:02:53 +0000 Subject: [PATCH] =?UTF-8?q?restool=E5=91=BD=E4=BB=A4=E9=80=89=E9=A1=B9?= =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: fangyunzhong --- README_zh.md | 26 +++++---- include/cmd_parser.h | 34 ++++++----- include/resource_data.h | 70 ++++++++++++++-------- src/cmd_list.cpp | 26 ++++----- src/cmd_parser.cpp | 125 +++++++++++++++++++++++++--------------- 5 files changed, 172 insertions(+), 109 deletions(-) diff --git a/README_zh.md b/README_zh.md index 5ea5101..ff70a78 100644 --- a/README_zh.md +++ b/README_zh.md @@ -29,20 +29,22 @@ restool当前支持以下命令选项: | 选项 | 是否可缺省 | 是否存在入参 | 描述 | | -------- | -------- | -------- | -------- | -| -i | 不可缺省 | 带参数 | 指定需要构建的资源目录或者需要构建的资源中间文件目录。同一个命令可以多次指定。 | -| -j | 不可缺省 | 带参数 | 指定config.json或者module.json文件路径。 | -| -o | 不可缺省 | 带参数 | 指定已编译资源的输出路径。 | -| -p | 不可缺省 | 带参数 | 指定编译资源的bundle名称。 | -| -r | 不可缺省 | 带参数 | 指定资源的头文件路径,有三种格式:“.txt”、“.js”、“.h”。 | -| -e | 可缺省 | 带参数 | 指定生成资源的起始ID值,例如:0x01000000,范围[0x01000000, 0x06FFFFFF),[0x08000000, 0x41FFFFFF) | -| -f | 可缺省 | 不带参数 | 如果输出路径已经存在。强制删除,重新生成。 | -| -h | 可缺省 | 不带参数 | 查看工具帮助信息。 | -| -m | 可缺省 | 带参数 | 多模块联合编译时,指定多个模块名。用“,”连接。 | -| -x | 可缺省 | 带参数 | 指定生成中间文件的资源目录或单个资源路径。同一个命令可以多次指定。 | -| -z | 可缺省 | 不带参数 | 针对资源终将文件目录,生成编译结果。 | -| -v | 可缺省 | 不带参数 | 查看工具版本号。 | +| -i/--inputPath | 不可缺省 | 带参数 | 指定需要构建的资源目录或者需要构建的资源中间文件目录。同一个命令可以多次指定。 | +| -j/--json | 不可缺省 | 带参数 | 指定config.json或者module.json文件路径。 | +| -o/--outputPath | 不可缺省 | 带参数 | 指定已编译资源的输出路径。 | +| -p/--packageName | 不可缺省 | 带参数 | 指定编译资源的bundle名称。 | +| -r/--resHeader | 不可缺省 | 带参数 | 指定资源的头文件路径,有三种格式:“.txt”、“.js”、“.h”。 | +| -e/--startId | 可缺省 | 带参数 | 指定生成资源的起始ID值,例如:0x01000000,范围[0x01000000, 0x06FFFFFF),[0x08000000, 0x41FFFFFF) | +| -f/--forceWrite | 可缺省 | 不带参数 | 如果输出路径已经存在。强制删除,重新生成。 | +| -h/--help | 可缺省 | 不带参数 | 查看工具帮助信息。 | +| -m/--modules | 可缺省 | 带参数 | 多模块联合编译时,指定多个模块名。用“,”连接。 | +| -x/--append | 可缺省 | 带参数 | 指定生成中间文件的资源目录或单个资源路径。同一个命令可以多次指定。 | +| -z/--combine | 可缺省 | 不带参数 | 针对资源终将文件目录,生成编译结果。 | +| -v/--version | 可缺省 | 不带参数 | 查看工具版本号。 | +| -l/--fileList | 可缺省 | 带参数 | 指定命令选项的集合json文件,例如:resConfig.json。详细格式查看开发文档。 | | --ids | 可缺省 | 带参数 | 指定生成id_defined.json的输出目录。 | | --defined-ids | 可缺省 | 带参数 | 指定id_defined.json文件路径,一般都是通过--ids生成。
id_defined.json包含资源类型、名称及其ID的列表。
开发者可以自定义id_defined.json中的资源ID。 | +| --dependEntry | 可缺省 | 带参数 | FA模型下单独编译feature时,指定entry模块的编译结果目录。 | ### 使用实例 diff --git a/include/cmd_parser.h b/include/cmd_parser.h index 7f650ff..299991b 100644 --- a/include/cmd_parser.h +++ b/include/cmd_parser.h @@ -81,6 +81,7 @@ private: uint32_t SetIdDefinedOutput(const std::string& argValue); uint32_t SetIdDefinedInputPath(const std::string& argValue); bool IsAscii(const std::string& argValue) const; + bool IsLongOpt(char *argv[]) const; static const struct option CMD_OPTS[]; static const std::string CMD_PARAMS; @@ -124,20 +125,25 @@ void CmdParser::ShowUseage() std::cout << "Usage:\n"; std::cout << TOOL_NAME << " [arguments] Package the OHOS resources.\n"; std::cout << "[arguments]:\n"; - std::cout << " -i input resource path, can add more.\n"; - std::cout << " -p package name.\n"; - std::cout << " -o output path.\n"; - std::cout << " -r resource header file path(like ./ResourceTable.js, ./ResrouceTable.h).\n"; - std::cout << " -f if output path exists,force delete it.\n"; - std::cout << " -v print tool version.\n"; - std::cout << " -m module name, can add more, split by ','(like entry1,entry2,...).\n"; - std::cout << " -j config.json path.\n"; - std::cout << " -e start id mask, e.g 0x01000000, in [0x01000000, 0x06FFFFFF),[0x08000000, 0x41FFFFFF)\n"; - std::cout << " -x resources folder path\n"; - std::cout << " -z flag for incremental compilation\n"; - std::cout << " -h Displays this help menu\n"; - std::cout << " --ids save id_defined.json direcory\n"; - std::cout << " --defined-ids input id_defined.json path\n"; + std::cout << " -i/--inputPath input resource path, can add more.\n"; + std::cout << " -p/--packageName package name.\n"; + std::cout << " -o/--outputPath output path.\n"; + std::cout << " -r/--resHeader resource header file path(like ./ResourceTable.js, ./ResrouceTable.h).\n"; + std::cout << " -f/--forceWrite if output path exists,force delete it.\n"; + std::cout << " -v/--version print tool version.\n"; + std::cout << " -m/--modules module name, can add more, split by ','(like entry1,entry2,...).\n"; + std::cout << " -j/--json config.json path.\n"; + std::cout << " -e/--startId start id mask, e.g 0x01000000,"; + std::cout << " in [0x01000000, 0x06FFFFFF),[0x08000000, 0x41FFFFFF)\n"; + std::cout << " -x/--append resources folder path\n"; + std::cout << " -z/--combine flag for incremental compilation\n"; + std::cout << " -h/--help Displays this help menu\n"; + std::cout << " -l/--fileList input json file of the option set, e.g resConfig.json."; + std::cout << " For details, see the developer documentation.\n"; + std::cout << " --ids save id_defined.json direcory\n"; + std::cout << " --defined-ids input id_defined.json path\n"; + std::cout << " --dependEntry Build result directory of the specified entry module when the feature"; + std::cout << " module resources are independently built in the FA model.\n"; } template diff --git a/include/resource_data.h b/include/resource_data.h index 5429746..b04f4f3 100644 --- a/include/resource_data.h +++ b/include/resource_data.h @@ -38,7 +38,7 @@ const static std::string LONG_PATH_HEAD = "\\\\?\\"; const static std::string ID_DEFINED_INDENTATION = " "; const static int32_t VERSION_MAX_LEN = 128; const static int32_t INT_TO_BYTES = sizeof(uint32_t); -static const int8_t RESTOOL_VERSION[VERSION_MAX_LEN] = { "Restool 4.001" }; +static const int8_t RESTOOL_VERSION[VERSION_MAX_LEN] = { "Restool 4.002" }; const static int32_t TAG_LEN = 4; enum class KeyType { @@ -83,11 +83,6 @@ enum class OrientationType { HORIZONTAL = 1, }; -const std::map g_orientaionMap = { - { "vertical", OrientationType::VERTICAL }, - { "horizontal", OrientationType::HORIZONTAL }, -}; - enum class DeviceType { PHONE = 0, TABLET = 1, @@ -97,14 +92,6 @@ enum class DeviceType { WEARABLE = 6, }; -const std::map g_deviceMap = { - { "phone", DeviceType::PHONE }, - { "tablet", DeviceType::TABLET }, - { "car", DeviceType::CAR }, - { "tv", DeviceType::TV }, - { "wearable", DeviceType::WEARABLE }, -}; - enum class ResolutionType { SDPI = 120, MDPI = 160, @@ -114,6 +101,51 @@ enum class ResolutionType { XXXLDPI = 640, }; +enum class NightMode { + DARK = 0, + LIGHT = 1, +}; + +enum class InputDevice { + INPUTDEVICE_NOT_SET = -1, + INPUTDEVICE_POINTINGDEVICE = 0, +}; + +enum Option { + END = -1, + IDS = 1, + DEFINED_IDS = 2, + DEPENDENTRY = 3, + STARTID = 'e', + FORCEWRITE = 'f', + HELP = 'h', + INPUTPATH = 'i', + JSON = 'j', + FILELIST = 'l', + MODULES = 'm', + OUTPUTPATH = 'o', + PACKAGENAME = 'p', + RESHEADER = 'r', + VERSION = 'v', + APPEND = 'x', + COMBINE = 'z', + UNKNOWN = '?', + NO_ARGUMENT = ':', +}; + +const std::map g_orientaionMap = { + { "vertical", OrientationType::VERTICAL }, + { "horizontal", OrientationType::HORIZONTAL }, +}; + +const std::map g_deviceMap = { + { "phone", DeviceType::PHONE }, + { "tablet", DeviceType::TABLET }, + { "car", DeviceType::CAR }, + { "tv", DeviceType::TV }, + { "wearable", DeviceType::WEARABLE }, +}; + const std::map g_resolutionMap = { { "sdpi", ResolutionType::SDPI }, { "mdpi", ResolutionType::MDPI }, @@ -123,21 +155,11 @@ const std::map g_resolutionMap = { { "xxxldpi", ResolutionType::XXXLDPI }, }; -enum class NightMode { - DARK = 0, - LIGHT = 1, -}; - const std::map g_nightModeMap = { { "dark", NightMode::DARK }, { "light", NightMode::LIGHT }, }; -enum class InputDevice { - INPUTDEVICE_NOT_SET = -1, - INPUTDEVICE_POINTINGDEVICE = 0, -}; - const std::map g_inputDeviceMap = { { "pointingdevice", InputDevice::INPUTDEVICE_POINTINGDEVICE }, }; diff --git a/src/cmd_list.cpp b/src/cmd_list.cpp index f2975e6..d12a17e 100644 --- a/src/cmd_list.cpp +++ b/src/cmd_list.cpp @@ -31,11 +31,11 @@ uint32_t CmdList::Init(const string &filePath, function +#include #include "cmd_list.h" #include "resource_util.h" @@ -23,28 +23,26 @@ namespace Global { namespace Restool { using namespace std; const struct option PackageParser::CMD_OPTS[] = { - { "inputPath", required_argument, nullptr, 'i' }, - { "packageName", required_argument, nullptr, 'p' }, - { "outputPath", required_argument, nullptr, 'o' }, - { "resHeader", required_argument, nullptr, 'r' }, - { "forceWrite", no_argument, nullptr, 'f' }, - { "version", no_argument, nullptr, 'v'}, - { "modules", optional_argument, nullptr, 'm' }, - { "json", optional_argument, nullptr, 'j' }, - { "startId", optional_argument, nullptr, 'e' }, - { "cache", optional_argument, nullptr, 'c' }, - { "fileList", required_argument, nullptr, 'l' }, - { "preview", no_argument, nullptr, 'a' }, - { "priority", required_argument, nullptr, 'g' }, - { "append", required_argument, nullptr, 'x' }, - { "combine", required_argument, nullptr, 'z' }, - { "dependEntry", required_argument, nullptr, 'd' }, - { "help", no_argument, nullptr, 'h'}, - { "ids", required_argument, nullptr, 's'}, - { "defined-ids", required_argument, nullptr, 't'}, + { "inputPath", required_argument, nullptr, Option::INPUTPATH }, + { "packageName", required_argument, nullptr, Option::PACKAGENAME }, + { "outputPath", required_argument, nullptr, Option::OUTPUTPATH }, + { "resHeader", required_argument, nullptr, Option::RESHEADER }, + { "forceWrite", no_argument, nullptr, Option::FORCEWRITE }, + { "version", no_argument, nullptr, Option::VERSION}, + { "modules", required_argument, nullptr, Option::MODULES }, + { "json", required_argument, nullptr, Option::JSON }, + { "startId", required_argument, nullptr, Option::STARTID }, + { "fileList", required_argument, nullptr, Option::FILELIST }, + { "append", required_argument, nullptr, Option::APPEND }, + { "combine", required_argument, nullptr, Option::COMBINE }, + { "dependEntry", required_argument, nullptr, Option::DEPENDENTRY }, + { "help", no_argument, nullptr, Option::HELP}, + { "ids", required_argument, nullptr, Option::IDS}, + { "defined-ids", required_argument, nullptr, Option::DEFINED_IDS}, + { 0, 0, 0, 0}, }; -const string PackageParser::CMD_PARAMS = "i:p:o:r:m:j:e:c:l:g:x:d:s:t:afhvz"; +const string PackageParser::CMD_PARAMS = ":i:p:o:r:m:j:e:l:x:fhvz"; uint32_t PackageParser::Parse(int argc, char *argv[]) { @@ -373,32 +371,29 @@ bool PackageParser::IsAscii(const string& argValue) const void PackageParser::InitCommand() { using namespace placeholders; - handles_.emplace('i', bind(&PackageParser::AddInput, this, _1)); - handles_.emplace('p', bind(&PackageParser::AddPackageName, this, _1)); - handles_.emplace('o', bind(&PackageParser::AddOutput, this, _1)); - handles_.emplace('r', bind(&PackageParser::AddResourceHeader, this, _1)); - handles_.emplace('f', [this](const string &) -> uint32_t { return ForceWrite(); }); - handles_.emplace('v', [this](const string &) -> uint32_t { return PrintVersion(); }); - handles_.emplace('m', bind(&PackageParser::AddMoudleNames, this, _1)); - handles_.emplace('j', bind(&PackageParser::AddConfig, this, _1)); - handles_.emplace('e', bind(&PackageParser::AddStartId, this, _1)); - handles_.emplace('c', bind(&PackageParser::AddCachePath, this, _1)); - handles_.emplace('a', [this](const string &) -> uint32_t { return SetPreviewMode(); }); - handles_.emplace('g', bind(&PackageParser::SetPriority, this, _1)); - handles_.emplace('x', bind(&PackageParser::AddAppend, this, _1)); - handles_.emplace('z', [this](const string &) -> uint32_t { return SetCombine(); }); - handles_.emplace('d', bind(&PackageParser::AddDependEntry, this, _1)); - handles_.emplace('h', [this](const string &) -> uint32_t { return ShowHelp(); }); - handles_.emplace('s', bind(&PackageParser::SetIdDefinedOutput, this, _1)); - handles_.emplace('t', bind(&PackageParser::SetIdDefinedInputPath, this, _1)); + handles_.emplace(Option::INPUTPATH, bind(&PackageParser::AddInput, this, _1)); + handles_.emplace(Option::PACKAGENAME, bind(&PackageParser::AddPackageName, this, _1)); + handles_.emplace(Option::OUTPUTPATH, bind(&PackageParser::AddOutput, this, _1)); + handles_.emplace(Option::RESHEADER, bind(&PackageParser::AddResourceHeader, this, _1)); + handles_.emplace(Option::FORCEWRITE, [this](const string &) -> uint32_t { return ForceWrite(); }); + handles_.emplace(Option::VERSION, [this](const string &) -> uint32_t { return PrintVersion(); }); + handles_.emplace(Option::MODULES, bind(&PackageParser::AddMoudleNames, this, _1)); + handles_.emplace(Option::JSON, bind(&PackageParser::AddConfig, this, _1)); + handles_.emplace(Option::STARTID, bind(&PackageParser::AddStartId, this, _1)); + handles_.emplace(Option::APPEND, bind(&PackageParser::AddAppend, this, _1)); + handles_.emplace(Option::COMBINE, [this](const string &) -> uint32_t { return SetCombine(); }); + handles_.emplace(Option::DEPENDENTRY, bind(&PackageParser::AddDependEntry, this, _1)); + handles_.emplace(Option::HELP, [this](const string &) -> uint32_t { return ShowHelp(); }); + handles_.emplace(Option::IDS, bind(&PackageParser::SetIdDefinedOutput, this, _1)); + handles_.emplace(Option::DEFINED_IDS, bind(&PackageParser::SetIdDefinedInputPath, this, _1)); } uint32_t PackageParser::HandleProcess(int c, const string& argValue) { auto handler = handles_.find(c); if (handler == handles_.end()) { - cout << "Warning: unsupport " << c << endl; - return RESTOOL_SUCCESS; + cout << "Error: unsupport " << c << endl; + return RESTOOL_ERROR; } return handler->second(argValue); } @@ -419,14 +414,42 @@ uint32_t PackageParser::ParseCommand(int argc, char *argv[]) { restoolPath_ = string(argv[0]); while (true) { - int optIndex = 0; + int optIndex = -1; + opterr = 0; int c = getopt_long(argc, argv, CMD_PARAMS.c_str(), CMD_OPTS, &optIndex); - if (c == -1) { - break; + if (optIndex != -1) { + string curOpt = (optarg == nullptr) ? argv[optind - 1] : argv[optind - 2]; + if (curOpt != ("--" + string(CMD_OPTS[optIndex].name))) { + cout << "Error: unknown option " << curOpt << endl; + return RESTOOL_ERROR; + } + } + if (c == Option::END) { + if (argc == optind) { + break; + } + string errmsg = "Error: invalid arguments : "; + for (int i = optind; i < argc; i++) { + errmsg.append(argv[i]).append(" "); + } + cout << errmsg << endl; + return RESTOOL_ERROR; + } + if (c == Option::UNKNOWN) { + string optUnknown = (optopt == 0) ? argv[optind - 1] : ("-" + string(1, optopt)); + cout << "Error: unknown option " << optUnknown << endl; + return RESTOOL_ERROR; + } + if (c == Option::NO_ARGUMENT) { + if (IsLongOpt(argv)) { + cout << "Error: option " << argv[optind - 1] << " must have argument" << endl; + } else { + cout << "Error: unknown option " << argv[optind - 1] << endl; + } + return RESTOOL_ERROR; } - string argValue = (optarg != nullptr) ? optarg : ""; - if (c == 'l') { + if (c == Option::FILELIST) { return ParseFileList(argValue); } if (HandleProcess(c, argValue) != RESTOOL_SUCCESS) { @@ -435,6 +458,16 @@ uint32_t PackageParser::ParseCommand(int argc, char *argv[]) } return RESTOOL_SUCCESS; } + +bool PackageParser::IsLongOpt(char *argv[]) const +{ + for (auto iter : CMD_OPTS) { + if (optopt == iter.val && argv[optind - 1] == ("--" + string(iter.name))) { + return true; + } + } + return false; +} } } }