Coverage Report - org.apache.maven.scm.provider.local.command.checkout.LocalCheckOutCommand
 
Classes in this File Line Coverage Branch Coverage Complexity
LocalCheckOutCommand
0 %
0/47
0 %
0/24
10,5
 
 1  
 package org.apache.maven.scm.provider.local.command.checkout;
 2  
 
 3  
 /*
 4  
  * Licensed to the Apache Software Foundation (ASF) under one
 5  
  * or more contributor license agreements.  See the NOTICE file
 6  
  * distributed with this work for additional information
 7  
  * regarding copyright ownership.  The ASF licenses this file
 8  
  * to you under the Apache License, Version 2.0 (the
 9  
  * "License"); you may not use this file except in compliance
 10  
  * with the License.  You may obtain a copy of the License at
 11  
  *
 12  
  * http://www.apache.org/licenses/LICENSE-2.0
 13  
  *
 14  
  * Unless required by applicable law or agreed to in writing,
 15  
  * software distributed under the License is distributed on an
 16  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 17  
  * KIND, either express or implied.  See the License for the
 18  
  * specific language governing permissions and limitations
 19  
  * under the License.
 20  
  */
 21  
 
 22  
 import java.io.File;
 23  
 import java.io.IOException;
 24  
 import java.util.ArrayList;
 25  
 import java.util.List;
 26  
 
 27  
 import org.apache.maven.scm.ScmException;
 28  
 import org.apache.maven.scm.ScmFile;
 29  
 import org.apache.maven.scm.ScmFileSet;
 30  
 import org.apache.maven.scm.ScmFileStatus;
 31  
 import org.apache.maven.scm.ScmVersion;
 32  
 import org.apache.maven.scm.command.checkout.AbstractCheckOutCommand;
 33  
 import org.apache.maven.scm.command.checkout.CheckOutScmResult;
 34  
 import org.apache.maven.scm.provider.ScmProviderRepository;
 35  
 import org.apache.maven.scm.provider.local.command.LocalCommand;
 36  
 import org.apache.maven.scm.provider.local.metadata.LocalScmMetadataUtils;
 37  
 import org.apache.maven.scm.provider.local.repository.LocalScmProviderRepository;
 38  
 import org.codehaus.plexus.util.FileUtils;
 39  
 
 40  
 /**
 41  
  * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
 42  
  * @version $Id: LocalCheckOutCommand.java 1054738 2011-01-03 20:40:39Z olamy $
 43  
  */
 44  0
 public class LocalCheckOutCommand
 45  
     extends AbstractCheckOutCommand
 46  
     implements LocalCommand
 47  
 {
 48  
     /** {@inheritDoc} */
 49  
     protected CheckOutScmResult executeCheckOutCommand( ScmProviderRepository repo, ScmFileSet fileSet,
 50  
                                                         ScmVersion version, boolean recursive )
 51  
         throws ScmException
 52  
     {
 53  0
         LocalScmProviderRepository repository = (LocalScmProviderRepository) repo;
 54  
 
 55  0
         if ( version != null )
 56  
         {
 57  0
             throw new ScmException( "The local scm doesn't support tags." );
 58  
         }
 59  
 
 60  0
         File root = new File( repository.getRoot() );
 61  
 
 62  0
         String module = repository.getModule();
 63  
 
 64  0
         File source = new File( root, module );
 65  
 
 66  0
         File baseDestination = fileSet.getBasedir();
 67  
 
 68  0
         if ( !root.exists() )
 69  
         {
 70  0
             throw new ScmException( "The base directory doesn't exist (" + root.getAbsolutePath() + ")." );
 71  
         }
 72  
 
 73  0
         if ( !source.exists() )
 74  
         {
 75  0
             throw new ScmException( "The module directory doesn't exist (" + source.getAbsolutePath() + ")." );
 76  
         }
 77  
 
 78  
         List<ScmFile> checkedOutFiles;
 79  
 
 80  
         try
 81  
         {
 82  0
             if ( baseDestination.exists() )
 83  
             {
 84  0
                 FileUtils.deleteDirectory( baseDestination );
 85  
             }
 86  
 
 87  0
             if ( !baseDestination.mkdirs() )
 88  
             {
 89  0
                 throw new ScmException(
 90  
                     "Could not create destination directory '" + baseDestination.getAbsolutePath() + "'." );
 91  
             }
 92  
 
 93  0
             if ( getLogger().isInfoEnabled() )
 94  
             {
 95  0
                 getLogger().info(
 96  
                                   "Checking out '" + source.getAbsolutePath() + "' to '"
 97  
                                       + baseDestination.getAbsolutePath() + "'." );
 98  
             }
 99  
 
 100  
             List<File> fileList;
 101  
 
 102  0
             if ( fileSet.getFileList().isEmpty() )
 103  
             {
 104  
                 @SuppressWarnings( "unchecked" )
 105  0
                 List<File> files = FileUtils.getFiles( source.getAbsoluteFile(), "**", null ); 
 106  0
                 fileList = files;
 107  0
             }
 108  
             else
 109  
             {
 110  0
                 fileList = fileSet.getFileList();
 111  
             }
 112  
 
 113  0
             checkedOutFiles = checkOut( source, baseDestination, fileList, repository.getModule() );
 114  
 
 115  
             // write metadata file
 116  0
             LocalScmMetadataUtils metadataUtils = new LocalScmMetadataUtils( getLogger() );
 117  0
             metadataUtils.writeMetadata( baseDestination, metadataUtils.buildMetadata( source ) );
 118  
         }
 119  0
         catch ( IOException ex )
 120  
         {
 121  0
             throw new ScmException( "Error while checking out the files.", ex );
 122  0
         }
 123  
 
 124  0
         return new LocalCheckOutScmResult( null, checkedOutFiles );
 125  
     }
 126  
 
 127  
     private List<ScmFile> checkOut( File source, File baseDestination, List<File> files, String module )
 128  
         throws ScmException, IOException
 129  
     {
 130  0
         String sourcePath = source.getAbsolutePath();
 131  
 
 132  0
         List<ScmFile> checkedOutFiles = new ArrayList<ScmFile>();
 133  
 
 134  0
         for ( File file : files )
 135  
         {
 136  0
             String dest = file.getAbsolutePath();
 137  
 
 138  0
             dest = dest.substring( sourcePath.length() + 1 );
 139  
 
 140  0
             File destination = new File( baseDestination, dest );
 141  
 
 142  0
             destination = destination.getParentFile();
 143  
 
 144  0
             if ( !destination.exists() && !destination.mkdirs() )
 145  
             {
 146  0
                 throw new ScmException(
 147  
                     "Could not create destination directory '" + destination.getAbsolutePath() + "'." );
 148  
             }
 149  
 
 150  0
             FileUtils.copyFileToDirectory( file, destination );
 151  
 
 152  0
             File parent = file.getParentFile();
 153  
 
 154  
             // TODO: Add more excludes here
 155  0
             if ( parent != null && parent.getName().equals( "CVS" ) )
 156  
             {
 157  0
                 continue;
 158  
             }
 159  
 
 160  0
             String fileName = "/" + module + "/" + dest;
 161  
 
 162  0
             checkedOutFiles.add( new ScmFile( fileName, ScmFileStatus.CHECKED_OUT ) );
 163  0
         }
 164  
 
 165  0
         return checkedOutFiles;
 166  
     }
 167  
 
 168  
 
 169  
 }