Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 1 | // Copyright 2014 The Bazel Authors. All rights reserved. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 2 | // |
| 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 | package com.google.devtools.build.docgen; |
| 15 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 16 | import com.google.devtools.build.lib.analysis.ConfiguredRuleClassProvider; |
David Chen | 753bcac | 2016-08-16 08:05:30 +0000 | [diff] [blame] | 17 | import com.google.devtools.common.options.OptionsParser; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 18 | import java.lang.reflect.InvocationTargetException; |
| 19 | import java.lang.reflect.Method; |
David Chen | 753bcac | 2016-08-16 08:05:30 +0000 | [diff] [blame] | 20 | import java.util.Collections; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 21 | |
| 22 | /** |
| 23 | * The main class for the docgen project. The class checks the input arguments |
| 24 | * and uses the BuildEncyclopediaProcessor for the actual documentation generation. |
| 25 | */ |
| 26 | public class BuildEncyclopediaGenerator { |
David Chen | 753bcac | 2016-08-16 08:05:30 +0000 | [diff] [blame] | 27 | private static void printUsage(OptionsParser parser) { |
| 28 | System.err.println( |
| 29 | "Usage: docgen_bin -p rule_class_provider (-i input_dir)+\n" |
David Chen | 915dbdf | 2016-08-29 10:25:17 +0000 | [diff] [blame] | 30 | + " [-o outputdir] [-b blacklist] [-1] [-h]\n\n" |
David Chen | 753bcac | 2016-08-16 08:05:30 +0000 | [diff] [blame] | 31 | + "Generates the Build Encyclopedia from embedded native rule documentation.\n" |
| 32 | + "The rule class provider (-p) and at least one input_dir (-i) must be specified.\n"); |
| 33 | System.err.println( |
| 34 | parser.describeOptions( |
| 35 | Collections.<String, String>emptyMap(), OptionsParser.HelpVerbosity.LONG)); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | private static void fail(Throwable e, boolean printStackTrace) { |
| 39 | System.err.println("ERROR: " + e.getMessage()); |
| 40 | if (printStackTrace) { |
| 41 | e.printStackTrace(); |
| 42 | } |
| 43 | Runtime.getRuntime().exit(1); |
| 44 | } |
| 45 | |
Luis Fernando Pino Duque | 0d1a53c | 2016-02-03 12:57:04 +0000 | [diff] [blame] | 46 | private static ConfiguredRuleClassProvider createRuleClassProvider(String classProvider) |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 47 | throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, |
| 48 | IllegalAccessException { |
Luis Fernando Pino Duque | 0d1a53c | 2016-02-03 12:57:04 +0000 | [diff] [blame] | 49 | Class<?> providerClass = Class.forName(classProvider); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 50 | Method createMethod = providerClass.getMethod("create"); |
| 51 | return (ConfiguredRuleClassProvider) createMethod.invoke(null); |
| 52 | } |
| 53 | |
| 54 | public static void main(String[] args) { |
David Chen | 753bcac | 2016-08-16 08:05:30 +0000 | [diff] [blame] | 55 | OptionsParser parser = OptionsParser.newOptionsParser(BuildEncyclopediaOptions.class); |
| 56 | parser.setAllowResidue(false); |
| 57 | parser.parseAndExitUponError(args); |
| 58 | BuildEncyclopediaOptions options = parser.getOptions(BuildEncyclopediaOptions.class); |
Damien Martin-Guillerez | 152181a | 2015-09-25 18:14:55 +0000 | [diff] [blame] | 59 | |
David Chen | 753bcac | 2016-08-16 08:05:30 +0000 | [diff] [blame] | 60 | if (options.help) { |
| 61 | printUsage(parser); |
| 62 | Runtime.getRuntime().exit(0); |
| 63 | } |
| 64 | |
| 65 | if (options.inputDirs.size() == 0 || options.provider.isEmpty()) { |
| 66 | printUsage(parser); |
| 67 | Runtime.getRuntime().exit(1); |
| 68 | } |
| 69 | |
| 70 | try { |
David Chen | 915dbdf | 2016-08-29 10:25:17 +0000 | [diff] [blame] | 71 | BuildEncyclopediaProcessor processor = null; |
| 72 | if (options.singlePage) { |
| 73 | processor = new SinglePageBuildEncyclopediaProcessor( |
| 74 | createRuleClassProvider(options.provider)); |
| 75 | } else { |
| 76 | processor = new MultiPageBuildEncyclopediaProcessor( |
| 77 | createRuleClassProvider(options.provider)); |
| 78 | } |
David Chen | 753bcac | 2016-08-16 08:05:30 +0000 | [diff] [blame] | 79 | processor.generateDocumentation( |
| 80 | options.inputDirs, options.outputDir, options.blacklist); |
| 81 | } catch (BuildEncyclopediaDocException e) { |
| 82 | fail(e, false); |
| 83 | } catch (Throwable e) { |
| 84 | fail(e, true); |
| 85 | } |
Damien Martin-Guillerez | 152181a | 2015-09-25 18:14:55 +0000 | [diff] [blame] | 86 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 87 | } |