Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for paths in summary files and per-directory summary files #32

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Portions copyright 2015 ForgeRock AS
*/
package net.nicoulaj.maven.plugins.checksum.execution.target;

Expand Down Expand Up @@ -68,6 +70,11 @@ public class CsvSummaryFileTarget
*/
protected File summaryFile;

/**
* The root path to be removed from summary files.
*/
protected String summaryRoot;

/**
* Build a new instance of {@link CsvSummaryFileTarget}.
*
Expand All @@ -80,6 +87,20 @@ public CsvSummaryFileTarget( File summaryFile, String encoding )
this.encoding = encoding;
}

/**
* Build a new instance of {@link CsvSummaryFileTarget}.
*
* @param summaryFile the file to which the summary should be written.
* @param encoding the encoding to use for generated files.
* @param summaryRoot the root path to remove from filepaths prior to writing to the summary file.
*/
public CsvSummaryFileTarget( File summaryFile, String encoding, String summaryRoot )
{
this.summaryFile = summaryFile;
this.encoding = encoding;
this.summaryRoot = summaryRoot;
}

/**
* {@inheritDoc}
*/
Expand Down Expand Up @@ -126,7 +147,7 @@ public void close()
// Write a line for each file.
for ( File file : filesHashcodes.keySet() )
{
sb.append( LINE_SEPARATOR ).append( file.getName() );
sb.append( LINE_SEPARATOR ).append( stripSummaryRoot(file) );
Map<String, String> fileHashcodes = filesHashcodes.get( file );
for ( String algorithm : algorithms )
{
Expand All @@ -151,4 +172,11 @@ public void close()
throw new ExecutionTargetCloseException( e.getMessage() );
}
}

private String stripSummaryRoot(File file) {
if (summaryRoot != null && file.getPath().startsWith(summaryRoot)) {
return file.getPath().substring(summaryRoot.length());
}
return file.getPath();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
/*
* Copyright 2010-2012 Julien Nicoulaud <[email protected]>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Portions copyright 2015 ForgeRock AS
*/
package net.nicoulaj.maven.plugins.checksum.execution.target;

import net.nicoulaj.maven.plugins.checksum.digest.DigesterFactory;
import org.codehaus.plexus.util.FileUtils;

import java.io.File;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;
import java.util.SortedSet;
import java.util.TreeSet;

/**
* An {@link net.nicoulaj.maven.plugins.checksum.execution.target.ExecutionTarget} that writes digests to separate
* files per directory.
*
* @since 1.3
*/
public class DirectorySummaryFileTarget
implements ExecutionTarget
{
/**
* The line separator character.
*/
public static final String LINE_SEPARATOR = System.getProperty( "line.separator" );

/**
* The CSV column separator character.
*/
public static final String CSV_COLUMN_SEPARATOR = ",";

/**
* The CSV comment marker character.
*/
public static final String CSV_COMMENT_MARKER = "#";

/**
* Encoding to use for generated files.
*/
protected String encoding;

/**
* Summary filename to create in each directory.
*/
protected String summaryFilename;

/**
* Summary path => hashed file => (algorithm,hashcode).
*/
protected Map<File, Map<File, Map<String, String>>> filesHashcodes;

/**
* The set of algorithms encountered.
*/
protected SortedSet<String> algorithms;

/**
* Build a new instance of {@link net.nicoulaj.maven.plugins.checksum.execution.target.DirectorySummaryFileTarget}.
*
* @param encoding the encoding to use for generated files.
*/
public DirectorySummaryFileTarget(String encoding, String summaryFilename)
{
this.encoding = encoding;
this.summaryFilename = summaryFilename;
}

/**
* {@inheritDoc}
*/
public void init()
throws ExecutionTargetInitializationException
{
filesHashcodes = new HashMap<File, Map<File, Map<String, String>>>();
algorithms = new TreeSet<String>();
}

/**
* {@inheritDoc}
*/
public void write( String digest, File file, String algorithm )
throws ExecutionTargetWriteException
{
File path = new File(file.getPath().substring(0, file.getPath().length() - file.getName().length()));

// Initialize an entry for the file if needed.
if ( !filesHashcodes.containsKey( path ) )
{
filesHashcodes.put( path, new HashMap<File, Map<String, String>>() );
}

// Store the algorithm => hashcode mapping for this file.
Map<File, Map<String, String>> fileHashcodes = filesHashcodes.get( path );
if ( !fileHashcodes.containsKey( file ))
{
fileHashcodes.put( file, new HashMap<String, String>() );
}
Map<String, String> hashcodes = fileHashcodes.get( file );
hashcodes.put( algorithm, digest );

// Store the algorithm.
algorithms.add( algorithm );
}

/**
* {@inheritDoc}
*/
public void close()
throws ExecutionTargetCloseException
{
// Write a file for each directory.
for (File summaryPath : filesHashcodes.keySet()) {
StringBuilder sb = new StringBuilder();

// Write the CSV file header.
sb.append( CSV_COMMENT_MARKER ).append( "File" );
for ( String algorithm : algorithms )
{
sb.append( CSV_COLUMN_SEPARATOR ).append( algorithm );
}

// Write a line for each file.
for ( File file : filesHashcodes.get(summaryPath).keySet() )
{
sb.append( LINE_SEPARATOR ).append( file.getName() );
Map<String, String> fileHashcodes = filesHashcodes.get(summaryPath).get( file );
for ( String algorithm : algorithms )
{
sb.append( CSV_COLUMN_SEPARATOR );
if ( fileHashcodes.containsKey( algorithm ) )
{
sb.append( fileHashcodes.get( algorithm ) );
}
}
}

// Write the result to the summary file.
try
{
FileUtils.fileWrite( summaryPath + "/" + summaryFilename, encoding, sb.toString() );
}
catch ( IOException e )
{
throw new ExecutionTargetCloseException( e.getMessage() );
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Portions copyright 2015 ForgeRock AS
*/
package net.nicoulaj.maven.plugins.checksum.execution.target;

Expand Down Expand Up @@ -58,6 +60,11 @@ public class XmlSummaryFileTarget
*/
protected File summaryFile;

/**
* The root path to be removed from summary files.
*/
protected String summaryRoot;

/**
* Build a new instance of {@link net.nicoulaj.maven.plugins.checksum.execution.target.XmlSummaryFileTarget}.
*
Expand All @@ -70,6 +77,20 @@ public XmlSummaryFileTarget( File summaryFile, String encoding )
this.encoding = encoding;
}

/**
* Build a new instance of {@link net.nicoulaj.maven.plugins.checksum.execution.target.XmlSummaryFileTarget}.
*
* @param summaryFile the file to which the summary should be written.
* @param encoding the encoding to use for generated files.
* @param summaryRoot the root path to remove from filepaths prior to writing to the summary file.
*/
public XmlSummaryFileTarget( File summaryFile, String encoding, String summaryRoot )
{
this.summaryFile = summaryFile;
this.encoding = encoding;
this.summaryRoot = summaryRoot;
}

/**
* {@inheritDoc}
*/
Expand Down Expand Up @@ -125,7 +146,7 @@ public void close()
for ( File file : filesHashcodes.keySet() )
{
xmlWriter.startElement( "file" );
xmlWriter.addAttribute( "name", file.getName() );
xmlWriter.addAttribute( "name", stripSummaryRoot(file) );
Map<String, String> fileHashcodes = filesHashcodes.get( file );
for ( String algorithm : fileHashcodes.keySet() )
{
Expand All @@ -148,4 +169,11 @@ public void close()
throw new ExecutionTargetCloseException( e.getMessage() );
}
}

private String stripSummaryRoot(File file) {
if (summaryRoot != null && file.getPath().startsWith(summaryRoot)) {
return file.getPath().substring(summaryRoot.length());
}
return file.getPath();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Portions copyright 2015 ForgeRock AS
*/
package net.nicoulaj.maven.plugins.checksum.mojo;

Expand All @@ -21,6 +23,7 @@
import net.nicoulaj.maven.plugins.checksum.execution.FailOnErrorExecution;
import net.nicoulaj.maven.plugins.checksum.execution.NeverFailExecution;
import net.nicoulaj.maven.plugins.checksum.execution.target.CsvSummaryFileTarget;
import net.nicoulaj.maven.plugins.checksum.execution.target.DirectorySummaryFileTarget;
import net.nicoulaj.maven.plugins.checksum.execution.target.MavenLogTarget;
import net.nicoulaj.maven.plugins.checksum.execution.target.OneHashPerFileTarget;
import net.nicoulaj.maven.plugins.checksum.execution.target.XmlSummaryFileTarget;
Expand Down Expand Up @@ -120,17 +123,21 @@ public void execute()
}
execution.addTarget( new OneHashPerFileTarget( encoding, outputDirectory ) );
}
if ( isDirectoryFiles() )
{
execution.addTarget(new DirectorySummaryFileTarget(encoding, getCsvSummaryFile()));
}
if ( isCsvSummary() )
{
execution.addTarget( new CsvSummaryFileTarget(
FileUtils.resolveFile( new File( project.getBuild().getDirectory() ), getCsvSummaryFile() ),
encoding ) );
encoding, getSummaryRoot() ) );
}
if ( isXmlSummary() )
{
execution.addTarget( new XmlSummaryFileTarget(
FileUtils.resolveFile( new File( project.getBuild().getDirectory() ), getXmlSummaryFile() ),
encoding ) );
encoding, getSummaryRoot() ) );
}

// Run the execution.
Expand All @@ -156,6 +163,10 @@ public void execute()

protected abstract String getIndividualFilesOutputDirectory();

protected abstract boolean isDirectoryFiles();

protected abstract String getSummaryRoot();

protected abstract boolean isCsvSummary();

protected abstract String getCsvSummaryFile();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Portions copyright 2015 ForgeRock AS
*/
package net.nicoulaj.maven.plugins.checksum.mojo;

Expand Down Expand Up @@ -61,6 +63,22 @@ public class ArtifactsMojo
@Parameter
protected String individualFilesOutputDirectory;

/**
* Indicates whether the build will store checksums to per-directory summary files.
*
* @since 1.3
*/
@Parameter( defaultValue = "false" )
protected boolean directoryFiles;

/**
* The root of the path to be stored in the summary file(s).
*
* @since 1.3
*/
@Parameter
protected String summaryRoot;

/**
* Indicates whether the build will store checksums to a single CSV summary file.
*
Expand Down Expand Up @@ -166,6 +184,20 @@ protected String getIndividualFilesOutputDirectory()
return individualFilesOutputDirectory;
}

/**
* {@inheritDoc}
*/
protected boolean isDirectoryFiles() {
return directoryFiles;
}

/**
* {@inheritDoc}
*/
protected String getSummaryRoot() {
return summaryRoot;
}

/**
* {@inheritDoc}
*/
Expand Down
Loading