Skip to content

Commit

Permalink
Switch to Paths, which are more reliable and powerful compared to Files
Browse files Browse the repository at this point in the history
  • Loading branch information
rfscholte committed Mar 4, 2018
1 parent c3608ee commit 52bfff2
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 169 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
Expand Down Expand Up @@ -169,7 +170,7 @@ private void transform( ReleaseDescriptor releaseDescriptor, ReleaseEnvironment
List<MavenProject> reactorProjects, boolean simulate, ReleaseResult result )
throws ReleaseExecutionException, ReleaseFailureException
{
String commonBasedir;
Path commonBasedir;
try
{
commonBasedir = ReleaseUtil.getCommonBasedir( reactorProjects );
Expand All @@ -189,7 +190,7 @@ private void transform( ReleaseDescriptor releaseDescriptor, ReleaseEnvironment
}

private void transformProject( MavenProject project, ReleaseDescriptor releaseDescriptor,
ReleaseEnvironment releaseEnvironment, String commonBasedir,
ReleaseEnvironment releaseEnvironment, Path commonBasedir,
boolean simulate, ReleaseResult result )
throws ReleaseExecutionException, ReleaseFailureException
{
Expand Down Expand Up @@ -244,7 +245,7 @@ private void transformProject( MavenProject project, ReleaseDescriptor releaseDe
}

private void transformDocument( MavenProject project, Model modelTarget, ReleaseDescriptor releaseDescriptor,
String commonBasedir, ScmRepository scmRepository,
Path commonBasedir, ScmRepository scmRepository,
ReleaseResult result, boolean simulate )
throws ReleaseExecutionException, ReleaseFailureException
{
Expand Down Expand Up @@ -568,7 +569,7 @@ protected abstract String getOriginalVersion( ReleaseDescriptor releaseDescripto

protected abstract void transformScm( MavenProject project, Model modelTarget, ReleaseDescriptor releaseDescriptor,
String projectId, ScmRepository scmRepository,
ReleaseResult result, String commonBasedir )
ReleaseResult result, Path commonBasedir )
throws ReleaseExecutionException;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ private ReleaseResult performCheckout( ReleaseDescriptor releaseDescriptor, Rele
String basedir;
try
{
basedir = ReleaseUtil.getCommonBasedir( reactorProjects );
basedir = ReleaseUtil.getCommonBasedir( reactorProjects ).toString();
}
catch ( IOException e )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
* under the License.
*/

import java.nio.file.Path;

import org.apache.maven.model.Model;
import org.apache.maven.project.MavenProject;
import org.apache.maven.scm.repository.ScmRepository;
Expand All @@ -45,7 +47,7 @@ protected final String getPomSuffix()
@Override
protected void transformScm( MavenProject project, Model modelTarget, ReleaseDescriptor releaseDescriptor,
String projectId, ScmRepository scmRepository, ReleaseResult result,
String commonBasedir )
Path commonBasedir )
throws ReleaseExecutionException
{
// We are only updating versions no mods to scm needed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
*/

import java.io.IOException;
import java.nio.file.Path;

import org.apache.maven.artifact.ArtifactUtils;
import org.apache.maven.model.Model;
Expand Down Expand Up @@ -52,7 +53,7 @@ protected final String getPomSuffix()
@Override
protected void transformScm( MavenProject project, Model modelTarget, ReleaseDescriptor releaseDescriptor,
String projectId, ScmRepository scmRepository, ReleaseResult result,
String commonBasedir )
Path commonBasedir )
throws ReleaseExecutionException
{
// If SCM is null in original model, it is inherited, no mods needed
Expand Down Expand Up @@ -105,7 +106,7 @@ protected void transformScm( MavenProject project, Model modelTarget, ReleaseDes
}

private boolean translateScm( MavenProject project, ReleaseDescriptor releaseDescriptor, Scm scmTarget,
ScmRepository scmRepository, ReleaseResult relResult, String commonBasedir )
ScmRepository scmRepository, ReleaseResult relResult, Path commonBasedir )
throws IOException
{
ScmTranslator translator = getScmTranslators().get( scmRepository.getProvider() );
Expand All @@ -127,9 +128,7 @@ private boolean translateScm( MavenProject project, ReleaseDescriptor releaseDes
branchBase = "scm:svn:" + branchBase;
}

String workingDirectory =
ReleaseUtil.isSymlink( project.getBasedir() ) ? project.getBasedir().getCanonicalPath()
: project.getBasedir().getAbsolutePath();
Path workingDirectory = project.getBasedir().toPath().toRealPath();

int count =
ReleaseUtil.getBaseWorkingDirectoryParentCount( commonBasedir, workingDirectory );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
* under the License.
*/

import java.nio.file.Path;

import org.apache.maven.model.Model;
import org.apache.maven.model.Scm;
import org.apache.maven.project.MavenProject;
Expand Down Expand Up @@ -47,7 +49,7 @@ protected final String getPomSuffix()
@Override
protected void transformScm( MavenProject project, Model modelTarget, ReleaseDescriptor releaseDescriptor,
String projectId, ScmRepository scmRepository, ReleaseResult result,
String commonBasedir )
Path commonBasedir )
throws ReleaseExecutionException
{
// If SCM is null in original model, it is inherited, no mods needed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
*/

import java.io.IOException;
import java.nio.file.Path;

import org.apache.maven.artifact.ArtifactUtils;
import org.apache.maven.model.Model;
Expand Down Expand Up @@ -51,7 +52,7 @@ protected final String getPomSuffix()
@Override
protected void transformScm( MavenProject project, Model modelTarget, ReleaseDescriptor releaseDescriptor,
String projectId, ScmRepository scmRepository, ReleaseResult result,
String commonBasedir )
Path commonBasedir )
throws ReleaseExecutionException
{
// If SCM is null in original model, it is inherited, no mods needed
Expand Down Expand Up @@ -103,7 +104,7 @@ protected void transformScm( MavenProject project, Model modelTarget, ReleaseDes
}

private boolean translateScm( MavenProject project, ReleaseDescriptor releaseDescriptor, Scm scmTarget,
ScmRepository scmRepository, ReleaseResult relResult, String commonBasedir )
ScmRepository scmRepository, ReleaseResult relResult, Path commonBasedir )
throws IOException
{
ScmTranslator translator = getScmTranslators().get( scmRepository.getProvider() );
Expand All @@ -125,9 +126,8 @@ private boolean translateScm( MavenProject project, ReleaseDescriptor releaseDes
tagBase = "scm:svn:" + tagBase;
}

String workingDirectory =
ReleaseUtil.isSymlink( project.getBasedir() ) ? project.getBasedir().getCanonicalPath()
: project.getBasedir().getAbsolutePath();
Path workingDirectory = project.getBasedir().toPath().toRealPath();

int count =
ReleaseUtil.getBaseWorkingDirectoryParentCount( commonBasedir, workingDirectory );
if ( scm.getConnection() != null )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;

import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.maven.model.Model;
import org.apache.maven.project.MavenProject;
Expand All @@ -42,7 +40,6 @@
import org.codehaus.plexus.interpolation.PrefixAwareRecursionInterceptor;
import org.codehaus.plexus.interpolation.PrefixedObjectValueSource;
import org.codehaus.plexus.interpolation.StringSearchInterpolator;
import org.codehaus.plexus.util.FileUtils;
import org.codehaus.plexus.util.IOUtil;
import org.codehaus.plexus.util.ReaderFactory;

Expand All @@ -58,8 +55,6 @@ public class ReleaseUtil
@SuppressWarnings( "checkstyle:constantname" )
public static final String POMv4 = "pom.xml";

private static final String FS = File.separator;

/**
* The line separator to use.
*/
Expand Down Expand Up @@ -172,7 +167,7 @@ public static ReleaseDescriptor createBasedirAlignedReleaseDescriptor( ReleaseDe
List<MavenProject> reactorProjects )
throws ReleaseExecutionException
{
String basedir;
Path basedir;
try
{
basedir = getCommonBasedir( reactorProjects );
Expand All @@ -185,82 +180,37 @@ public static ReleaseDescriptor createBasedirAlignedReleaseDescriptor( ReleaseDe

int parentLevels =
getBaseWorkingDirectoryParentCount( basedir,
FileUtils.normalize( releaseDescriptor.getWorkingDirectory() ) );
Paths.get( releaseDescriptor.getWorkingDirectory() ) );

String url = releaseDescriptor.getScmSourceUrl();
url = realignScmUrl( parentLevels, url );

ReleaseDescriptorBuilder builder = new ReleaseDescriptorBuilder();
builder.setWorkingDirectory( basedir );
builder.setWorkingDirectory( basedir.toFile().getAbsolutePath() );
builder.setScmSourceUrl( url );
return ReleaseUtils.buildReleaseDescriptor( builder );
}

public static String getCommonBasedir( List<MavenProject> reactorProjects )
throws IOException
{
return getCommonBasedir( reactorProjects, FS );
}

public static String getCommonBasedir( List<MavenProject> reactorProjects, String separator )
public static Path getCommonBasedir( List<MavenProject> reactorProjects )
throws IOException
{
String[] baseDirs = new String[reactorProjects.size()];
int idx = 0;
for ( MavenProject p : reactorProjects )
Path basePath = reactorProjects.get( 0 ).getBasedir().toPath();

for ( MavenProject reactorProject : reactorProjects )
{
String dir = p.getBasedir().getCanonicalPath();

// always end with separator so that we know what is a path and what is a partial directory name in the
// next call
if ( !dir.endsWith( separator ) )
Path matchPath = reactorProject.getBasedir().toPath();
while ( !basePath.startsWith( matchPath ) )
{
dir = dir + separator;
matchPath = matchPath.getParent();
}
baseDirs[idx++] = dir;
basePath = matchPath;
}

String basedir = StringUtils.getCommonPrefix( baseDirs );

int separatorPos = basedir.lastIndexOf( separator );
if ( !basedir.endsWith( separator ) && separatorPos >= 0 )
{
basedir = basedir.substring( 0, separatorPos );
}

if ( basedir.endsWith( separator ) && basedir.length() > 1 )
{
basedir = basedir.substring( 0, basedir.length() - 1 );
}

return basedir;
return basePath;
}

public static int getBaseWorkingDirectoryParentCount( String basedir, String workingDirectory )
public static int getBaseWorkingDirectoryParentCount( final Path baseDirectory, final Path workingDirectory )
{
int num = 0;

// we can safely assume case-insensitivity as we are just backtracking, not comparing. This helps with issues
// on Windows with C: vs c:
workingDirectory = FilenameUtils.normalize( workingDirectory.toLowerCase( Locale.ENGLISH ) );
basedir = FilenameUtils.normalize( basedir.toLowerCase( Locale.ENGLISH ) );

// MRELEASE-663
// For Windows is does matter if basedir ends with a file-separator or not to be able to compare.
// Using the parent of a dummy file makes it possible to compare them OS-independent
Path workingDirectoryFile = Paths.get( workingDirectory, ".tmp" ).getParent();
Path basedirFile = Paths.get( basedir, ".tmp" ).getParent();

if ( !workingDirectoryFile.equals( basedirFile ) && workingDirectory.startsWith( basedir ) )
{
do
{
workingDirectoryFile = workingDirectoryFile.getParent();
num++;
}
while ( !workingDirectoryFile.equals( basedirFile ) );
}
return num;
return Math.max( 0, workingDirectory.normalize().getNameCount() - baseDirectory.normalize().getNameCount() );
}

public static String realignScmUrl( int parentLevels, String url )
Expand Down
Loading

0 comments on commit 52bfff2

Please sign in to comment.