diff --git a/ohos.build b/ohos.build index 6cf9af0cd1b87b996afbf2b30ef8f48d85018313..2ff2799a410883537cf2065f5add1c69744990ee 100644 --- a/ohos.build +++ b/ohos.build @@ -112,7 +112,7 @@ "module_list": [ "//foundation/appexecfwk/standard/common:common_target", "//foundation/appexecfwk/standard/services:services_target", - "//foundation/appexecfwk/standard/tools:tools_target", + "//foundation/appexecfwk/standard/tools/fm:tools_fm", "//foundation/appexecfwk/standard/interfaces/innerkits:innerkits_target", "//foundation/appexecfwk/standard/kits:app_context", "//foundation/appexecfwk/standard/kits:appkit_native", @@ -132,9 +132,6 @@ "//foundation/appexecfwk/standard/services/appmgr/test:unittest", "//foundation/appexecfwk/standard/services/formmgr/test:unittest", "//foundation/appexecfwk/standard/test/systemtest:systemtest", - "//foundation/appexecfwk/standard/tools/test:moduletest", - "//foundation/appexecfwk/standard/tools/test:systemtest", - "//foundation/appexecfwk/standard/tools/test:unittest", "//foundation/appexecfwk/standard/libs/test:moduletest", "//foundation/appexecfwk/standard/interfaces/innerkits/task_dispatcher/test:unittest", "//foundation/appexecfwk/standard/interfaces/innerkits/test:moduletest" diff --git a/tools/bm/BUILD.gn b/tools/bm/BUILD.gn index b59386f6f8dd6bf00da46e30eabeed4777792616..4737cd6258deb199a268a3515e23c06bc1d5222d 100644 --- a/tools/bm/BUILD.gn +++ b/tools/bm/BUILD.gn @@ -16,56 +16,43 @@ import("//foundation/appexecfwk/standard/appexecfwk.gni") config("tools_bm_config") { include_dirs = [ - "${aafwk_path}/interfaces/innerkits/want/include", - "${aafwk_path}/interfaces/innerkits/base/include", - "${aafwk_path}/interfaces/innerkits/want/include/ohos/aafwk/content", - "${aafwk_path}/services/abilitymgr/include", - "${aafwk_path}/services/common/include", - "${aafwk_path}/tools/aa/include", - "${appexecfwk_path}/common/log/include", - "${appexecfwk_path}/interfaces/innerkits/libeventhandler/include", - "//foundation/distributedschedule/samgr/interfaces/innerkits/samgr_proxy/include", - "//foundation/distributedschedule/dmsfwk/services/dtbschedmgr/include", - "//foundation/appexecfwk/standard/kits/appkit/napi/bundlemgr", - "//utils/native/base/include", "include", + "${common_path}/log/include", + "${kits_path}/appkit/napi/bundlemgr", + "//utils/system/safwk/native/include", ] - configs = [ "${appexecfwk_path}/services/bundlemgr:bundlemgr_common_config" ] + defines = [ + "APP_LOG_TAG = \"BundleTool\"", + "LOG_DOMAIN = 0xD001120", + ] } ohos_source_set("tools_bm_source_set") { sources = [ - "${aafwk_path}/tools/aa/src/shell_command.cpp", - "${appexecfwk_path}/kits/appkit/napi/bundlemgr/bundle_death_recipient.cpp", + "${common_path}/log/src/app_log_wrapper.cpp", + "${kits_path}/appkit/napi/bundlemgr/bundle_death_recipient.cpp", "src/bundle_command.cpp", "src/main.cpp", + "src/shell_command.cpp", "src/status_receiver_impl.cpp", ] - public_configs = [ - "${aafwk_path}/tools/aa:ability_command_config", - ":tools_bm_config", - ] + public_configs = [ ":tools_bm_config" ] cflags = [] if (target_cpu == "arm") { cflags += [ "-DBINDER_IPC_32BIT" ] } - deps = [ - "${aafwk_path}/interfaces/innerkits/want:want", - "${aafwk_path}/services/abilitymgr:abilityms", - "${appexecfwk_path}/common:libappexecfwk_common", - "${appexecfwk_path}/interfaces/innerkits/appexecfwk_base:appexecfwk_base", - "${appexecfwk_path}/interfaces/innerkits/appexecfwk_core:appexecfwk_core", - "//foundation/distributedschedule/samgr/interfaces/innerkits/samgr_proxy:samgr_proxy", - "//utils/native/base:utils", - ] - external_deps = [ + "aafwk_standard:want", + "appexecfwk_standard:appexecfwk_base", + "appexecfwk_standard:appexecfwk_core", "hiviewdfx_hilog_native:libhilog", "ipc:ipc_core", + "samgr_standard:samgr_proxy", + "utils_base:utils", ] } @@ -75,7 +62,7 @@ ohos_executable("bm") { install_enable = true subsystem_name = "appexecfwk" - part_name = "appexecfwk_standard" + part_name = "bundle_tool" } group("tools_bm") { diff --git a/tools/bm/bundle.json b/tools/bm/bundle.json new file mode 100644 index 0000000000000000000000000000000000000000..b329b0b35de3f7afefcb48a9088c8537719a0a82 --- /dev/null +++ b/tools/bm/bundle.json @@ -0,0 +1,39 @@ +{ + "name": "@ohos/bundle_tool", + "description": "包管理命令行工具:提供命令行中执行hap包的安装、更新、卸载及信息查询的能力", + "version": "3.1", + "license": "Apache 2.0", + "component": { + "name": "bundle_tool", + "subsystem": "appexecfwk", + "syscap": [], + "features": [], + "adapted_system_type": [ + "standard" + ], + "rom": "100KB", + "ram": "100KB", + "deps": { + "components": [ + "aafwk_standard", + "appexecfwk_standard", + "hiviewdfx_hilog_native", + "ipc", + "samgr_standard", + "utils_base" + ], + "third_party": [] + }, + "build": { + "sub_component": [ + "//foundation/appexecfwk/standard/tools/bm:tools_bm" + ], + "inner_kits": [], + "test": [ + "//foundation/appexecfwk/standard/tools/test:moduletest", + "//foundation/appexecfwk/standard/tools/test:systemtest", + "//foundation/appexecfwk/standard/tools/test:unittest" + ] + } + } +} \ No newline at end of file diff --git a/tools/bm/include/bundle_command.h b/tools/bm/include/bundle_command.h index ac66a450ee255a7751ff2fae54cae696a95ba9d3..27a1d468c7d3f21d814df46f57b889055c4c92eb 100644 --- a/tools/bm/include/bundle_command.h +++ b/tools/bm/include/bundle_command.h @@ -121,7 +121,7 @@ const std::string STRING_RECOVER_BUNDLE_OK = "recover bundle successfully."; const std::string STRING_RECOVER_BUNDLE_NG = "error: failed to recover bundle."; } // namespace -class BundleManagerShellCommand : public OHOS::AAFwk::ShellCommand { +class BundleManagerShellCommand : public ShellCommand { public: BundleManagerShellCommand(int argc, char *argv[]); ~BundleManagerShellCommand() override diff --git a/tools/bm/include/shell_command.h b/tools/bm/include/shell_command.h new file mode 100644 index 0000000000000000000000000000000000000000..375aaeadc306714cf6429374cfcc2b72f1596a11 --- /dev/null +++ b/tools/bm/include/shell_command.h @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2021 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. + */ + +#ifndef FOUNDATION_APPEXECFWK_STANDARD_TOOLS_BM_INCLUDE_SHELL_COMMAND_H +#define FOUNDATION_APPEXECFWK_STANDARD_TOOLS_BM_INCLUDE_SHELL_COMMAND_H + +#include +#include +#include +#include + +#include "utils/native/base/include/errors.h" + +namespace OHOS { +namespace AppExecFwk { +namespace { +const std::string HELP_MSG_NO_OPTION = "error: you must specify an option at least."; + +const int OFFSET_REQUIRED_ARGUMENT = 2; +} // namespace + +class ShellCommand { +public: + ShellCommand(int argc, char *argv[], std::string name); + virtual ~ShellCommand(); + + ErrCode OnCommand(); + std::string ExecCommand(); + std::string GetCommandErrorMsg() const; + std::string GetUnknownOptionMsg(std::string &unknownOption) const; + std::string GetMessageFromCode(const int32_t code) const; + + virtual ErrCode CreateCommandMap() = 0; + virtual ErrCode CreateMessageMap() = 0; + virtual ErrCode init() = 0; + +protected: + static constexpr int MIN_ARGUMENT_NUMBER = 2; + + int argc_; + char **argv_; + + std::string cmd_; + std::vector argList_; + + std::string name_; + std::map> commandMap_; + std::map messageMap_; + + std::string resultReceiver_ = ""; +}; + +} // namespace AppExecFwk +} // namespace OHOS + +#endif // FOUNDATION_APPEXECFWK_STANDARD_TOOLS_BM_INCLUDE_SHELL_COMMAND_H \ No newline at end of file diff --git a/tools/bm/src/bundle_command.cpp b/tools/bm/src/bundle_command.cpp index 126888d5353d577ede5188fb2b4b73aafbe804da..8e54e5d1137b5b76c251c21c078c87327b6ba8d1 100644 --- a/tools/bm/src/bundle_command.cpp +++ b/tools/bm/src/bundle_command.cpp @@ -29,8 +29,6 @@ #include "status_receiver_impl.h" #include "system_ability_definition.h" -using namespace OHOS::AAFwk; - namespace OHOS { namespace AppExecFwk { namespace { diff --git a/tools/bm/src/shell_command.cpp b/tools/bm/src/shell_command.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c129718e0ed71f69a522cf2cec99ce1d26aebc07 --- /dev/null +++ b/tools/bm/src/shell_command.cpp @@ -0,0 +1,127 @@ +/* + * Copyright (c) 2021 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 "shell_command.h" + +#include +#include "app_log_wrapper.h" + +namespace OHOS { +namespace AppExecFwk { + +ShellCommand::ShellCommand(int argc, char *argv[], std::string name) +{ + opterr = 0; + argc_ = argc; + argv_ = argv; + name_ = name; + + if (argc < MIN_ARGUMENT_NUMBER) { + cmd_ = "help"; + return; + } + cmd_ = argv[1]; + for (int i = 2; i < argc; i++) { + argList_.push_back(argv[i]); + } +} + +ShellCommand::~ShellCommand() +{} + +ErrCode ShellCommand::OnCommand() +{ + int result = OHOS::ERR_OK; + + auto respond = commandMap_[cmd_]; + if (respond == nullptr) { + resultReceiver_.append(GetCommandErrorMsg()); + respond = commandMap_["help"]; + } + + if (init() == OHOS::ERR_OK) { + respond(); + } else { + result = OHOS::ERR_INVALID_VALUE; + } + + return result; +} + +std::string ShellCommand::ExecCommand() +{ + int result = CreateCommandMap(); + if (result != OHOS::ERR_OK) { + APP_LOGE("failed to create command map.\n"); + } + + result = CreateMessageMap(); + if (result != OHOS::ERR_OK) { + APP_LOGE("failed to create message map.\n"); + } + + result = OnCommand(); + if (result != OHOS::ERR_OK) { + APP_LOGE("failed to execute your command.\n"); + + resultReceiver_ = "error: failed to execute your command.\n"; + } + + return resultReceiver_; +} + +std::string ShellCommand::GetCommandErrorMsg() const +{ + std::string commandErrorMsg = + name_ + ": '" + cmd_ + "' is not a valid " + name_ + " command. See '" + name_ + " help'.\n"; + + return commandErrorMsg; +} + +std::string ShellCommand::GetUnknownOptionMsg(std::string &unknownOption) const +{ + std::string result = ""; + + if (optind < 0 || optind > argc_) { + return result; + } + + result.append("error: unknown option"); + result.append(".\n"); + + return result; +} + +std::string ShellCommand::GetMessageFromCode(const int32_t code) const +{ + APP_LOGI("[%{public}s(%{public}s)] enter", __FILE__, __FUNCTION__); + APP_LOGI("code = %{public}d", code); + + std::string result = ""; + if (messageMap_.find(code) != messageMap_.end()) { + std::string message = messageMap_.at(code); + + if (message.size() != 0) { + result.append(message + "\n"); + } + } + + APP_LOGI("result = %{public}s", result.c_str()); + + return result; +} + +} // namespace AppExecFwk +} // namespace OHOS \ No newline at end of file