Skip to content

Commit 6157eba

Browse files
author
TheSnoozer
committed
Experiment with up-to-date checks and exports of properties to the project
This is yet another idea of experting the generated properties to the project while keeping the task that generates them as "up-to-date". in this implementation the task essentially generates two properties. One is the desired one configured by the user, the other is the "internal" one used to expose the properties to the project. The concept relies on groovy closures. Not sure if this ideal.
1 parent 925f5be commit 6157eba

4 files changed

Lines changed: 96 additions & 29 deletions

File tree

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

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,15 @@
1717

1818
package io.github.git.commit.id.gradle.plugin;
1919

20+
import groovy.lang.Closure;
21+
import java.util.Map;
22+
import java.util.Properties;
2023
import org.gradle.api.Plugin;
2124
import org.gradle.api.Project;
2225
import org.gradle.api.plugins.ExtensionAware;
26+
import pl.project13.core.CommitIdPropertiesOutputFormat;
27+
import pl.project13.core.GitCommitIdExecutionException;
28+
import pl.project13.core.util.GenericFileManager;
2329

2430
/**
2531
* The GitCommitIdPlugin or also known as git-commit-id-gradle-plugin is a plugin
@@ -63,6 +69,69 @@
6369
* to {@link GitCommitIdPluginGenerationTask}.
6470
*/
6571
public class GitCommitIdPlugin implements Plugin<Project> {
72+
private static final class PropertyExposingClosure extends Closure<String> {
73+
private final GitCommitIdPluginGenerationTask task;
74+
75+
public PropertyExposingClosure(Object owner, GitCommitIdPluginGenerationTask task) {
76+
super(owner, owner);
77+
this.task = task;
78+
}
79+
80+
@Override
81+
public String toString() {
82+
StringBuilder s = new StringBuilder();
83+
for (Map.Entry<Object, Object> e : getProps().entrySet()) {
84+
s.append(e.getValue());
85+
s.append(":");
86+
s.append(e.getKey());
87+
s.append(",");
88+
}
89+
return s.toString();
90+
}
91+
92+
private Properties getProps() {
93+
try {
94+
final Properties p = GenericFileManager.readPropertiesAsUtf8(
95+
// task.getGitCommitIdPluginOutputSettingsExtension().getOutputFormat().get(),
96+
CommitIdPropertiesOutputFormat.PROPERTIES,
97+
task.getInternalOutput().getAsFile()
98+
);
99+
return p;
100+
} catch (GitCommitIdExecutionException e) {
101+
throw new RuntimeException(e);
102+
}
103+
}
104+
105+
@Override
106+
public Object getProperty(String property) {
107+
return getProps().get(property);
108+
}
109+
110+
public String doCall() {
111+
return toString();
112+
}
113+
114+
public String doCall(Object obj) {
115+
return toString();
116+
}
117+
118+
public String doCall(String property) {
119+
return (String) getProps().get(property);
120+
}
121+
122+
// Do NOT IMPLEMENT!
123+
// https://issues.apache.org/jira/browse/GROOVY-1665
124+
// public String doCall(Object obj1, Object obj2) {
125+
// }
126+
public String get(String property) {
127+
return (String) getProps().get(property);
128+
}
129+
130+
public String get(String property, String defaultString) {
131+
return (String) getProps().getOrDefault(property, defaultString);
132+
}
133+
}
134+
66135
/**
67136
* Apply this plugin to the given target project.
68137
*
@@ -94,5 +163,10 @@ public void apply(Project project) {
94163
GitCommitIdPluginGenerationTask.NAME,
95164
GitCommitIdPluginGenerationTask.class);
96165
task.onlyIf(ignore -> extension.getSkip().get() == false);
166+
167+
project
168+
.getExtensions()
169+
.getExtraProperties()
170+
.set("gitProperties", new PropertyExposingClosure(this, task));
97171
}
98172
}

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

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import java.nio.charset.Charset;
2222
import java.nio.charset.StandardCharsets;
2323
import java.util.Date;
24-
import java.util.HashMap;
2524
import java.util.List;
2625
import java.util.Map;
2726
import java.util.Properties;
@@ -30,10 +29,10 @@
3029
import javax.annotation.Nullable;
3130
import org.gradle.api.DefaultTask;
3231
import org.gradle.api.file.DirectoryProperty;
32+
import org.gradle.api.file.RegularFile;
3333
import org.gradle.api.file.RegularFileProperty;
3434
import org.gradle.api.plugins.ExtensionAware;
3535
import org.gradle.api.tasks.CacheableTask;
36-
import org.gradle.api.tasks.Input;
3736
import org.gradle.api.tasks.InputDirectory;
3837
import org.gradle.api.tasks.Internal;
3938
import org.gradle.api.tasks.OutputFile;
@@ -44,10 +43,10 @@
4443
import pl.project13.core.CommitIdPropertiesOutputFormat;
4544
import pl.project13.core.GitCommitIdExecutionException;
4645
import pl.project13.core.GitCommitIdPlugin;
47-
import pl.project13.core.GitCommitPropertyConstant;
4846
import pl.project13.core.git.GitDescribeConfig;
4947
import pl.project13.core.log.LogInterface;
5048
import pl.project13.core.util.BuildFileChangeListener;
49+
import pl.project13.core.util.GenericFileManager;
5150

5251
/**
5352
* The task that generates the "git" information.
@@ -64,7 +63,6 @@ public class GitCommitIdPluginGenerationTask extends DefaultTask {
6463
public static final String NAME = "gitCommitIdGenerationTask";
6564
public static final String PLUGIN_EXECUTION_MESSAGE =
6665
"Executing GitCommitIdPlugin to gather relevant properties";
67-
private HashMap<String, String> lastGeneratedOutput;
6866

6967
/**
7068
* The {@link GitCommitIdPluginExtension} that serves as configuration of the plugin / task.
@@ -127,21 +125,9 @@ public RegularFileProperty getOutput() {
127125
return getGitCommitIdPluginOutputSettingsExtension().getOutputFile();
128126
}
129127

130-
/**
131-
* Attempt to support up-to-date checks with the generated properties.
132-
* A similar concept is used in {@link org.gradle.api.tasks.WriteProperties}.
133-
*
134-
* @return
135-
* The properties that may have been generated by previous runs, or if not up-to-date
136-
* recalculates the properties by execution the plugin again.
137-
*/
138-
@Input
139-
public HashMap<String, String> getGeneratedProperties() {
140-
if (lastGeneratedOutput != null) {
141-
return lastGeneratedOutput;
142-
}
143-
lastGeneratedOutput = runThePlugin();
144-
return lastGeneratedOutput;
128+
@OutputFile
129+
public RegularFile getInternalOutput() {
130+
return getProject().getLayout().getBuildDirectory().file("internal_git.properties").get();
145131
}
146132

147133
/**
@@ -150,13 +136,10 @@ public HashMap<String, String> getGeneratedProperties() {
150136
*/
151137
@TaskAction
152138
public void runTheTask() {
153-
getProject()
154-
.getExtensions()
155-
.getExtraProperties()
156-
.set("gitProperties", getGeneratedProperties());
139+
runThePlugin();
157140
}
158141

159-
private HashMap<String, String> runThePlugin() {
142+
private void runThePlugin() {
160143
getLogger().debug(PLUGIN_EXECUTION_MESSAGE);
161144
GitCommitIdPluginExtension extension = getGitCommitIdPluginExtension();
162145
boolean verbose = extension.getVerbose().get();
@@ -373,7 +356,19 @@ public boolean shouldPropertiesEscapeUnicode() {
373356
Properties properties = new Properties();
374357
pl.project13.core.GitCommitIdPlugin.runPlugin(cb, properties);
375358

376-
// Convert it to a hashmap, excluding build_time
359+
GenericFileManager.dumpProperties(
360+
null,
361+
CommitIdPropertiesOutputFormat.PROPERTIES,
362+
getInternalOutput().getAsFile(),
363+
StandardCharsets.UTF_8,
364+
false,
365+
null,
366+
properties
367+
);
368+
369+
// When we want to use it as TaskInput or something,
370+
// it should be convert to a hashmap, excluding build_time
371+
/*
377372
var prefix = getGitCommitIdPluginFormatSettingsExtension().getPropertyPrefix().get();
378373
HashMap<String, String> hm = new HashMap<>();
379374
for (Map.Entry<Object, Object> e : properties.entrySet()) {
@@ -385,7 +380,7 @@ public boolean shouldPropertiesEscapeUnicode() {
385380
hm.put(key, (String) e.getValue());
386381
}
387382
}
388-
return hm;
383+
*/
389384
} catch (GitCommitIdExecutionException e) {
390385
throw new RuntimeException(e);
391386
}

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,10 @@ class AbstractGradleTest {
8080

8181
protected void assertPluginSkipped(BuildResult result) {
8282
assertTaskOutcome(result, TaskOutcome.UP_TO_DATE)
83-
/*
84-
// TODO: Why is the execution message printed, while the task claims to be up-to-date???
8583
Assertions.assertFalse(
8684
result.output.contains(GitCommitIdPluginGenerationTask.PLUGIN_EXECUTION_MESSAGE),
8785
result.output
8886
)
89-
*/
9087
}
9188

9289
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ class GradleIntegrationTest extends AbstractGradleTest {
136136
"""
137137
task printPropTask(type: DefaultTask) {
138138
outputs.upToDateWhen { false }
139+
// dependsOn (tasks.gitCommitIdGenerationTask)
139140
doLast {
140141
// println("${marker}1: \${project?.ext?.gitProperties}${marker}")
141142
// println("${marker}2: \${project?.ext?.gitProperties.get('git.commit.id.full')}${marker}")

0 commit comments

Comments
 (0)