Skip to content

Commit 4c27543

Browse files
committed
feat: manually apply git versioning after version was set to make git version availalbe to all upcoming gradle configuration
1 parent a7a2cd0 commit 4c27543

File tree

8 files changed

+325
-261
lines changed

8 files changed

+325
-261
lines changed

README.md

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# Gradle Git Versioning Plugin
22

33
[![Gradle Plugin Portal](https://img.shields.io/maven-metadata/v/https/plugins.gradle.org/m2/me/qoomon/gradle-git-versioning-plugin/maven-metadata.xml.svg?colorB=007ec6&label=gradle-plugin)](https://plugins.gradle.org/plugin/me.qoomon.git-versioning)
4+
[![Changelog](https://badgen.net/badge/changelog/%E2%98%85/blue)](#changelog)
45

56
[![Build Workflow](https://github.com/qoomon/gradle-git-versioning-plugin/workflows/Build/badge.svg)](https://github.com/qoomon/gradle-git-versioning-plugin/actions)
6-
77
[![LGTM Grade](https://img.shields.io/lgtm/grade/java/github/qoomon/gradle-git-versioning-plugin)](https://lgtm.com/projects/g/qoomon/gradle-git-versioning-plugin)
88

99
**ℹ Also available as [Maven Extension](https://github.com/qoomon/maven-git-versioning-extension)**
@@ -25,14 +25,29 @@ This extension will set project versions, based on current **Git branch** or **G
2525
#### Groovy DSL `build.gradle`
2626
```groovy
2727
plugins {
28-
id 'me.qoomon.git-versioning' version '1.2.3'
28+
id 'me.qoomon.git-versioning' version '2.0.0'
29+
}
30+
31+
// ...
32+
33+
version = '0.0.0-SNAPSHOT'
34+
gitVersioning.apply {
35+
// see configuration documentation below
2936
}
3037
```
3138

3239
#### Kotlin DSL `build.gradle.kts`
3340
```kotlin
3441
plugins {
35-
id("me.qoomon.git-versioning") version "1.2.3"
42+
id("me.qoomon.git-versioning") version "2.0.0"
43+
}
44+
45+
46+
// ...
47+
48+
version = '0.0.0-SNAPSHOT'
49+
gitVersioning.apply {
50+
// see configuration documentation below
3651
}
3752
```
3853

@@ -46,7 +61,7 @@ You can configure the final version format for specific branches and tags separa
4661

4762
##### Groovy DSL `build.gradle`
4863
```groovy
49-
gitVersioning {
64+
gitVersioning.apply {
5065
branch {
5166
pattern = 'master'
5267
versionFormat = '${version}'
@@ -69,7 +84,7 @@ gitVersioning {
6984
```kotlin
7085
import me.qoomon.gradle.gitversioning.GitVersioningPluginExtension.VersionDescription
7186
import me.qoomon.gradle.gitversioning.GitVersioningPluginExtension.CommitVersionDescription
72-
gitVersioning {
87+
gitVersioning.apply {
7388
branch(closureOf<VersionDescription> {
7489
pattern = "master"
7590
versionFormat = "\${version}"
@@ -246,3 +261,16 @@ fi
246261
```shell
247262
- ./gradlew build
248263
```
264+
265+
# Changelog
266+
### 2.0.0
267+
268+
#### Breaking Changes
269+
* New way of apply git versioning
270+
* You need to call `apply` method with config, after version was set.
271+
```
272+
version = '0.0.0-SNAPSHOT'
273+
gitVersioning.apply {
274+
// see configuration documentatiomn below
275+
}
276+
```

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ plugins {
1212
}
1313

1414
group 'me.qoomon'
15-
version '1.4.0'
15+
version '2.0.0'
1616

1717
sourceCompatibility = JavaVersion.VERSION_1_8
1818
targetCompatibility = JavaVersion.VERSION_1_8
Lines changed: 5 additions & 161 deletions
Original file line numberDiff line numberDiff line change
@@ -1,175 +1,19 @@
11
package me.qoomon.gradle.gitversioning;
22

3-
import me.qoomon.gitversioning.*;
43
import org.gradle.api.Plugin;
54
import org.gradle.api.Project;
6-
import org.gradle.api.plugins.ExtraPropertiesExtension;
75

86
import javax.annotation.Nonnull;
97

10-
import java.time.Instant;
11-
import java.time.ZoneOffset;
12-
import java.time.format.DateTimeFormatter;
13-
import java.util.*;
14-
import java.util.Map.Entry;
15-
import java.util.stream.Collectors;
16-
17-
import static java.lang.Boolean.parseBoolean;
18-
import static java.util.Collections.emptyList;
19-
import static java.util.Collections.singletonList;
20-
import static java.util.Optional.ofNullable;
21-
import static java.util.stream.Collectors.toList;
22-
238
public class GitVersioningPlugin implements Plugin<Project> {
24-
private static final String OPTION_NAME_GIT_TAG = "git.tag";
25-
private static final String OPTION_NAME_GIT_BRANCH = "git.branch";
26-
private static final String OPTION_NAME_DISABLE = "versioning.disable";
27-
private static final String OPTION_PREFER_TAGS = "versioning.preferTags";
289

10+
/**
11+
* for main logic see {@link GitVersioningPluginExtension#apply}
12+
*/
2913
public void apply(@Nonnull Project rootProject) {
30-
rootProject.getAllprojects().forEach(it -> it.getTasks().create("version", VersionTask.class));
31-
32-
GitVersioningPluginExtension config = rootProject.getExtensions()
33-
.create("gitVersioning", GitVersioningPluginExtension.class, rootProject);
34-
35-
if (parseBoolean(getCommandOption(rootProject, OPTION_NAME_DISABLE))) {
36-
rootProject.getLogger().warn("skip - versioning is disabled");
37-
return;
38-
}
39-
40-
rootProject.afterEvaluate(evaluatedProject -> {
41-
42-
GitRepoSituation repoSituation = GitUtil.situation(rootProject.getProjectDir());
43-
String providedTag = getCommandOption(rootProject, OPTION_NAME_GIT_TAG);
44-
if (providedTag != null) {
45-
repoSituation.setHeadBranch(null);
46-
repoSituation.setHeadTags(providedTag.isEmpty() ? emptyList() : singletonList(providedTag));
47-
}
48-
String providedBranch = getCommandOption(rootProject, OPTION_NAME_GIT_BRANCH);
49-
if (providedBranch != null) {
50-
repoSituation.setHeadBranch(providedBranch.isEmpty() ? null : providedBranch);
51-
}
52-
53-
final boolean preferTagsOption = getPreferTagsOption(rootProject, config);
54-
GitVersionDetails gitVersionDetails = GitVersioning.determineVersion(repoSituation,
55-
ofNullable(config.commit)
56-
.map(it -> new VersionDescription(null, it.versionFormat, mapPropertyDescription(it.properties)))
57-
.orElse(new VersionDescription()),
58-
config.branches.stream()
59-
.map(it -> new VersionDescription(it.pattern, it.versionFormat, mapPropertyDescription(it.properties)))
60-
.collect(toList()),
61-
config.tags.stream()
62-
.map(it -> new VersionDescription(it.pattern, it.versionFormat, mapPropertyDescription(it.properties)))
63-
.collect(toList()),
64-
preferTagsOption);
65-
66-
67-
final Map<String, Object> originVersionMap = rootProject.getAllprojects().stream().collect(Collectors.toMap(Project::getPath, p -> p.getVersion()));
68-
69-
rootProject.getAllprojects().forEach(project -> {
70-
// TODO check for version is equals to root project
71-
72-
// update version
73-
String gitProjectVersion = gitVersionDetails.getVersionTransformer().apply(resolveOriginVersion(project, originVersionMap));
74-
75-
project.getLogger().info(project.getDisplayName() + " - git versioning [" + project.getVersion() + " -> " + gitProjectVersion + "]"
76-
+ " (" + gitVersionDetails.getCommitRefType() + ":" + gitVersionDetails.getCommitRefName() + ")");
77-
78-
project.setVersion(gitProjectVersion);
79-
80-
81-
// update properties
82-
Map<String, String> gitProjectProperties = gitVersionDetails.getPropertiesTransformer().apply(getProjectStringProperties(project), project.getVersion().toString());
83-
84-
gitProjectProperties.forEach((key, value) -> {
85-
if (!Objects.equals(project.property(key), value)) {
86-
project.getLogger().info(project.getDisplayName() + " - set property " + key + ": " + value);
87-
project.setProperty(key, value);
88-
}
89-
});
90-
91-
ExtraPropertiesExtension extraProperties = project.getExtensions().getExtraProperties();
92-
extraProperties.set("git.commit", gitVersionDetails.getCommit());
93-
extraProperties.set("git.commit.timestamp", Long.toString(gitVersionDetails.getCommitTimestamp()));
94-
extraProperties.set("git.commit.timestamp.datetime", toTimestampDateTime(gitVersionDetails.getCommitTimestamp()));
95-
96-
extraProperties.set("git.ref", gitVersionDetails.getCommitRefName());
97-
extraProperties.set("git." + gitVersionDetails.getCommitRefType(), gitVersionDetails.getCommitRefName());
98-
});
99-
});
100-
}
101-
102-
private String resolveOriginVersion(Project project, Map<String,Object> originVersionMap) {
103-
104-
String projectVersion = originVersionMap.get(project.getPath()).toString();
105-
if (!projectVersion.equals("unspecified")) {
106-
return projectVersion;
107-
}
108-
109-
if(project.getParent() == null){
110-
return "unspecified";
111-
}
112-
113-
return resolveOriginVersion(project.getParent(), originVersionMap);
14+
rootProject.getExtensions().create("gitVersioning", GitVersioningPluginExtension.class, rootProject);
11415

115-
}
116-
117-
private Map<String, String> getProjectStringProperties(Project project) {
118-
Map<String, String> result = new HashMap<>();
119-
for (Entry<String, ?> entry : project.getProperties().entrySet()) {
120-
if (entry.getValue() instanceof String) {
121-
result.put(entry.getKey(), (String) entry.getValue());
122-
}
123-
}
124-
return result;
125-
}
126-
127-
private List<PropertyDescription> mapPropertyDescription(List<GitVersioningPluginExtension.PropertyDescription> properties) {
128-
return properties.stream()
129-
.map(it -> new PropertyDescription(it.pattern, mapPropertyValueDescription(it.value))
130-
).collect(toList());
131-
}
132-
133-
private PropertyValueDescription mapPropertyValueDescription(GitVersioningPluginExtension.ValueDescription value) {
134-
return Optional.of(value)
135-
.map(it -> new PropertyValueDescription(it.pattern, it.format)).get();
136-
}
137-
138-
private String getCommandOption(final Project project, final String name) {
139-
String value = (String) project.getProperties().get(name);
140-
if (value == null) {
141-
String plainName = name.replaceFirst("^versioning\\.", "");
142-
String environmentVariableName = "VERSIONING_"
143-
+ String.join("_", plainName.split("(?=\\p{Lu})"))
144-
.replaceAll("\\.", "_")
145-
.toUpperCase();
146-
value = System.getenv(environmentVariableName);
147-
}
148-
if(value == null) {
149-
value = System.getProperty(name);
150-
}
151-
return value;
152-
}
153-
154-
private boolean getPreferTagsOption(final Project rootProject, final GitVersioningPluginExtension config) {
155-
final boolean preferTagsOption;
156-
final String preferTagsCommandOption = getCommandOption(rootProject, OPTION_PREFER_TAGS);
157-
if(preferTagsCommandOption != null){
158-
preferTagsOption = parseBoolean(preferTagsCommandOption);
159-
} else {
160-
preferTagsOption = config.preferTags;
161-
}
162-
return preferTagsOption;
163-
}
164-
165-
private static String toTimestampDateTime(long timestamp) {
166-
if (timestamp == 0) {
167-
return "0000-00-00T00:00:00Z";
168-
}
169-
170-
return DateTimeFormatter.ISO_DATE_TIME
171-
.withZone(ZoneOffset.UTC)
172-
.format(Instant.ofEpochSecond(timestamp));
16+
rootProject.getAllprojects().forEach(it -> it.getTasks().create("version", VersionTask.class));
17317
}
17418
}
17519

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package me.qoomon.gradle.gitversioning;
2+
3+
import groovy.lang.Closure;
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
import static org.gradle.util.ConfigureUtil.configure;
9+
10+
public class GitVersioningPluginConfig {
11+
12+
public CommitVersionDescription commitVersionDescription;
13+
public final List<VersionDescription> branchVersionDescriptions = new ArrayList<>();
14+
public final List<VersionDescription> tagVersionDescriptions = new ArrayList<>();
15+
16+
public boolean preferTags = false;
17+
18+
public void commit(Closure closure) {
19+
CommitVersionDescription versionDescription = new CommitVersionDescription();
20+
configure(closure, versionDescription);
21+
this.commitVersionDescription = versionDescription;
22+
}
23+
24+
public void branchVersionDescription(Closure closure) {
25+
VersionDescription versionDescription = new VersionDescription();
26+
configure(closure, versionDescription);
27+
addBranchVersionDescription(versionDescription);
28+
}
29+
30+
public void addBranchVersionDescription(VersionDescription versionDescription) {
31+
this.branchVersionDescriptions.add(versionDescription);
32+
}
33+
34+
public void tag(Closure closure) {
35+
VersionDescription versionDescription = new VersionDescription();
36+
configure(closure, versionDescription);
37+
addTagVersionDescription(versionDescription);
38+
}
39+
40+
public void addTagVersionDescription(VersionDescription versionDescription) {
41+
this.tagVersionDescriptions.add(versionDescription);
42+
}
43+
44+
public static class VersionDescription {
45+
46+
public String pattern;
47+
public String versionFormat;
48+
public List<PropertyDescription> properties = new ArrayList<>();
49+
50+
public void property(Closure closure) {
51+
PropertyDescription propertyDescription = new PropertyDescription();
52+
configure(closure, propertyDescription);
53+
this.properties.add(propertyDescription);
54+
}
55+
}
56+
57+
public static class CommitVersionDescription {
58+
59+
public String versionFormat;
60+
public List<PropertyDescription> properties = new ArrayList<>();
61+
62+
public void property(Closure closure) {
63+
PropertyDescription propertyDescription = new PropertyDescription();
64+
configure(closure, propertyDescription);
65+
this.properties.add(propertyDescription);
66+
}
67+
}
68+
69+
public static class PropertyDescription {
70+
71+
public String pattern;
72+
public ValueDescription value;
73+
74+
public void value(Closure closure) {
75+
ValueDescription valueDescription = new ValueDescription();
76+
configure(closure, valueDescription);
77+
this.value = valueDescription;
78+
}
79+
}
80+
81+
public static class ValueDescription {
82+
83+
public String pattern;
84+
public String format;
85+
86+
}
87+
}

0 commit comments

Comments
 (0)