From a382e73eb83b8a2471b82ef24030d03c53c8a109 Mon Sep 17 00:00:00 2001 From: liduo Date: Wed, 19 Feb 2025 11:41:59 +0800 Subject: [PATCH] support specify ignored files patterns Signed-off-by: liduo --- BUILD.gn | 2 +- README_zh.md | 2 ++ include/cmd/package_parser.h | 1 + include/resource_data.h | 9 ++++++++- include/resource_util.h | 15 +++++++++------ include/restool_errors.h | 1 + src/cmd/cmd_parser.cpp | 36 +++++++++++++++++++----------------- src/cmd/package_parser.cpp | 32 +++++++++++++++++++++++++++++--- src/resconfig_parser.cpp | 2 ++ src/resource_util.cpp | 21 +++++++++++++++++++-- src/restool_errors.cpp | 7 +++++++ 11 files changed, 98 insertions(+), 30 deletions(-) diff --git a/BUILD.gn b/BUILD.gn index e9c1129..ce5fafb 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -73,7 +73,7 @@ ohos_executable("restool") { } else { external_deps = [ "zlib:libz" ] } - + use_exceptions = true cflags = [ "-std=c++17" ] if (is_mingw) { ldflags = [ diff --git a/README_zh.md b/README_zh.md index 36db009..9e921cf 100644 --- a/README_zh.md +++ b/README_zh.md @@ -47,6 +47,8 @@ restool(资源编译工具)是一种资源构建工具。通过编译资源 | --dependEntry | 可缺省 | 带参数 | FA模型下单独编译feature时,指定entry模块的编译结果目录。 | | --icon-check | 可缺省 | 不带参数 | 开启icon和startWindowIcon的PNG图片校验功能。 | | --target-config | 可缺省 | 带参数 | 与“-i”命令同时使用,支持选择编译。
具体可参考如下**target-config参数说明**。| +| --thread | 可缺省 | 带参数 | 指定资源编译时开启的子线程数量。| +| --ignored-file | 可缺省 | 带参数 | 指定资源编译时文件和文件夹的忽略规则,格式为正则表达式,多个规则之间以“:”分隔。例如:“\\.git:\\.svn”表示忽略名称为“.git”、“.svn”的文件和文件夹。| **target-config参数说明** diff --git a/include/cmd/package_parser.h b/include/cmd/package_parser.h index a1dee33..510b78e 100644 --- a/include/cmd/package_parser.h +++ b/include/cmd/package_parser.h @@ -86,6 +86,7 @@ private: uint32_t ParseTargetConfig(const std::string &argValue); uint32_t AddCompressionPath(const std::string &argValue); uint32_t ParseThread(const std::string &argValue); + uint32_t ParseIgnoreFileRegex(const std::string &argValue); static const struct option CMD_OPTS[]; static const std::string CMD_PARAMS; diff --git a/include/resource_data.h b/include/resource_data.h index 68bd0b1..5056ebb 100644 --- a/include/resource_data.h +++ b/include/resource_data.h @@ -49,13 +49,19 @@ const static std::string SOLUTIONS_ARROW = "> "; const static std::string LONG_PATH_HEAD = "\\\\?\\"; 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 5.1.0.007" }; +static const int8_t RESTOOL_VERSION[VERSION_MAX_LEN] = { "Restool 5.1.0.008" }; const static int32_t TAG_LEN = 4; constexpr static int DEFAULT_POOL_SIZE = 8; static std::set g_resourceSet; static std::set g_hapResourceSet; const static int8_t INVALID_ID = -1; +enum class IgnoreType { + IGNORE_FILE, + IGNORE_DIR, + IGNORE_ALL +}; + enum class KeyType { LANGUAGE = 0, REGION = 1, @@ -148,6 +154,7 @@ enum Option { DEFINED_SYSIDS = 6, COMPRESSED_CONFIG = 7, THREAD = 8, + IGNORED_FILE = 9, STARTID = 'e', FORCEWRITE = 'f', HELP = 'h', diff --git a/include/resource_util.h b/include/resource_util.h index 8f487ec..862dc20 100644 --- a/include/resource_util.h +++ b/include/resource_util.h @@ -266,13 +266,16 @@ public: * @return limit type string */ static std::string KeyTypeToStr(KeyType type); + + /** + * @brief add ignore file regex pattern + * @param regex: the regex pattern + * @param ignoreType: the ignore file type + */ + static void AddIgnoreFileRegex(const std::string ®ex, IgnoreType ignoreType); + private: - enum class IgnoreType { - IGNORE_FILE, - IGNORE_DIR, - IGNORE_ALL - }; - static const std::map IGNORE_FILE_REGEX; + static const std::map DEFAULT_IGNORE_FILE_REGEX; static std::string GetLocaleLimitkey(const KeyParam &KeyParam); static std::string GetDeviceTypeLimitkey(const KeyParam &KeyParam); static std::string GetResolutionLimitkey(const KeyParam &KeyParam); diff --git a/include/restool_errors.h b/include/restool_errors.h index 6e76210..d89f647 100644 --- a/include/restool_errors.h +++ b/include/restool_errors.h @@ -84,6 +84,7 @@ constexpr uint32_t ERR_CODE_RES_HEADER_PATH_EMPTY = 11210023; constexpr uint32_t ERR_CODE_DUMP_MISSING_INPUT = 11210024; constexpr uint32_t ERR_CODE_DUMP_INVALID_INPUT = 11210025; constexpr uint32_t ERR_CODE_INVALID_THREAD_COUNT = 11210026; +constexpr uint32_t ERR_CODE_INVALID_IGNORE_FILE = 11210027; // 11211xxx resource pack error const std::string ERR_TYPE_RESOURCE_PACK = "Resource Pack Error"; diff --git a/src/cmd/cmd_parser.cpp b/src/cmd/cmd_parser.cpp index 1652df7..6ae4ab5 100644 --- a/src/cmd/cmd_parser.cpp +++ b/src/cmd/cmd_parser.cpp @@ -90,28 +90,30 @@ void CmdParser::ShowUseage() "For details about the usage of dump, see '-h'.\n"; std::cout << "\n"; std::cout << "[options]:\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, 0xFFFFFFFF)\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 << " -i/--inputPath Input resource path, can add multiple.\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 names, can add multiple, split by ','(like entry1,entry2,...).\n"; + std::cout << " -j/--json Path of module.json(in Stage model) or onfig.json(in FA model).\n"; + std::cout << " -e/--startId Start id mask, e.g 0x01000000,"; + std::cout << " in the scope [0x01000000, 0x06FFFFFF),[0x08000000, 0xFFFFFFFF).\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 << " --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"; std::cout << " --icon-check Enable the PNG image verification function for icons and startwindows.\n"; std::cout << " --target-config When used with '-i', selective compilation is supported.\n"; - std::cout << " --compressed-config opt-compression.json path.\n"; + std::cout << " --compressed-config Path of opt-compression.json.\n"; + std::cout << " --thread Subthreads count.\n"; + std::cout << " --ignored-file Regular patterns of ignored files, split by ':'(like \\.git:\\.svn).\n"; } } } diff --git a/src/cmd/package_parser.cpp b/src/cmd/package_parser.cpp index 67ad9cd..987c9be 100644 --- a/src/cmd/package_parser.cpp +++ b/src/cmd/package_parser.cpp @@ -14,13 +14,17 @@ */ #include "cmd/package_parser.h" + #include #include +#include +#include + +#include "file_entry.h" #include "resconfig_parser.h" +#include "resource_pack.h" #include "resource_util.h" #include "select_compile_parse.h" -#include "file_entry.h" -#include "resource_pack.h" namespace OHOS { namespace Global { @@ -48,6 +52,7 @@ const struct option PackageParser::CMD_OPTS[] = { { "defined-sysids", required_argument, nullptr, Option::DEFINED_SYSIDS}, { "compressed-config", required_argument, nullptr, Option::COMPRESSED_CONFIG}, { "thread", required_argument, nullptr, Option::THREAD}, + { "ignored-file", required_argument, nullptr, Option::IGNORED_FILE}, { 0, 0, 0, 0}, }; @@ -243,7 +248,7 @@ uint32_t PackageParser::ForceWrite() uint32_t PackageParser::PrintVersion() { - cout << "Info: Restool version= " << RESTOOL_VERSION << endl; + cout << "Info: Restool version = " << RESTOOL_VERSION << endl; exit(RESTOOL_SUCCESS); return RESTOOL_SUCCESS; } @@ -472,6 +477,26 @@ uint32_t PackageParser::ParseThread(const std::string &argValue) return RESTOOL_SUCCESS; } +uint32_t PackageParser::ParseIgnoreFileRegex(const std::string &argValue) +{ + if (argValue.empty()) { + PrintError(GetError(ERR_CODE_INVALID_IGNORE_FILE).FormatCause(argValue.c_str(), "empty value.")); + return RESTOOL_ERROR; + } + std::stringstream in(argValue); + std::string regex; + while (getline(in, regex, ':')) { + try { + std::regex rg(regex); + } catch (std::regex_error err) { + PrintError(GetError(ERR_CODE_INVALID_IGNORE_FILE).FormatCause(regex.c_str(), err.what())); + return RESTOOL_ERROR; + } + ResourceUtil::AddIgnoreFileRegex(regex, IgnoreType::IGNORE_ALL); + } + return RESTOOL_SUCCESS; +} + size_t PackageParser::GetThreadCount() const { return threadCount_; @@ -547,6 +572,7 @@ void PackageParser::InitCommand() handles_.emplace(Option::DEFINED_SYSIDS, bind(&PackageParser::AddSysIdDefined, this, _1)); handles_.emplace(Option::COMPRESSED_CONFIG, bind(&PackageParser::AddCompressionPath, this, _1)); handles_.emplace(Option::THREAD, bind(&PackageParser::ParseThread, this, _1)); + handles_.emplace(Option::IGNORED_FILE, bind(&PackageParser::ParseIgnoreFileRegex, this, _1)); } uint32_t PackageParser::HandleProcess(int c, const string &argValue) diff --git a/src/resconfig_parser.cpp b/src/resconfig_parser.cpp index 478bfe2..bcff7d9 100644 --- a/src/resconfig_parser.cpp +++ b/src/resconfig_parser.cpp @@ -94,6 +94,8 @@ void ResConfigParser::InitFileListCommand(HandleBack callback) Option::COMPRESSED_CONFIG, callback)); fileListHandles_.emplace("thread", bind(&ResConfigParser::GetNumber, this, "thread", _1, Option::THREAD, callback)); + fileListHandles_.emplace("ignoreResourcePattern", bind(&ResConfigParser::GetArray, this, "ignoreResourcePattern", + _1, Option::IGNORED_FILE, callback)); } uint32_t ResConfigParser::GetString(const std::string &nodeName, const cJSON *node, int c, HandleBack callback) diff --git a/src/resource_util.cpp b/src/resource_util.cpp index 70c4250..ee84f39 100644 --- a/src/resource_util.cpp +++ b/src/resource_util.cpp @@ -29,7 +29,7 @@ namespace OHOS { namespace Global { namespace Restool { using namespace std; -const map ResourceUtil::IGNORE_FILE_REGEX = { +const map ResourceUtil::DEFAULT_IGNORE_FILE_REGEX = { { "\\.git", IgnoreType::IGNORE_ALL }, { "\\.svn", IgnoreType::IGNORE_ALL }, { ".+\\.scc", IgnoreType::IGNORE_ALL }, @@ -41,6 +41,7 @@ const map ResourceUtil::IGNORE_FILE_REGEX = { { "thumbs\\.db", IgnoreType::IGNORE_ALL }, { ".+~", IgnoreType::IGNORE_ALL } }; +static std::map userIgnoreFileRegex; static std::mutex fileMutex_; @@ -247,14 +248,25 @@ bool ResourceUtil::CreateDirs(const string &filePath) bool ResourceUtil::IsIgnoreFile(const string &filename, bool isFile) { + map regexs; + std::string regexSources; + if (userIgnoreFileRegex.size() > 0) { + regexs = userIgnoreFileRegex; + regexSources = "user"; + } else { + regexs = DEFAULT_IGNORE_FILE_REGEX; + regexSources = "default"; + } string key = filename; transform(key.begin(), key.end(), key.begin(), ::tolower); - for (const auto &iter : IGNORE_FILE_REGEX) { + for (const auto &iter : regexs) { if ((iter.second == IgnoreType::IGNORE_FILE && !isFile) || (iter.second == IgnoreType::IGNORE_DIR && isFile)) { continue; } if (regex_match(key, regex(iter.first))) { + cout << "Info: file '" << filename << "' is ignored by " << regexSources << " regular pattern '" + << iter.first << "'." << endl; return true; } } @@ -506,6 +518,11 @@ string ResourceUtil::KeyTypeToStr(KeyType type) } return ret; } + +void ResourceUtil::AddIgnoreFileRegex(const std::string ®ex, IgnoreType ignoreType) +{ + userIgnoreFileRegex[regex] = ignoreType; +} } } } diff --git a/src/restool_errors.cpp b/src/restool_errors.cpp index 236707b..b306f2d 100644 --- a/src/restool_errors.cpp +++ b/src/restool_errors.cpp @@ -307,6 +307,13 @@ const std::map ERRORS_MAP = { "", {}, {} } }, + { ERR_CODE_INVALID_IGNORE_FILE, + { ERR_CODE_INVALID_IGNORE_FILE, + ERR_TYPE_COMMAND_PARSE, + "Invalid ignore file pattern '%s', %s", + "", + { "Make sure the argument of the option --ignored-file is not empty and contains valid regular expressions." }, + {} } }, // 11211xxx { ERR_CODE_OUTPUT_EXIST,