View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.scm.provider.local.command.list;
20  
21  import java.io.File;
22  import java.util.ArrayList;
23  import java.util.List;
24  
25  import org.apache.commons.lang3.StringUtils;
26  import org.apache.maven.scm.ScmException;
27  import org.apache.maven.scm.ScmFile;
28  import org.apache.maven.scm.ScmFileSet;
29  import org.apache.maven.scm.ScmFileStatus;
30  import org.apache.maven.scm.ScmVersion;
31  import org.apache.maven.scm.command.list.AbstractListCommand;
32  import org.apache.maven.scm.command.list.ListScmResult;
33  import org.apache.maven.scm.provider.ScmProviderRepository;
34  import org.apache.maven.scm.provider.local.repository.LocalScmProviderRepository;
35  
36  /**
37   * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
38   *
39   */
40  public class LocalListCommand extends AbstractListCommand {
41      /** {@inheritDoc} */
42      protected ListScmResult executeListCommand(
43              ScmProviderRepository repo, ScmFileSet fileSet, boolean recursive, ScmVersion version) throws ScmException {
44          if (version != null) {
45              throw new ScmException("The local scm doesn't support tags.");
46          }
47  
48          LocalScmProviderRepository repository = (LocalScmProviderRepository) repo;
49  
50          File root = new File(repository.getRoot());
51  
52          String module = repository.getModule();
53  
54          File source = new File(root, module);
55  
56          if (!root.exists()) {
57              throw new ScmException("The base directory doesn't exist (" + root.getAbsolutePath() + ").");
58          }
59  
60          if (!source.exists()) {
61              throw new ScmException("The module directory doesn't exist (" + source.getAbsolutePath() + ").");
62          }
63  
64          if (logger.isInfoEnabled()) {
65              logger.info("Listing files of '" + source.getAbsolutePath() + "'.");
66          }
67  
68          try {
69              if (fileSet.getFileList() == null || fileSet.getFileList().isEmpty()) {
70                  return new LocalListScmResult(null, getFiles(source, source, recursive));
71              } else {
72                  List<ScmFile> files = new ArrayList<>();
73  
74                  for (File file : fileSet.getFileList()) {
75                      files.addAll(getFiles(source, new File(source, file.getPath()), recursive));
76                  }
77  
78                  return new LocalListScmResult(null, files);
79              }
80          } catch (Exception e) {
81              return new ListScmResult(null, "The svn command failed.", e.getMessage(), false);
82          }
83      }
84  
85      private List<ScmFile> getFiles(File source, File directory, boolean recursive) throws Exception {
86          if (!directory.exists()) {
87              throw new Exception("Directory '" + directory.getAbsolutePath() + "' doesn't exist.");
88          }
89  
90          List<ScmFile> files = new ArrayList<>();
91  
92          File[] filesArray = directory.listFiles();
93  
94          if (filesArray != null) {
95              for (int i = 0; i < filesArray.length; i++) {
96                  File f = filesArray[i];
97  
98                  String path =
99                          f.getAbsolutePath().substring(source.getAbsolutePath().length());
100                 path = StringUtils.replace(path, "\\", "/");
101                 path = StringUtils.replace(path, "/./", "/");
102 
103                 files.add(new ScmFile(path, ScmFileStatus.CHECKED_IN));
104 
105                 if (f.isDirectory() && recursive) {
106                     files.addAll(getFiles(source, f, recursive));
107                 }
108             }
109         }
110 
111         return files;
112     }
113 }