blob: bc38cfc7e76ec0d0a436665473b6f6665bd63009 [file] [log] [blame]
Damien Martin-Guillerezf88f4d82015-09-25 13:56:55 +00001// Copyright 2014 The Bazel Authors. All rights reserved.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01002//
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.
14package com.google.devtools.build.docgen;
15
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010016import com.google.devtools.build.lib.analysis.ConfiguredRuleClassProvider;
David Chen753bcac2016-08-16 08:05:30 +000017import com.google.devtools.common.options.OptionsParser;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010018import java.lang.reflect.InvocationTargetException;
19import java.lang.reflect.Method;
David Chen753bcac2016-08-16 08:05:30 +000020import java.util.Collections;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010021
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 */
26public class BuildEncyclopediaGenerator {
David Chen753bcac2016-08-16 08:05:30 +000027 private static void printUsage(OptionsParser parser) {
28 System.err.println(
29 "Usage: docgen_bin -p rule_class_provider (-i input_dir)+\n"
David Chen915dbdf2016-08-29 10:25:17 +000030 + " [-o outputdir] [-b blacklist] [-1] [-h]\n\n"
David Chen753bcac2016-08-16 08:05:30 +000031 + "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 Nienhuysd08b27f2015-02-25 16:45:20 +010036 }
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 Duque0d1a53c2016-02-03 12:57:04 +000046 private static ConfiguredRuleClassProvider createRuleClassProvider(String classProvider)
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010047 throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException,
48 IllegalAccessException {
Luis Fernando Pino Duque0d1a53c2016-02-03 12:57:04 +000049 Class<?> providerClass = Class.forName(classProvider);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010050 Method createMethod = providerClass.getMethod("create");
51 return (ConfiguredRuleClassProvider) createMethod.invoke(null);
52 }
53
54 public static void main(String[] args) {
David Chen753bcac2016-08-16 08:05:30 +000055 OptionsParser parser = OptionsParser.newOptionsParser(BuildEncyclopediaOptions.class);
56 parser.setAllowResidue(false);
57 parser.parseAndExitUponError(args);
58 BuildEncyclopediaOptions options = parser.getOptions(BuildEncyclopediaOptions.class);
Damien Martin-Guillerez152181a2015-09-25 18:14:55 +000059
David Chen753bcac2016-08-16 08:05:30 +000060 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 Chen915dbdf2016-08-29 10:25:17 +000071 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 Chen753bcac2016-08-16 08:05:30 +000079 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-Guillerez152181a2015-09-25 18:14:55 +000086 }
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010087}