Skip to content

Commit

Permalink
Improve warning message when destination file is going to be replaced
Browse files Browse the repository at this point in the history
  • Loading branch information
abelsromero committed Jan 5, 2024
1 parent d543552 commit 8bcc78e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Improvements::
* Upgrade Asciidoctorj to v2.5.7 (#604)
* Upgrade Asciidoctorj to v2.5.10 and jRuby to v9.4.2.0 (#647)
* Upgrade Asciidoctorj to v2.5.11 (#688)
* Improve warning message when destination file is going to be replaced (#728)

Build / Infrastructure::

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.asciidoctor.maven;

import org.apache.commons.io.FilenameUtils;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
Expand Down Expand Up @@ -227,9 +228,16 @@ public void processSources(List<File> sourceFiles, ResourcesProcessor resourcesP

final Set<File> uniquePaths = new HashSet<>();
for (final File source : sourceFiles) {
final File destinationPath = setDestinationPaths(source, optionsBuilder, sourceDir, this);
if (!uniquePaths.add(destinationPath))
getLog().warn("Duplicated destination found: overwriting file: " + destinationPath.getAbsolutePath());
final Destination destination = setDestinationPaths(source, optionsBuilder, sourceDir, this);
final File destinationPath = destination.path;
if (!uniquePaths.add(destinationPath)) {
String destinationFile = destinationPath.getAbsolutePath();
if (!destination.isOutput) {
String baseName = FilenameUtils.getBaseName(destinationPath.getName());
destinationFile = destinationPath.getParentFile().getAbsolutePath() + File.separator + baseName + ".*";
}
getLog().warn("Duplicated destination found: overwriting file: " + destinationFile);
}

convertFile(asciidoctor, optionsBuilder.build(), source);

Expand Down Expand Up @@ -269,8 +277,8 @@ private boolean isRelativePath(String candidateName) {
* @return the final destination file path.
* @throws MojoExecutionException If output is not valid
*/
public File setDestinationPaths(final File sourceFile, final OptionsBuilder optionsBuilder, final File sourceDirectory,
final AsciidoctorMojo configuration) throws MojoExecutionException {
public Destination setDestinationPaths(final File sourceFile, final OptionsBuilder optionsBuilder, final File sourceDirectory,
final AsciidoctorMojo configuration) throws MojoExecutionException {
try {
if (configuration.getBaseDir() != null) {
optionsBuilder.baseDir(configuration.getBaseDir());
Expand All @@ -295,15 +303,28 @@ public File setDestinationPaths(final File sourceFile, final OptionsBuilder opti
if (outputFile != null) {
// allow overriding the output file name
optionsBuilder.toFile(outputFile);
return outputFile.isAbsolute() ? outputFile : new File(toDir, outputFile.getPath());
return outputFile.isAbsolute()
? new Destination(outputFile, true)
: new Destination(new File(toDir, outputFile.getPath()), true);
} else {
return new File(toDir, sourceFile.getName());
return new Destination(new File(toDir, sourceFile.getName()), false);
}
} catch (IOException e) {
throw new MojoExecutionException("Unable to locate output directory", e);
}
}

class Destination {
final File path;
// Whether path is the actual output file or an approximation
final boolean isOutput;

Destination(File destination, boolean isSource) {
this.path = destination;
this.isOutput = isSource;
}
}

protected Asciidoctor getAsciidoctorInstance(String gemPath) throws MojoExecutionException {
Asciidoctor asciidoctor;
if (gemPath == null) {
Expand Down

0 comments on commit 8bcc78e

Please sign in to comment.