1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "shell_command.h"
17
18 #include <getopt.h>
19 #include <iostream>
20 #include <memory>
21 #include <string>
22
23 #include "app_packager.h"
24 #include "appqf_packager.h"
25 #include "constants.h"
26 #include "fast_app_packager.h"
27 #include "hap_packager.h"
28 #include "hqf_packager.h"
29 #include "hsp_packager.h"
30 #include "log.h"
31 #include "multiapp_packager.h"
32 #include "packager.h"
33 #include "package_normalize.h"
34 #include "res_packager.h"
35 #include "version_normalize.h"
36
37 namespace OHOS {
38 namespace AppPackingTool {
ShellCommand(int argc,char * argv[],std::string name)39 ShellCommand::ShellCommand(int argc, char *argv[], std::string name)
40 {
41 opterr = 0;
42 argc_ = argc;
43 argv_ = argv;
44 name_ = name;
45
46 if (argc < MIN_ARGUMENT_NUMBER) {
47 cmd_ = Constants::CMD_HELP;
48 return;
49 }
50 cmd_ = argv[1];
51 }
52
~ShellCommand()53 ShellCommand::~ShellCommand()
54 {}
55
CreateCommandMap()56 int32_t ShellCommand::CreateCommandMap()
57 {
58 commandMap_ = {
59 {"help", std::bind(&ShellCommand::RunAsHelpCommand, this)},
60 {"pack", std::bind(&ShellCommand::RunAsPackCommand, this)},
61 {"unpack", std::bind(&ShellCommand::RunAsUnpackCommand, this)},
62 };
63 return ERR_OK;
64 }
65
ParseParam()66 int32_t ShellCommand::ParseParam()
67 {
68 int n = 0;
69 while (n < Constants::OPTIONS_SIZE) {
70 int32_t option = getopt_long(argc_, argv_, Constants::SHORT_OPTIONS, Constants::LONG_OPTIONS, nullptr);
71 if (optind < 0 || optind > argc_) {
72 return ERR_INVALID_VALUE;
73 }
74 if (option < 0) {
75 break;
76 } else if (option == '?') {
77 resultReceiver_.append("not support param: ").append(argv_[optind - 1]).append("\n");
78 return ERR_INVALID_VALUE;
79 } else {
80 // example: --mode hap
81 std::string paramName = argv_[optind - OFFSET_REQUIRED_ARGUMENT];
82 paramName = paramName.substr(Constants::PARAM_PREFIX.length());
83 std::string paramValue = optarg;
84 parameterMap_[paramName] = optarg;
85 }
86 n++;
87 }
88 return ERR_OK;
89 }
90
OnCommand()91 int32_t ShellCommand::OnCommand()
92 {
93 auto respond = commandMap_[cmd_];
94 if (respond == nullptr) {
95 resultReceiver_.append("not support command: ").append(cmd_).append("\n");
96 respond = commandMap_[Constants::CMD_HELP];
97 respond();
98 return ERR_INVALID_VALUE;
99 }
100 return respond();
101 }
102
ExecCommand(int32_t & ret)103 std::string ShellCommand::ExecCommand(int32_t& ret)
104 {
105 int32_t result = CreateCommandMap();
106 if (result != ERR_OK) {
107 resultReceiver_.append("failed to create command map.\n");
108 ret = ERR_INVALID_VALUE;
109 return resultReceiver_;
110 }
111 result = ParseParam();
112 if (result != ERR_OK) {
113 resultReceiver_.append("failed to init parameter map.\n");
114 ret = ERR_INVALID_VALUE;
115 return resultReceiver_;
116 }
117
118 result = OnCommand();
119 if (result != ERR_OK) {
120 resultReceiver_.append("failed to execute your command.\n");
121 ret = ERR_INVALID_VALUE;
122 return resultReceiver_;
123 }
124 ret = ERR_OK;
125 return resultReceiver_;
126 }
127
RunAsHelpCommand()128 int32_t ShellCommand::RunAsHelpCommand()
129 {
130 resultReceiver_.append(HELP_MSG);
131 return ERR_OK;
132 }
133
RunAsPackCommand()134 int32_t ShellCommand::RunAsPackCommand()
135 {
136 LOGI("RunAsPackCommand ");
137 std::unique_ptr<Packager> packager = getPackager();
138 if (packager != nullptr) {
139 return packager->MakePackage();
140 }
141 return ERR_INVALID_VALUE;
142 }
143
RunAsUnpackCommand()144 int32_t ShellCommand::RunAsUnpackCommand()
145 {
146 LOGI("RunAsUnpackCommand ");
147 return ERR_OK;
148 }
149
getPackager()150 std::unique_ptr<Packager> ShellCommand::getPackager()
151 {
152 std::string mode = parameterMap_[Constants::PARAM_MODE];
153 if (mode == Constants::MODE_HAP) {
154 std::unique_ptr<Packager> packager =
155 std::make_unique<HapPackager>(parameterMap_, resultReceiver_);
156 return packager;
157 } else if (mode == Constants::MODE_HSP) {
158 std::unique_ptr<Packager> packager =
159 std::make_unique<HspPackager>(parameterMap_, resultReceiver_);
160 return packager;
161 } else if (mode == Constants::MODE_APP) {
162 std::unique_ptr<Packager> packager =
163 std::make_unique<AppPackager>(parameterMap_, resultReceiver_);
164 return packager;
165 } else if (mode == Constants::MODE_MULTIAPP) {
166 std::unique_ptr<Packager> packager =
167 std::make_unique<MultiAppPackager>(parameterMap_, resultReceiver_);
168 return packager;
169 } else if (mode == Constants::MODE_FAST_APP) {
170 std::unique_ptr<Packager> packager =
171 std::make_unique<FastAppPackager>(parameterMap_, resultReceiver_);
172 return packager;
173 } else if (mode == Constants::MODE_VERSION_NORMALIZE) {
174 std::unique_ptr<Packager> packager =
175 std::make_unique<VersionNormalize>(parameterMap_, resultReceiver_);
176 return packager;
177 } else if (mode == Constants::MODE_HQF) {
178 std::unique_ptr<Packager> packager =
179 std::make_unique<HqfPackager>(parameterMap_, resultReceiver_);
180 return packager;
181 } else if (mode == Constants::MODE_APPQF) {
182 std::unique_ptr<Packager> packager =
183 std::make_unique<APPQFPackager>(parameterMap_, resultReceiver_);
184 return packager;
185 } else if (mode == Constants::MODE_PACKAGE_NORMALIZE) {
186 std::unique_ptr<Packager> packager =
187 std::make_unique<PackageNormalize>(parameterMap_, resultReceiver_);
188 return packager;
189 } else if (mode == Constants::MODE_RES) {
190 std::unique_ptr<Packager> packager =
191 std::make_unique<ResPackager>(parameterMap_, resultReceiver_);
192 return packager;
193 }
194 resultReceiver_.append("not support --mode: ").append(mode).append("\n");
195 return nullptr;
196 }
197 } // namespace AppPackingTool
198 } // namespace OHOS