add command to nitool to remove system and app ni
[platform/core/dotnet/launcher.git] / NativeLauncher / installer-plugin / nitool.cc
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "common.h"
18
19 #include <cstdio>
20 #include <cstring>
21
22 #include <algorithm>
23 #include <vector>
24
25 std::vector<const char*> getCmdArgs(char** begin, char** end)
26 {
27         std::vector<const char*> list;
28         for (char** itr = begin+1; itr != end; itr++) {
29                 if (strncmp(*itr, "--", 2) != 0)
30                         list.push_back(*itr);
31         }
32         return list;
33 }
34
35 bool cmdOptionExists(char** begin, char** end, const std::string& option)
36 {
37         return std::find(begin, end, option) != end;
38 }
39
40 static void help(const char *argv0)
41 {
42         const char* helpDesc =
43                 "Usage: %s [args] <root paths or pkg name>\n"
44                 "               --help                  - Display this screen\n"
45                 "               --system                - Create NI under System DLLs\n"
46                 "               --dll                   - Create NI for DLL\n"
47                 "               --pkg                   - Create NI for package\n"
48                 "               --reset-system  - Remove System NI files\n"
49                 "               --reset-pkg             - Remove App NI files\n"
50                 "\n"
51                 "Example:\n"
52                 "Create native image for dlls and exes under platform directories\n"
53                 "%s --system\n"
54                 "Create native image for dll\n"
55                 "%s --dll /usr/bin/Tizen.Runtime.dll\n"
56                 "Create native image under the package's bin and lib directory\n"
57                 "%s --pkg org.tizen.FormsGallery\n\n";
58         printf(helpDesc, argv0, argv0, argv0, argv0);
59 }
60
61 int main(int argc, char* argv[])
62 {
63         bool pkgMode = false;
64         bool dllMode = false;
65         bool rmPkgMode = false;
66
67         if (cmdOptionExists(argv, argv+argc, "--help")) {
68                 help(argv[0]);
69                 return 0;
70         } else if (cmdOptionExists(argv, argv+argc, "--system")) {
71                 createNiPlatform();
72                 return 0;
73         } else if (cmdOptionExists(argv, argv+argc, "--dll")) {
74                 dllMode = true;
75         } else if (cmdOptionExists(argv, argv+argc, "--pkg")) {
76                 pkgMode = true;
77         } else if (cmdOptionExists(argv, argv+argc, "--reset-system")) {
78                 removeNiPlatform();
79                 return 0;
80         } else if (cmdOptionExists(argv, argv+argc, "--reset-pkg")) {
81                 rmPkgMode = true;
82         } else {
83                 help(argv[0]);
84                 return 1;
85         }
86
87         std::vector<const char*> args = getCmdArgs(argv, argv+argc);
88
89         if (args.size() < 1) {
90                 if (pkgMode)
91                         fprintf(stderr, "Package name is missed\n");
92                 else if (dllMode)
93                         fprintf(stderr, "DLL path is missed\n");
94                 help(argv[0]);
95                 return 1;
96         }
97
98         if (pkgMode) {
99                 for (const char* pkg : args) {
100                         if (createNiUnderPkgRoot(pkg) != 0) {
101                                 fprintf(stderr, "Failed to get root path from [%s]\n", pkg);
102                                 return 1;
103                         }
104                 }
105         } else if (rmPkgMode) {
106                 for (const char* pkg : args) {
107                         if (removeNiUnderPkgRoot(pkg) != 0) {
108                                 fprintf(stderr, "Failed to get root path from [%s]\n", pkg);
109                                 return 1;
110                         }
111                 }
112         } else if (dllMode) {
113                 for (const char* dll : args)
114                         createNiSelect(dll);
115         } else {
116                 createNiUnderDirs(args.data(), args.size());
117         }
118
119         return 0;
120 }