42
42
import static me .qoomon .gitversioning .commons .StringUtil .*;
43
43
import static org .apache .commons .lang3 .StringUtils .leftPad ;
44
44
import static org .apache .commons .lang3 .StringUtils .rightPad ;
45
+ import static org .eclipse .jgit .lib .Constants .HEAD ;
45
46
46
47
public abstract class GitVersioningPluginExtension {
47
48
48
- public static final Logger LOGGER = LoggerFactory .getLogger (GitVersioningPluginExtension .class );
49
-
50
49
private static final Pattern VERSION_PATTERN = Pattern .compile (".*?(?<version>(?<core>(?<major>\\ d+)(?:\\ .(?<minor>\\ d+)(?:\\ .(?<patch>\\ d+))?)?)(?:-(?<label>.*))?)|" );
51
50
52
51
private static final String OPTION_NAME_GIT_REF = "git.ref" ;
@@ -88,74 +87,75 @@ private void apply() throws IOException {
88
87
if (commandOptionDisable != null ) {
89
88
boolean disabled = parseBoolean (commandOptionDisable );
90
89
if (disabled ) {
91
- LOGGER . info ("skip - versioning is disabled by command option" );
90
+ project . getLogger (). warn ("skip - versioning is disabled by command option" );
92
91
return ;
93
92
}
94
93
} else {
95
94
// check if extension is disabled by config option
96
95
if (config .disable ) {
97
- LOGGER . info ("skip - versioning is disabled by config option" );
96
+ project . getLogger (). warn ("skip - versioning is disabled by config option" );
98
97
return ;
99
98
}
100
99
}
101
100
102
101
final GitSituation gitSituation = getGitSituation (project .getProjectDir ());
103
102
if (gitSituation == null ) {
104
- LOGGER .warn ("skip - project is not part of a git repository" );
103
+ project . getLogger () .warn ("skip - project is not part of a git repository" );
105
104
return ;
106
105
}
107
106
108
- if (LOGGER .isDebugEnabled ()) {
109
- LOGGER .debug ("git situation:" );
110
- LOGGER .debug (" root directory: " + gitSituation .getRootDirectory ());
111
- LOGGER .debug (" head commit: " + gitSituation .getRev ());
112
- LOGGER .debug (" head commit timestamp: " + gitSituation .getTimestamp ());
113
- LOGGER .debug (" head branch: " + gitSituation .getBranch ());
114
- LOGGER .debug (" head tags: " + gitSituation .getTags ());
115
- LOGGER .debug (" head description: " + gitSituation .getDescription ());
107
+ if (project . getLogger () .isDebugEnabled ()) {
108
+ project . getLogger () .debug ("git situation:" );
109
+ project . getLogger () .debug (" root directory: " + gitSituation .getRootDirectory ());
110
+ project . getLogger () .debug (" head commit: " + gitSituation .getRev ());
111
+ project . getLogger () .debug (" head commit timestamp: " + gitSituation .getTimestamp ());
112
+ project . getLogger () .debug (" head branch: " + gitSituation .getBranch ());
113
+ project . getLogger () .debug (" head tags: " + gitSituation .getTags ());
114
+ project . getLogger () .debug (" head description: " + gitSituation .getDescription ());
116
115
}
117
116
118
117
// determine git version details
119
118
gitVersionDetails = getGitVersionDetails (gitSituation , config );
120
119
if (gitVersionDetails == null ) {
121
- LOGGER .warn ("skip - no matching ref configuration and no rev configuration defined" );
122
- LOGGER .warn ("git refs:" );
123
- LOGGER .warn (" branch: " + gitSituation .getBranch ());
124
- LOGGER .warn (" tags: " + gitSituation .getTags ());
125
- LOGGER .warn ("defined ref configurations:" );
126
- config .refs .list .forEach (ref -> LOGGER .warn (" " + rightPad (ref .type .name (), 6 ) + " - pattern: " + ref .pattern ));
120
+ project . getLogger () .warn ("skip - no matching ref configuration and no rev configuration defined" );
121
+ project . getLogger () .warn ("git refs:" );
122
+ project . getLogger () .warn (" branch: " + gitSituation .getBranch ());
123
+ project . getLogger () .warn (" tags: " + gitSituation .getTags ());
124
+ project . getLogger () .warn ("defined ref configurations:" );
125
+ config .refs .list .forEach (ref -> project . getLogger () .warn (" " + rightPad (ref .type .name (), 6 ) + " - pattern: " + ref .pattern ));
127
126
return ;
128
127
}
129
128
130
- LOGGER . info ("matching ref: " + gitVersionDetails .getRefType ().name () + " - " + gitVersionDetails .getRefName ());
129
+ project . getLogger (). lifecycle ("matching ref: " + gitVersionDetails .getRefType ().name () + " - " + gitVersionDetails .getRefName ());
131
130
final RefPatchDescription patchDescription = gitVersionDetails .getPatchDescription ();
132
- LOGGER . info ("ref configuration: " + gitVersionDetails .getRefType ().name () + " - pattern: " + patchDescription .pattern );
131
+ project . getLogger (). lifecycle ("ref configuration: " + gitVersionDetails .getRefType ().name () + " - pattern: " + patchDescription .pattern );
133
132
if (patchDescription .describeTagPattern != null ) {
134
- LOGGER . info (" describeTagPattern: " + patchDescription .describeTagPattern );
133
+ project . getLogger (). lifecycle (" describeTagPattern: " + patchDescription .describeTagPattern );
135
134
gitSituation .setDescribeTagPattern (patchDescription .getDescribeTagPattern ());
136
135
}
137
136
if (patchDescription .version != null ) {
138
- LOGGER . info (" version: " + patchDescription .version );
137
+ project . getLogger (). lifecycle (" version: " + patchDescription .version );
139
138
}
140
139
if (!patchDescription .properties .isEmpty ()) {
141
- LOGGER . info ( " properties: " + patchDescription . version );
142
- patchDescription .properties .forEach ((key , value ) -> LOGGER . info ( " " + key + " - " + value ));
140
+ project . getLogger (). lifecycle ( " properties:" );
141
+ patchDescription .properties .forEach ((key , value ) -> project . getLogger (). lifecycle ( " " + key + ": " + value ));
143
142
}
144
143
boolean updateGradleProperties = getUpdateGradlePropertiesOption (patchDescription );
145
144
if (updateGradleProperties ) {
146
- LOGGER . info (" updateGradleProperties: " + updateGradleProperties );
145
+ project . getLogger (). lifecycle (" updateGradleProperties: " + updateGradleProperties );
147
146
}
148
147
149
148
globalFormatPlaceholderMap = generateGlobalFormatPlaceholderMap (gitSituation , gitVersionDetails , project );
150
149
Map <String , String > gitProjectProperties = generateGitProjectProperties (gitSituation , gitVersionDetails );
151
150
151
+ project .getLogger ().lifecycle ("" );
152
152
project .getAllprojects ().forEach (project -> {
153
153
final String originalProjectVersion = project .getVersion ().toString ();
154
154
155
155
final String versionFormat = patchDescription .version ;
156
156
if (versionFormat != null ) {
157
157
updateVersion (project , versionFormat );
158
- LOGGER . info ("project version: " + project .getVersion ());
158
+ project . getLogger (). lifecycle ("project version: " + project .getVersion ());
159
159
}
160
160
161
161
final Map <String , String > propertyFormats = patchDescription .properties ;
@@ -198,14 +198,14 @@ private void updatePropertyValues(Project project, Map<String, String> propertyF
198
198
originalProjectVersion );
199
199
if (!gitPropertyValue .equals (projectPropertyValue )) {
200
200
if (logHeader ) {
201
- LOGGER . info ("properties:" );
201
+ project . getLogger (). lifecycle ("properties:" );
202
202
logHeader = false ;
203
203
}
204
- LOGGER . info ( "set property " + projectPropertyName + " to " + gitPropertyValue );
204
+ project . getLogger (). lifecycle ( " " + projectPropertyName + ": " + gitPropertyValue );
205
205
project .setProperty (projectPropertyName , gitPropertyValue );
206
206
}
207
207
} else {
208
- LOGGER .warn ("Can not update property " + projectPropertyName + "." +
208
+ project . getLogger () .warn ("Can not update property " + projectPropertyName + "." +
209
209
" Expected value type is String, but was " + projectPropertyValue .getClass ().getName ());
210
210
}
211
211
}
@@ -292,13 +292,21 @@ private GitSituation getGitSituation(File executionRootDirectory) throws IOExcep
292
292
}
293
293
}
294
294
295
+ // TODO
296
+ // skip if we are on a branch
297
+ // if (repository.getBranch() == null) {}
298
+
295
299
// GitHub Actions support
296
300
if (overrideBranch == null && overrideTag == null ) {
297
301
final String githubEnv = System .getenv ("GITHUB_ACTIONS" );
298
302
if (githubEnv != null && githubEnv .equals ("true" )) {
299
- LOGGER .info ("gather git situation from GitHub Actions environment variable: GITHUB_REF" );
303
+ // TODO
304
+ // skip if head hash differs from GITHUB_SHA
305
+ // if(System.getenv("GITHUB_SHA").equals(repository.resolve(HEAD).getName())) {}
306
+
307
+ project .getLogger ().lifecycle ("gather git situation from GitHub Actions environment variable: GITHUB_REF" );
300
308
String githubRef = System .getenv ("GITHUB_REF" );
301
- LOGGER .debug (" GITHUB_REF: " + githubRef );
309
+ project . getLogger () .debug (" GITHUB_REF: " + githubRef );
302
310
if (githubRef != null && githubRef .startsWith ("refs/" )) {
303
311
if (githubRef .startsWith ("refs/tags/" )) {
304
312
overrideTag = githubRef ;
@@ -315,13 +323,13 @@ private GitSituation getGitSituation(File executionRootDirectory) throws IOExcep
315
323
if (overrideBranch == null && overrideTag == null ) {
316
324
final String gitlabEnv = System .getenv ("GITLAB_CI" );
317
325
if (gitlabEnv != null && gitlabEnv .equals ("true" )) {
318
- LOGGER . info ("gather git situation from GitLab CI environment variables: CI_COMMIT_BRANCH, CI_COMMIT_TAG and CI_MERGE_REQUEST_SOURCE_BRANCH_NAME" );
326
+ project . getLogger (). lifecycle ("gather git situation from GitLab CI environment variables: CI_COMMIT_BRANCH, CI_COMMIT_TAG and CI_MERGE_REQUEST_SOURCE_BRANCH_NAME" );
319
327
String commitBranch = System .getenv ("CI_COMMIT_BRANCH" );
320
328
String commitTag = System .getenv ("CI_COMMIT_TAG" );
321
329
String mrSourceBranch = System .getenv ("CI_MERGE_REQUEST_SOURCE_BRANCH_NAME" );
322
- LOGGER .debug (" CI_COMMIT_BRANCH: " + commitBranch );
323
- LOGGER .debug (" CI_COMMIT_TAG: " + commitTag );
324
- LOGGER .debug (" CI_MERGE_REQUEST_SOURCE_BRANCH_NAME: " + mrSourceBranch );
330
+ project . getLogger () .debug (" CI_COMMIT_BRANCH: " + commitBranch );
331
+ project . getLogger () .debug (" CI_COMMIT_TAG: " + commitTag );
332
+ project . getLogger () .debug (" CI_MERGE_REQUEST_SOURCE_BRANCH_NAME: " + mrSourceBranch );
325
333
overrideBranch = commitBranch == null ? mrSourceBranch : commitBranch ;
326
334
overrideTag = commitTag ;
327
335
}
@@ -331,11 +339,11 @@ private GitSituation getGitSituation(File executionRootDirectory) throws IOExcep
331
339
if (overrideBranch == null && overrideTag == null ) {
332
340
final String circleciEnv = System .getenv ("CIRCLECI" );
333
341
if (circleciEnv != null && circleciEnv .equals ("true" )) {
334
- LOGGER . info ("gather git situation from Circle CI environment variables: CIRCLE_BRANCH and CIRCLE_TAG" );
342
+ project . getLogger (). lifecycle ("gather git situation from Circle CI environment variables: CIRCLE_BRANCH and CIRCLE_TAG" );
335
343
String commitBranch = System .getenv ("CIRCLE_BRANCH" );
336
344
String commitTag = System .getenv ("CIRCLE_TAG" );
337
- LOGGER .debug (" CIRCLE_BRANCH: " + commitBranch );
338
- LOGGER .debug (" CIRCLE_TAG: " + commitTag );
345
+ project . getLogger () .debug (" CIRCLE_BRANCH: " + commitBranch );
346
+ project . getLogger () .debug (" CIRCLE_TAG: " + commitTag );
339
347
overrideBranch = System .getenv ("CIRCLE_BRANCH" );
340
348
overrideTag = System .getenv ("CIRCLE_TAG" );
341
349
}
@@ -345,11 +353,11 @@ private GitSituation getGitSituation(File executionRootDirectory) throws IOExcep
345
353
if (overrideBranch == null && overrideTag == null ) {
346
354
final String jenkinsEnv = System .getenv ("JENKINS_HOME" );
347
355
if (jenkinsEnv != null && !jenkinsEnv .trim ().isEmpty ()) {
348
- LOGGER . info ("gather git situation from jenkins environment variables: BRANCH_NAME and TAG_NAME" );
356
+ project . getLogger (). lifecycle ("gather git situation from jenkins environment variables: BRANCH_NAME and TAG_NAME" );
349
357
String branchName = System .getenv ("BRANCH_NAME" );
350
358
String tagName = System .getenv ("TAG_NAME" );
351
- LOGGER .debug (" BRANCH_NAME: " + branchName );
352
- LOGGER .debug (" TAG_NAME: " + tagName );
359
+ project . getLogger () .debug (" BRANCH_NAME: " + branchName );
360
+ project . getLogger () .debug (" TAG_NAME: " + tagName );
353
361
if (branchName != null && branchName .equals (tagName )) {
354
362
overrideTag = tagName ;
355
363
overrideBranch = "" ;
@@ -382,7 +390,7 @@ void overrideBranch(String branch) {
382
390
.replaceFirst ("^heads/" , "" );
383
391
}
384
392
385
- LOGGER .debug ("override git branch with: " + branch );
393
+ project . getLogger () .debug ("override git branch with: " + branch );
386
394
setBranch (branch );
387
395
}
388
396
@@ -399,7 +407,7 @@ void overrideTags(String tag) {
399
407
tag = tag .replaceFirst ("^refs/tags/" , "" );
400
408
}
401
409
402
- LOGGER .debug ("override git tags with: " + tag );
410
+ project . getLogger () .debug ("override git tags with: " + tag );
403
411
setTags (tag == null ? emptyList () : singletonList (tag ));
404
412
}
405
413
};
0 commit comments