-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
BuildTools
committed
Nov 7, 2023
1 parent
2e4db3d
commit 0ed2c03
Showing
6 changed files
with
303 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<parent> | ||
<artifactId>template</artifactId> | ||
<groupId>com.github.terefang.template</groupId> | ||
<version>2023.11.378</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>preproc-maven-plugin</artifactId> | ||
|
||
<packaging>maven-plugin</packaging> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>commons-codec</groupId> | ||
<artifactId>commons-codec</artifactId> | ||
<version>1.14</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.projectlombok</groupId> | ||
<artifactId>lombok</artifactId> | ||
<version>RELEASE</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.maven</groupId> | ||
<artifactId>maven-plugin-api</artifactId> | ||
<version>${maven.version}</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.maven</groupId> | ||
<artifactId>maven-core</artifactId> | ||
<version>${maven.version}</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.maven</groupId> | ||
<artifactId>maven-artifact</artifactId> | ||
<version>${maven.version}</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.maven.plugin-tools</groupId> | ||
<artifactId>maven-plugin-annotations</artifactId> | ||
<version>3.5.1</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.maven</groupId> | ||
<artifactId>maven-compat</artifactId> | ||
<version>${maven.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.codehaus.plexus</groupId> | ||
<artifactId>plexus-utils</artifactId> | ||
<version>3.3.0</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-plugin-plugin</artifactId> | ||
<version>3.5.1</version> | ||
<configuration> | ||
<!-- <goalPrefix>maven-archetype-plugin</goalPrefix> --> | ||
<skipErrorNoDescriptorsFound>true</skipErrorNoDescriptorsFound> | ||
</configuration> | ||
<executions> | ||
<execution> | ||
<id>mojo-descriptor</id> | ||
<phase>prepare-package</phase> | ||
<goals> | ||
<goal>descriptor</goal> | ||
</goals> | ||
</execution> | ||
<execution> | ||
<id>help-goal</id> | ||
<phase>prepare-package</phase> | ||
<goals> | ||
<goal>helpmojo</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
198 changes: 198 additions & 0 deletions
198
...maven-plugin/src/main/java/com/github/terefang/preproc_maven_plugin/PreProcessorMojo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,198 @@ | ||
package com.github.terefang.preproc_maven_plugin; | ||
|
||
import lombok.Data; | ||
import org.apache.maven.plugin.AbstractMojo; | ||
import org.apache.maven.plugin.MojoExecutionException; | ||
import org.apache.maven.plugin.MojoFailureException; | ||
import org.apache.maven.plugins.annotations.LifecyclePhase; | ||
import org.apache.maven.plugins.annotations.Mojo; | ||
import org.apache.maven.plugins.annotations.Parameter; | ||
import org.codehaus.plexus.util.DirectoryScanner; | ||
import org.codehaus.plexus.util.IOUtil; | ||
import org.codehaus.plexus.util.StringUtils; | ||
|
||
import java.io.*; | ||
import java.util.ArrayDeque; | ||
import java.util.Arrays; | ||
|
||
@Mojo(name = "pre-processor", defaultPhase = LifecyclePhase.GENERATE_RESOURCES) | ||
@Data | ||
public class PreProcessorMojo extends AbstractMojo | ||
{ | ||
@Parameter(defaultValue = "${project.build.scriptSourceDirectory}") | ||
protected File resourcesDirectory; | ||
|
||
@Parameter(defaultValue = "${project.build.directory}/generated-resources") | ||
protected File resourcesOutput; | ||
|
||
@Parameter(defaultValue = "false") | ||
private boolean singleFileOutput; | ||
|
||
@Parameter(defaultValue = "false") | ||
private boolean flattenOutput; | ||
|
||
@Parameter(defaultValue = ".txt") | ||
private String destinationExtension; | ||
|
||
@Parameter(defaultValue = "false") | ||
private boolean processSingleLineMarker; | ||
|
||
@Parameter(defaultValue = "false") | ||
private boolean processIncludes; | ||
|
||
@Parameter(defaultValue = "MARK") | ||
private String marker; | ||
|
||
@Parameter | ||
private String includes; | ||
|
||
@Parameter | ||
private String excludes; | ||
|
||
@Override | ||
public void execute() throws MojoExecutionException, MojoFailureException | ||
{ | ||
DirectoryScanner scanner = new DirectoryScanner(); | ||
|
||
if( resourcesDirectory.isDirectory()) | ||
{ | ||
scanner.setBasedir(resourcesDirectory); | ||
if (StringUtils.isNotEmpty(includes)) { | ||
scanner.setIncludes(StringUtils.split(includes)); | ||
} else { | ||
scanner.setIncludes(new String[]{"**/*"}); | ||
} | ||
|
||
if (StringUtils.isNotEmpty(excludes)) { | ||
scanner.setExcludes(StringUtils.split(excludes)); | ||
} | ||
|
||
scanner.addDefaultExcludes(); | ||
scanner.scan(); | ||
|
||
process(scanner.getIncludedFiles()); | ||
} | ||
else | ||
if( resourcesDirectory.isFile()) | ||
{ | ||
process(new String[]{ resourcesDirectory.getPath() }); | ||
} | ||
} | ||
|
||
public void process(String[] includedFiles) | ||
{ | ||
includedFiles = Arrays.stream(includedFiles) | ||
.sorted((x,y) -> { return x.compareToIgnoreCase(y); }) | ||
.toArray((x) -> { return new String[x]; }); | ||
File file = null; | ||
PrintWriter out = null; | ||
String _current = null; | ||
try | ||
{ | ||
if(singleFileOutput) | ||
{ | ||
file = resourcesOutput; | ||
file.getParentFile().mkdirs(); | ||
out = new PrintWriter(file); | ||
} | ||
|
||
for (String key : includedFiles) | ||
{ | ||
this.getLog().info("processing: "+key); | ||
_current = key; | ||
String keybase = key.substring(0, key.lastIndexOf(".")); | ||
if(!singleFileOutput) | ||
{ | ||
file = new File(resourcesOutput, keybase.concat(destinationExtension)); | ||
if(flattenOutput) | ||
{ | ||
file = new File(resourcesOutput, file.getName()); | ||
} | ||
file.getParentFile().mkdirs(); | ||
out = new PrintWriter(file); | ||
} | ||
|
||
File localFile = new File(resourcesDirectory, key); | ||
|
||
resolveAndWrite(localFile.getParentFile(), localFile, out); | ||
|
||
if(!singleFileOutput) | ||
{ | ||
out.close(); | ||
} | ||
} | ||
|
||
if(singleFileOutput) | ||
{ | ||
out.close(); | ||
} | ||
} | ||
catch (Exception _xe) | ||
{ | ||
this.getLog().error((_current!=null ? _current+": " : "")+_xe.getMessage(), _xe); | ||
} | ||
} | ||
|
||
public void resolveAndWrite(File _parent, File _file, PrintWriter _out) throws IOException | ||
{ | ||
ArrayDeque<BufferedReader> _queue = new ArrayDeque<>(); | ||
_queue.add(new BufferedReader(new FileReader(_file))); | ||
ArrayDeque<File> _dirs = new ArrayDeque<>(); | ||
_dirs.add(_parent); | ||
|
||
while(_queue.size()>0) | ||
{ | ||
BufferedReader _lr = _queue.poll(); | ||
File _bd = _dirs.poll(); | ||
|
||
String _line = ""; | ||
boolean _in=false; | ||
while((_line = _lr.readLine()) != null) | ||
{ | ||
if(_line.startsWith("%!end ")) | ||
{ | ||
if(this.marker.equalsIgnoreCase(_line.substring(6).trim())) | ||
{ | ||
_in=false; | ||
} | ||
} | ||
else | ||
if(_in) | ||
{ | ||
_out.println(_line); | ||
} | ||
else | ||
if(_line.startsWith("%!begin ")) | ||
{ | ||
if(this.marker.equalsIgnoreCase(_line.substring(8).trim())) | ||
{ | ||
_in=true; | ||
} | ||
} | ||
else | ||
if(_line.startsWith("%!include ") && processIncludes) | ||
{ | ||
_queue.push(_lr); | ||
_dirs.push(_bd); | ||
File _next = new File(_bd, _line.substring(10).trim()); | ||
this.getLog().info("including: "+_next.getName()); | ||
_bd = _next.getParentFile(); | ||
_lr = new BufferedReader(new FileReader(_next)); | ||
} | ||
else | ||
if(_line.startsWith("%% ") && processSingleLineMarker) | ||
{ | ||
_out.println(_line.substring(3)); | ||
} | ||
else | ||
if(_line.equalsIgnoreCase("%%") && processSingleLineMarker) | ||
{ | ||
_out.println(); | ||
} | ||
|
||
} | ||
_out.flush(); | ||
IOUtil.close(_lr); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters