001    package org.apache.maven.scm.provider.local.command.list;
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    
022    import org.apache.maven.scm.ScmException;
023    import org.apache.maven.scm.ScmFile;
024    import org.apache.maven.scm.ScmFileSet;
025    import org.apache.maven.scm.ScmFileStatus;
026    import org.apache.maven.scm.ScmVersion;
027    import org.apache.maven.scm.command.list.AbstractListCommand;
028    import org.apache.maven.scm.command.list.ListScmResult;
029    import org.apache.maven.scm.provider.ScmProviderRepository;
030    import org.apache.maven.scm.provider.local.repository.LocalScmProviderRepository;
031    import org.codehaus.plexus.util.StringUtils;
032    
033    import java.io.File;
034    import java.util.ArrayList;
035    import java.util.Iterator;
036    import java.util.List;
037    
038    /**
039     * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
040     *
041     */
042    public class LocalListCommand
043        extends AbstractListCommand
044    {
045        /** {@inheritDoc} */
046        protected ListScmResult executeListCommand( ScmProviderRepository repo, ScmFileSet fileSet, boolean recursive,
047                                                    ScmVersion version )
048            throws ScmException
049        {
050            if ( version != null )
051            {
052                throw new ScmException( "The local scm doesn't support tags." );
053            }
054    
055            LocalScmProviderRepository repository = (LocalScmProviderRepository) repo;
056    
057            File root = new File( repository.getRoot() );
058    
059            String module = repository.getModule();
060    
061            File source = new File( root, module );
062    
063            if ( !root.exists() )
064            {
065                throw new ScmException( "The base directory doesn't exist (" + root.getAbsolutePath() + ")." );
066            }
067    
068            if ( !source.exists() )
069            {
070                throw new ScmException( "The module directory doesn't exist (" + source.getAbsolutePath() + ")." );
071            }
072    
073            if ( getLogger().isInfoEnabled() )
074            {
075                getLogger().info( "Listing files of '" + source.getAbsolutePath() + "'." );
076            }
077    
078            try
079            {
080                if ( fileSet.getFileList() == null || fileSet.getFileList().isEmpty() )
081                {
082                    return new LocalListScmResult( null, getFiles( source, source, recursive ) );
083                }
084                else
085                {
086                    List<ScmFile> files = new ArrayList<ScmFile>();
087                    Iterator<File> it = fileSet.getFileList().iterator();
088    
089                    while ( it.hasNext() )
090                    {
091                        File file = (File) it.next();
092    
093                        files.addAll( getFiles( source, new File( source, file.getPath() ), recursive ) );
094                    }
095    
096                    return new LocalListScmResult( null, files );
097                }
098            }
099            catch ( Exception e )
100            {
101                return new ListScmResult( null, "The svn command failed.", e.getMessage(), false );
102            }
103        }
104    
105        private List<ScmFile> getFiles( File source, File directory, boolean recursive )
106            throws Exception
107        {
108            if ( !directory.exists() )
109            {
110                throw new Exception( "Directory '" + directory.getAbsolutePath() + "' doesn't exist." );
111            }
112    
113            List<ScmFile> files = new ArrayList<ScmFile>();
114    
115            File[] filesArray = directory.listFiles();
116    
117            if ( filesArray != null )
118            {
119                for ( int i = 0; i < filesArray.length; i++ )
120                {
121                    File f = filesArray[i];
122    
123                    String path = f.getAbsolutePath().substring( source.getAbsolutePath().length() );
124                    path = StringUtils.replace( path, "\\", "/" );
125                    path = StringUtils.replace( path, "/./", "/" );
126    
127                    files.add( new ScmFile( path, ScmFileStatus.CHECKED_IN ) );
128    
129                    if ( f.isDirectory() && recursive )
130                    {
131                        files.addAll( getFiles( source, f, recursive ) );
132                    }
133                }
134            }
135    
136            return files;
137        }
138    }