diff options
author | Tor Norbye <tnorbye@google.com> | 2013-10-04 14:43:47 +0000 |
---|---|---|
committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2013-10-04 14:43:47 +0000 |
commit | 23ac2e1148d1863bb4b9781379d1e286e5ed5d44 (patch) | |
tree | 0b1a2dbfc746bbb1c942cfdbd886039c84e096b3 | |
parent | 43bd83ce02ac45d2de5daa2c7171d5ecee43af61 (diff) | |
parent | 14e30af3597c9f80b5e433c5b671e920c2f27a14 (diff) | |
download | build-23ac2e1148d1863bb4b9781379d1e286e5ed5d44.tar.gz |
Merge "Make lint task use the plugin's SDK home, not environment"
3 files changed, 143 insertions, 75 deletions
diff --git a/gradle/src/main/groovy/com/android/build/gradle/BasePlugin.groovy b/gradle/src/main/groovy/com/android/build/gradle/BasePlugin.groovy index 7c6906a..000b418 100644 --- a/gradle/src/main/groovy/com/android/build/gradle/BasePlugin.groovy +++ b/gradle/src/main/groovy/com/android/build/gradle/BasePlugin.groovy @@ -798,6 +798,7 @@ public abstract class BasePlugin { Task lintCheck = project.tasks.create("lint" + variantName, Lint) lintCheck.dependsOn baseVariantData.javaCompileTask, lintCompile lint.dependsOn lintCheck + lintCheck.setPlugin(this) String outputName = "$project.buildDir/lint/" + variantName VariantConfiguration config = baseVariantData.variantConfiguration diff --git a/gradle/src/main/groovy/com/android/build/gradle/internal/LintGradleClient.java b/gradle/src/main/groovy/com/android/build/gradle/internal/LintGradleClient.java index e565bcc..6833ffc 100644 --- a/gradle/src/main/groovy/com/android/build/gradle/internal/LintGradleClient.java +++ b/gradle/src/main/groovy/com/android/build/gradle/internal/LintGradleClient.java @@ -19,6 +19,7 @@ package com.android.build.gradle.internal; import com.google.common.collect.Lists; import com.android.annotations.NonNull; +import com.android.annotations.Nullable; import com.android.tools.lint.LintCliClient; import com.android.tools.lint.LintCliFlags; import com.android.tools.lint.detector.api.Project; @@ -28,9 +29,11 @@ import java.util.List; public class LintGradleClient extends LintCliClient { private List<File> mCustomRules = Lists.newArrayList(); + private File mySdkHome; - public LintGradleClient(LintCliFlags flags) { + public LintGradleClient(@NonNull LintCliFlags flags, @Nullable File sdkHome) { super(flags); + mySdkHome = sdkHome; } public void setCustomRules(List<File> customRules) { @@ -46,4 +49,16 @@ public class LintGradleClient extends LintCliClient { protected Project createProject(@NonNull File dir, @NonNull File referenceDir) { return new LintGradleProject(this, dir, referenceDir); } + + public void setSdkHome(File home) { + mySdkHome = home; + } + + @Override + public File getSdkHome() { + if (mySdkHome != null) { + return mySdkHome; + } + return super.getSdkHome(); + } } diff --git a/gradle/src/main/groovy/com/android/build/gradle/tasks/Lint.groovy b/gradle/src/main/groovy/com/android/build/gradle/tasks/Lint.groovy index 3aa884d..2c756ce 100644 --- a/gradle/src/main/groovy/com/android/build/gradle/tasks/Lint.groovy +++ b/gradle/src/main/groovy/com/android/build/gradle/tasks/Lint.groovy @@ -16,6 +16,9 @@ package com.android.build.gradle.tasks +import com.android.annotations.NonNull +import com.android.annotations.Nullable +import com.android.build.gradle.BasePlugin import com.android.build.gradle.internal.LintGradleClient import com.android.tools.lint.HtmlReporter import com.android.tools.lint.LintCliFlags @@ -29,58 +32,41 @@ import org.gradle.api.GradleException import org.gradle.api.tasks.TaskAction public class Lint extends DefaultTask { + @NonNull private BasePlugin mPlugin + @Nullable private List<File> mCustomRules + @Nullable private File mConfigFile + @Nullable private File mHtmlOutput + @Nullable private File mXmlOutput + @Nullable private List<Set<File>> mSourceSets + @Nullable private String mClassPath + @Nullable private List<Set<File>> mResourceSets + private boolean mQuiet + + public void setPlugin(@NonNull BasePlugin plugin) { + mPlugin = plugin + } - private LintCliFlags flags = new LintCliFlags() - private LintGradleClient client = new LintGradleClient(flags) - private IssueRegistry registry = new BuiltinIssueRegistry() - private List<File> customRules = new ArrayList<File>() - - public void addCustomRule(File f) { - customRules.add(f) + public void addCustomRule(@NonNull File jar) { + if (mCustomRules == null) { + mCustomRules = new ArrayList<File>() + } + mCustomRules.add(jar) } public void setQuiet() { - flags.setQuiet(true) + mQuiet = true } - public void setConfig(File f) { - flags.setDefaultConfiguration(client.createConfigurationFromFile(f)) + public void setConfig(@NonNull File configFile) { + mConfigFile = configFile } - public void setHtmlOutput(File output) { - output = output.getAbsoluteFile() - if (output.exists()) { - boolean delete = output.delete() - if (!delete) { - throw new GradleException("Could not delete old " + output) - } - } - if (output.getParentFile() != null && !output.getParentFile().canWrite()) { - throw new GradleException("Cannot write HTML output file " + output) - } - try { - flags.getReporters().add(new HtmlReporter(client, output)) - } catch (IOException e) { - throw new GradleException("HTML invalid argument.") - } + public void setHtmlOutput(@NonNull File htmlOutput) { + mHtmlOutput = htmlOutput } - public void setXmlOutput(File output) { - output = output.getAbsoluteFile() - if (output.exists()) { - boolean delete = output.delete(); - if (!delete) { - throw new GradleException("Could not delete old " + output) - } - } - if (output.getParentFile() != null && !output.getParentFile().canWrite()) { - throw new GradleException("Cannot write XML output file " + output) - } - try { - flags.getReporters().add(new XmlReporter(client, output)) - } catch (IOException e) { - throw new GradleException("XML invalid argument.") - } + public void setXmlOutput(@NonNull File xmlOutput) { + mXmlOutput = xmlOutput; } /** @@ -88,8 +74,97 @@ public class Lint extends DefaultTask { * * @param sourceSets files to be added to sources. */ - public void setSources(List<Set<File>> sourceSets) { - for (Set<File> args : sourceSets) { + public void setSources(@NonNull List<Set<File>> sourceSets) { + mSourceSets = sourceSets + } + + /** + * Adds all class files in directory specified by paths for lint. + * + * @param paths A set of paths to class files separated with path separators + */ + public void setClasspath(@NonNull String paths) { + mClassPath = paths + } + + /** + * Adds all files in resourceSets as a resource file for lint. + * + * @param resourceSets files to be added to resources. + */ + public void setLintResources(@NonNull List<Set<File>> resourceSets) { + mResourceSets = resourceSets + } + + @SuppressWarnings("GroovyUnusedDeclaration") + @TaskAction + public void lint() { + IssueRegistry registry = new BuiltinIssueRegistry() + LintCliFlags flags = new LintCliFlags() + LintGradleClient client = new LintGradleClient(flags, mPlugin.getSdkDirectory()) + + // Configure Reporters + + if (mHtmlOutput != null) { + mHtmlOutput = mHtmlOutput.getAbsoluteFile() + if (mHtmlOutput.exists()) { + boolean delete = mHtmlOutput.delete() + if (!delete) { + throw new GradleException("Could not delete old " + mHtmlOutput) + } + } + if (mHtmlOutput.getParentFile() != null && !mHtmlOutput.getParentFile().canWrite()) { + throw new GradleException("Cannot write HTML output file " + mHtmlOutput) + } + try { + flags.getReporters().add(new HtmlReporter(client, mHtmlOutput)) + } catch (IOException e) { + throw new GradleException("HTML invalid argument.", e) + } + } + + if (mXmlOutput != null) { + mXmlOutput = mXmlOutput.getAbsoluteFile() + if (mXmlOutput.exists()) { + boolean delete = mXmlOutput.delete(); + if (!delete) { + throw new GradleException("Could not delete old " + mXmlOutput) + } + } + if (mXmlOutput.getParentFile() != null && !mXmlOutput.getParentFile().canWrite()) { + throw new GradleException("Cannot write XML output file " + mXmlOutput) + } + try { + flags.getReporters().add(new XmlReporter(client, mXmlOutput)) + } catch (IOException e) { + throw new GradleException("XML invalid argument.", e) + } + } + + List<Reporter> reporters = flags.getReporters() + if (reporters.isEmpty()) { + throw new GradleException("No reporter specified.") + } + + Map<String, String> map = new HashMap<String, String>(){{ + put("", "file://") + }} + for (Reporter reporter : reporters) { + reporter.setUrlMap(map) + } + + // Flags + + if (mQuiet) { + flags.setQuiet(true) + } + if (mConfigFile != null) { + flags.setDefaultConfiguration(client.createConfigurationFromFile(mConfigFile)) + } + + // Flags: sources, resources, classes + + for (Set<File> args : mSourceSets) { for (File input : args) { if (input.exists()) { List<File> sources = flags.getSourcesOverride() @@ -101,15 +176,8 @@ public class Lint extends DefaultTask { } } } - } - /** - * Adds all class files in directory specified by paths for lint. - * - * @param paths The absolute path to a directory containing class files - */ - public void setClasspath(String paths) { - for (String path : LintUtils.splitPath(paths)) { + for (String path : LintUtils.splitPath(mClassPath)) { File input = new File(path); if (!input.exists()) { throw new GradleException("Class path entry " + input + " does not exist.") @@ -121,15 +189,8 @@ public class Lint extends DefaultTask { } classes.add(input); } - } - /** - * Adds all files in resourceSets as a resource file for lint. - * - * @param resourceSets files to be added to resources. - */ - public void setLintResources(List<Set<File>> resourceSets) { - for (Set<File> args : resourceSets) { + for (Set<File> args : mResourceSets) { for (File input : args) { if (input.exists()) { List<File> resources = flags.getResourcesOverride() @@ -141,28 +202,19 @@ public class Lint extends DefaultTask { } } } - } - @TaskAction - public void lint() { - List<Reporter> reporters = flags.getReporters() - if (reporters.isEmpty()) { - throw new GradleException("No reporter specified.") - } + // Client setup - Map<String, String> map = new HashMap<String, String>(){{ - put("", "file://") - }} - for (Reporter reporter : reporters) { - reporter.setUrlMap(map) + if (mCustomRules != null) { + client.setCustomRules(mCustomRules) } - client.setCustomRules(customRules) + // Finally perform lint run try { client.run(registry, Arrays.asList(project.projectDir)); } catch (IOException e) { - throw new GradleException("Invalid arguments.") + throw new GradleException("Invalid arguments.", e) } } } |