|
| 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 | +} |
0 commit comments