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.mkdir;
20  
21  import java.io.File;
22  import java.util.ArrayList;
23  import java.util.List;
24  
25  import org.apache.maven.scm.CommandParameter;
26  import org.apache.maven.scm.CommandParameters;
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.command.add.AddScmResult;
32  import org.apache.maven.scm.command.mkdir.AbstractMkdirCommand;
33  import org.apache.maven.scm.command.mkdir.MkdirScmResult;
34  import org.apache.maven.scm.provider.ScmProviderRepository;
35  import org.apache.maven.scm.provider.local.command.add.LocalAddCommand;
36  import org.apache.maven.scm.provider.local.repository.LocalScmProviderRepository;
37  import org.codehaus.plexus.util.FileUtils;
38  
39  /**
40   * @author <a href="mailto:oching@apache.org">Maria Odea Ching</a>
41   *
42   */
43  public class LocalMkdirCommand extends AbstractMkdirCommand {
44      protected MkdirScmResult executeMkdirCommand(
45              ScmProviderRepository repository, ScmFileSet fileSet, String message, boolean createInLocal)
46              throws ScmException {
47          LocalScmProviderRepository repo = (LocalScmProviderRepository) repository;
48          List<ScmFile> createdDirs = new ArrayList<>();
49  
50          // create/commit the directory directly in the repository
51          if (!createInLocal) {
52              File file = fileSet.getFileList().get(0);
53              File modulePath = new File(repo.getRoot(), repo.getModule());
54              File dir = new File(modulePath, file.getName());
55  
56              if (dir.exists()) {
57                  return new MkdirScmResult(null, "Directory already exists!", "Directory already exists.", false);
58              } else {
59                  if (logger.isInfoEnabled()) {
60                      logger.info("Creating directory in '" + modulePath.getAbsolutePath() + "'");
61                  }
62  
63                  FileUtils.mkdir(dir.getAbsolutePath());
64                  createdDirs.add(new ScmFile(dir.getPath(), ScmFileStatus.ADDED));
65              }
66          } else {
67              // add the directory, but not commit
68              LocalAddCommand addCmd = new LocalAddCommand();
69  
70              CommandParameters parameters = new CommandParameters();
71              parameters.setString(CommandParameter.MESSAGE, message);
72              parameters.setString(CommandParameter.BINARY, "false");
73  
74              String path = (fileSet.getFileList().get(0)).getPath();
75              if (repo.isFileAdded(path)) {
76                  return new MkdirScmResult(null, "Directory already exists!", "Directory already exists.", false);
77              }
78  
79              AddScmResult result = (AddScmResult) addCmd.execute(repository, fileSet, parameters);
80              createdDirs.addAll(result.getAddedFiles());
81          }
82  
83          return new MkdirScmResult(null, createdDirs);
84      }
85  }