Skip to content

Commit 8ef9f1a

Browse files
committed
fixes
1 parent 453e144 commit 8ef9f1a

File tree

3 files changed

+41
-21
lines changed

3 files changed

+41
-21
lines changed
Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package com.igormaznitsa.jcp.gradle;
22

3+
import javax.annotation.Nonnull;
34
import org.gradle.api.Plugin;
45
import org.gradle.api.Project;
56

67
public class JcpGradlePlugin implements Plugin<Project> {
7-
public void apply(Project project) {
8-
project.getTasks().create("preprocess", JcpPreprocessTask.class);
9-
project.getExtensions().create("preprocessSettings", JcpPreprocessExtension.class, project);
8+
9+
@Override
10+
public void apply(@Nonnull final Project project) {
11+
project.getTasks().create(JcpPreprocessTask.ID, JcpPreprocessTask.class);
12+
project.getExtensions().create(JcpPreprocessExtension.ID, JcpPreprocessExtension.class, project);
1013
}
1114
}

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

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,14 @@
1010
import java.util.HashMap;
1111
import java.util.List;
1212
import java.util.Map;
13+
import javax.annotation.Nullable;
1314
import org.gradle.execution.commandline.TaskConfigurationException;
1415

1516
@Data
1617
public class JcpPreprocessExtension {
1718

19+
public static final String ID = "preprocessSettings";
20+
1821
/**
1922
* Source root folders for preprocessing, if it is empty then project provided
2023
* folders will be used.
@@ -72,7 +75,7 @@ public class JcpPreprocessExtension {
7275
private boolean unknownVarAsFalse = false;
7376

7477
/**
75-
* Dry run, making preprocessing but without output
78+
* Dry run, making pre-processing but without output
7679
*/
7780
private boolean dryRun = false;
7881

@@ -155,38 +158,38 @@ public class JcpPreprocessExtension {
155158
public JcpPreprocessExtension(final Project project) {
156159
if (this.baseDir == null) {
157160
this.baseDir = project.getProjectDir();
158-
project.getLogger().debug("Got basedir from project: " + this.baseDir);
161+
project.getLogger().debug("Basedir of the project: " + this.baseDir);
159162
}
160163
}
161164

162-
private void assertCharSet(final String name) {
163-
if (!Charset.isSupported(name)) {
164-
throw new TaskConfigurationException("preprocess", "Unsupported charset: " + name, null);
165+
private void assertCharSet(@Nullable final String name) {
166+
if (name == null || !Charset.isSupported(name)) {
167+
throw new TaskConfigurationException(JcpPreprocessTask.ID, "Unsupported charset: " + name, null);
165168
}
166169
}
167170

168171
public void validate(final Project project) {
169172
if (this.baseDir == null) {
170-
throw new TaskConfigurationException("preprocess", "Basedir must be defined", null);
173+
throw new TaskConfigurationException(JcpPreprocessTask.ID, "Basedir must be defined", null);
171174
}
172175

173176
if (!this.baseDir.isDirectory()) {
174-
throw new TaskConfigurationException("preprocess", "Basedir doesn't exist: " + this.baseDir, null);
177+
throw new TaskConfigurationException(JcpPreprocessTask.ID, "Basedir doesn't exist: " + this.baseDir, null);
175178
}
176179

177180
assertCharSet(this.sourceEncoding);
178181
assertCharSet(this.targetEncoding);
179182

180183
if (this.sources == null) {
181-
throw new TaskConfigurationException("preprocess", "Source folders are not deined in 'sources'", null);
184+
throw new TaskConfigurationException(JcpPreprocessTask.ID, "Source folders are not deined in 'sources'", null);
182185
}
183186

184187
if (this.sources.isEmpty()) {
185-
throw new TaskConfigurationException("preprocess", "Source folders are empty", null);
188+
throw new TaskConfigurationException(JcpPreprocessTask.ID, "Source folder list is empty", null);
186189
}
187190

188191
if (this.target == null) {
189-
throw new TaskConfigurationException("preprocess", "Target folder is not deined in 'target'", null);
192+
throw new TaskConfigurationException(JcpPreprocessTask.ID, "Target folder is not deined in 'target'", null);
190193
}
191194
}
192195

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

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.igormaznitsa.jcp.JcpPreprocessor;
44
import com.igormaznitsa.jcp.context.PreprocessorContext;
5+
import com.igormaznitsa.jcp.exceptions.PreprocessorException;
56
import com.igormaznitsa.jcp.expression.Value;
67
import com.igormaznitsa.jcp.logger.PreprocessorLogger;
78
import org.gradle.api.DefaultTask;
@@ -12,9 +13,12 @@
1213
import java.nio.charset.Charset;
1314
import java.util.Map;
1415
import org.gradle.api.logging.Logger;
16+
import org.gradle.api.tasks.TaskExecutionException;
1517

1618
public class JcpPreprocessTask extends DefaultTask {
1719

20+
public static final String ID = "preprocess";
21+
1822
@TaskAction
1923
public void preprocessTask() throws IOException {
2024
final Logger logger = getProject().getLogger();
@@ -51,13 +55,16 @@ public void warning(@Nullable final String message) {
5155
preprocessorContext.setTarget(preprocessExtension.getTarget());
5256
preprocessorContext.setSources(preprocessExtension.getSources());
5357

54-
if (preprocessExtension.getEol() != null) {
55-
preprocessorContext.setEol(preprocessExtension.getEol());
56-
} else {
58+
if (preprocessExtension.getEol() == null) {
5759
logger.debug("Using default EOL");
60+
} else {
61+
preprocessorContext.setEol(preprocessExtension.getEol());
62+
}
63+
64+
if (preprocessExtension.getExcludeFolders() != null) {
65+
preprocessorContext.setExcludeFolders(preprocessExtension.getExcludeFolders());
5866
}
5967

60-
preprocessorContext.setExcludeFolders(preprocessExtension.getExcludeFolders());
6168
preprocessorContext.setDontOverwriteSameContent(preprocessExtension.isDontOverwriteSameContent());
6269
preprocessorContext.setClearTarget(preprocessExtension.isClearTarget());
6370
preprocessorContext.setCareForLastEol(preprocessExtension.isCareForLastEol());
@@ -77,9 +84,11 @@ public void warning(@Nullable final String message) {
7784
preprocessorContext.setExtensions(preprocessExtension.getExtensions());
7885
}
7986

80-
for (final Map.Entry<String, String> var : preprocessExtension.getVars().entrySet()) {
81-
logger.debug(String.format("Registering global variable: %s=%s", var.getKey(), var.getValue()));
82-
preprocessorContext.setGlobalVariable(var.getKey(), Value.recognizeRawString(var.getValue()));
87+
if (preprocessExtension.getVars() != null) {
88+
for (final Map.Entry<String, String> var : preprocessExtension.getVars().entrySet()) {
89+
logger.debug(String.format("Registering global variable: %s=%s", var.getKey(), var.getValue()));
90+
preprocessorContext.setGlobalVariable(var.getKey(), Value.recognizeRawString(var.getValue()));
91+
}
8392
}
8493

8594
preprocessorContext.setPreserveIndents(preprocessExtension.isPreserveIndents());
@@ -90,6 +99,11 @@ public void warning(@Nullable final String message) {
9099

91100
final JcpPreprocessor preprocessor = new JcpPreprocessor(preprocessorContext);
92101
logger.debug("Start preprocessing...");
93-
preprocessor.execute();
102+
103+
try {
104+
preprocessor.execute();
105+
} catch (final PreprocessorException ex) {
106+
throw new TaskExecutionException(this, ex);
107+
}
94108
}
95109
}

0 commit comments

Comments
 (0)