Skip to content

Commit

Permalink
Add a failOnFailedPatch flag and fix writing partial patches.
Browse files Browse the repository at this point in the history
  • Loading branch information
LukasMansour committed Mar 3, 2024
1 parent ea16230 commit 689b573
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .github/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ This plugin is only available for Maven and can be installed with the following
<plugin>
<groupId>io.github.lukasmansour</groupId>
<artifactId>patch-maven-plugin</artifactId>
<version>1.0-SNAPSHOT</version>
<version>1.1.0-SNAPSHOT</version>
<configuration>
<targetDirectory>${project.basedir}</targetDirectory>
<patchDirectory>${project.basedir}\src\main\patches</patchDirectory>
Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ plugins {
}

group = "io.github.lukasmansour"
version = "1.0"
version = "1.1.0-SNAPSHOT"
description = "A Maven patch plugin with no dependency on GNU Patch."

java {
Expand Down
23 changes: 20 additions & 3 deletions src/main/java/io/github/lukasmansour/patch/PatchMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
Expand All @@ -45,6 +48,9 @@ public class PatchMojo extends AbstractMojo {
@Parameter(property = "patch.patchDirectory", defaultValue = "src/main/patches")
private File patchDirectory;

@Parameter(property = "patch.failOnFailedPatch", defaultValue = "false")
private boolean failOnFailedPatch;

public void execute() throws MojoExecutionException {
getLog().info(targetDirectory.toString());
getLog().info("Applying patches...");
Expand All @@ -66,6 +72,7 @@ public void execute() throws MojoExecutionException {
UnifiedDiff diff = UnifiedDiffReader.parseUnifiedDiff(
new FileInputStream(patchFile));

Map<Path, List<String>> writeResults = new HashMap<>();
for (UnifiedDiffFile file : diff.getFiles()) {
Path targetFile = targetDirectory.toPath().resolve(file.getToFile());
String targetFileName = targetFile.getFileName().toString();
Expand All @@ -75,17 +82,27 @@ public void execute() throws MojoExecutionException {
Files.readAllLines(targetFile)
);

Files.write(targetFile, results);
writeResults.put(targetFile, results);

getLog().info(
String.format("Applied diff to '%s' successfully.", targetFileName));
} catch (PatchFailedException pfe1) {
getLog().warn(String.format(
String failureMessage = String.format(
"Failed to apply patch file '%s' to file '%s'. (It may already have been applied!)",
patchFileName, targetFileName));
patchFileName, targetFileName);

if (failOnFailedPatch) {
throw new MojoExecutionException(failureMessage);
} else {
getLog().warn(failureMessage);
}
break;
}
getLog().info(String.format("Finished applying diff to '%s'.", targetFileName));
}
for (Entry<Path, List<String>> entry : writeResults.entrySet()) {
Files.write(entry.getKey(), entry.getValue());
}
getLog().info(String.format("Finished applying patch '%s'.", patchFileName));
}

Expand Down
12 changes: 11 additions & 1 deletion test-project/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,21 @@
<plugin>
<groupId>io.github.lukasmansour</groupId>
<artifactId>patch-maven-plugin</artifactId>
<version>1.0-SNAPSHOT</version>
<version>1.1.0</version>
<configuration>
<!-- <targetDirectory>${project.basedir}</targetDirectory>-->
<patchDirectory>${project.basedir}\src\main\patches</patchDirectory>
<failOnFailedPatch>true</failOnFailedPatch>
</configuration>

<!-- And add the execution to the building phase-->
<executions>
<execution>
<goals>
<goal>apply</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Expand Down

0 comments on commit 689b573

Please sign in to comment.