-
Notifications
You must be signed in to change notification settings - Fork 96
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding org.ajoberstar.grgit Gradle plugin #187
Merged
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5a6285b
Adding org.ajoberstar.grgit Gradle plugin
ajoberstar 6281350
Adding functional tests for org.ajoberstar.grgit
ajoberstar 0a64535
Close repo via a build listener instead of a task
ajoberstar ba63569
log out exception instead of burying
ajoberstar 9691ecc
Remove Consumer variant of op syntax
ajoberstar cd424cd
add missing license header
ajoberstar 82d7c99
resolve sonar issues
ajoberstar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
125 changes: 125 additions & 0 deletions
125
src/compatTest/groovy/org/ajoberstar/grgit/gradle/BaseCompatTest.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,125 @@ | ||
/* | ||
* Copyright 2012-2017 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.ajoberstar.grgit.gradle | ||
|
||
import spock.lang.Specification | ||
import spock.lang.Unroll | ||
|
||
import org.ajoberstar.grgit.Grgit | ||
import org.gradle.testkit.runner.GradleRunner | ||
import org.gradle.testkit.runner.BuildResult | ||
import org.gradle.testkit.runner.TaskOutcome | ||
import org.junit.Rule | ||
import org.junit.rules.TemporaryFolder | ||
|
||
class BaseCompatTest extends Specification { | ||
@Rule TemporaryFolder tempDir = new TemporaryFolder() | ||
File projectDir | ||
File buildFile | ||
|
||
def setup() { | ||
projectDir = tempDir.newFolder('project') | ||
buildFile = projectFile('build.gradle') | ||
|
||
} | ||
|
||
def 'with no repo, plugin sets grgit to null'() { | ||
given: | ||
buildFile << '''\ | ||
plugins { | ||
id 'org.ajoberstar.grgit' | ||
} | ||
|
||
task doStuff { | ||
doLast { | ||
assert grgit == null | ||
} | ||
} | ||
''' | ||
when: | ||
def result = build('doStuff') | ||
then: | ||
result.task(':doStuff').outcome == TaskOutcome.SUCCESS | ||
} | ||
|
||
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 { | ||
doLast { | ||
println grgit.describe() | ||
} | ||
} | ||
''' | ||
when: | ||
def result = build('doStuff', '--quiet') | ||
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') | ||
|
||
buildFile << '''\ | ||
plugins { | ||
id 'org.ajoberstar.grgit' | ||
} | ||
|
||
task doStuff { | ||
doLast { | ||
println grgit.describe() | ||
} | ||
} | ||
''' | ||
when: | ||
def result = build('doStuff', '--info') | ||
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 File projectFile(String path) { | ||
File file = new File(projectDir, path) | ||
file.parentFile.mkdirs() | ||
return file | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
src/main/groovy/org/ajoberstar/grgit/gradle/GrgitPlugin.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,55 @@ | ||
/* | ||
* Copyright 2012-2017 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.ajoberstar.grgit.gradle | ||
|
||
import org.ajoberstar.grgit.Grgit | ||
import org.gradle.api.Plugin | ||
import org.gradle.api.Project | ||
import org.gradle.api.Task | ||
|
||
/** | ||
* Plugin adding a {@code grgit} property to all projects | ||
* that searches for a Git repo from the project's | ||
* directory. | ||
* @since 2.0.0 | ||
*/ | ||
class GrgitPlugin implements Plugin<Project> { | ||
private static final String CLOSE_TASK = 'gitClose' | ||
|
||
@Override | ||
void apply(Project project) { | ||
try { | ||
Grgit grgit = Grgit.open(currentDir: project.rootDir) | ||
|
||
// Make sure Git repo is closed when the build is over. Ideally, this avoids issues with the daemon. | ||
project.gradle.buildFinished { | ||
project.logger.info "Closing Git repo: ${grgit.repository.rootDir}" | ||
grgit.close() | ||
} | ||
|
||
project.allprojects { prj -> | ||
if (prj.ext.has('grgit')) { | ||
prj.logger.warn("Project ${prj.path} already has a grgit property. Remove org.ajoberstar.grgit from either ${prj.path} or ${project.path}.") | ||
} | ||
prj.ext.grgit = grgit | ||
} | ||
} catch (Exception e) { | ||
project.logger.error("No git repository found for ${project.path}. Accessing grgit will cause an NPE.") | ||
project.logger.debug("Failed trying to find git repository for ${project.path}", e) | ||
project.ext.grgit = null | ||
} | ||
} | ||
} |
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
1 change: 1 addition & 0 deletions
1
src/main/resources/META-INF/gradle-plugins/org.ajoberstar.grgit.properties
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 @@ | ||
implementation-class=org.ajoberstar.grgit.gradle.GrgitPlugin |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The type Exception should not be caught