Skip to content

Commit 32a9126

Browse files
committed
#21 Gradle plugin: exposing task parameters directly rather than via extension
1 parent aa22107 commit 32a9126

2 files changed

Lines changed: 348 additions & 45 deletions

File tree

jcp/src/main/java/com/igormaznitsa/jcp/gradle/JcpPreprocessExtension.java

Lines changed: 85 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package com.igormaznitsa.jcp.gradle;
22

3-
import lombok.Data;
4-
import org.gradle.api.Project;
5-
3+
import com.igormaznitsa.meta.annotation.MustNotContainNull;
64
import java.io.File;
75
import java.nio.charset.Charset;
86
import java.nio.charset.StandardCharsets;
@@ -11,6 +9,8 @@
119
import java.util.List;
1210
import java.util.Map;
1311
import javax.annotation.Nullable;
12+
import lombok.Data;
13+
import org.gradle.api.Project;
1414
import org.gradle.execution.commandline.TaskConfigurationException;
1515

1616
@Data
@@ -151,6 +151,87 @@ public class JcpPreprocessExtension {
151151
*/
152152
private boolean dontOverwriteSameContent = false;
153153

154+
@Nullable
155+
private static File merge(@Nullable final File prefered, @Nullable final File second) {
156+
return prefered == null ? second : prefered;
157+
}
158+
159+
@Nullable
160+
private static String merge(@Nullable final String prefered, @Nullable final String second) {
161+
return prefered == null ? second : prefered;
162+
}
163+
164+
@Nullable
165+
@MustNotContainNull
166+
private static Map<String, String> merge(
167+
@Nullable @MustNotContainNull final Map<String, String> preferred,
168+
@Nullable @MustNotContainNull final Map<String, String> second) {
169+
if (preferred == null && second == null) {
170+
return null;
171+
}
172+
final Map<String, String> result = new HashMap<>();
173+
if (preferred != null) {
174+
result.putAll(preferred);
175+
}
176+
if (second != null) {
177+
second.forEach(result::putIfAbsent);
178+
}
179+
return result;
180+
}
181+
182+
@Nullable
183+
private static List<String> merge(
184+
@Nullable @MustNotContainNull final List<String> preferred,
185+
@Nullable @MustNotContainNull final List<String> second
186+
) {
187+
if (preferred == null && second == null) {
188+
return null;
189+
}
190+
final List<String> result = new ArrayList<>();
191+
192+
if (preferred != null) {
193+
result.addAll(preferred);
194+
}
195+
196+
if (second != null) {
197+
second.stream().filter(x -> !result.contains(x)).forEach(result::add);
198+
}
199+
200+
return result;
201+
}
202+
203+
public JcpPreprocessExtension() {
204+
this.keepLines = false;
205+
this.keepComments = false;
206+
}
207+
208+
public JcpPreprocessExtension(final JcpPreprocessExtension preferred, final JcpPreprocessExtension dflt) {
209+
this.allowWhitespaces = preferred.isAllowWhitespaces() || dflt.isAllowWhitespaces();
210+
this.careForLastEol = preferred.isCareForLastEol() || dflt.isCareForLastEol();
211+
this.clearTarget = preferred.isClearTarget() || dflt.isClearTarget();
212+
this.dontOverwriteSameContent = preferred.isDontOverwriteSameContent() || dflt.isDontOverwriteSameContent();
213+
this.dryRun = preferred.isDryRun() || dflt.isDryRun();
214+
this.ignoreMissingSources = preferred.isIgnoreMissingSources() || dflt.isIgnoreMissingSources();
215+
this.keepAttributes = preferred.isKeepAttributes() || dflt.isKeepAttributes();
216+
this.keepComments = preferred.isKeepComments() || dflt.isKeepComments();
217+
this.keepLines = preferred.isKeepLines() || dflt.isKeepLines();
218+
this.unknownVarAsFalse = preferred.isUnknownVarAsFalse() || dflt.isUnknownVarAsFalse();
219+
this.preserveIndents = preferred.isPreserveIndents() || dflt.isPreserveIndents();
220+
this.skip = preferred.isSkip() || dflt.isSkip();
221+
this.verbose = preferred.isVerbose() || dflt.isVerbose();
222+
this.configFiles = merge(preferred.getConfigFiles(), dflt.getConfigFiles());
223+
this.excludeExtensions = merge(preferred.getExcludeExtensions(), dflt.getExcludeExtensions());
224+
this.excludeFolders = merge(preferred.getExcludeFolders(), dflt.getExcludeFolders());
225+
this.extensions = merge(preferred.getExtensions(), dflt.getExtensions());
226+
this.sources = merge(preferred.getSources(), dflt.getSources());
227+
this.baseDir = merge(preferred.getBaseDir(), dflt.getBaseDir());
228+
this.eol = merge(preferred.getEol(), dflt.getEol());
229+
this.sourceEncoding = merge(preferred.getSourceEncoding(), dflt.getSourceEncoding());
230+
this.targetEncoding = merge(preferred.getTargetEncoding(), dflt.getTargetEncoding());
231+
this.vars = merge(preferred.getVars(), dflt.getVars());
232+
this.target = merge(preferred.getTarget(), dflt.getTarget());
233+
}
234+
154235
public JcpPreprocessExtension(final Project project) {
155236
if (this.baseDir == null) {
156237
this.baseDir = project.getProjectDir();
@@ -163,7 +244,7 @@ private void assertCharSet(@Nullable final String name) {
163244
}
164245
}
165246

166-
public void validate(final Project project) {
247+
public void validate() {
167248
if (this.baseDir == null) {
168249
throw new TaskConfigurationException(JcpPreprocessTask.ID, "Basedir must be defined", null);
169250
}

0 commit comments

Comments
 (0)