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  public class LocalMkdirCommand extends AbstractMkdirCommand {
43      protected MkdirScmResult executeMkdirCommand(
44              ScmProviderRepository repository, ScmFileSet fileSet, String message, boolean createInLocal)
45              throws ScmException {
46          LocalScmProviderRepository repo = (LocalScmProviderRepository) repository;
47          List<ScmFile> createdDirs = new ArrayList<>();
48  
49          // create/commit the directory directly in the repository
50          if (!createInLocal) {
51              File file = fileSet.getFileList().get(0);
52              File modulePath = new File(repo.getRoot(), repo.getModule());
53              File dir = new File(modulePath, file.getName());
54  
55              if (dir.exists()) {
56                  return new MkdirScmResult(null, "Directory already exists!", "Directory already exists.", false);
57              } else {
58                  if (logger.isInfoEnabled()) {
59                      logger.info("Creating directory in '" + modulePath.getAbsolutePath() + "'");
60                  }
61  
62                  FileUtils.mkdir(dir.getAbsolutePath());
63                  createdDirs.add(new ScmFile(dir.getPath(), ScmFileStatus.ADDED));
64              }
65          } else {
66              // add the directory, but not commit
67              LocalAddCommand addCmd = new LocalAddCommand();
68  
69              CommandParameters parameters = new CommandParameters();
70              parameters.setString(CommandParameter.MESSAGE, message);
71              parameters.setString(CommandParameter.BINARY, "false");
72  
73              String path = (fileSet.getFileList().get(0)).getPath();
74              if (repo.isFileAdded(path)) {
75                  return new MkdirScmResult(null, "Directory already exists!", "Directory already exists.", false);
76              }
77  
78              AddScmResult result = (AddScmResult) addCmd.execute(repository, fileSet, parameters);
79              createdDirs.addAll(result.getAddedFiles());
80          }
81  
82          return new MkdirScmResult(null, createdDirs);
83      }
84  }