001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.maven.scm.provider.local.command.list;
020
021import java.io.File;
022import java.io.IOException;
023import java.util.ArrayList;
024import java.util.List;
025
026import org.apache.commons.lang3.StringUtils;
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.list.AbstractListCommand;
033import org.apache.maven.scm.command.list.ListScmResult;
034import org.apache.maven.scm.provider.ScmProviderRepository;
035import org.apache.maven.scm.provider.local.repository.LocalScmProviderRepository;
036
037/**
038 * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
039 */
040public class LocalListCommand extends AbstractListCommand {
041    /**
042     * {@inheritDoc}
043     */
044    protected ListScmResult executeListCommand(
045            ScmProviderRepository repo, ScmFileSet fileSet, boolean recursive, ScmVersion version) throws ScmException {
046        if (version != null) {
047            throw new ScmException("The local scm doesn't support tags.");
048        }
049
050        LocalScmProviderRepository repository = (LocalScmProviderRepository) repo;
051
052        File root = new File(repository.getRoot());
053
054        String module = repository.getModule();
055
056        File source = new File(root, module);
057
058        if (!root.exists()) {
059            throw new ScmException("The base directory doesn't exist (" + root.getAbsolutePath() + ").");
060        }
061
062        if (!source.exists()) {
063            throw new ScmException("The module directory doesn't exist (" + source.getAbsolutePath() + ").");
064        }
065
066        if (logger.isInfoEnabled()) {
067            logger.info("Listing files of '" + source.getAbsolutePath() + "'.");
068        }
069
070        try {
071            if (fileSet.getFileList() == null || fileSet.getFileList().isEmpty()) {
072                return new LocalListScmResult(null, getFiles(source, source, recursive));
073            } else {
074                List<ScmFile> files = new ArrayList<>();
075
076                for (File file : fileSet.getFileList()) {
077                    files.addAll(getFiles(source, new File(source, file.getPath()), recursive));
078                }
079
080                return new LocalListScmResult(null, files);
081            }
082        } catch (IOException e) {
083            return new ListScmResult(null, "The svn command failed.", e.getMessage(), false);
084        }
085    }
086
087    private List<ScmFile> getFiles(File source, File directory, boolean recursive) throws IOException {
088        if (!directory.exists()) {
089            throw new IOException("Directory '" + directory.getAbsolutePath() + "' doesn't exist.");
090        }
091
092        List<ScmFile> files = new ArrayList<>();
093
094        File[] filesArray = directory.listFiles();
095
096        if (filesArray != null) {
097            for (int i = 0; i < filesArray.length; i++) {
098                File f = filesArray[i];
099
100                String path =
101                        f.getAbsolutePath().substring(source.getAbsolutePath().length());
102                path = StringUtils.replace(path, "\\", "/");
103                path = StringUtils.replace(path, "/./", "/");
104
105                files.add(new ScmFile(path, ScmFileStatus.CHECKED_IN));
106
107                if (f.isDirectory() && recursive) {
108                    files.addAll(getFiles(source, f, recursive));
109                }
110            }
111        }
112
113        return files;
114    }
115}