Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 26 additions & 34 deletions src/main/java/com/github/hexomod/macro/Preprocessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,11 @@ public class Preprocessor {
static Map<String, Map<String, String>> EXTENSION_KEYWORDS = new HashMap<String, Map<String, String>>() {{
put("java", SLASH_KEYWORDS);
put("gradle", SLASH_KEYWORDS);
put("hjson", SLASH_KEYWORDS);
put("json5", SLASH_KEYWORDS);
put("yaml", HASH_KEYWORDS);
put("yml", HASH_KEYWORDS);
put("toml", HASH_KEYWORDS);
}};

private final Map<String, Object> vars;
Expand All @@ -87,7 +90,7 @@ public void process(File inFile, File outFile) throws IOException {
String fileString = FileUtils.readFileToString(inFile, StandardCharsets.UTF_8);
if (!known) {
for (String slash : SLASH_KEYWORDS.values()) {
if (slash != "///" && fileString.contains(slash)) {
if (!"///".equals(slash) && fileString.contains(slash)) {
known = true;
keywords = SLASH_KEYWORDS;
break;
Expand All @@ -96,7 +99,7 @@ public void process(File inFile, File outFile) throws IOException {
}
if (!known) {
for (String slash : HASH_KEYWORDS.values()) {
if (slash != "###" && fileString.contains(slash)) {
if (!"###".equals(slash) && fileString.contains(slash)) {
known = true;
keywords = HASH_KEYWORDS;
break;
Expand Down Expand Up @@ -134,9 +137,10 @@ public void process(File inFile, File outFile) throws IOException {

List<String> processLines(List<String> lines, Map<String, String> keywords) throws ParserException {
LinkedList<Boolean> state = new LinkedList<>();
// Used for tracking elseif trees
LinkedList<Boolean> skips = new LinkedList<>();
List<String> newLines = new ArrayList<>();
// By default the line is considered as active
// By default, the line is considered as active
state.push(true);
skips.push(false);

Expand All @@ -150,7 +154,7 @@ List<String> processLines(List<String> lines, Map<String, String> keywords) thro
boolean active = this.vars.get(trimLine.substring(keywords.get("ifdef").length()).trim()) != null;
// Store the last active state
state.push(active & state.getFirst());
//
// Skip all further else statements
skips.push(active);
// Keep macro line
if (!remove) newLines.add(line);
Expand All @@ -161,71 +165,59 @@ else if (trimLine.startsWith(keywords.get("if"))) {
boolean active = evaluateExpression(trimLine.substring(keywords.get("if").length()));
// Store the last active state
state.push(active & state.getFirst());
//
// Skip all further else statements
skips.push(active);
// Keep macro line
if (!remove) newLines.add(line);
}
// elseif
else if (trimLine.startsWith(keywords.get("elseif"))) {
// get last skip
boolean skip = skips.getFirst();
//
if (!skip) {
// get last active state
// Check for skip
if (skips.getFirst()) {
state.pop();
state.push(false);
} else {
// Get last active state
boolean active = state.getFirst();
// Evaluate elseif condition
active = (!active) & evaluateExpression(trimLine.substring(keywords.get("elseif").length()));
// Revert the last state
state.pop();
// Store the last active state
// Store the current active state
state.push((active) & state.getFirst());
//
skips.pop();
skips.push(skip & skips.getFirst());
} else {
state.pop();
state.push(false);
skips.push(active);
}
// Keep macro line
if (!remove) newLines.add(line);
}
// else
else if (trimLine.startsWith(keywords.get("else"))) {
// get last skip
boolean skip = skips.getFirst();
//
if (!skip) {
// check for skip
if (skips.getFirst()) {
state.pop();
state.push(false);
} else {
// get last active state
boolean active = state.getFirst();
// Revert the last state
state.pop();
state.push((!active) & state.getFirst());
//
skips.pop();
skips.push((!skip) & skips.getFirst());
} else {
state.pop();
state.push(false);
}
// Keep macro line
if (!remove) newLines.add(line);
}
// endif
else if (trimLine.startsWith(keywords.get("endif"))) {
// Enable
// Pop the states
state.pop();
//
skips.pop();
// Keep macro line
if (!remove) newLines.add(line);
} else {
// get last active state
boolean active = state.getFirst();
//
if (active)
// get current active state
if (state.getFirst()) {
newLines.add(uncommentLine(line, keywords));
else {
} else {
if (!remove) newLines.add(commentLine(line, keywords));
}
}
Expand Down
48 changes: 46 additions & 2 deletions src/main/java/com/github/hexomod/macro/PreprocessorExtension.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.gradle.api.Action;
import org.gradle.api.Project;
import org.gradle.api.internal.project.ProjectInternal;
import org.gradle.api.tasks.Internal;
import org.gradle.internal.Actions;
import org.gradle.util.ConfigureUtil;

Expand Down Expand Up @@ -97,19 +98,35 @@ public Map<String, Object> getVars() {
return this.vars;
}

/**
* Sets the preprocessor variables.
* @param vars the variables
*/
public void setVars(Map<String, Object> vars) {
this.vars.putAll(vars);
}


/**
* Returns the directory where preprocessed files will be stored.
* @return the directory where preprocessed files will be stored.
*/
public File getProcessDir() {
return processDir;
}

/**
* Sets the directory where preprocessed files will be stored.
* @param processDir the directory
*/
public void setProcessDir(File processDir) {
this.processDir = processDir;
}

/**
* Sets the directory where preprocessed files will be stored.
* @param processDir the directory, relative to the project build directory
*/
public void setProcessDir(String processDir) {
String buildName = this.project.getBuildDir().getName();
if (processDir.startsWith(buildName)) {
Expand All @@ -119,11 +136,18 @@ public void setProcessDir(String processDir) {
}
}


/**
* Returns whether verbose logging to console is enabled.
* @return whether verbose logging to console is enabled.
*/
public boolean getVerbose() {
return verbose;
}

/**
* Sets the verbose logging mode.
* @param verbose whether to enable verbose mode
*/
public void setVerbose(boolean verbose) {
this.verbose = verbose;
}
Expand All @@ -133,10 +157,20 @@ public Java getJava() {
return java;
}

/**
* Configure how to process java files.
* @param closure the configuration closure
* @return the configuration object
*/
public Java java(Closure closure) {
return ConfigureUtil.configure(closure, java);
}

/**
* Configure how to process java files.
* @param action the configuration action
* @return the configuration object
*/
public Java java(Action<? super Java> action) {
return Actions.with(java, action);
}
Expand All @@ -146,15 +180,25 @@ public Resources getResources() {
return resources;
}

/**
* Configure how to process resources files.
* @param closure the configuration closure
* @return the configuration object
*/
public Resources resources(Closure closure) {
return ConfigureUtil.configure(closure, resources);
}

/**
* Configure how to process resources files.
* @param action the configuration action
* @return the configuration object
*/
public Resources resources(Action<? super Resources> action) {
return Actions.with(resources, action);
}


@Internal
// Print out a string if verbose is enabled
public void log(String msg) {
if (getVerbose()) {
Expand Down
26 changes: 26 additions & 0 deletions src/main/java/com/github/hexomod/macro/extensions/SourceType.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,26 +36,52 @@ public SourceType() {
remove = false;
}

/**
* Returns whether this source type is enabled.
* @return whether this source type is enabled.
*/
public boolean getEnable() {
return enable;
}

/**
* Sets whether the preprocessor is enabled for this source type.
* @param enable Whether this source type is enabled.
*/
public void setEnable(boolean enable) {
this.enable = enable;
}

/**
* Returns whether changes are made in-place.
* @return Whether changes are made in-place.
*/
public boolean getInPlace() {
return inPlace;
}

/**
* Sets whether the preprocessor changes should be made in-place.
* That means that the changes will be applied directly to the source files.
* The latter is especially useful for development on Java source files.
* @param inPlace Whether the preprocessor should be applied in-place.
*/
public void setInPlace(boolean inPlace) {
this.inPlace = inPlace;
}

/**
* Returns whether preprocessor statements shall be removed.
* @return Whether preprocessor statements shall be removed.
*/
public boolean getRemove() {
return remove;
}

/**
* Sets whether preprocessor statements shall be removed.
* @param remove Whether preprocessor statements shall be removed.
*/
public void setRemove(boolean remove) {
this.remove = remove;
}
Expand Down
Loading