!203 重构dump命令解析

Merge pull request !203 from sunjie/dev
This commit is contained in:
openharmony_ci
2025-01-08 05:49:50 +00:00
committed by Gitee
14 changed files with 230 additions and 163 deletions
+1
View File
@@ -19,6 +19,7 @@ ohos_executable("restool") {
sources = [
"src/append_compiler.cpp",
"src/binary_file_packer.cpp",
"src/cmd/cmd_parser.cpp",
"src/cmd/dump_parser.cpp",
"src/cmd/package_parser.cpp",
"src/compression_parser.cpp",
+20 -61
View File
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2024 - 2024 Huawei Device Co., Ltd.
* Copyright (c) 2024-2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
@@ -21,6 +21,8 @@
#include <iostream>
#include <string>
#include <type_traits>
#include <vector>
#include "cmd/package_parser.h"
#include "singleton.h"
#include "restool_errors.h"
@@ -28,73 +30,30 @@ namespace OHOS {
namespace Global {
namespace Restool {
class ICmdParser {
class CmdParserBase {
public:
virtual uint32_t Parse(int argc, char *argv[]) = 0;
CmdParserBase(const std::string &name);
virtual ~CmdParserBase() = default;
virtual uint32_t Parse(int argc, char *argv[], int currentIndex);
virtual uint32_t ParseOption(int argc, char *argv[], int currentIndex);
virtual uint32_t ExecCommand() = 0;
virtual void ShowUseage() = 0;
protected:
std::vector<std::unique_ptr<CmdParserBase>> subcommands_;
const std::string name_;
bool showUseage_{true};
};
template<class T>
class CmdParser : public Singleton<CmdParser<T>> {
static_assert(std::is_base_of_v<ICmdParser, T>, "Template T must inherit ICmdParser.");
class CmdParser : public CmdParserBase, public Singleton<CmdParser> {
public:
T &GetCmdParser();
uint32_t Parse(int argc, char *argv[]);
uint32_t ExecCommand();
static void ShowUseage();
CmdParser();
uint32_t ParseOption(int argc, char *argv[], int currentIndex) override;
uint32_t ExecCommand() override;
void ShowUseage() override;
PackageParser &GetPackageParser();
private:
T cmdParser_;
};
template<class T>
void CmdParser<T>::ShowUseage()
{
std::cout << "This is an OHOS Packaging Tool.\n";
std::cout << "Usage:\n";
std::cout << TOOL_NAME << " [arguments] Package the OHOS resources.\n";
std::cout << "[arguments]:\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 << " 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";
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 << " dump [config] Dump rsource in hap to json. If with 'config', will only dump limit path.\n";
}
template<class T>
T &CmdParser<T>::GetCmdParser()
{
return cmdParser_;
}
template<class T>
uint32_t CmdParser<T>::Parse(int argc, char *argv[])
{
return cmdParser_.Parse(argc, argv);
}
template<class T>
uint32_t CmdParser<T>::ExecCommand()
{
return cmdParser_.ExecCommand();
PackageParser packageParser_;
};
}
}
+20 -8
View File
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2024 - 2024 Huawei Device Co., Ltd.
* Copyright (c) 2024-2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
@@ -16,23 +16,35 @@
#ifndef OHOS_RESTOOL_DUMP_PARSER_H
#define OHOS_RESTOOL_DUMP_PARSER_H
#include "cmd_parser.h"
#include <memory>
#include <string>
#include "cmd_parser.h"
namespace OHOS {
namespace Global {
namespace Restool {
class DumpParser : public ICmdParser {
class DumpParserBase : public virtual CmdParserBase {
public:
virtual ~DumpParser() = default;
uint32_t Parse(int argc, char *argv[]) override;
uint32_t ExecCommand() override;
virtual ~DumpParserBase() = default;
uint32_t ParseOption(int argc, char *argv[], int currentIndex) override;
void ShowUseage() override;
const std::string &GetInputPath() const;
private:
protected:
std::string inputPath_;
std::string type_;
};
class DumpParser : public virtual DumpParserBase {
public:
DumpParser();
uint32_t ExecCommand() override;
};
class DumpConfigParser : public virtual DumpParserBase {
public:
DumpConfigParser();
virtual ~DumpConfigParser() = default;
uint32_t ExecCommand() override;
};
} // namespace Restool
} // namespace Global
+8 -5
View File
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2024 - 2024 Huawei Device Co., Ltd.
* Copyright (c) 2024-2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
@@ -18,17 +18,20 @@
#include <functional>
#include <getopt.h>
#include "cmd_parser.h"
#include <string>
#include <iostream>
#include "resource_data.h"
#include "restool_errors.h"
namespace OHOS {
namespace Global {
namespace Restool {
class PackageParser : public ICmdParser {
class PackageParser {
public:
PackageParser(){};
virtual ~PackageParser() = default;
uint32_t Parse(int argc, char *argv[]) override;
uint32_t ExecCommand() override;
uint32_t Parse(int argc, char *argv[]);
uint32_t ExecCommand();
const std::vector<std::string> &GetInputs() const;
const std::string &GetPackageName() const;
const std::string &GetOutput() const;
+2 -7
View File
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2024 - 2024 Huawei Device Co., Ltd.
* Copyright (c) 2024-2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
@@ -36,7 +36,7 @@ namespace Restool {
class ResourceDumper {
public:
virtual ~ResourceDumper() = default;
virtual uint32_t Dump(const DumpParser &parser);
virtual uint32_t Dump(const DumpParserBase &parser);
protected:
virtual uint32_t DumpRes(std::string &out) const = 0;
void ReadHapInfo(const std::unique_ptr<char[]> &buffer, size_t len);
@@ -69,11 +69,6 @@ private:
uint32_t AddItemCommonPropToJson(int32_t resId, const ResourceItem &item, cJSON* json) const;
};
class ResourceDumperFactory {
public:
static std::unique_ptr<ResourceDumper> CreateResourceDumper(const std::string &type);
static const std::set<std::string> GetSupportDumpType();
};
} // namespace Restool
} // namespace Global
} // namespace OHOS
+2 -2
View File
@@ -20,7 +20,7 @@
#include <vector>
#include "config_parser.h"
#include "restool_errors.h"
#include "cmd/package_parser.h"
#include "cmd/cmd_parser.h"
namespace OHOS {
namespace Global {
@@ -29,7 +29,7 @@ class ResourceMerge {
public:
ResourceMerge();
virtual ~ResourceMerge();
uint32_t Init(const PackageParser &packageParser = CmdParser<PackageParser>::GetInstance().GetCmdParser());
uint32_t Init(const PackageParser &packageParser = CmdParser::GetInstance().GetPackageParser());
const std::vector<std::string> &GetInputs() const;
private:
std::vector<std::string> inputsOrder_;
+118
View File
@@ -0,0 +1,118 @@
/*
* Copyright (c) 2024-2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "cmd/cmd_parser.h"
#include <cstdint>
#include <cstring>
#include <memory>
#include "cmd/dump_parser.h"
#include "cmd/package_parser.h"
#include "restool_errors.h"
namespace OHOS {
namespace Global {
namespace Restool {
CmdParserBase::CmdParserBase(const std::string &name) : name_(name)
{}
uint32_t CmdParserBase::Parse(int argc, char *argv[], int currentIndex)
{
if (currentIndex < argc) {
for (const auto &subcommand : subcommands_) {
if (subcommand->name_ == argv[currentIndex]) {
return subcommand->Parse(argc, argv, currentIndex + 1);
};
}
if (std::strcmp(argv[currentIndex], "-h") == 0) {
ShowUseage();
return RESTOOL_SUCCESS;
}
}
uint32_t errorCode = ParseOption(argc, argv, currentIndex);
if (errorCode != RESTOOL_SUCCESS) {
if (showUseage_) {
ShowUseage();
}
return errorCode;
}
return ExecCommand();
}
uint32_t CmdParserBase::ParseOption(int argc, char *argv[], int currentIndex)
{
return RESTOOL_SUCCESS;
}
CmdParser::CmdParser() : CmdParserBase("")
{
subcommands_.emplace_back(std::make_unique<DumpParser>());
}
uint32_t CmdParser::ParseOption(int argc, char *argv[], int currentIndex)
{
if (currentIndex >= argc) {
return RESTOOL_ERROR;
}
return packageParser_.Parse(argc, argv);
}
uint32_t CmdParser::ExecCommand()
{
return packageParser_.ExecCommand();
}
PackageParser &CmdParser::GetPackageParser()
{
return packageParser_;
}
void CmdParser::ShowUseage()
{
std::cout << "This is an OHOS Packaging Tool.\n";
std::cout << "Usage:\n";
std::cout << TOOL_NAME << " [subcommand] files...\n";
std::cout << TOOL_NAME << " [option1] [option2] [options] files...\n";
std::cout << "[subcommands]:\n";
std::cout << " dump Print the contents of the resource in the hap."
"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 << " 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";
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";
}
}
}
}
+32 -21
View File
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2024 - 2024 Huawei Device Co., Ltd.
* Copyright (c) 2024-2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
@@ -14,6 +14,9 @@
*/
#include "cmd/dump_parser.h"
#include <memory>
#include <string>
#include "cmd/cmd_parser.h"
#include "resource_util.h"
#include "resource_dumper.h"
@@ -21,21 +24,10 @@ namespace OHOS {
namespace Global {
namespace Restool {
using namespace std;
uint32_t DumpParser::Parse(int argc, char *argv[])
uint32_t DumpParserBase::ParseOption(int argc, char *argv[], int currentIndex)
{
// argv[0] is restool path, argv[1] is 'dump' subcommand.
int currentIndex = 2;
if (currentIndex >= argc) {
cerr << "Error: missing input Hap path." << endl;
return RESTOOL_ERROR;
}
const set<string> supportedDumpType = ResourceDumperFactory::GetSupportDumpType();
if (supportedDumpType.count(argv[currentIndex]) != 0) {
type_ = argv[currentIndex];
currentIndex++;
}
if (currentIndex >= argc) {
cerr << "Error: missing input Hap path." << endl;
cerr << "Error: missing input path." << endl;
return RESTOOL_ERROR;
}
inputPath_ = ResourceUtil::RealPath(argv[currentIndex]);
@@ -46,19 +38,38 @@ uint32_t DumpParser::Parse(int argc, char *argv[])
return RESTOOL_SUCCESS;
}
const string& DumpParser::GetInputPath() const
const string& DumpParserBase::GetInputPath() const
{
return inputPath_;
}
DumpParser::DumpParser() : CmdParserBase("dump")
{
subcommands_.emplace_back(std::make_unique<DumpConfigParser>());
}
uint32_t DumpParser::ExecCommand()
{
auto resourceDump = ResourceDumperFactory::CreateResourceDumper(type_);
if (resourceDump) {
return resourceDump->Dump(*this);
}
cerr << "Error: not support dump type '" << type_ << "'" << endl;
return RESTOOL_ERROR;
return CommonDumper().Dump(*this);
}
void DumpParserBase::ShowUseage()
{
std::cout << "Usage:\n";
std::cout << "restool dump [subcommand] [options] file.\n";
std::cout << "[subcommands]:\n";
std::cout << " config Print config of the resource in the hap.\n";
std::cout << "\n";
std::cout << "[options]:\n";
std::cout << " -h Print dump subcommand help info.\n";
}
DumpConfigParser::DumpConfigParser() : CmdParserBase("config")
{}
uint32_t DumpConfigParser::ExecCommand()
{
return ConfigDumper().Dump(*this);
}
} // namespace Restool
} // namespace Global
+1 -1
View File
@@ -357,7 +357,7 @@ uint32_t PackageParser::AddDependEntry(const string& argValue)
uint32_t PackageParser::ShowHelp() const
{
auto &parser = CmdParser<PackageParser>::GetInstance();
auto &parser = CmdParser::GetInstance();
parser.ShowUseage();
exit(RESTOOL_SUCCESS);
return RESTOOL_SUCCESS;
+3 -2
View File
@@ -16,6 +16,7 @@
#include "id_worker.h"
#include <iostream>
#include <regex>
#include "cmd/cmd_parser.h"
namespace OHOS {
namespace Global {
@@ -24,8 +25,8 @@ using namespace std;
uint32_t IdWorker::Init(ResourceIdCluster &type, int64_t startId)
{
type_ = type;
CmdParser<PackageParser> &parser = CmdParser<PackageParser>::GetInstance();
PackageParser &packageParser = parser.GetCmdParser();
CmdParser &parser = CmdParser::GetInstance();
PackageParser &packageParser = parser.GetPackageParser();
IdDefinedParser idDefinedParser = IdDefinedParser(packageParser, type_);
if (idDefinedParser.Init() != RESTOOL_SUCCESS) {
return RESTOOL_ERROR;
+9 -25
View File
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2024 - 2024 Huawei Device Co., Ltd.
* Copyright (c) 2024-2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
@@ -39,7 +39,7 @@ static std::map<std::string, std::function<std::unique_ptr<ResourceDumper>()>> d
{"config", std::make_unique<ConfigDumper>}
};
uint32_t ResourceDumper::Dump(const DumpParser &packageParser)
uint32_t ResourceDumper::Dump(const DumpParserBase &packageParser)
{
inputPath_ = packageParser.GetInputPath();
if (LoadHap() != RESTOOL_SUCCESS) {
@@ -64,6 +64,8 @@ uint32_t ResourceDumper::LoadHap()
size_t len;
if (ReadFileFromZip(zipFile, "module.json", buffer, len) == RESTOOL_SUCCESS) {
ReadHapInfo(buffer, len);
} else if (ReadFileFromZip(zipFile, "config.json", buffer, len) == RESTOOL_SUCCESS) {
ReadHapInfo(buffer, len);
}
if (ReadFileFromZip(zipFile, "resources.index", buffer, len) != RESTOOL_SUCCESS) {
unzClose(zipFile);
@@ -80,12 +82,12 @@ uint32_t ResourceDumper::LoadHap()
void ResourceDumper::ReadHapInfo(const std::unique_ptr<char[]> &buffer, size_t len)
{
cJSON *module = cJSON_Parse(buffer.get());
if (!module) {
cJSON *config = cJSON_Parse(buffer.get());
if (!config) {
return;
}
cJSON *appConfig = cJSON_GetObjectItemCaseSensitive(module, "app");
cJSON *moduleConfig = cJSON_GetObjectItemCaseSensitive(module, "module");
cJSON *appConfig = cJSON_GetObjectItemCaseSensitive(config, "app");
cJSON *moduleConfig = cJSON_GetObjectItemCaseSensitive(config, "module");
if (appConfig) {
cJSON *bundleName = cJSON_GetObjectItemCaseSensitive(appConfig, "bundleName");
if (cJSON_IsString(bundleName) && bundleName->valuestring) {
@@ -98,7 +100,7 @@ void ResourceDumper::ReadHapInfo(const std::unique_ptr<char[]> &buffer, size_t l
moduleName_ = moduleName->valuestring;
}
}
cJSON_Delete(module);
cJSON_Delete(config);
}
uint32_t ResourceDumper::ReadFileFromZip(
@@ -385,24 +387,6 @@ uint32_t ConfigDumper::DumpRes(std::string &out) const
rawStr = nullptr;
return RESTOOL_SUCCESS;
}
std::unique_ptr<ResourceDumper> ResourceDumperFactory::CreateResourceDumper(const std::string &type)
{
auto it = dumpMap_.find(type);
if (it != dumpMap_.end()) {
return it->second();
}
return std::make_unique<CommonDumper>();
}
const std::set<std::string> ResourceDumperFactory::GetSupportDumpType()
{
std::set<std::string> dumpType;
for (auto &it : dumpMap_) {
dumpType.insert(it.first);
}
return dumpType;
}
}
}
}
+3 -3
View File
@@ -16,7 +16,7 @@
#include "resource_table.h"
#include <cJSON.h>
#include <cstdint>
#include "cmd/package_parser.h"
#include "cmd/cmd_parser.h"
#include "file_entry.h"
#include "file_manager.h"
#include "resource_util.h"
@@ -28,8 +28,8 @@ namespace Restool {
using namespace std;
ResourceTable::ResourceTable()
{
auto &parser = CmdParser<PackageParser>::GetInstance();
auto &packageParser = parser.GetCmdParser();
auto &parser = CmdParser::GetInstance();
auto &packageParser = parser.GetPackageParser();
if (!packageParser.GetIdDefinedOutput().empty()) {
idDefinedPath_ = FileEntry::FilePath(packageParser.GetIdDefinedOutput()).Append(ID_DEFINED_FILE).GetPath();
}
+9 -26
View File
@@ -13,40 +13,23 @@
* limitations under the License.
*/
#include <cstring>
#include "cmd/package_parser.h"
#include "cmd/dump_parser.h"
#include "resource_pack.h"
#include "compression_parser.h"
#include "cmd/cmd_parser.h"
#ifdef _WIN32
#include <windows.h>
#endif
using namespace std;
using namespace OHOS::Global::Restool;
constexpr int PARAM_MIN_NUM = 2;
int main(int argc, char *argv[])
{
#ifdef _WIN32
SetConsoleOutputCP(CP_UTF8);
#endif
if (argv == nullptr) {
cerr << "Error: argv null" << endl;
return RESTOOL_ERROR;
}
if (argc < PARAM_MIN_NUM) {
cerr << "Error: At least 1 parameters are required, but no parameter is passed in." << endl;
return RESTOOL_ERROR;
}
if (strcmp(argv[1], "dump") == 0) {
auto &dumpParser = CmdParser<DumpParser>::GetInstance();
if (dumpParser.Parse(argc, argv) != RESTOOL_SUCCESS) {
dumpParser.ShowUseage();
return RESTOOL_ERROR;
}
return dumpParser.ExecCommand();
}
auto &packParser = CmdParser<PackageParser>::GetInstance();
if (packParser.Parse(argc, argv) != RESTOOL_SUCCESS) {
packParser.ShowUseage();
return RESTOOL_ERROR;
}
return packParser.ExecCommand();
auto &parser = CmdParser::GetInstance();
return parser.Parse(argc, argv, 1);
}
+2 -2
View File
@@ -16,7 +16,7 @@
#include "select_compile_parse.h"
#include <algorithm>
#include "key_parser.h"
#include "cmd/package_parser.h"
#include "cmd/cmd_parser.h"
#include "resource_util.h"
namespace OHOS {
@@ -65,7 +65,7 @@ bool SelectCompileParse::IsSelectCompile(vector<KeyParam> &keyParams)
if (keyParams.empty()) {
return true;
}
auto &cmdParser = CmdParser<PackageParser>::GetInstance().GetCmdParser();
auto &cmdParser = CmdParser::GetInstance().GetPackageParser();
bool isTtargetConfig = cmdParser.IsTargetConfig();
if (!isTtargetConfig) {
return true;