-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #354 from ajoberstar/config-cache
Introduce the grgit-service plugin
- Loading branch information
Showing
10 changed files
with
315 additions
and
42 deletions.
There are no files selected for viewing
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
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
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
109 changes: 109 additions & 0 deletions
109
...dle/src/compatTest/groovy/org/ajoberstar/grgit/gradle/GrgitServicePluginCompatTest.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,109 @@ | ||
package org.ajoberstar.grgit.gradle | ||
|
||
import spock.lang.Specification | ||
|
||
import org.ajoberstar.grgit.Grgit | ||
import org.gradle.testkit.runner.GradleRunner | ||
import org.gradle.testkit.runner.BuildResult | ||
import org.gradle.testkit.runner.TaskOutcome | ||
import spock.lang.TempDir | ||
|
||
class GrgitServicePluginCompatTest extends Specification { | ||
@TempDir File tempDir | ||
File projectDir | ||
File buildFile | ||
|
||
def setup() { | ||
projectDir = new File(tempDir, 'project') | ||
buildFile = projectFile('build.gradle') | ||
buildFile << '''\ | ||
import org.ajoberstar.grgit.gradle.GrgitService | ||
plugins { | ||
id 'org.ajoberstar.grgit-service' | ||
} | ||
tasks.register("doStuff", DoStuffTask.class) { | ||
service = grgitService.service | ||
} | ||
class DoStuffTask extends DefaultTask { | ||
@Input | ||
final Property<GrgitService> service | ||
@Inject | ||
DoStuffTask(ObjectFactory objectFactory) { | ||
this.service = objectFactory.property(GrgitService.class); | ||
} | ||
@TaskAction | ||
void execute() { | ||
println service.get().grgit.describe() | ||
} | ||
} | ||
''' | ||
} | ||
|
||
def 'with no repo, accessing service fails'() { | ||
given: | ||
// nothing | ||
when: | ||
def result = buildAndFail('doStuff', '--configuration-cache') | ||
then: | ||
result.task(':doStuff').outcome == TaskOutcome.FAILED | ||
} | ||
|
||
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') | ||
when: | ||
def result = build('doStuff', '--quiet', '--configuration-cache') | ||
then: | ||
result.task(':doStuff').outcome == TaskOutcome.SUCCESS | ||
result.output.normalize() == '1.0.0\n' | ||
} | ||
|
||
def 'with repo, plugin closes the repo after build is finished'() { | ||
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') | ||
when: | ||
def result = build('doStuff', '--info', '--configuration-cache') | ||
then: | ||
result.task(':doStuff').outcome == TaskOutcome.SUCCESS | ||
result.output.contains('Closing Git repo') | ||
} | ||
|
||
private BuildResult build(String... args) { | ||
return GradleRunner.create() | ||
.withGradleVersion(System.properties['compat.gradle.version']) | ||
.withPluginClasspath() | ||
.withProjectDir(projectDir) | ||
.forwardOutput() | ||
.withArguments((args + '--stacktrace') as String[]) | ||
.build() | ||
} | ||
|
||
private BuildResult buildAndFail(String... args) { | ||
return GradleRunner.create() | ||
.withGradleVersion(System.properties['compat.gradle.version']) | ||
.withPluginClasspath() | ||
.withProjectDir(projectDir) | ||
.forwardOutput() | ||
.withArguments((args + '--stacktrace') as String[]) | ||
.buildAndFail() | ||
} | ||
|
||
private File projectFile(String path) { | ||
File file = new File(projectDir, path) | ||
file.parentFile.mkdirs() | ||
return file | ||
} | ||
} |
36 changes: 0 additions & 36 deletions
36
grgit-gradle/src/main/groovy/org/ajoberstar/grgit/gradle/GrgitPlugin.groovy
This file was deleted.
Oops, something went wrong.
27 changes: 27 additions & 0 deletions
27
grgit-gradle/src/main/java/org/ajoberstar/grgit/gradle/GrgitPlugin.java
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,27 @@ | ||
package org.ajoberstar.grgit.gradle; | ||
|
||
import org.ajoberstar.grgit.Grgit; | ||
import org.gradle.api.Plugin; | ||
import org.gradle.api.Project; | ||
import org.gradle.api.provider.Provider; | ||
|
||
/** | ||
* Plugin adding a {@code grgit} property to all projects that searches for a Git repo from the | ||
* project's directory. | ||
* | ||
* @since 2.0.0 | ||
*/ | ||
public class GrgitPlugin implements Plugin<Project> { | ||
@Override | ||
public void apply(Project project) { | ||
project.getPluginManager().apply(GrgitServicePlugin.class); | ||
var serviceExtension = project.getExtensions().getByType(GrgitServiceExtension.class); | ||
try { | ||
project.getLogger().info("The org.ajoberstar.grgit plugin eagerly opens a Grgit instance. Use org.ajoberstar.grgit-service for better performance."); | ||
project.getExtensions().add(Grgit.class, "grgit", serviceExtension.getService().get().getGrgit()); | ||
} catch (Exception e) { | ||
project.getLogger().debug("Failed to open Grgit instance", e); | ||
project.getExtensions().getExtraProperties().set("grgit", null); | ||
} | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
grgit-gradle/src/main/java/org/ajoberstar/grgit/gradle/GrgitService.java
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,58 @@ | ||
package org.ajoberstar.grgit.gradle; | ||
|
||
import javax.inject.Inject; | ||
|
||
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.provider.Property; | ||
import org.gradle.api.services.BuildService; | ||
import org.gradle.api.services.BuildServiceParameters; | ||
|
||
public abstract class GrgitService implements BuildService<GrgitService.Params>, AutoCloseable { | ||
private static final Logger logger = Logging.getLogger(GrgitService.class); | ||
|
||
public interface Params extends BuildServiceParameters { | ||
DirectoryProperty getCurrentDirectory(); | ||
|
||
DirectoryProperty getDirectory(); | ||
|
||
Property<Boolean> getInitIfNotExists(); | ||
} | ||
|
||
private final Grgit grgit; | ||
|
||
@Inject | ||
public GrgitService() { | ||
if (getParameters().getCurrentDirectory().isPresent()) { | ||
this.grgit = Grgit.open(op -> { | ||
op.setCurrentDir(getParameters().getCurrentDirectory().get().getAsFile()); | ||
}); | ||
return; | ||
} | ||
|
||
var dir = getParameters().getCurrentDirectory().get().getAsFile(); | ||
if (dir.exists()) { | ||
this.grgit = Grgit.open(op -> { | ||
op.setDir(dir); | ||
}); | ||
} else if (getParameters().getInitIfNotExists().get()) { | ||
this.grgit = Grgit.init(op -> { | ||
op.setDir(dir); | ||
}); | ||
} else { | ||
throw new IllegalStateException("No Git repo exists at " + dir + " and initIfNotExists is false. Cannot proceed."); | ||
} | ||
} | ||
|
||
public Grgit getGrgit() { | ||
return grgit; | ||
} | ||
|
||
@Override | ||
public void close() throws Exception { | ||
logger.info("Closing Git repo: {}", grgit.getRepository().getRootDir()); | ||
grgit.close(); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
grgit-gradle/src/main/java/org/ajoberstar/grgit/gradle/GrgitServiceExtension.java
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,19 @@ | ||
package org.ajoberstar.grgit.gradle; | ||
|
||
import javax.inject.Inject; | ||
|
||
import org.gradle.api.model.ObjectFactory; | ||
import org.gradle.api.provider.Property; | ||
|
||
public class GrgitServiceExtension { | ||
private Property<GrgitService> service; | ||
|
||
@Inject | ||
public GrgitServiceExtension(ObjectFactory objectFactory) { | ||
this.service = objectFactory.property(GrgitService.class); | ||
} | ||
|
||
public Property<GrgitService> getService() { | ||
return service; | ||
} | ||
} |
Oops, something went wrong.