1 /*
2  * Copyright (C) 2017 The Android Open Source Project
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 package com.android.tools.appbundle.bundletool;
18 
19 import com.android.tools.appbundle.bundletool.utils.FlagParser;
20 import java.io.IOException;
21 import java.util.List;
22 
23 /**
24  * Main entry point of the bundle tool.
25  *
26  * <p>Consider running with -Dsun.zip.disableMemoryMapping when dealing with large bundles.
27  */
28 public class BundleToolMain {
29 
30   public static final String LINK_CMD = "link";
31   public static final String HELP_CMD = "help";
32 
33   /** Parses the flags and routes to the appropriate command handler */
main(String[] args)34   public static void main(String[] args) throws IOException {
35 
36     FlagParser flagParser = new FlagParser();
37     try {
38       flagParser.parse(args);
39     } catch (FlagParser.ParseException e) {
40       System.out.println(String.format("Error while parsing the flags: %s", e.getMessage()));
41       return;
42     }
43     List<String> commands = flagParser.getCommands();
44 
45     if (commands.isEmpty()) {
46       System.out.println("Error: you have to specify a command");
47       help();
48       return;
49     }
50 
51     try {
52       switch (commands.get(0)) {
53         case BuildModuleCommand.COMMAND_NAME:
54           BuildModuleCommand.fromFlags(flagParser).execute();
55           break;
56         case SplitModuleCommand.COMMAND_NAME:
57           new SplitModuleCommand(flagParser).execute();
58           break;
59         case LINK_CMD:
60           throw new UnsupportedOperationException("Not implemented.");
61         case HELP_CMD:
62           if (commands.size() > 1) {
63             help(commands.get(1));
64           } else {
65             help();
66           }
67           return;
68         default:
69           System.out.println("Error: unrecognized command.");
70           help();
71       }
72     } catch (Exception e) {
73       System.out.println("Error: " + e.getMessage());
74     }
75   }
76 
77   /** Displays a general help. */
help()78   public static void help() {
79     System.out.println(
80         String.format(
81             "bundletool [%s|%s|%s|%s] ...",
82             BuildModuleCommand.COMMAND_NAME, SplitModuleCommand.COMMAND_NAME, LINK_CMD, HELP_CMD));
83     System.out.println("Type: bundletool help [command] to learn more about a given command.");
84   }
85 
86   /** Displays help about a given command. */
help(String commandName)87   public static void help(String commandName) {
88     switch (commandName) {
89       case BuildModuleCommand.COMMAND_NAME:
90         BuildModuleCommand.help();
91         break;
92       case SplitModuleCommand.COMMAND_NAME:
93         SplitModuleCommand.help();
94         break;
95       case LINK_CMD:
96         System.out.println("Help is not yet available.");
97         break;
98       default:
99         System.out.println("Unrecognized command.");
100         help();
101         break;
102     }
103   }
104 }
105