-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add configuration caching support to grgit.
- Loading branch information
1 parent
e8b01fd
commit 1982af3
Showing
4 changed files
with
191 additions
and
13 deletions.
There are no files selected for viewing
101 changes: 101 additions & 0 deletions
101
grgit-gradle/src/compatTest/groovy/org/ajoberstar/grgit/gradle/ConfigCacheTest.groovy
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,101 @@ | ||
package org.ajoberstar.grgit.gradle | ||
|
||
import org.ajoberstar.grgit.Grgit | ||
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 ConfigCacheTest extends Specification { | ||
@Rule TemporaryFolder tempDir = new TemporaryFolder() | ||
File projectDir | ||
File buildFile | ||
|
||
def setup() { | ||
projectDir = tempDir.newFolder('project') | ||
buildFile = projectFile('build.gradle') | ||
} | ||
|
||
def "grgit build service can be fetched from registered services"() { | ||
given: | ||
buildFile << """ | ||
import org.ajoberstar.grgit.gradle.GrgitBuildService | ||
plugins { | ||
id 'org.ajoberstar.grgit' | ||
} | ||
task doStuff { | ||
def injected = project.gradle.sharedServices.registrations.getByName("grgit").getService() | ||
doLast { | ||
assert injected.get().grgit == null | ||
} | ||
} | ||
""" | ||
|
||
when: | ||
runner() | ||
.withArguments('--configuration-cache', 'doStuff') | ||
.build() | ||
|
||
and: | ||
def result = runner() | ||
.withArguments('--configuration-cache', 'doStuff') | ||
.build() | ||
|
||
then: | ||
result.output.contains('Reusing configuration cache.') | ||
} | ||
|
||
|
||
def 'with repo, plugin opens the repo as grgit'() { | ||
given: | ||
Grgit git = Grgit.init(dir: projectDir) | ||
projectFile('1.txt') << '1' | ||
git.add(patterns: ['1.txt']) | ||
git.commit(message: 'yay') | ||
git.tag.add(name: '1.0.0') | ||
|
||
buildFile << '''\ | ||
plugins { | ||
id 'org.ajoberstar.grgit' | ||
} | ||
task doStuff { | ||
def injected = project.grgitExtension | ||
doLast { | ||
println injected.describe() | ||
} | ||
} | ||
''' | ||
when: | ||
runner() | ||
.withArguments('--configuration-cache', 'doStuff') | ||
.build() | ||
|
||
and: | ||
def result = runner() | ||
.withArguments('--configuration-cache', 'doStuff') | ||
.build() | ||
then: | ||
result.task(':doStuff').outcome == TaskOutcome.SUCCESS | ||
result.output.contains('Reusing configuration cache.') | ||
result.output.contains('1.0.0\n') | ||
} | ||
|
||
private GradleRunner runner(String... args) { | ||
return GradleRunner.create() | ||
.withGradleVersion("6.6-milestone-3") | ||
.withPluginClasspath() | ||
.withProjectDir(projectDir) | ||
.forwardOutput() | ||
.withArguments((args + '--stacktrace') as String[]) | ||
} | ||
|
||
private File projectFile(String path) { | ||
File file = new File(projectDir, path) | ||
file.parentFile.mkdirs() | ||
return file | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
grgit-gradle/src/main/groovy/org/ajoberstar/grgit/gradle/GrgitBuildService.groovy
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,41 @@ | ||
package org.ajoberstar.grgit.gradle | ||
|
||
import org.ajoberstar.grgit.Grgit | ||
import org.gradle.api.file.DirectoryProperty | ||
import org.gradle.api.logging.Logger | ||
import org.gradle.api.logging.Logging | ||
import org.gradle.api.services.BuildService | ||
import org.gradle.api.services.BuildServiceParameters | ||
|
||
abstract class GrgitBuildService implements BuildService<GrgitBuildService.Params>, AutoCloseable { | ||
|
||
private static final Logger LOGGER = Logging.getLogger(GrgitBuildService.class); | ||
|
||
interface Params extends BuildServiceParameters { | ||
DirectoryProperty getRootDirectory(); | ||
} | ||
|
||
Grgit grgit; | ||
|
||
GrgitBuildService() { | ||
try { | ||
grgit = Grgit.open(currentDir: parameters.rootDirectory.get()) | ||
} catch (Exception e) { | ||
LOGGER.debug("Failed trying to find git repository for ${parameters.rootDirectory.get()}", e) | ||
grgit = null | ||
} | ||
} | ||
|
||
@Delegate | ||
public Grgit lookup() { | ||
return grgit; | ||
} | ||
|
||
@Override | ||
public void close() throws Exception { | ||
if (grgit != null) { | ||
LOGGER.info("Closing Git repo: ${grgit.repository.rootDir}") | ||
grgit.close() | ||
} | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
grgit-gradle/src/main/groovy/org/ajoberstar/grgit/gradle/GrgitExtension.groovy
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,17 @@ | ||
package org.ajoberstar.grgit.gradle | ||
|
||
import org.ajoberstar.grgit.Grgit | ||
import org.gradle.api.provider.Provider | ||
|
||
public class GrgitExtension { | ||
public final Provider<GrgitBuildService> grgitBuildServiceProvider; | ||
|
||
public GrgitExtension(Provider<GrgitBuildService> grgitBuildServiceProvider) { | ||
this.grgitBuildServiceProvider = grgitBuildServiceProvider | ||
} | ||
|
||
@Delegate | ||
public Grgit lookup() { | ||
return grgitBuildServiceProvider.get().grgit; | ||
} | ||
} |
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