aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChris Warrington <cmw@google.com>2015-07-14 17:20:07 -0700
committerChris Warrington <cmw@google.com>2015-07-20 15:25:55 -0700
commit78ead9d9776fe3e981df7c0f81125f21a0f3118d (patch)
tree3726b3636e89268a6f04d5533266a819e07ad1a0
parentbaa5e147614e739b1c2444968e15553c88820e21 (diff)
downloadbuildSrc-78ead9d9776fe3e981df7c0f81125f21a0f3118d.tar.gz
PresubmitRunner plugin.
Change-Id: I8bff43b57c4318c5371dd7b6728b0b7523735ad5
-rw-r--r--base/build.gradle2
-rw-r--r--src/main/groovy/com/android/tools/internal/PresubmitRunner.java106
-rw-r--r--src/main/resources/META-INF/gradle-plugins/presubmit-runner.properties1
3 files changed, 108 insertions, 1 deletions
diff --git a/base/build.gradle b/base/build.gradle
index 82165b8..e883f3b 100644
--- a/base/build.gradle
+++ b/base/build.gradle
@@ -132,7 +132,7 @@ task clean << {
}
}
}
-
+apply plugin: 'presubmit-runner'
// Task for initializing a fresh repo.
task init {
dependsOn prepareRepo
diff --git a/src/main/groovy/com/android/tools/internal/PresubmitRunner.java b/src/main/groovy/com/android/tools/internal/PresubmitRunner.java
new file mode 100644
index 0000000..c91c55d
--- /dev/null
+++ b/src/main/groovy/com/android/tools/internal/PresubmitRunner.java
@@ -0,0 +1,106 @@
+/*
+ * Copyright 2015 Google Inc. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.tools.internal;
+
+import com.google.common.base.Charsets;
+import com.google.common.base.Joiner;
+import com.google.common.base.Splitter;
+
+import com.google.common.collect.ArrayListMultimap;
+import com.google.common.collect.Lists;
+import com.google.common.collect.Multimap;
+import com.google.common.collect.Sets;
+import com.google.common.io.Files;
+import org.gradle.api.Action;
+import org.gradle.api.Plugin;
+import org.gradle.api.Project;
+import org.gradle.api.Task;
+import org.gradle.api.artifacts.Configuration;
+import org.gradle.api.artifacts.Dependency;
+import org.gradle.api.artifacts.ProjectDependency;
+import org.gradle.api.logging.LogLevel;
+import org.gradle.api.logging.Logger;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Collection;
+import java.util.List;
+import java.util.Set;
+
+/**
+ * Infrastructure to allow presubmit checks.
+ *
+ * Rather than run all of the tests, this means only the tests that
+ * could have been affected by a change to be run.
+ *
+ * When passed a file listing absolute paths, one per line,
+ * e.g. -Pcom.android.build.presubmitChangedFiles=/path/to/file
+ * creates a runPresubmitTests task that depends on buildDependents
+ * of all the subprojects that contain files in that list.
+ * The buildDependents task task also builds and tests all subprojects
+ * that transitively depend on the modified subprojects.
+ *
+ * The file could be generated by something like
+ * <pre>
+ * git diff --name-only HEAD `git merge-base HEAD aosp/studio-1.4-dev` | \
+ * sed 's#^#'`git rev-parse --show-toplevel`'/#' \
+ * > ../../out/presubmit_diff.txt
+ * ../gradlew -Pcom.android.build.presubmitChangedFiles=../../out/presubmit_diff.txt :runPresubmitTests
+ * </pre>
+ */
+public class PresubmitRunner implements Plugin<Project> {
+
+ private static final String PRESUBMIT_FILES_PROPERTY = "com.android.build.presubmitChangedFiles";
+
+ @Override
+ public void apply(final Project project) {
+
+ if (project.hasProperty(PRESUBMIT_FILES_PROPERTY)) {
+ final Logger logger = project.getLogger();
+ logger.info("Creating presubmit task.");
+
+ final Task runPresubmitTestsTask = project.getTasks().create("runPresubmitTests");
+ runPresubmitTestsTask.setGroup("verification");
+
+ final Iterable<String> files;
+ try {
+ files = Files.readLines(new File((String) project.property(PRESUBMIT_FILES_PROPERTY)), Charsets.UTF_8);
+ } catch (IOException e) {
+ project.getLogger().error("Unable to read presubmit file", e);
+ return;
+ }
+
+ // Find projects that contain files that have changed.
+ project.subprojects(new Action<Project>() {
+ @Override
+ public void execute(final Project subProject) {
+ String subProjectPath = subProject.getProjectDir().toString();
+ for (String file : files) {
+ if (file.startsWith(subProjectPath)) {
+ Object subProjectTestTask = subProject.getTasks().findByName("buildDependents");
+ if (subProjectTestTask != null) {
+ runPresubmitTestsTask.dependsOn(subProjectTestTask);
+ }
+ break;
+ }
+ }
+ }
+ });
+ }
+ }
+
+}
diff --git a/src/main/resources/META-INF/gradle-plugins/presubmit-runner.properties b/src/main/resources/META-INF/gradle-plugins/presubmit-runner.properties
new file mode 100644
index 0000000..1f14883
--- /dev/null
+++ b/src/main/resources/META-INF/gradle-plugins/presubmit-runner.properties
@@ -0,0 +1 @@
+implementation-class=com.android.tools.internal.PresubmitRunner