Skip to content

Commit

Permalink
Adding org.ajoberstar.grgit Gradle plugin
Browse files Browse the repository at this point in the history
This is being migrated from gradle-git. Differences from 1.7.x
gradle-git:

- Allow applying to projects besides the rootProject
- Warn if project was applied to root project and a subproject
- Add a close task that finalizes all other tasks

This resolves #178.
  • Loading branch information
ajoberstar committed Jul 30, 2017
1 parent 0e4660e commit 5a6285b
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 0 deletions.
22 changes: 22 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
plugins {
id 'org.ajoberstar.defaults' version '0.7.3'
id 'java-gradle-plugin'
id 'groovy'
}

Expand All @@ -19,6 +20,9 @@ dependencies {
compileOnly 'org.codehaus.groovy:groovy-all:2.4.12'
testCompile 'org.codehaus.groovy:groovy-all:2.4.12'

// gradle
compileOnly gradleApi()

// jgit
compile "org.eclipse.jgit:org.eclipse.jgit:$jgitVersion"
compile "org.eclipse.jgit:org.eclipse.jgit.ui:$jgitVersion"
Expand Down Expand Up @@ -65,6 +69,24 @@ model {
}
}

pluginBundle {
website = 'https://github.com/ajoberstar/grgit'
vcsUrl = 'https://github.com/ajoberstar/grgit.git'
description = 'The Groovy way to use Git'
plugins {
publishPlugin {
id = 'org.ajoberstar.grgit'
displayName = 'The Groovy way to use Git'
tags = ['git', 'groovy']
}
}
mavenCoordinates {
groupId = project.group
artifactId = project.name
version = project.version
}
}

wrapper {
gradleVersion = '3.5.1'
}
65 changes: 65 additions & 0 deletions src/main/groovy/org/ajoberstar/grgit/gradle/GrgitPlugin.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* 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)

Task closeTask = createCloseTask(project, grgit)

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

prj.tasks.all { task ->
if (task.name != CLOSE_TASK) {
task.finalizedBy(closeTask)
}
}
}
} catch (Exception ignored) {
project.logger.error('No git repository found for ${project.path}. Accessing grgit will cause an NPE.')
project.ext.grgit = null
}
}

private Task createCloseTask(Project project, Grgit grgit) {
Task task = project.tasks.create(CLOSE_TASK)
task.doLast {
grgit.close()
}
return task
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
implementation-class=org.ajoberstar.grgit.gradle.GrgitPlugin

0 comments on commit 5a6285b

Please sign in to comment.