Search in sources :

Example 1 with BlazeCommand

use of com.google.devtools.build.lib.runtime.BlazeCommand in project bazel by bazelbuild.

the class DocumentationTestUtil method validateUserManual.

/**
   * Validates that a user manual {@code documentationSource} contains only the flags actually
   * provided by a given set of modules.
   */
public static void validateUserManual(List<Class<? extends BlazeModule>> modules, ConfiguredRuleClassProvider ruleClassProvider, String documentationSource) throws Exception {
    // if there is a class missing, one can find it using
    //   find . -name "*.java" -exec grep -Hn "@Option(name = " {} \; | grep "xxx"
    // where 'xxx' is a flag name.
    List<BlazeModule> blazeModules = BlazeRuntime.createModules(modules);
    Map<String, Object> optionsMap = new HashMap<>();
    // collect all startup options
    for (Class<? extends OptionsBase> optionsClass : BlazeCommandUtils.getStartupOptions(blazeModules)) {
        optionsMap.putAll(Options.getDefaults(optionsClass).asMap());
    }
    // collect all command options
    ServerBuilder serverBuilder = new ServerBuilder();
    new BuiltinCommandModule().serverInit(null, serverBuilder);
    for (BlazeModule module : blazeModules) {
        module.serverInit(null, serverBuilder);
    }
    List<BlazeCommand> blazeCommands = serverBuilder.getCommands();
    for (BlazeCommand command : blazeCommands) {
        for (Class<? extends OptionsBase> optionClass : BlazeCommandUtils.getOptions(command.getClass(), blazeModules, ruleClassProvider)) {
            optionsMap.putAll(Options.getDefaults(optionClass).asMap());
        }
    }
    // check validity of option flags in manual
    Matcher anchorMatcher = CODE_FLAG_PATTERN.matcher(documentationSource);
    String flag;
    boolean found;
    while (anchorMatcher.find()) {
        flag = anchorMatcher.group(1);
        found = optionsMap.containsKey(flag);
        if (!found && flag.startsWith("no")) {
            found = optionsMap.containsKey(flag.substring(2));
        }
        if (!found && flag.startsWith("[no]")) {
            found = optionsMap.containsKey(flag.substring(4));
        }
        assertWithMessage("flag '" + flag + "' is not a blaze option (anymore)").that(found).isTrue();
    }
    String unclosedTag = DocCheckerUtils.getFirstUnclosedTagAndPrintHelp(documentationSource);
    assertWithMessage("Unclosed tag found: " + unclosedTag).that(unclosedTag).isNull();
}
Also used : BlazeCommand(com.google.devtools.build.lib.runtime.BlazeCommand) HashMap(java.util.HashMap) Matcher(java.util.regex.Matcher) BuiltinCommandModule(com.google.devtools.build.lib.runtime.BuiltinCommandModule) BlazeModule(com.google.devtools.build.lib.runtime.BlazeModule) ServerBuilder(com.google.devtools.build.lib.runtime.ServerBuilder)

Example 2 with BlazeCommand

use of com.google.devtools.build.lib.runtime.BlazeCommand in project bazel by bazelbuild.

the class CanonicalizeCommand method exec.

@Override
public ExitCode exec(CommandEnvironment env, OptionsProvider options) {
    BlazeRuntime runtime = env.getRuntime();
    Options canonicalizeOptions = options.getOptions(Options.class);
    String commandName = canonicalizeOptions.forCommand;
    BlazeCommand command = runtime.getCommandMap().get(commandName);
    if (command == null) {
        env.getReporter().handle(Event.error("Not a valid command: '" + commandName + "' (should be one of " + Joiner.on(", ").join(runtime.getCommandMap().keySet()) + ")"));
        return ExitCode.COMMAND_LINE_ERROR;
    }
    Collection<Class<? extends OptionsBase>> optionsClasses = BlazeCommandUtils.getOptions(command.getClass(), runtime.getBlazeModules(), runtime.getRuleClassProvider());
    try {
        OptionsParser parser = OptionsParser.newOptionsParser(optionsClasses);
        parser.setAllowResidue(false);
        parser.parse(options.getResidue());
        InvocationPolicyEnforcer invocationPolicyEnforcer = InvocationPolicyEnforcer.create(canonicalizeOptions.invocationPolicy);
        invocationPolicyEnforcer.enforce(parser, commandName);
        List<String> result = parser.canonicalize();
        for (String piece : result) {
            env.getReporter().getOutErr().printOutLn(piece);
        }
    } catch (OptionsParsingException e) {
        env.getReporter().handle(Event.error(e.getMessage()));
        return ExitCode.COMMAND_LINE_ERROR;
    }
    return ExitCode.SUCCESS;
}
Also used : OptionsBase(com.google.devtools.common.options.OptionsBase) BlazeCommand(com.google.devtools.build.lib.runtime.BlazeCommand) InvocationPolicyEnforcer(com.google.devtools.build.lib.flags.InvocationPolicyEnforcer) OptionsParsingException(com.google.devtools.common.options.OptionsParsingException) OptionsParser(com.google.devtools.common.options.OptionsParser) BlazeRuntime(com.google.devtools.build.lib.runtime.BlazeRuntime)

Example 3 with BlazeCommand

use of com.google.devtools.build.lib.runtime.BlazeCommand in project bazel by bazelbuild.

the class HelpCommand method exec.

@Override
public ExitCode exec(CommandEnvironment env, OptionsProvider options) {
    env.getEventBus().post(new NoBuildEvent());
    BlazeRuntime runtime = env.getRuntime();
    OutErr outErr = env.getReporter().getOutErr();
    Options helpOptions = options.getOptions(Options.class);
    if (options.getResidue().isEmpty()) {
        emitBlazeVersionInfo(outErr, runtime.getProductName());
        emitGenericHelp(outErr, runtime);
        return ExitCode.SUCCESS;
    }
    if (options.getResidue().size() != 1) {
        env.getReporter().handle(Event.error("You must specify exactly one command"));
        return ExitCode.COMMAND_LINE_ERROR;
    }
    String helpSubject = options.getResidue().get(0);
    if (helpSubject.equals("startup_options")) {
        emitBlazeVersionInfo(outErr, runtime.getProductName());
        emitStartupOptions(outErr, helpOptions.helpVerbosity, runtime, getOptionCategories(runtime));
        return ExitCode.SUCCESS;
    } else if (helpSubject.equals("target-syntax")) {
        emitBlazeVersionInfo(outErr, runtime.getProductName());
        emitTargetSyntaxHelp(outErr, getOptionCategories(runtime), runtime.getProductName());
        return ExitCode.SUCCESS;
    } else if (helpSubject.equals("info-keys")) {
        emitInfoKeysHelp(env, outErr);
        return ExitCode.SUCCESS;
    } else if (helpSubject.equals("completion")) {
        emitCompletionHelp(runtime, outErr);
        return ExitCode.SUCCESS;
    } else if (helpSubject.equals("everything-as-html")) {
        new HtmlEmitter(runtime).emit(outErr);
        return ExitCode.SUCCESS;
    }
    BlazeCommand command = runtime.getCommandMap().get(helpSubject);
    if (command == null) {
        ConfiguredRuleClassProvider provider = runtime.getRuleClassProvider();
        RuleClass ruleClass = provider.getRuleClassMap().get(helpSubject);
        if (ruleClass != null && ruleClass.isDocumented()) {
            // There is a rule with a corresponding name
            outErr.printOut(BlazeRuleHelpPrinter.getRuleDoc(helpSubject, provider));
            return ExitCode.SUCCESS;
        } else {
            env.getReporter().handle(Event.error(null, "'" + helpSubject + "' is neither a command nor a build rule"));
            return ExitCode.COMMAND_LINE_ERROR;
        }
    }
    emitBlazeVersionInfo(outErr, runtime.getProductName());
    outErr.printOut(BlazeCommandUtils.getUsage(command.getClass(), getOptionCategories(runtime), helpOptions.helpVerbosity, runtime.getBlazeModules(), runtime.getRuleClassProvider(), runtime.getProductName()));
    return ExitCode.SUCCESS;
}
Also used : OutErr(com.google.devtools.build.lib.util.io.OutErr) BlazeCommand(com.google.devtools.build.lib.runtime.BlazeCommand) ConfiguredRuleClassProvider(com.google.devtools.build.lib.analysis.ConfiguredRuleClassProvider) NoBuildEvent(com.google.devtools.build.lib.analysis.NoBuildEvent) RuleClass(com.google.devtools.build.lib.packages.RuleClass) BlazeRuntime(com.google.devtools.build.lib.runtime.BlazeRuntime)

Example 4 with BlazeCommand

use of com.google.devtools.build.lib.runtime.BlazeCommand in project bazel by bazelbuild.

the class HelpCommand method emitCompletionHelp.

private void emitCompletionHelp(BlazeRuntime runtime, OutErr outErr) {
    // First startup_options
    Iterable<BlazeModule> blazeModules = runtime.getBlazeModules();
    ConfiguredRuleClassProvider ruleClassProvider = runtime.getRuleClassProvider();
    Map<String, BlazeCommand> commandsByName = getSortedCommands(runtime);
    outErr.printOutLn("BAZEL_COMMAND_LIST=\"" + SPACE_JOINER.join(commandsByName.keySet()) + "\"");
    outErr.printOutLn("BAZEL_INFO_KEYS=\"");
    for (String name : InfoCommand.getHardwiredInfoItemNames(runtime.getProductName())) {
        outErr.printOutLn(name);
    }
    outErr.printOutLn("\"");
    outErr.printOutLn("BAZEL_STARTUP_OPTIONS=\"");
    Iterable<Class<? extends OptionsBase>> options = BlazeCommandUtils.getStartupOptions(blazeModules);
    outErr.printOut(OptionsParser.newOptionsParser(options).getOptionsCompletion());
    outErr.printOutLn("\"");
    for (Map.Entry<String, BlazeCommand> e : commandsByName.entrySet()) {
        BlazeCommand command = e.getValue();
        String varName = e.getKey().toUpperCase(Locale.US).replace('-', '_');
        Command annotation = command.getClass().getAnnotation(Command.class);
        if (!annotation.completion().isEmpty()) {
            outErr.printOutLn("BAZEL_COMMAND_" + varName + "_ARGUMENT=\"" + annotation.completion() + "\"");
        }
        options = BlazeCommandUtils.getOptions(command.getClass(), blazeModules, ruleClassProvider);
        outErr.printOutLn("BAZEL_COMMAND_" + varName + "_FLAGS=\"");
        outErr.printOut(OptionsParser.newOptionsParser(options).getOptionsCompletion());
        outErr.printOutLn("\"");
    }
}
Also used : OptionsBase(com.google.devtools.common.options.OptionsBase) BlazeCommand(com.google.devtools.build.lib.runtime.BlazeCommand) ConfiguredRuleClassProvider(com.google.devtools.build.lib.analysis.ConfiguredRuleClassProvider) BlazeCommand(com.google.devtools.build.lib.runtime.BlazeCommand) Command(com.google.devtools.build.lib.runtime.Command) RuleClass(com.google.devtools.build.lib.packages.RuleClass) BlazeModule(com.google.devtools.build.lib.runtime.BlazeModule) Map(java.util.Map) ImmutableSortedMap(com.google.common.collect.ImmutableSortedMap) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 5 with BlazeCommand

use of com.google.devtools.build.lib.runtime.BlazeCommand in project bazel by bazelbuild.

the class HelpCommand method emitGenericHelp.

private void emitGenericHelp(OutErr outErr, BlazeRuntime runtime) {
    outErr.printOut(String.format("Usage: %s <command> <options> ...\n\n", runtime.getProductName()));
    outErr.printOut("Available commands:\n");
    Map<String, BlazeCommand> commandsByName = runtime.getCommandMap();
    List<String> namesInOrder = new ArrayList<>(commandsByName.keySet());
    Collections.sort(namesInOrder);
    for (String name : namesInOrder) {
        BlazeCommand command = commandsByName.get(name);
        Command annotation = command.getClass().getAnnotation(Command.class);
        if (annotation.hidden()) {
            continue;
        }
        String shortDescription = annotation.shortDescription().replace("%{product}", runtime.getProductName());
        outErr.printOut(String.format("  %-19s %s\n", name, shortDescription));
    }
    outErr.printOut("\n");
    outErr.printOut("Getting more help:\n");
    outErr.printOut(String.format("  %s help <command>\n", runtime.getProductName()));
    outErr.printOut("                   Prints help and options for <command>.\n");
    outErr.printOut(String.format("  %s help startup_options\n", runtime.getProductName()));
    outErr.printOut(String.format("                   Options for the JVM hosting %s.\n", runtime.getProductName()));
    outErr.printOut(String.format("  %s help target-syntax\n", runtime.getProductName()));
    outErr.printOut("                   Explains the syntax for specifying targets.\n");
    outErr.printOut(String.format("  %s help info-keys\n", runtime.getProductName()));
    outErr.printOut("                   Displays a list of keys used by the info command.\n");
}
Also used : BlazeCommand(com.google.devtools.build.lib.runtime.BlazeCommand) BlazeCommand(com.google.devtools.build.lib.runtime.BlazeCommand) Command(com.google.devtools.build.lib.runtime.Command) ArrayList(java.util.ArrayList)

Aggregations

BlazeCommand (com.google.devtools.build.lib.runtime.BlazeCommand)5 ConfiguredRuleClassProvider (com.google.devtools.build.lib.analysis.ConfiguredRuleClassProvider)2 RuleClass (com.google.devtools.build.lib.packages.RuleClass)2 BlazeModule (com.google.devtools.build.lib.runtime.BlazeModule)2 BlazeRuntime (com.google.devtools.build.lib.runtime.BlazeRuntime)2 Command (com.google.devtools.build.lib.runtime.Command)2 OptionsBase (com.google.devtools.common.options.OptionsBase)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 ImmutableSortedMap (com.google.common.collect.ImmutableSortedMap)1 NoBuildEvent (com.google.devtools.build.lib.analysis.NoBuildEvent)1 InvocationPolicyEnforcer (com.google.devtools.build.lib.flags.InvocationPolicyEnforcer)1 BuiltinCommandModule (com.google.devtools.build.lib.runtime.BuiltinCommandModule)1 ServerBuilder (com.google.devtools.build.lib.runtime.ServerBuilder)1 OutErr (com.google.devtools.build.lib.util.io.OutErr)1 OptionsParser (com.google.devtools.common.options.OptionsParser)1 OptionsParsingException (com.google.devtools.common.options.OptionsParsingException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Matcher (java.util.regex.Matcher)1