13 Commits
9 changed files with 239 additions and 77 deletions
+1 -1
View File
@@ -7,7 +7,7 @@
<groupId>com.inteligr8</groupId>
<artifactId>regex-maven-plugin</artifactId>
<version>1.0.0</version>
<version>1.0.3</version>
<packaging>maven-plugin</packaging>
<name>A Maven plugin for regex operations</name>
+87 -14
View File
@@ -1,8 +1,8 @@
<?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">
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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.inteligr8</groupId>
@@ -11,7 +11,7 @@
<packaging>pom</packaging>
<name>Replace File Plugin Tests</name>
<build>
<plugins>
<plugin>
@@ -62,7 +62,7 @@
<excludes>
<exclude>pom.xml</exclude>
<exclude>*.log</exclude>
<exclude>target</exclude>
<exclude>target/**/*</exclude>
</excludes>
<outputDirectory>${project.build.directory}/noreplace-one</outputDirectory>
</fileset>
@@ -75,31 +75,104 @@
</regexes>
</configuration>
</execution>
<execution>
<id>replace-linestart</id>
<phase>validate</phase>
<goals>
<goal>replace-file</goal>
</goals>
<configuration>
<filesets>
<fileset>
<directory>${basedir}/folder1</directory>
<includes>
<include>file12.txt</include>
</includes>
<outputDirectory>${project.build.directory}/replace-linestart</outputDirectory>
</fileset>
</filesets>
<regexes>
<regex>
<pattern>^it is</pattern>
<replacement># it is</replacement>
</regex>
</regexes>
</configuration>
</execution>
<execution>
<id>inplace</id>
<phase>validate</phase>
<goals>
<goal>replace-file</goal>
</goals>
<configuration>
<filesets>
<fileset>
<includes>
<include>**/file12.txt</include>
</includes>
<excludes>
<exclude>target/**/*</exclude>
</excludes>
</fileset>
</filesets>
<regexes>
<regex>
<pattern>is supposed</pattern>
<replacement>is likely</replacement>
</regex>
</regexes>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-enforcer-plugin</artifactId>
<version>1.4.1</version>
<version>3.0.0-M3</version>
<executions>
<execution>
<id>assert</id>
<goals><goal>enforce</goal></goals>
<goals>
<goal>enforce</goal>
</goals>
<configuration>
<rules>
<requireFilesExist>
<files>
<file>${project.build.directory}/replace-one/file1.txt</file>
<file>${project.build.directory}/noreplace-one/file1.txt</file>
<file>${project.build.directory}/noreplace-one/folder1/file11.txt</file>
<file>${project.build.directory}/noreplace-one/folder1/file12.txt</file>
</files>
</requireFilesExist>
<requireFilesDontExist>
<files>
<file>${project.build.directory}/replace-one/folder1/file11.txt</file>
<file>${project.build.directory}/replace-one/folder1/file12.txt</file>
</files>
</requireFilesDontExist>
<requireFileChecksum>
<file>${project.build.directory}/replace-one/file1.txt</file>
<checksum>6f1ed002ab5595859014ebf0951522d9</checksum>
<type>md5</type>
</requireFileChecksum>
<requireFileChecksum>
<file>${project.build.directory}/noreplace-one/file1.txt</file>
<checksum>6f1ed002ab5595859014ebf0951522d9</checksum>
<type>md5</type>
</requireFileChecksum>
<requireFileChecksum>
<file>${project.build.directory}/noreplace-one/folder1/file11.txt</file>
<checksum>72cd622783716925706f49d392089b48</checksum>
<type>md5</type>
</requireFileChecksum>
<requireFileChecksum>
<file>${project.build.directory}/noreplace-one/folder1/file12.txt</file>
<checksum>925a6e3bd25cdd61695aa55d008ab4d2</checksum>
<type>md5</type>
</requireFileChecksum>
<requireFileChecksum>
<file>${project.build.directory}/replace-linestart/file12.txt</file>
<checksum>fb1f2b040970526fc99b1339f22465f1</checksum>
<type>md5</type>
</requireFileChecksum>
<requireFileChecksum>
<file>${basedir}/folder1/file12.txt</file>
<checksum>a7f509275e16ff57cd7756cdca70822a</checksum>
<type>md5</type>
</requireFileChecksum>
</rules>
</configuration>
</execution>
@@ -14,8 +14,6 @@
*/
package com.inteligr8.maven.model;
import org.apache.commons.lang3.StringUtils;
public class Regex implements Normalizable {
private String pattern;
@@ -41,8 +39,8 @@ public class Regex implements Normalizable {
@Override
public void normalize() {
this.pattern = StringUtils.trimToNull(this.pattern);
this.replacement = StringUtils.trimToNull(this.replacement);
if (this.replacement == null)
this.replacement = "";
}
}
@@ -39,6 +39,9 @@ public abstract class AbstractMatchMojo extends AbstractRegexMojo {
@Parameter( property = "patterns", required = true )
protected List<String> patterns;
@Parameter( property = "negate", required = true, defaultValue = "false" )
protected boolean negate = false;
private List<Pattern> compiledPatterns;
@@ -47,6 +50,8 @@ public abstract class AbstractMatchMojo extends AbstractRegexMojo {
boolean matches = this.matches(text);
if (matches)
this.getLog().debug("Matches!");
if (props == null)
props = this.project.getProperties();
props.setProperty(newPropertyName, String.valueOf(matches));
}
@@ -79,30 +84,28 @@ public abstract class AbstractMatchMojo extends AbstractRegexMojo {
if (this.allowMultiLineMatch)
return this.matches(strbuilder.toString());
return false;
return this.negate;
}
protected boolean matches(String text) {
if (text == null)
text = "";
for (Pattern pattern : this.compiledPatterns) {
this.getLog().debug("Applying regex pattern: " + pattern);
if (text == null) {
// TODO we want to capture this somehow
this.getLog().debug("Operating on value: " + text);
Matcher matcher = pattern.matcher(text);
if (this.allowPartialMatch) {
if (matcher.find())
return !this.negate;
} else {
this.getLog().debug("Operating on value: " + text);
Matcher matcher = pattern.matcher(text);
if (this.allowPartialMatch) {
if (matcher.find())
return true;
} else {
if (matcher.matches())
return true;
}
if (matcher.matches())
return !this.negate;
}
}
return false;
return this.negate;
}
@Override
@@ -82,12 +82,10 @@ public abstract class AbstractRegexMojo extends AbstractMojo {
this.getLog().debug("Finding property: " + propertyName);
Properties props = this.propResolver.resolveScope(propertyName);
if (props == null) {
this.getLog().info("Property not found: " + propertyName);
return;
}
if (props == null)
this.getLog().debug("Property not found: " + propertyName);
String propertyValue = props.getProperty(propertyName);
String propertyValue = props == null ? null : props.getProperty(propertyName);
this.executeOnText(props, propertyValue, newPropertyName);
}
@@ -14,10 +14,13 @@
*/
package com.inteligr8.maven.regex;
import java.io.File;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.nio.file.StandardOpenOption;
import java.util.LinkedList;
import java.util.List;
@@ -45,11 +48,15 @@ public abstract class AbstractReplaceMojo extends AbstractRegexMojo {
@Override
protected void executeOnText(Properties props, String text, String newPropertyName) {
if (text == null)
text = "";
String originalText = text;
text = this.replaceAll(text);
if (this.getLog().isDebugEnabled() && !text.equals(originalText))
this.getLog().debug("Manipulated value: " + text);
if (props == null)
props = this.project.getProperties();
props.setProperty(newPropertyName, text);
}
@@ -106,20 +113,17 @@ public abstract class AbstractReplaceMojo extends AbstractRegexMojo {
}
protected String replaceFirst(String text) {
if (text == null)
text = "";
if (this.getLog().isDebugEnabled())
this.getLog().debug("replace first: " + text.length());
for (Pair<Pattern, String> regex : this.compiledRegexes) {
this.getLog().debug("Applying regex pattern: " + regex.getLeft());
if (text == null) {
// TODO we want to capture this somehow
} else {
this.getLog().debug("Operating on value: " + text);
Matcher matcher = regex.getLeft().matcher(text);
text = matcher.replaceFirst(regex.getRight());
}
this.getLog().debug("Operating on value: " + text);
Matcher matcher = regex.getLeft().matcher(text);
text = matcher.replaceFirst(regex.getRight());
}
return text;
@@ -129,10 +133,16 @@ public abstract class AbstractReplaceMojo extends AbstractRegexMojo {
if (this.getLog().isDebugEnabled())
this.getLog().debug("replace all: " + file + " => " + tofile);
boolean overwrite = file.equals(tofile);
boolean didReplace = false;
Charset charset = Charset.forName(this.charsetName);
StringBuilder strbuilder = new StringBuilder();
if (overwrite) {
// if overwriting the existing file, use a temporary file
tofile = File.createTempFile("regexed-", ".tmp").toPath();
}
FileChannel targetChannel = FileChannel.open(tofile, StandardOpenOption.WRITE, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
try {
FileChannel sourceChannel = FileChannel.open(file, StandardOpenOption.READ);
@@ -176,24 +186,25 @@ public abstract class AbstractReplaceMojo extends AbstractRegexMojo {
targetChannel.close();
}
if (overwrite) {
Files.move(tofile, file, StandardCopyOption.REPLACE_EXISTING);
}
return didReplace;
}
protected String replaceAll(String text) {
if (text == null)
text = "";
if (this.getLog().isDebugEnabled())
this.getLog().debug("replace all: " + text.length());
for (Pair<Pattern, String> regex : this.compiledRegexes) {
this.getLog().debug("Applying regex pattern: " + regex.getLeft());
if (text == null) {
// TODO we want to capture this somehow
} else {
this.getLog().debug("Operating on value: " + text);
Matcher matcher = regex.getLeft().matcher(text);
text = matcher.replaceAll(regex.getRight());
}
this.getLog().debug("Operating on value: " + text);
Matcher matcher = regex.getLeft().matcher(text);
text = matcher.replaceAll(regex.getRight());
}
return text;
@@ -14,6 +14,7 @@
*/
package com.inteligr8.maven.regex;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
@@ -57,9 +58,11 @@ public class MatchFileContentMojo extends AbstractMatchMojo {
try {
for (FileSet fileSet : this.filesets) {
Path baseInputPath = this.resolveDirectory(basepath, fileSet.getDirectory(), "fileset input");
String[] filePathsAndNames = fsman.getIncludedFiles(fileSet);
for (String filePathAndName : filePathsAndNames) {
Path file = basepath.resolve(filePathAndName);
Path file = baseInputPath.resolve(filePathAndName);
if (!Files.isDirectory(file))
if (this.matches(file, this.chunkSize))
return true;
@@ -69,13 +72,40 @@ public class MatchFileContentMojo extends AbstractMatchMojo {
throw new MojoExecutionException("Execution failed due to an I/O related issue", ie);
}
return false;
return this.negate;
}
private Path resolveDirectory(Path basepath, String directory, String errorName) throws IOException, MojoExecutionException {
Path path = new File(directory).toPath();
if (!path.isAbsolute())
path = basepath.resolve(path);
if (!Files.exists(path))
throw new MojoExecutionException("A " + errorName + " directory does not exist: " + directory);
if (!Files.isDirectory(path))
throw new MojoExecutionException("A " + errorName + " does reference a directory: " + directory);
return path;
}
@Override
protected void validateParamsPreNormalization() throws MojoFailureException {
super.validateParamsPreNormalization();
if (this.filesets == null || this.filesets.isEmpty())
throw new MojoFailureException("At least one 'fileset' is required");
}
@Override
protected void normalizeParameters() throws MojoFailureException {
super.normalizeParameters();
for (FileSet fileset : this.filesets) {
if (fileset.getDirectory() == null)
fileset.setDirectory(this.project.getBasedir().getAbsolutePath());
}
this.newProperty = StringUtils.trimToNull(this.newProperty);
}
@@ -83,8 +113,6 @@ public class MatchFileContentMojo extends AbstractMatchMojo {
protected void validateParamsPostNormalization() throws MojoFailureException {
super.validateParamsPostNormalization();
if (this.filesets == null || this.filesets.isEmpty())
throw new MojoFailureException("At least one 'fileset' is required");
if (this.newProperty == null)
throw new MojoFailureException("The 'newProperty' element is required");
}
@@ -52,16 +52,12 @@ public class ReplaceFileContentMojo extends AbstractReplaceMojo {
try {
for (FileSet fileSet : this.filesets) {
String outputDir = fileSet.getOutputDirectory();
Path baseOutputPath = new File(outputDir).toPath();
if (!Files.exists(baseOutputPath))
Files.createDirectories(baseOutputPath);
if (!Files.isDirectory(baseOutputPath))
throw new MojoExecutionException("A fileset output directory does not reference a directory: " + outputDir);
Path baseInputPath = this.resolveDirectory(basepath, fileSet.getDirectory(), false, "fileset input");
Path baseOutputPath = this.resolveDirectory(basepath, fileSet.getOutputDirectory(), true, "fileset output");
String[] filePathsAndNames = fsman.getIncludedFiles(fileSet);
for (String filePathAndName : filePathsAndNames) {
Path file = basepath.resolve(filePathAndName);
Path file = baseInputPath.resolve(filePathAndName);
Path tofile = baseOutputPath.resolve(filePathAndName);
if (!Files.exists(tofile.getParent()))
Files.createDirectories(tofile.getParent());
@@ -77,20 +73,46 @@ public class ReplaceFileContentMojo extends AbstractReplaceMojo {
return didReplace;
}
private Path resolveDirectory(Path basepath, String directory, boolean createIfMissing, String errorName) throws IOException, MojoExecutionException {
if (directory == null)
return this.project.getBasedir().toPath();
Path path = new File(directory).toPath();
if (!path.isAbsolute())
path = basepath.resolve(path);
if (!Files.exists(path)) {
if (createIfMissing) {
Files.createDirectories(path);
} else {
throw new MojoExecutionException("A " + errorName + " directory does not exist: " + directory);
}
}
if (!Files.isDirectory(path))
throw new MojoExecutionException("A " + errorName + " does reference a directory: " + directory);
return path;
}
@Override
protected void validateParamsPreNormalization() throws MojoFailureException {
super.validateParamsPreNormalization();
if (this.filesets == null || this.filesets.isEmpty())
throw new MojoFailureException("At least one 'fileset' is required");
}
@Override
protected void normalizeParameters() throws MojoFailureException {
super.normalizeParameters();
}
@Override
protected void validateParamsPostNormalization() throws MojoFailureException {
super.validateParamsPostNormalization();
if (this.filesets == null || this.filesets.isEmpty())
throw new MojoFailureException("At least one 'fileset' is required");
for (FileSet fileset : this.filesets)
for (FileSet fileset : this.filesets) {
if (fileset.getDirectory() == null)
fileset.setDirectory(this.project.getBasedir().getAbsolutePath());
if (fileset.getOutputDirectory() == null)
throw new MojoFailureException("All 'fileset' must have an 'outputDirectory'");
fileset.setOutputDirectory(fileset.getDirectory());
}
}
}
@@ -0,0 +1,29 @@
package com.inteligr8.maven.regex;
import java.io.File;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import org.apache.maven.shared.model.fileset.FileSet;
import org.apache.maven.shared.model.fileset.util.FileSetManager;
import org.junit.Assert;
import org.junit.Test;
public class FileSetUnitTest {
@Test
public void srcMainJava() {
FileSet fileset = new FileSet();
fileset.setDirectory("src/main/java");
fileset.setIncludes(Arrays.asList("**/*.java"));
String fs = File.separator;
FileSetManager fsman = new FileSetManager();
Set<String> files = new HashSet<>(Arrays.asList(fsman.getIncludedFiles(fileset)));
Assert.assertTrue(files.size() > 15);
System.err.println(files);
Assert.assertTrue(files.contains("com"+fs+"inteligr8"+fs+"maven"+fs+"regex"+fs+"AbstractFileMojo.java"));
}
}