|
1 | 1 | package me.qoomon.gradle.gitversioning;
|
2 | 2 |
|
3 |
| -import me.qoomon.gitversioning.*; |
4 | 3 | import org.gradle.api.Plugin;
|
5 | 4 | import org.gradle.api.Project;
|
6 |
| -import org.gradle.api.plugins.ExtraPropertiesExtension; |
7 | 5 |
|
8 | 6 | import javax.annotation.Nonnull;
|
9 | 7 |
|
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 |
| - |
23 | 8 | 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"; |
28 | 9 |
|
| 10 | + /** |
| 11 | + * for main logic see {@link GitVersioningPluginExtension#apply} |
| 12 | + */ |
29 | 13 | 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); |
114 | 15 |
|
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)); |
173 | 17 | }
|
174 | 18 | }
|
175 | 19 |
|
0 commit comments