Skip to content

Commit

Permalink
Introduce TextResourceFactory
Browse files Browse the repository at this point in the history
- cleaner API/DSL for creating text resources
- more modular (factory methods no longer declared in ResourceHandler)
  • Loading branch information
pniederw committed Oct 8, 2014
1 parent 038e8fd commit 8862b15
Show file tree
Hide file tree
Showing 17 changed files with 151 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ abstract class AbstractFindBugsPluginIntegrationTest extends AbstractIntegration
findbugs {
effort 'max'
reportLevel 'high'
includeFilterConfig resources.fileText(file('include.xml'))
includeFilterConfig resources.text.fromFile('include.xml')
excludeFilter file('exclude.xml')
visitors = ['FindDeadLocalStores', 'UnreadFields']
omitVisitors = ['WaitInLoop', 'UnnecessaryMath']
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ class PmdPluginIntegrationTest extends WellBehavedPluginTest {
buildFile << """
pmd {
ruleSets = []
ruleSetConfig = resources.text('''${customRuleSetText()}''')
ruleSetConfig = resources.text.fromString('''${customRuleSetText()}''')
}
"""

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class CheckstyleExtension extends CodeQualityExtension {
* The Checkstyle configuration file to use.
*/
void setConfigFile(File configFile) {
setConfig(prj.resources.fileText(configFile))
setConfig(prj.resources.text.fromFile(configFile))
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class CheckstylePlugin extends AbstractCodeQualityPlugin<Checkstyle> {

extension.with {
toolVersion = "5.7"
config = project.resources.fileText(project.file("config/checkstyle/checkstyle.xml"))
config = project.resources.text.fromFile("config/checkstyle/checkstyle.xml")
}

return extension
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class CodeNarcExtension extends CodeQualityExtension {
* The CodeNarc configuration file to use.
*/
void setConfigFile(File file) {
setConfig(prj.resources.fileText(file))
setConfig(prj.resources.text.fromFile(file))
}

void setReportFormat(String reportFormat) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class CodeNarcPlugin extends AbstractCodeQualityPlugin<CodeNarc> {
extension = project.extensions.create("codenarc", CodeNarcExtension, project)
extension.with {
toolVersion = "0.21"
config = project.resources.fileText(project.rootProject.file("config/codenarc/codenarc.xml"))
config = project.resources.text.fromFile(project.rootProject.file("config/codenarc/codenarc.xml"))
maxPriority1Violations = 0
maxPriority2Violations = 0
maxPriority3Violations = 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ class FindBugsExtension extends CodeQualityExtension {
* The filename of a filter specifying which bugs are reported.
*/
void setIncludeFilter(File filter) {
setIncludeFilterConfig(prj.resources.fileText(filter))
setIncludeFilterConfig(prj.resources.text.fromFile(filter))
}

/**
Expand All @@ -120,6 +120,6 @@ class FindBugsExtension extends CodeQualityExtension {
* The filename of a filter specifying bugs to exclude from being reported.
*/
void setExcludeFilter(File filter) {
setExcludeFilterConfig(prj.resources.fileText(filter))
setExcludeFilterConfig(prj.resources.text.fromFile(filter))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class PmdExtension extends CodeQualityExtension {
* <a href="http://pmd.sourceforge.net/howtomakearuleset.html">official documentation</a>
* for how to author a rule set.
*
* Example: ruleSetConfig = resources.fileText(file("config/pmd/myRuleSet.xml"))
* Example: ruleSetConfig = resources.text.fromFile("config/pmd/myRuleSet.xml")
*
* @since 2.2
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ class CheckstylePluginTest extends Specification {

project.checkstyle {
sourceSets = [project.sourceSets.main]
config = project.resources.fileText(project.file("checkstyle-config"))
config = project.resources.text.fromFile("checkstyle-config")
configProperties = [foo: "foo"]
reportsDir = project.file("checkstyle-reports")
ignoreFailures = true
Expand Down Expand Up @@ -159,7 +159,7 @@ class CheckstylePluginTest extends Specification {
def "can customize any additional checkstyle tasks via extension"() {
def task = project.tasks.create("checkstyleCustom", Checkstyle)
project.checkstyle {
config = project.resources.fileText(project.file("checkstyle-config"))
config = project.resources.text.fromFile("checkstyle-config")
configProperties = [foo: "foo"]
reportsDir = project.file("checkstyle-reports")
ignoreFailures = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ class CodeNarcPluginTest extends Specification {
def task = project.tasks.create("codenarcCustom", CodeNarc)

project.codenarc {
config = project.resources.fileText(project.file("codenarc-config"))
config = project.resources.text.fromFile("codenarc-config")
maxPriority1Violations = 10
maxPriority2Violations = 50
maxPriority3Violations = 200
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,8 @@ class FindBugsPluginTest extends Specification {
reportLevel = 'high'
visitors = ['org.gradle.Class']
omitVisitors = ['org.gradle.Interface']
includeFilterConfig = project.resources.fileText(project.file("include.txt"))
excludeFilterConfig = project.resources.fileText(project.file("exclude.txt"))
includeFilterConfig = project.resources.text.fromFile("include.txt")
excludeFilterConfig = project.resources.text.fromFile("exclude.txt")
}

expect:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ class PmdPluginTest extends Specification {
project.pmd {
sourceSets = [project.sourceSets.main]
ruleSets = ["braces", "unusedcode"]
ruleSetConfig = project.resources.text("ruleset contents")
ruleSetConfig = project.resources.text.fromString("ruleset contents")
ruleSetFiles = project.files("my-ruleset.xml")
reportsDir = project.file("pmd-reports")
ignoreFailures = true
Expand Down Expand Up @@ -183,7 +183,7 @@ class PmdPluginTest extends Specification {
def task = project.tasks.create("pmdCustom", Pmd)
project.pmd {
ruleSets = ["braces", "unusedcode"]
ruleSetConfig = project.resources.text("ruleset contents")
ruleSetConfig = project.resources.text.fromString("ruleset contents")
ruleSetFiles = project.files("my-ruleset.xml")
reportsDir = project.file("pmd-reports")
ignoreFailures = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,21 @@ dependencies {
}

task stringText(type: MyTask) {
config = resources.text("my config")
config = resources.text.fromString("my config")
output = project.file("output.txt")
}

task fileText(type: MyTask) {
config = resources.fileText("config.txt")
config = resources.text.fromFile("config.txt")
output = project.file("output.txt")
}

task fileCollectionText(type: MyTask) {
config = resources.fileText(generateConfigFile)
config = resources.text.fromFile(generateConfigFile)
output = project.file("output.txt")
}

task archiveEntryText(type: MyTask) {
config = resources.archiveEntryText(configurations.sharedConfig, "config.txt")
config = resources.text.fromArchiveEntry(configurations.sharedConfig, "config.txt")
output = project.file("output.txt")
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,15 @@
import org.gradle.api.internal.file.archive.compression.GzipArchiver;
import org.gradle.api.resources.ReadableResource;
import org.gradle.api.resources.ResourceHandler;
import org.gradle.api.resources.TextResource;

import java.nio.charset.Charset;
import org.gradle.api.resources.TextResourceFactory;

public class DefaultResourceHandler implements ResourceHandler {
private final FileOperations fileOperations;
private final TemporaryFileProvider tempFileProvider;
private final TextResourceFactory textResourceFactory;

public DefaultResourceHandler(FileOperations fileOperations, TemporaryFileProvider tempFileProvider) {
this.fileOperations = fileOperations;
this.tempFileProvider = tempFileProvider;
textResourceFactory = new DefaultTextResourceFactory(fileOperations, tempFileProvider);
}

public ReadableResource gzip(Object path) {
Expand All @@ -53,23 +51,7 @@ public ReadableResource maybeCompressed(Object tarPath) {
}
}

public TextResource text(String string) {
return new StringBackedTextResource(tempFileProvider, string);
}

public TextResource fileText(Object file) {
return fileText(file, Charset.defaultCharset().name());
}

public TextResource fileText(Object file, String charset) {
return new FileCollectionBackedTextResource(fileOperations.files(file), Charset.forName(charset));
}

public TextResource archiveEntryText(Object archive, String entryPath) {
return archiveEntryText(archive, entryPath, Charset.defaultCharset().name());
}

public TextResource archiveEntryText(Object archive, String entryPath, String charset) {
return new FileCollectionBackedArchiveTextResource(fileOperations, fileOperations.files(archive), entryPath, Charset.forName(charset));
public TextResourceFactory getText() {
return textResourceFactory;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright 2014 the original author or authors.
*
* 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 org.gradle.api.internal.resources;

import org.gradle.api.internal.file.FileOperations;
import org.gradle.api.internal.file.TemporaryFileProvider;
import org.gradle.api.resources.TextResource;
import org.gradle.api.resources.TextResourceFactory;

import java.nio.charset.Charset;

public class DefaultTextResourceFactory implements TextResourceFactory {
private final FileOperations fileOperations;
private final TemporaryFileProvider tempFileProvider;

public DefaultTextResourceFactory(FileOperations fileOperations, TemporaryFileProvider tempFileProvider) {
this.fileOperations = fileOperations;
this.tempFileProvider = tempFileProvider;
}

public TextResource fromString(String string) {
return new StringBackedTextResource(tempFileProvider, string);
}

public TextResource fromFile(Object file, String charset) {
return new FileCollectionBackedTextResource(fileOperations.files(file), Charset.forName(charset));
}

public TextResource fromFile(Object file) {
return fromFile(file, Charset.defaultCharset().name());

}

public TextResource fromArchiveEntry(Object archive, String entryPath, String charset) {
return new FileCollectionBackedArchiveTextResource(fileOperations, fileOperations.files(archive), entryPath, Charset.forName(charset));
}

public TextResource fromArchiveEntry(Object archive, String entryPath) {
return fromArchiveEntry(archive, entryPath, Charset.defaultCharset().name());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,57 +40,13 @@ public interface ResourceHandler {
ReadableResource bzip2(Object path);

/**
* Creates a text resource backed by the given string.
*
* @param string a string
* @return a text resource backed by the given string
*
* @since 2.2
*/
@Incubating
TextResource text(String string);

/**
* Creates a text resource backed by the given file.
*
* @param file a text file evaluated as per {@link org.gradle.api.Project#files(Object...)}
* @param charset the file's character encoding (e.g. {@code "utf-8"})
* @return a text resource backed by the given file
* Returns a factory for creating {@code TextResource}s from various sources such as
* strings, files, and archive entries.
*
* @since 2.2
*/
@Incubating
TextResource fileText(Object file, String charset);

/**
* Same as {@code text(file, Charset.defaultCharset())}.
*
* @since 2.2
*/
@Incubating
TextResource fileText(Object file);

/**
* Creates a text resource backed by the archive entry at the given path within the given archive.
* The archive format is determined based on the archive's file extension. If the archive format
* is not supported or cannot be determined, any attempt to access the resource will fail with an exception.
*
* @param archive an archive file evaluated as per {@link org.gradle.api.Project#files(Object...)}
* @param entryPath the path to an archive entry
* @param charset the archive entry's character encoding (e.g. {@code "utf-8"})
*
* @return a text resource backed by the archive entry at the given path within the given archive
*
* @since 2.2
*/
@Incubating
TextResource archiveEntryText(Object archive, String entryPath, String charset);

/**
* Same as {@code archiveEntryText(archive, path, Charset.defaultCharset().name())}.
*
* @since 2.2
* @return a factory for creating {@code TextResource}s
*/
@Incubating
TextResource archiveEntryText(Object archive, String path);
TextResourceFactory getText();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright 2014 the original author or authors.
*
* 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 org.gradle.api.resources;

import org.gradle.api.Incubating;

/**
* Creates {@code TextResource}s backed by sources such as strings, files, and archive entries.
*
* @since 2.2
*/
@Incubating
public interface TextResourceFactory {
/**
* Creates a text resource backed by the given string.
*
* @param string a string
* @return a text resource backed by the given string
*/
TextResource fromString(String string);

/**
* Creates a text resource backed by the given file.
*
* @param file a text file evaluated as per {@link org.gradle.api.Project#files(Object...)}
* @param charset the file's character encoding (e.g. {@code "utf-8"})
* @return a text resource backed by the given file
*/
TextResource fromFile(Object file, String charset);

/**
* Same as {@code fromFile(file, Charset.defaultCharset())}.
*/
TextResource fromFile(Object file);

/**
* Creates a text resource backed by the archive entry at the given path within the given archive.
* The archive format is determined based on the archive's file extension. If the archive format
* is not supported or cannot be determined, any attempt to access the resource will fail with an exception.
*
* @param archive an archive file evaluated as per {@link org.gradle.api.Project#files(Object...)}
* @param entryPath the path to an archive entry
* @param charset the archive entry's character encoding (e.g. {@code "utf-8"})
*
* @return a text resource backed by the archive entry at the given path within the given archive
*/
TextResource fromArchiveEntry(Object archive, String entryPath, String charset);

/**
* Same as {@code fromArchiveEntry(archive, path, Charset.defaultCharset().name())}.
*/
TextResource fromArchiveEntry(Object archive, String path);
}

0 comments on commit 8862b15

Please sign in to comment.