Skip to content

Commit 771608c

Browse files
author
TheSnoozer
committed
move some settings from GitCommitIdPluginExtension to GitCommitIdPluginFormatSettingsExtension
1 parent 24cb3b7 commit 771608c

4 files changed

Lines changed: 130 additions & 42 deletions

File tree

src/main/java/io/github/git/commit/id/gradle/plugin/GitCommitIdPlugin.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,23 @@ public class GitCommitIdPlugin implements Plugin<Project> {
6969
* @param project The target project
7070
*/
7171
public void apply(Project project) {
72+
// gitCommitId
7273
var extension = project.getExtensions().create(
7374
GitCommitIdPluginExtension.NAME,
7475
GitCommitIdPluginExtension.class);
76+
// gitCommitId -> outputSettings
7577
((ExtensionAware) extension).getExtensions().create(
7678
GitCommitIdPluginOutputSettingsExtension.NAME,
7779
GitCommitIdPluginOutputSettingsExtension.class);
80+
// gitCommitId -> gitSettings
7881
((ExtensionAware) extension).getExtensions().create(
7982
GitCommitIdPluginGitSettingsExtension.NAME,
8083
GitCommitIdPluginGitSettingsExtension.class);
84+
// gitCommitId -> formatSettings
85+
((ExtensionAware) extension).getExtensions().create(
86+
GitCommitIdPluginFormatSettingsExtension.NAME,
87+
GitCommitIdPluginFormatSettingsExtension.class);
88+
// Task
8189
var task = project.getTasks().create(
8290
GitCommitIdPluginGenerationTask.NAME,
8391
GitCommitIdPluginGenerationTask.class);

src/main/java/io/github/git/commit/id/gradle/plugin/GitCommitIdPluginExtension.java

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -69,42 +69,6 @@ public abstract class GitCommitIdPluginExtension {
6969
*/
7070
public abstract Property<Boolean> getVerbose();
7171

72-
/**
73-
* This configuration allows you to configure the "prefix" of the generated properties and thus
74-
* will be used as the "namespace" prefix for all exposed/generated properties.
75-
*
76-
* <p>An example the plugin may generate the property {@code ${configured-prefix}.commit.id}.
77-
* Such behaviour can be used to generate properties for multiple git repositories.
78-
*
79-
* <p>Refer to {@link pl.project13.core.GitCommitPropertyConstant} for all properties
80-
* that can be generated. The configured prefix would need to be added in front.
81-
*
82-
* <p>Defaults to {@code git} which then results in {@code git.commit.id} and
83-
* similar generated properties.
84-
*/
85-
public abstract Property<String> getPropertyPrefix();
86-
87-
/**
88-
* Allows to configure the date format in which "time" properties should be converted into.
89-
* For example the commit time, or the author time would be converted into the specified format.
90-
*
91-
* <p>Defaults to {@code yyyy-MM-dd'T'HH:mm:ssZ}
92-
*
93-
* <p>Refer to {@link #getExportDateFormatTimeZone()} if you want to change the time-zone.
94-
*/
95-
public abstract Property<String> getExportDateFormat();
96-
97-
/**
98-
* Allows to configure the time zone which is utilized for {@link #getExportDateFormat()}.
99-
*
100-
* <p>Defaults to {@code java.util.TimeZone.getDefault().getID()}.
101-
* Allows various formats of timezone configuration
102-
* (e.g. 'America/Los_Angeles', 'GMT+10', 'PST').
103-
* As a general warning try to avoid three-letter time zone IDs because the same
104-
* abbreviation are often used for multiple time zones.
105-
*/
106-
public abstract Property<String> getExportDateFormatTimeZone();
107-
10872
/**
10973
* Allows to configure to skip the execution of the {@link GitCommitIdPluginGenerationTask}
11074
* that will generate the information you have requested from this plugin.
@@ -172,9 +136,6 @@ public GitCommitIdPluginExtension() {
172136
// injectAllReactorProjects
173137
getVerbose().convention(false);
174138
// skipPoms
175-
getPropertyPrefix().convention("git");
176-
getExportDateFormat().convention("yyyy-MM-dd'T'HH:mm:ssZ");
177-
getExportDateFormatTimeZone().convention(TimeZone.getDefault().getID());
178139
getSkip().convention(false);
179140
getExcludeProperties().convention(Collections.emptyList());
180141
getIncludeOnlyProperties().convention(Collections.emptyList());
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/*
2+
* This file is part of git-commit-id-gradle-plugin.
3+
*
4+
* git-commit-id-gradle-plugin is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU Lesser General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* git-commit-id-gradle-plugin is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Lesser General Public License
15+
* along with git-commit-id-gradle-plugin. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
package io.github.git.commit.id.gradle.plugin;
19+
20+
21+
import java.util.TimeZone;
22+
import javax.inject.Inject;
23+
import org.gradle.api.file.ProjectLayout;
24+
import org.gradle.api.provider.Property;
25+
26+
27+
/**
28+
* The {@link GitCommitIdPlugin} comes with a sensible set of default configurations and settings.
29+
* However, there might be cases where a default doesn't fit your project needs.
30+
* Gradle allows user to configure with
31+
* <a href="https://docs.gradle.org/current/userguide/implementing_gradle_plugins.html#modeling_dsl_like_apis">Modeling DSL-like APIs</a>.
32+
* Perhaps one additional item you need to know about configuration is that the
33+
* {@link GitCommitIdPlugin} makes use of gradle's
34+
* <a href="https://docs.gradle.org/current/userguide/lazy_configuration.html">Lazy Configuration</a>
35+
* which is gradle's way to manage growing build complexity.
36+
*
37+
* <p>This Extension exposes all configuration options to configure the formatting
38+
* any properties (also the ones that may be exported into an output
39+
* file as part of the plugin execution).
40+
* This extension is generally only made available through the {@link GitCommitIdPluginExtension}.
41+
* If you want to learn about the plugin, refer to {@link GitCommitIdPlugin}.
42+
* If you are interested in the task that generates the actual information refer to
43+
* {@link GitCommitIdPluginGenerationTask}.
44+
*/
45+
public abstract class GitCommitIdPluginFormatSettingsExtension {
46+
/**
47+
* Name of the extension how it's made available to the end-user as
48+
* DSL like configuration in the {@code build.gradle}:
49+
* <pre>
50+
* gitCommitId {
51+
* outputSettings {
52+
* shouldGenerateOutputFile.set(true)
53+
* }
54+
* }
55+
* </pre>
56+
* As may notice this extension is made available as "nested" extension
57+
* of the {@link GitCommitIdPluginExtension}.
58+
*/
59+
public static final String NAME = "formatSettings";
60+
61+
/**
62+
* This configuration allows you to configure the "prefix" of the generated properties and thus
63+
* will be used as the "namespace" prefix for all exposed/generated properties.
64+
*
65+
* <p>An example the plugin may generate the property {@code ${configured-prefix}.commit.id}.
66+
* Such behaviour can be used to generate properties for multiple git repositories.
67+
*
68+
* <p>Refer to {@link pl.project13.core.GitCommitPropertyConstant} for all properties
69+
* that can be generated. The configured prefix would need to be added in front.
70+
*
71+
* <p>Defaults to {@code git} which then results in {@code git.commit.id} and
72+
* similar generated properties.
73+
*/
74+
public abstract Property<String> getPropertyPrefix();
75+
76+
/**
77+
* Allows to configure the date format in which "time" properties should be converted into.
78+
* For example the commit time, or the author time would be converted into the specified format.
79+
*
80+
* <p>Defaults to {@code yyyy-MM-dd'T'HH:mm:ssZ}
81+
*
82+
* <p>Refer to {@link #getExportDateFormatTimeZone()} if you want to change the time-zone.
83+
*/
84+
public abstract Property<String> getExportDateFormat();
85+
86+
/**
87+
* Allows to configure the time zone which is utilized for {@link #getExportDateFormat()}.
88+
*
89+
* <p>Defaults to {@code java.util.TimeZone.getDefault().getID()}.
90+
* Allows various formats of timezone configuration
91+
* (e.g. 'America/Los_Angeles', 'GMT+10', 'PST').
92+
* As a general warning try to avoid three-letter time zone IDs because the same
93+
* abbreviation are often used for multiple time zones.
94+
*/
95+
public abstract Property<String> getExportDateFormatTimeZone();
96+
97+
@Inject
98+
public ProjectLayout getProjectLayout() {
99+
throw new IllegalStateException("Should have been injected!");
100+
}
101+
102+
/**
103+
* Setup the default values / conventions for the GitCommitIdPluginFormatSettingsExtension.
104+
*/
105+
@Inject
106+
public GitCommitIdPluginFormatSettingsExtension() {
107+
getPropertyPrefix().convention("git");
108+
getExportDateFormat().convention("yyyy-MM-dd'T'HH:mm:ssZ");
109+
getExportDateFormatTimeZone().convention(TimeZone.getDefault().getID());
110+
}
111+
}

src/main/java/io/github/git/commit/id/gradle/plugin/GitCommitIdPluginGenerationTask.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ private GitCommitIdPluginGitSettingsExtension getGitCommitIdPluginGitSettingsExt
7878
.findByType(GitCommitIdPluginGitSettingsExtension.class);
7979
}
8080

81+
private GitCommitIdPluginFormatSettingsExtension getGitCommitIdPluginFormatSettingsExtension() {
82+
return ((ExtensionAware) getGitCommitIdPluginExtension()).getExtensions()
83+
.findByType(GitCommitIdPluginFormatSettingsExtension.class);
84+
}
85+
8186
/**
8287
* Since we are generating "git" information this task needs to specify the git-directory
8388
* as input. The input can then be used by gradle to determine if the task is "up-to-date"
@@ -170,19 +175,22 @@ public LogInterface getLogInterface() {
170175
@Nonnull
171176
@Override
172177
public String getDateFormat() {
173-
return extension.getExportDateFormat().get();
178+
return getGitCommitIdPluginFormatSettingsExtension()
179+
.getExportDateFormat().get();
174180
}
175181

176182
@Nonnull
177183
@Override
178184
public String getDateFormatTimeZone() {
179-
return extension.getExportDateFormatTimeZone().get();
185+
return getGitCommitIdPluginFormatSettingsExtension()
186+
.getExportDateFormatTimeZone().get();
180187
}
181188

182189
@Nonnull
183190
@Override
184191
public String getPrefixDot() {
185-
String trimmedPrefix = extension.getPropertyPrefix().get().trim();
192+
String trimmedPrefix = getGitCommitIdPluginFormatSettingsExtension()
193+
.getPropertyPrefix().get().trim();
186194
return trimmedPrefix.equals("") ? "" : trimmedPrefix + ".";
187195
}
188196

0 commit comments

Comments
 (0)