Skip to content

Commit

Permalink
Enable task caching (Fixes #83)
Browse files Browse the repository at this point in the history
- all inputs to `licenseReport` are task inputs
- all project configurations are resolved via an `@InputFiles` annotated
  method to ensure that changing the project dependencies causes the
  task to not use cached values
  • Loading branch information
ketan committed Jun 18, 2018
1 parent a1620d9 commit c6aed1d
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 17 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ plugins {
}

group = "com.github.jk1"
version = "1.1"
version = "1.2"

sourceCompatibility = 1.6
targetCompatibility = 1.6
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,4 @@ class LicenseReportExtension {
excludes.contains("$module.moduleGroup:$module.moduleName")
}

// configuration snapshot for the up-to-date check
String getSnapshot() {
StringBuilder builder = new StringBuilder()
projects.each { builder.append(it.name) }
renderers.each { builder.append(it.class.name) }
importers.each { builder.append(it.class.name) }
filters.each { builder.append(it.class.name) }
configurations.each { builder.append(it) }
excludeGroups.each { builder.append(it) }
excludes.each { builder.append(it) }
return builder.toString()
}
}
33 changes: 30 additions & 3 deletions src/main/groovy/com/github/jk1/license/ReportTask.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,50 @@ package com.github.jk1.license

import com.github.jk1.license.reader.ProjectReader
import org.gradle.api.DefaultTask
import org.gradle.api.file.FileCollection
import org.gradle.api.logging.Logger
import org.gradle.api.logging.Logging
import org.gradle.api.tasks.CacheableTask
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.InputFiles
import org.gradle.api.tasks.OutputDirectory
import org.gradle.api.tasks.TaskAction

@CacheableTask
class ReportTask extends DefaultTask {

private Logger LOGGER = Logging.getLogger(ReportTask.class)

@InputFiles
FileCollection getClasspath() {
ProjectReader.findConfigured(getProject()).inject(project.files(), { FileCollection memo, eachConfiguration ->
memo + eachConfiguration
})
}

@Input
String getConfigurationSnapshot(){
return getProject().licenseReport.snapshot
String getConfigurationSnapshot() {
LicenseReportExtension licenseReport = getProject().licenseReport
def snapshot = []
snapshot << 'projects'
snapshot += licenseReport.projects.collect { it.path }
snapshot << 'renderers'
snapshot += licenseReport.renderers.collect { it.class.name }
snapshot << 'importers'
snapshot += licenseReport.importers.collect { it.class.name }
snapshot << 'filters'
snapshot += licenseReport.filters.collect { it.class.name }
snapshot << 'configurations '
snapshot += licenseReport.configurations
snapshot << 'exclude'
snapshot += licenseReport.excludeGroups
snapshot << 'excludes'
snapshot += licenseReport.excludes
snapshot.join("!")
}

@OutputDirectory
File getOutputFolder(){
File getOutputFolder() {
return new File(getProject().licenseReport.outputDir)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class ProjectReader {
}
}

private static Set<Configuration> findConfigured(Project project) {
static Set<Configuration> findConfigured(Project project) {
project.configurations.findAll { config -> config.name in project.licenseReport.configurations }
}

Expand Down
105 changes: 105 additions & 0 deletions src/test/groovy/com/github/jk1/license/ReportTaskTestSpec.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* Copyright 2018 Evgeny Naumenko <jk.vc@mail.ru>
*
* 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.github.jk1.license

import org.gradle.testkit.runner.BuildResult
import org.gradle.testkit.runner.GradleRunner
import org.gradle.testkit.runner.TaskOutcome
import org.junit.Rule
import org.junit.rules.TemporaryFolder
import spock.lang.Specification

class ReportTaskTestSpec extends Specification {
@Rule
final TemporaryFolder testProjectDir = new TemporaryFolder()

File buildFile
File localBuildCacheDirectory

def setup() {
buildFile = testProjectDir.newFile('build.gradle')
localBuildCacheDirectory = testProjectDir.newFolder('.local-cache')
testProjectDir.newFile('settings.gradle') << """
buildCache {
local {
directory '${localBuildCacheDirectory.toURI()}'
}
}
"""

}

def "should cache task outputs"() {
given:
buildFile << """
plugins {
id 'com.github.jk1.dependency-license-report'
}
repositories {
mavenCentral()
}
apply plugin: 'java'
dependencies {
compile "junit:junit:\${project.ext.junitVersion}"
}
"""

when:
BuildResult result = GradleRunner.create()
.withPluginClasspath()
.withProjectDir(testProjectDir.getRoot())
.withArguments('--build-cache', "generateLicenseReport", "-PjunitVersion=4.12")
.build()

then:
result.task(':generateLicenseReport').outcome == TaskOutcome.SUCCESS

when:
result = GradleRunner.create()
.withPluginClasspath()
.withProjectDir(testProjectDir.getRoot())
.withArguments('--build-cache', "generateLicenseReport", "-PjunitVersion=4.12")
.build()

then:
result.task(':generateLicenseReport').outcome == TaskOutcome.UP_TO_DATE

when:
result = GradleRunner.create()
.withPluginClasspath()
.withProjectDir(testProjectDir.getRoot())
.withArguments('--build-cache', "clean", "generateLicenseReport", "-PjunitVersion=4.12")
.build()

then:
result.task(':generateLicenseReport').outcome == TaskOutcome.FROM_CACHE

when:
result = GradleRunner.create()
.withPluginClasspath()
.withProjectDir(testProjectDir.getRoot())
.withArguments('--build-cache', "generateLicenseReport", "-PjunitVersion=4.11")
.build()

then:
result.task(':generateLicenseReport').outcome == TaskOutcome.SUCCESS

}
}

0 comments on commit c6aed1d

Please sign in to comment.