-
Notifications
You must be signed in to change notification settings - Fork 156
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add execute-command-before BuildMutator #310
Open
AlexBeggs
wants to merge
1
commit into
gradle:master
Choose a base branch
from
AlexBeggs:execute-command-before-build-mutator
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+396
−6
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package org.gradle.profiler; | ||
|
||
import static org.gradle.profiler.ScenarioLoader.BAZEL; | ||
import static org.gradle.profiler.ScenarioLoader.BUCK; | ||
import static org.gradle.profiler.ScenarioLoader.MAVEN; | ||
|
||
import com.typesafe.config.Config; | ||
import javax.annotation.Nullable; | ||
|
||
public class ScenarioUtil { | ||
|
||
/** | ||
* Returns the specific config for type of build that is running, | ||
* if the scenario doesn't define the build, then this default value is returned | ||
* | ||
* @param rootScenario the root scenario config | ||
* @param settings the invocation settings that indicates the type of build selected | ||
* @param defaultScenario if the scenario doesn't define the build, then this default value is returned, this can be null. | ||
* @return if the build config exists or if the scenario doesn't define the build, then this default value is returned | ||
*/ | ||
public static Config getBuildConfig(Config rootScenario, InvocationSettings settings, @Nullable | ||
Config defaultScenario) { | ||
Config scenario; | ||
if (settings.isBazel()) { | ||
scenario = getConfigOrDefault(rootScenario, BAZEL, defaultScenario); | ||
} else if (settings.isBuck()) { | ||
scenario = getConfigOrDefault(rootScenario, BUCK, defaultScenario); | ||
} else if (settings.isMaven()) { | ||
scenario = getConfigOrDefault(rootScenario, MAVEN, defaultScenario); | ||
} else { | ||
scenario = rootScenario; | ||
} | ||
return scenario; | ||
} | ||
|
||
private static Config getConfigOrDefault(Config rootScenario, String key, @Nullable Config defaultScenario) { | ||
Config scenario; | ||
if (rootScenario.hasPath(key)) { | ||
scenario = rootScenario.getConfig(key); | ||
} else { | ||
scenario = defaultScenario; | ||
} return scenario; | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/org/gradle/profiler/mutations/CommandInvoker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package org.gradle.profiler.mutations; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* Interface which provides a means to execute a command | ||
*/ | ||
public interface CommandInvoker { | ||
|
||
/** | ||
* @param command the command to execute | ||
* @return the exit code of the result of executing the command | ||
*/ | ||
int execute(List<String> command); | ||
|
||
} |
121 changes: 121 additions & 0 deletions
121
src/main/java/org/gradle/profiler/mutations/ExecuteCommandBuildMutator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package org.gradle.profiler.mutations; | ||
|
||
import static org.gradle.profiler.ScenarioUtil.getBuildConfig; | ||
|
||
import com.google.common.annotations.VisibleForTesting; | ||
import com.typesafe.config.Config; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import org.gradle.profiler.BuildContext; | ||
import org.gradle.profiler.BuildMutator; | ||
import org.gradle.profiler.CompositeBuildMutator; | ||
import org.gradle.profiler.ConfigUtil; | ||
import org.gradle.profiler.InvocationSettings; | ||
import org.gradle.profiler.ScenarioContext; | ||
|
||
public class ExecuteCommandBuildMutator implements BuildMutator { | ||
|
||
private ExecuteCommandSchedule schedule; | ||
private List<String> commands; | ||
private CommandInvoker commandInvoker; | ||
|
||
public ExecuteCommandBuildMutator(ExecuteCommandSchedule schedule, | ||
List<String> commands, CommandInvoker commandInvoker) { | ||
this.schedule = schedule; | ||
this.commands = commands; | ||
this.commandInvoker = commandInvoker; | ||
} | ||
|
||
@Override | ||
public void beforeBuild(BuildContext context) { | ||
if (schedule == ExecuteCommandSchedule.BUILD) { | ||
execute(); | ||
} | ||
} | ||
|
||
@Override | ||
public void beforeScenario(ScenarioContext context) { | ||
if (schedule == ExecuteCommandSchedule.SCENARIO) { | ||
execute(); | ||
} | ||
} | ||
|
||
protected void execute() { | ||
String commandStr = String.join(" ", commands); | ||
System.out.println(String.format("> Executing command `%s`", commandStr)); | ||
int result = commandInvoker.execute(commands); | ||
if (result != 0) { | ||
System.err.println( | ||
String.format("Unexpected exit code %s for command `%s`", result, commandStr) | ||
); | ||
} | ||
} | ||
|
||
public static class Configurator implements BuildMutatorConfigurator { | ||
|
||
private CommandInvoker commandInvoker; | ||
|
||
public Configurator() { | ||
this(new ProcessBuilderCommandInvoker()); | ||
} | ||
|
||
@VisibleForTesting | ||
Configurator(CommandInvoker commandInvoker) { | ||
this.commandInvoker = commandInvoker; | ||
} | ||
|
||
private BuildMutator newInstance(Config scenario, String scenarioName, | ||
InvocationSettings settings, String key, | ||
CommandInvoker commandInvoker, ExecuteCommandSchedule schedule, List<String> commands) { | ||
return new ExecuteCommandBuildMutator(schedule, commands, commandInvoker); | ||
} | ||
|
||
@Override | ||
public BuildMutator configure(Config rootScenario, String scenarioName, | ||
InvocationSettings settings, String key) { | ||
if (enabled(rootScenario, scenarioName, settings, key)) { | ||
Config scenario = getBuildConfig(rootScenario, settings, null); | ||
final List<BuildMutator> mutators = new ArrayList<>(); | ||
final List<? extends Config> list = scenario.getConfigList(key); | ||
for (Config config : list) { | ||
final ExecuteCommandSchedule schedule = ConfigUtil | ||
.enumValue(config, "schedule", ExecuteCommandSchedule.class, null); | ||
if (schedule == null) { | ||
throw new IllegalArgumentException( | ||
"Schedule for executing commands is not specified"); | ||
} | ||
List<String> commands = ConfigUtil.strings(config, "commands"); | ||
if (commands.isEmpty()) { | ||
throw new IllegalArgumentException( | ||
String.format( | ||
"No commands specified for 'execute-command-before' in scenario %s", | ||
scenarioName) | ||
); | ||
} | ||
mutators.add( | ||
newInstance(scenario, scenarioName, settings, key, commandInvoker, schedule, | ||
commands)); | ||
} | ||
return new CompositeBuildMutator(mutators); | ||
} else { | ||
return BuildMutator.NOOP; | ||
} | ||
} | ||
|
||
private boolean enabled(Config rootScenario, String scenarioName, InvocationSettings settings, String key) { | ||
Config scenario = getBuildConfig(rootScenario, settings, null); | ||
return scenario != null && scenario.hasPath(key) && !scenario.getConfigList(key) | ||
.isEmpty(); | ||
} | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return getClass().getSimpleName() + "(" + schedule + ")"; | ||
} | ||
|
||
public enum ExecuteCommandSchedule { | ||
SCENARIO, BUILD | ||
} | ||
|
||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/org/gradle/profiler/mutations/ProcessBuilderCommandInvoker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package org.gradle.profiler.mutations; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
public class ProcessBuilderCommandInvoker implements CommandInvoker { | ||
|
||
@Override | ||
public int execute(List<String> command) { | ||
try { | ||
if (command == null || command.isEmpty()) { | ||
throw new IllegalArgumentException( | ||
String.format("command cannot be null or empty, was %s", command)); | ||
} | ||
ProcessBuilder processBuilder = new ProcessBuilder(command) | ||
.redirectOutput(ProcessBuilder.Redirect.INHERIT) | ||
.redirectError(ProcessBuilder.Redirect.INHERIT); | ||
Process process = processBuilder.start(); | ||
return process.waitFor(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could potentially add a timeout option but for now I left it |
||
} catch (IOException | InterruptedException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
doesn't handle CLEANUP, Bazel, Buck and Maven don't implement that