Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ buildscript {
}
dependencies {
classpath "com.gradle.publish:plugin-publish-plugin:0.9.9"
classpath "gradle.plugin.com.gorylenko.gradle-git-properties:gradle-git-properties:1.4.21"
}
}

apply plugin: "com.gradle.plugin-publish"
apply plugin: "com.gorylenko.gradle-git-properties"
apply plugin: 'maven-publish'
apply plugin: 'groovy'

Expand All @@ -28,6 +30,12 @@ dependencies {
version = "1.5.0"
group = "com.gorylenko.gradle-git-properties"

gitProperties {
keys = ['git.branch','git.commit.id','git.commit.time']
dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
dateFormatTimeZone = "PST"
}

tasks.withType(GroovyCompile) {
targetCompatibility = JavaVersion.VERSION_1_8
}
Expand Down
5 changes: 3 additions & 2 deletions src/main/groovy/com/gorylenko/GitPropertiesPlugin.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class GitPropertiesPlugin implements Plugin<Project> {
private static final String KEY_GIT_TOTAL_COMMIT_COUNT = "git.total.commit.count"
private static final String KEY_GIT_DIRTY = "git.dirty"

private static final String[] KEY_ALL = [
public static final String[] KEY_ALL = [
KEY_GIT_BRANCH,
KEY_GIT_COMMIT_ID, KEY_GIT_COMMIT_ID_ABBREVIATED,
KEY_GIT_COMMIT_USER_NAME, KEY_GIT_COMMIT_USER_EMAIL,
Expand Down Expand Up @@ -75,7 +75,6 @@ class GitPropertiesPlugin implements Plugin<Project> {
}

static class GenerateGitPropertiesTask extends DefaultTask {
private final File dotGitDirectory = getDotGitDirectory(project)

GenerateGitPropertiesTask() {
// Description for the task
Expand All @@ -84,6 +83,7 @@ class GitPropertiesPlugin implements Plugin<Project> {

@InputFiles
public FileTree getSource() {
File dotGitDirectory = getDotGitDirectory(project)
return (dotGitDirectory == null) ? project.files().asFileTree : project.files(dotGitDirectory).asFileTree
}

Expand All @@ -98,6 +98,7 @@ class GitPropertiesPlugin implements Plugin<Project> {
def source = getSource()
if (!project.gitProperties.failOnNoGitDirectory && source.empty)
return
File dotGitDirectory = getDotGitDirectory(project)
logger.info "dotGitDirectory = [${dotGitDirectory.absolutePath}]"
def repo = Grgit.open(dir: dotGitDirectory)
def dir = project.gitProperties.gitPropertiesDir ?: new File(project.buildDir, DEFAULT_OUTPUT_DIR)
Expand Down
51 changes: 27 additions & 24 deletions src/test/groovy/com/gorylenko/GitPropertiesPluginTests.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package com.gorylenko

import org.gradle.api.Project
import org.gradle.testfixtures.ProjectBuilder
import org.junit.After
import org.junit.Before
import org.junit.Test

import static org.junit.Assert.assertFalse
Expand All @@ -10,15 +12,30 @@ import static org.junit.Assert.assertTrue

class GitPropertiesPluginTests {

File projectDir

@Before
public void setUp() throws Exception {
projectDir = File.createTempDir("gradle-git-properties", ".tmp");
}

@After
public void tearDown() throws Exception {
projectDir.deleteDir()
}

@Test
public void testGenerate() {
def projectDir = new File('.')

// copy this project dir to temp directory (including git repository folder)
new AntBuilder().copy(todir: projectDir) { fileset(dir : ".") }

Project project = ProjectBuilder.builder().withProjectDir(projectDir).build()
project.pluginManager.apply 'com.gorylenko.gradle-git-properties'

// FIXME: Didn't find any way to change `rootProject`, so just set the property.
project.gitProperties.dotGitDirectory = projectDir
GitPropertiesPluginExtension ext = project.getExtensions().getByName("gitProperties")
ext.dotGitDirectory = new File('.git')

def task = project.tasks.generateGitProperties
assertTrue(task instanceof GitPropertiesPlugin.GenerateGitPropertiesTask)
Expand All @@ -29,34 +46,21 @@ class GitPropertiesPluginTests {

Properties properties = new Properties()
properties.load(new FileInputStream(gitPropertiesFile))
assertNotNull(properties.getProperty("git.branch"))
assertNotNull(properties.getProperty("git.commit.id"))
assertNotNull(properties.getProperty("git.commit.id.abbrev"))
assertNotNull(properties.getProperty("git.commit.user.name"))
assertNotNull(properties.getProperty("git.commit.user.email"))
assertNotNull(properties.getProperty("git.commit.message.short"))
assertNotNull(properties.getProperty("git.commit.message.full"))
assertNotNull(properties.getProperty("git.commit.time"))
assertNotNull(properties.getProperty("git.commit.id.describe"))
assertNotNull(properties.getProperty("git.remote.origin.url"))
assertNotNull(properties.getProperty("git.tags"))
assertNotNull(properties.getProperty("git.closest.tag.name"))
assertNotNull(properties.getProperty("git.closest.tag.commit.count"))
assertNotNull(properties.getProperty("git.total.commit.count"))
assertNotNull(properties.getProperty("git.dirty"))
GitPropertiesPlugin.KEY_ALL.each{
assertNotNull(properties.getProperty(it))
}
}

@Test
public void testGenerateWithMissingGitRepoShouldNotFail() {
def projectDir = File.createTempDir("gradle-git-properties", ".tmp");
projectDir.deleteOnExit()

Project project = ProjectBuilder.builder().withProjectDir(projectDir).build()
project.pluginManager.apply 'com.gorylenko.gradle-git-properties'

// FIXME: Didn't find any way to change `rootProject`, so just set the property.
project.gitProperties.dotGitDirectory = projectDir
project.gitProperties.failOnNoGitDirectory = false;
GitPropertiesPluginExtension ext = project.getExtensions().getByName("gitProperties")
ext.dotGitDirectory = projectDir
ext.failOnNoGitDirectory = false;

def task = project.tasks.generateGitProperties
assertTrue(task instanceof GitPropertiesPlugin.GenerateGitPropertiesTask)
Expand All @@ -69,14 +73,13 @@ class GitPropertiesPluginTests {

@Test
public void testGenerateWithMissingGitRepoShouldFail() {
def projectDir = File.createTempDir("gradle-git-properties", ".tmp");
projectDir.deleteOnExit()

Project project = ProjectBuilder.builder().withProjectDir(projectDir).build()
project.pluginManager.apply 'com.gorylenko.gradle-git-properties'

// FIXME: Didn't find any way to change `rootProject`, so just set the property.
project.gitProperties.dotGitDirectory = projectDir
GitPropertiesPluginExtension ext = project.getExtensions().getByName("gitProperties")
ext.dotGitDirectory = projectDir
// failOnNoGitDirectory is true by default

def task = project.tasks.generateGitProperties
Expand Down