Home | History | Annotate | Download | only in signature
      1 /*
      2  * Copyright (C) 2009 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 signature;
     18 
     19 /**
     20  * The main entry point for the signature tool. If invoked with
     21  * <code>--create</code> the call is delegated to
     22  * {@link signature.converter.Main}. If invoked with <code>--compare</code> the
     23  * call is delegated to {@link signature.compare.Main}.
     24  *
     25  * @see signature.converter.Main
     26  * @see signature.compare.Main
     27  */
     28 public class Main {
     29 
     30     private static final String USAGE_MESSAGE = "usage:\n"
     31             + "  sig --create (doclet | dex) sourcefiles --out directory"
     32             + " --name <name> --packages packageName{ packageName}\n"
     33             + "  sig --compare --from=(doclet | dex | sig) <sourcefiles>"
     34             + " --name <name> --to=(doclet | dex | sig) <sourcefiles>"
     35             + " --name <name> --out directory"
     36             + " --packages packageName{ packageName}\n  sig --help";
     37 
     38     /**
     39      * This class is uninstantiable.
     40      */
     41     private Main() {
     42         // This space intentionally left blank.
     43     }
     44 
     45     /**
     46      * Run!
     47      */
     48     public static void main(String[] args) {
     49         boolean gotCmd = false;
     50         boolean showUsage = false;
     51 
     52         try {
     53             for (int i = 0; i < args.length; i++) {
     54                 String arg = args[i];
     55                 if (arg.equals("--") || !arg.startsWith("--")) {
     56                     gotCmd = false;
     57                     showUsage = true;
     58                     break;
     59                 }
     60 
     61                 gotCmd = true;
     62                 if (arg.equals("--create")) {
     63                     signature.converter.Main.main(without(args, i));
     64                     break;
     65                 } else if (arg.equals("--compare")) {
     66                     signature.compare.Main.main(without(args, i));
     67                     break;
     68                 } else if (arg.equals("--version")) {
     69                     version();
     70                     break;
     71                 } else if (arg.equals("--help")) {
     72                     showUsage = true;
     73                     break;
     74                 } else {
     75                     gotCmd = false;
     76                 }
     77             }
     78         } catch (UsageException ex) {
     79             showUsage = true;
     80         } catch (RuntimeException ex) {
     81             System.err.println("\nUNEXPECTED TOP-LEVEL EXCEPTION:");
     82             ex.printStackTrace();
     83             System.exit(2);
     84         } catch (Throwable ex) {
     85             System.err.println("\nUNEXPECTED TOP-LEVEL ERROR:");
     86             ex.printStackTrace();
     87             System.exit(3);
     88         }
     89 
     90         if (!gotCmd) {
     91             System.err.println("error: no command specified");
     92             showUsage = true;
     93         }
     94 
     95         if (showUsage) {
     96             usage();
     97             System.exit(1);
     98         }
     99     }
    100 
    101     /**
    102      * Prints the version message.
    103      */
    104     private static void version() {
    105         System.err.println("android sigtools version " + Version.VERSION);
    106         System.exit(0);
    107     }
    108 
    109     /**
    110      * Prints the usage message.
    111      */
    112     private static void usage() {
    113         System.err.println(USAGE_MESSAGE);
    114     }
    115 
    116     /**
    117      * Returns a copy of the given argument array, but without the indicated
    118      * element.
    119      *
    120      * @param orig
    121      *            non-null; original array
    122      * @param n
    123      *            which element to omit
    124      * @return non-null; new array
    125      */
    126     private static String[] without(String[] orig, int n) {
    127         int len = orig.length - 1;
    128         String[] newa = new String[len];
    129         System.arraycopy(orig, 0, newa, 0, n);
    130         System.arraycopy(orig, n + 1, newa, n, len - n);
    131         return newa;
    132     }
    133 
    134 }
    135