Skip to content

Commit 2b4f054

Browse files
author
TheSnoozer
committed
split up some configurations of the GitCommitIdPluginExtension into a dedicated GitCommitIdPluginOutputSettingsExtension
1 parent 6dd528a commit 2b4f054

5 files changed

Lines changed: 143 additions & 65 deletions

File tree

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
import org.gradle.api.Plugin;
2121
import org.gradle.api.Project;
22+
import org.gradle.api.plugins.ExtensionAware;
23+
import pl.project13.core.CommitIdPropertiesOutputFormat;
2224

2325
/**
2426
* The GitCommitIdPlugin or also known as git-commit-id-gradle-plugin is a plugin
@@ -73,6 +75,8 @@ public class GitCommitIdPlugin implements Plugin<Project> {
7375
public void apply(Project project) {
7476
var extension = project.getExtensions().create(
7577
GIT_COMMIT_ID_EXTENSION_NAME, GitCommitIdPluginExtension.class);
78+
((ExtensionAware) extension).getExtensions().create(
79+
"outputSettings", GitCommitIdPluginOutputSettingsExtension.class);
7680
var task = project.getTasks().create(
7781
GIT_COMMIT_ID_TASK_NAME, GitCommitIdPluginGenerationTask.class);
7882
task.onlyIf(ignore -> extension.getSkip().get() == false);

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

Lines changed: 0 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -60,49 +60,6 @@ public abstract class GitCommitIdPluginExtension {
6060
*/
6161
public abstract Property<Boolean> getVerbose();
6262

63-
/**
64-
* Configuration option to enable or disable the generation of a properties file.
65-
* To enable such generation, set this to {@code true}.
66-
* By default, this setting is disabled (set to {@code false}).
67-
*
68-
* <p>To configure the location of the properties file refer to
69-
* {@link #getGenerateGitPropertiesFilename()}, to configure the format (json, properties, ...)
70-
* of this file refer to {@link #getGenerateGitPropertiesFormat()}.
71-
*/
72-
public abstract Property<Boolean> getGenerateGitPropertiesFile();
73-
74-
/**
75-
* Configuration location of a properties file that can be generated by the plugin.
76-
* Defaults to {@code ${project.getBuildDir()}/git.properties}.
77-
*
78-
* <p>To enable or disable the generation of such properties file refer to
79-
* {@link #getGenerateGitPropertiesFile()}, to configure the format (json, properties, ...)
80-
* of this file refer to {@link #getGenerateGitPropertiesFormat()}.
81-
*/
82-
// @OutputFile
83-
public abstract RegularFileProperty getGenerateGitPropertiesFilename();
84-
85-
/**
86-
* Configuration option to enable or disable unicode escaping in the generated properties file.
87-
*
88-
* <p>Java has a long history with properties files.
89-
* Before Java 9, the encoding of a {@code .properties} file is {@code ISO-8859-1}.
90-
* All non-ASCII characters must be entered by using Unicode escape characters
91-
* (e.g. {@code \\uHHHH}).
92-
* In Java 9 and newer, the default encoding specifically for property resource bundles is
93-
* {@code UTF-8}.
94-
*
95-
* <p>To allow various use-cases this plugin allows to configure an automated unicode escaping.
96-
* Since the core module of this plugin will write the properties file with {@code UTF-8} it is
97-
* reasonable to assume that generated properties do not need special Unicode escape characters
98-
* (e.g. {@code \\uHHHH}).
99-
*
100-
* <p>Hence by default this setting is set to {@code false}.
101-
* If you so desire and wish for unicode escaping (e.g. \\u6E2C\\u8A66\\u4E2D\\u6587) you
102-
* can set this to {@code true}.
103-
*/
104-
public abstract Property<Boolean> getGenerateGitPropertiesFileWithEscapedUnicode();
105-
10663
/**
10764
* The information on what git revision your project is built with must come from the
10865
* git itself.
@@ -146,17 +103,6 @@ public abstract class GitCommitIdPluginExtension {
146103
*/
147104
public abstract Property<Integer> getAbbrevLength();
148105

149-
/**
150-
* Allows to configure the format (json, properties, ...) in which the properties
151-
* should be saved in.
152-
* To enable or disable the generation of such properties file refer to
153-
* {@link #getGenerateGitPropertiesFile()} and to configure the location
154-
* of the properties file refer to {@link #getGenerateGitPropertiesFilename()}.
155-
*
156-
* <p>By default will assume that the desired format is "properties".
157-
*/
158-
public abstract Property<CommitIdPropertiesOutputFormat> getGenerateGitPropertiesFormat();
159-
160106
/**
161107
* This configuration allows you to configure the "prefix" of the generated properties and thus
162108
* will be used as the "namespace" prefix for all exposed/generated properties.
@@ -346,15 +292,10 @@ public GitCommitIdPluginExtension() {
346292
// injectAllReactorProjects
347293
getVerbose().convention(false);
348294
// skipPoms
349-
getGenerateGitPropertiesFile().convention(false);
350-
getGenerateGitPropertiesFilename().convention(
351-
getProjectLayout().getBuildDirectory().file("git.properties"));
352-
getGenerateGitPropertiesFileWithEscapedUnicode().convention(false);
353295
getDotGitDirectory().convention(
354296
getProjectLayout().getProjectDirectory().dir(".git"));
355297
getGitDescribeConfig().convention(new GitDescribeConfig());
356298
getAbbrevLength().convention(7);
357-
getGenerateGitPropertiesFormat().convention(CommitIdPropertiesOutputFormat.PROPERTIES);
358299
getPropertyPrefix().convention("git");
359300
getExportDateFormat().convention("yyyy-MM-dd'T'HH:mm:ssZ");
360301
getExportDateFormatTimeZone().convention(TimeZone.getDefault().getID());

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

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.gradle.api.DefaultTask;
3131
import org.gradle.api.file.DirectoryProperty;
3232
import org.gradle.api.file.RegularFileProperty;
33+
import org.gradle.api.plugins.ExtensionAware;
3334
import org.gradle.api.tasks.CacheableTask;
3435
import org.gradle.api.tasks.InputDirectory;
3536
import org.gradle.api.tasks.OutputFile;
@@ -61,6 +62,11 @@ private GitCommitIdPluginExtension getExtension() {
6162
return getProject().getExtensions().findByType(GitCommitIdPluginExtension.class);
6263
}
6364

65+
private GitCommitIdPluginOutputSettingsExtension getGitCommitIdPluginOutputSettingsExtension() {
66+
return ((ExtensionAware) getExtension()).getExtensions()
67+
.findByType(GitCommitIdPluginOutputSettingsExtension.class);
68+
}
69+
6470
/**
6571
* Since we are generating "git" information this task needs to specify the git-directory
6672
* as input. The input can then be used by gradle to determine if the task is "up-to-date"
@@ -85,7 +91,7 @@ public DirectoryProperty getInput() {
8591
*/
8692
@OutputFile
8793
public RegularFileProperty getOutput() {
88-
return getExtension().getGenerateGitPropertiesFilename();
94+
return getGitCommitIdPluginOutputSettingsExtension().getGenerateGitPropertiesFilename();
8995
}
9096

9197
/**
@@ -233,7 +239,8 @@ public File getDotGitDirectory() {
233239

234240
@Override
235241
public boolean shouldGenerateGitPropertiesFile() {
236-
return extension.getGenerateGitPropertiesFile().get();
242+
return getGitCommitIdPluginOutputSettingsExtension()
243+
.getGenerateGitPropertiesFile().get();
237244
}
238245

239246
@Override
@@ -248,7 +255,9 @@ public void performPropertiesReplacement(Properties properties) {
248255

249256
@Override
250257
public CommitIdPropertiesOutputFormat getPropertiesOutputFormat() {
251-
return extension.getGenerateGitPropertiesFormat().get();
258+
return getGitCommitIdPluginOutputSettingsExtension()
259+
.getGenerateGitPropertiesFormat()
260+
.get();
252261
}
253262

254263
@Override
@@ -270,7 +279,9 @@ public File getProjectBaseDir() {
270279

271280
@Override
272281
public File getGenerateGitPropertiesFile() {
273-
return extension.getGenerateGitPropertiesFilename().get().getAsFile();
282+
return getGitCommitIdPluginOutputSettingsExtension()
283+
.getGenerateGitPropertiesFilename()
284+
.get().getAsFile();
274285
}
275286

276287
@Override
@@ -288,7 +299,8 @@ public Charset getPropertiesSourceCharset() {
288299

289300
@Override
290301
public boolean shouldPropertiesEscapeUnicode() {
291-
return extension.getGenerateGitPropertiesFileWithEscapedUnicode().get();
302+
return getGitCommitIdPluginOutputSettingsExtension()
303+
.getGenerateGitPropertiesFileWithEscapedUnicode().get();
292304
}
293305
};
294306

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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 javax.inject.Inject;
22+
import org.gradle.api.file.ProjectLayout;
23+
import org.gradle.api.file.RegularFileProperty;
24+
import org.gradle.api.provider.Property;
25+
import pl.project13.core.CommitIdPropertiesOutputFormat;
26+
27+
28+
/**
29+
* The {@link GitCommitIdPlugin} comes with a sensible set of default configurations and settings.
30+
* However, there might be cases where a default doesn't fit your project needs.
31+
* Gradle allows user to configure with
32+
* <a href="https://docs.gradle.org/current/userguide/implementing_gradle_plugins.html#modeling_dsl_like_apis">Modeling DSL-like APIs</a>.
33+
* Perhaps one additional item you need to know about configuration is that the
34+
* {@link GitCommitIdPlugin} makes use of gradle's
35+
* <a href="https://docs.gradle.org/current/userguide/lazy_configuration.html">Lazy Configuration</a>
36+
* which is gradle's way to manage growing build complexity.
37+
*
38+
* <p>This Extension exposes all configuration options to configure the settings for
39+
* the output file that may be generated 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 GitCommitIdPluginOutputSettingsExtension {
46+
47+
/**
48+
* Configuration option to enable or disable the generation of a properties file.
49+
* To enable such generation, set this to {@code true}.
50+
* By default, this setting is disabled (set to {@code false}).
51+
*
52+
* <p>To configure the location of the properties file refer to
53+
* {@link #getGenerateGitPropertiesFilename()}, to configure the format (json, properties, ...)
54+
* of this file refer to {@link #getGenerateGitPropertiesFormat()}.
55+
*/
56+
public abstract Property<Boolean> getGenerateGitPropertiesFile();
57+
58+
/**
59+
* Configuration location of a properties file that can be generated by the plugin.
60+
* Defaults to {@code ${project.getBuildDir()}/git.properties}.
61+
*
62+
* <p>To enable or disable the generation of such properties file refer to
63+
* {@link #getGenerateGitPropertiesFile()}, to configure the format (json, properties, ...)
64+
* of this file refer to {@link #getGenerateGitPropertiesFormat()}.
65+
*/
66+
// @OutputFile
67+
public abstract RegularFileProperty getGenerateGitPropertiesFilename();
68+
69+
/**
70+
* Configuration option to enable or disable unicode escaping in the generated properties file.
71+
*
72+
* <p>Java has a long history with properties files.
73+
* Before Java 9, the encoding of a {@code .properties} file is {@code ISO-8859-1}.
74+
* All non-ASCII characters must be entered by using Unicode escape characters
75+
* (e.g. {@code \\uHHHH}).
76+
* In Java 9 and newer, the default encoding specifically for property resource bundles is
77+
* {@code UTF-8}.
78+
*
79+
* <p>To allow various use-cases this plugin allows to configure an automated unicode escaping.
80+
* Since the core module of this plugin will write the properties file with {@code UTF-8} it is
81+
* reasonable to assume that generated properties do not need special Unicode escape characters
82+
* (e.g. {@code \\uHHHH}).
83+
*
84+
* <p>Hence by default this setting is set to {@code false}.
85+
* If you so desire and wish for unicode escaping (e.g. \\u6E2C\\u8A66\\u4E2D\\u6587) you
86+
* can set this to {@code true}.
87+
*/
88+
public abstract Property<Boolean> getGenerateGitPropertiesFileWithEscapedUnicode();
89+
90+
/**
91+
* Allows to configure the format (json, properties, ...) in which the properties
92+
* should be saved in.
93+
* To enable or disable the generation of such properties file refer to
94+
* {@link #getGenerateGitPropertiesFile()} and to configure the location
95+
* of the properties file refer to {@link #getGenerateGitPropertiesFilename()}.
96+
*
97+
* <p>By default will assume that the desired format is "properties".
98+
*/
99+
public abstract Property<CommitIdPropertiesOutputFormat> getGenerateGitPropertiesFormat();
100+
101+
@Inject
102+
public ProjectLayout getProjectLayout() {
103+
throw new IllegalStateException("Should have been injected!");
104+
}
105+
106+
/**
107+
* Setup the default values / conventions for the GitCommitIdPluginOutputSettingsExtension.
108+
*/
109+
@Inject
110+
public GitCommitIdPluginOutputSettingsExtension() {
111+
getGenerateGitPropertiesFile()
112+
.convention(false);
113+
getGenerateGitPropertiesFilename().convention(
114+
getProjectLayout().getBuildDirectory().file("git.properties"));
115+
getGenerateGitPropertiesFileWithEscapedUnicode().convention(false);
116+
getGenerateGitPropertiesFormat()
117+
.convention(CommitIdPropertiesOutputFormat.PROPERTIES);
118+
}
119+
}

src/test/groovy/io.github.git.commit.id.gradle.plugin/GradleIntegrationTest.groovy

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,9 @@ class GradleIntegrationTest extends AbstractGradleTest {
9090
it.write(
9191
"""
9292
${GitCommitIdPlugin.GIT_COMMIT_ID_EXTENSION_NAME} {
93-
generateGitPropertiesFile.set(true)
93+
outputSettings {
94+
generateGitPropertiesFile.set(true)
95+
}
9496
}
9597
""".stripIndent()
9698
)

0 commit comments

Comments
 (0)