001package org.apache.maven.scm.provider.local.command.checkout;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 * http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.io.File;
023import java.io.IOException;
024import java.util.ArrayList;
025import java.util.List;
026
027import org.apache.maven.scm.ScmException;
028import org.apache.maven.scm.ScmFile;
029import org.apache.maven.scm.ScmFileSet;
030import org.apache.maven.scm.ScmFileStatus;
031import org.apache.maven.scm.ScmVersion;
032import org.apache.maven.scm.command.checkout.AbstractCheckOutCommand;
033import org.apache.maven.scm.command.checkout.CheckOutScmResult;
034import org.apache.maven.scm.provider.ScmProviderRepository;
035import org.apache.maven.scm.provider.local.command.LocalCommand;
036import org.apache.maven.scm.provider.local.metadata.LocalScmMetadataUtils;
037import org.apache.maven.scm.provider.local.repository.LocalScmProviderRepository;
038import org.codehaus.plexus.util.FileUtils;
039
040/**
041 * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
042 *
043 */
044public class LocalCheckOutCommand
045    extends AbstractCheckOutCommand
046    implements LocalCommand
047{
048    /** {@inheritDoc} */
049    protected CheckOutScmResult executeCheckOutCommand( ScmProviderRepository repo, ScmFileSet fileSet,
050                                                       ScmVersion version, boolean recursive, boolean shallow )
051        throws ScmException
052    {
053        LocalScmProviderRepository repository = (LocalScmProviderRepository) repo;
054
055        if ( version != null )
056        {
057            throw new ScmException( "The local scm doesn't support tags." );
058        }
059
060        File root = new File( repository.getRoot() );
061
062        String module = repository.getModule();
063
064        File source = new File( root, module );
065
066        File baseDestination = fileSet.getBasedir();
067
068        if ( !root.exists() )
069        {
070            throw new ScmException( "The base directory doesn't exist (" + root.getAbsolutePath() + ")." );
071        }
072
073        if ( !source.exists() )
074        {
075            throw new ScmException( "The module directory doesn't exist (" + source.getAbsolutePath() + ")." );
076        }
077
078        List<ScmFile> checkedOutFiles;
079
080        try
081        {
082            if ( baseDestination.exists() )
083            {
084                FileUtils.deleteDirectory( baseDestination );
085            }
086
087            if ( !baseDestination.mkdirs() )
088            {
089                throw new ScmException(
090                    "Could not create destination directory '" + baseDestination.getAbsolutePath() + "'." );
091            }
092
093            if ( getLogger().isInfoEnabled() )
094            {
095                getLogger().info(
096                                  "Checking out '" + source.getAbsolutePath() + "' to '"
097                                      + baseDestination.getAbsolutePath() + "'." );
098            }
099
100            List<File> fileList;
101
102            if ( fileSet.getFileList().isEmpty() )
103            {
104                @SuppressWarnings( "unchecked" )
105                List<File> files = FileUtils.getFiles( source.getAbsoluteFile(), "**", null ); 
106                fileList = files;
107            }
108            else
109            {
110                fileList = fileSet.getFileList();
111            }
112
113            checkedOutFiles = checkOut( source, baseDestination, fileList, repository.getModule() );
114
115            // write metadata file
116            LocalScmMetadataUtils metadataUtils = new LocalScmMetadataUtils( getLogger() );
117            metadataUtils.writeMetadata( baseDestination, metadataUtils.buildMetadata( source ) );
118        }
119        catch ( IOException ex )
120        {
121            throw new ScmException( "Error while checking out the files.", ex );
122        }
123
124        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        String sourcePath = source.getAbsolutePath();
131
132        List<ScmFile> checkedOutFiles = new ArrayList<ScmFile>();
133
134        for ( File file : files )
135        {
136            String dest = file.getAbsolutePath();
137
138            dest = dest.substring( sourcePath.length() + 1 );
139
140            File destination = new File( baseDestination, dest );
141
142            destination = destination.getParentFile();
143
144            if ( !destination.exists() && !destination.mkdirs() )
145            {
146                throw new ScmException(
147                    "Could not create destination directory '" + destination.getAbsolutePath() + "'." );
148            }
149
150            FileUtils.copyFileToDirectory( file, destination );
151
152            File parent = file.getParentFile();
153
154            // TODO: Add more excludes here
155            if ( parent != null && parent.getName().equals( "CVS" ) )
156            {
157                continue;
158            }
159
160            String fileName = "/" + module + "/" + dest;
161
162            checkedOutFiles.add( new ScmFile( fileName, ScmFileStatus.CHECKED_OUT ) );
163        }
164
165        return checkedOutFiles;
166    }
167
168
169}