|
| 1 | +package org.gradle.profiler.mutations; |
| 2 | + |
| 3 | +import static org.gradle.profiler.ScenarioUtil.getBuildConfig; |
| 4 | + |
| 5 | +import com.google.common.annotations.VisibleForTesting; |
| 6 | +import com.typesafe.config.Config; |
| 7 | +import java.util.ArrayList; |
| 8 | +import java.util.List; |
| 9 | +import org.gradle.profiler.BuildContext; |
| 10 | +import org.gradle.profiler.BuildMutator; |
| 11 | +import org.gradle.profiler.CompositeBuildMutator; |
| 12 | +import org.gradle.profiler.ConfigUtil; |
| 13 | +import org.gradle.profiler.InvocationSettings; |
| 14 | +import org.gradle.profiler.ScenarioContext; |
| 15 | + |
| 16 | +public class ExecuteCommandBuildMutator implements BuildMutator { |
| 17 | + |
| 18 | + private ExecuteCommandSchedule schedule; |
| 19 | + private List<String> commands; |
| 20 | + private CommandInvoker commandInvoker; |
| 21 | + |
| 22 | + public ExecuteCommandBuildMutator(ExecuteCommandSchedule schedule, |
| 23 | + List<String> commands, CommandInvoker commandInvoker) { |
| 24 | + this.schedule = schedule; |
| 25 | + this.commands = commands; |
| 26 | + this.commandInvoker = commandInvoker; |
| 27 | + } |
| 28 | + |
| 29 | + @Override |
| 30 | + public void beforeBuild(BuildContext context) { |
| 31 | + if (schedule == ExecuteCommandSchedule.BUILD) { |
| 32 | + execute(); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + @Override |
| 37 | + public void beforeScenario(ScenarioContext context) { |
| 38 | + if (schedule == ExecuteCommandSchedule.SCENARIO) { |
| 39 | + execute(); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + protected void execute() { |
| 44 | + String commandStr = String.join(" ", commands); |
| 45 | + System.out.println(String.format("> Executing command `%s`", commandStr)); |
| 46 | + int result = commandInvoker.execute(commands); |
| 47 | + if (result != 0) { |
| 48 | + System.err.println( |
| 49 | + String.format("Unexpected exit code %s for command `%s`", result, commandStr) |
| 50 | + ); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + public static class Configurator implements BuildMutatorConfigurator { |
| 55 | + |
| 56 | + private CommandInvoker commandInvoker; |
| 57 | + |
| 58 | + public Configurator() { |
| 59 | + this(new ProcessBuilderCommandInvoker()); |
| 60 | + } |
| 61 | + |
| 62 | + @VisibleForTesting |
| 63 | + Configurator(CommandInvoker commandInvoker) { |
| 64 | + this.commandInvoker = commandInvoker; |
| 65 | + } |
| 66 | + |
| 67 | + private BuildMutator newInstance(Config scenario, String scenarioName, |
| 68 | + InvocationSettings settings, String key, |
| 69 | + CommandInvoker commandInvoker, ExecuteCommandSchedule schedule, List<String> commands) { |
| 70 | + return new ExecuteCommandBuildMutator(schedule, commands, commandInvoker); |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public BuildMutator configure(Config rootScenario, String scenarioName, |
| 75 | + InvocationSettings settings, String key) { |
| 76 | + if (enabled(rootScenario, scenarioName, settings, key)) { |
| 77 | + Config scenario = getBuildConfig(rootScenario, settings, null); |
| 78 | + final List<BuildMutator> mutators = new ArrayList<>(); |
| 79 | + final List<? extends Config> list = scenario.getConfigList(key); |
| 80 | + for (Config config : list) { |
| 81 | + final ExecuteCommandSchedule schedule = ConfigUtil |
| 82 | + .enumValue(config, "schedule", ExecuteCommandSchedule.class, null); |
| 83 | + if (schedule == null) { |
| 84 | + throw new IllegalArgumentException( |
| 85 | + "Schedule for executing commands is not specified"); |
| 86 | + } |
| 87 | + List<String> commands = ConfigUtil.strings(config, "commands"); |
| 88 | + if (commands.isEmpty()) { |
| 89 | + throw new IllegalArgumentException( |
| 90 | + String.format( |
| 91 | + "No commands specified for 'execute-command-before' in scenario %s", |
| 92 | + scenarioName) |
| 93 | + ); |
| 94 | + } |
| 95 | + mutators.add( |
| 96 | + newInstance(scenario, scenarioName, settings, key, commandInvoker, schedule, |
| 97 | + commands)); |
| 98 | + } |
| 99 | + return new CompositeBuildMutator(mutators); |
| 100 | + } else { |
| 101 | + return BuildMutator.NOOP; |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + private boolean enabled(Config rootScenario, String scenarioName, InvocationSettings settings, String key) { |
| 106 | + Config scenario = getBuildConfig(rootScenario, settings, null); |
| 107 | + return scenario != null && scenario.hasPath(key) && !scenario.getConfigList(key) |
| 108 | + .isEmpty(); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + public String toString() { |
| 114 | + return getClass().getSimpleName() + "(" + schedule + ")"; |
| 115 | + } |
| 116 | + |
| 117 | + public enum ExecuteCommandSchedule { |
| 118 | + SCENARIO, BUILD |
| 119 | + } |
| 120 | + |
| 121 | +} |
0 commit comments