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.mkdir;
020
021import java.io.File;
022import java.util.ArrayList;
023import java.util.List;
024
025import org.apache.maven.scm.CommandParameter;
026import org.apache.maven.scm.CommandParameters;
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.command.add.AddScmResult;
032import org.apache.maven.scm.command.mkdir.AbstractMkdirCommand;
033import org.apache.maven.scm.command.mkdir.MkdirScmResult;
034import org.apache.maven.scm.provider.ScmProviderRepository;
035import org.apache.maven.scm.provider.local.command.add.LocalAddCommand;
036import org.apache.maven.scm.provider.local.repository.LocalScmProviderRepository;
037import org.codehaus.plexus.util.FileUtils;
038
039/**
040 * @author <a href="mailto:oching@apache.org">Maria Odea Ching</a>
041 *
042 */
043public class LocalMkdirCommand extends AbstractMkdirCommand {
044    protected MkdirScmResult executeMkdirCommand(
045            ScmProviderRepository repository, ScmFileSet fileSet, String message, boolean createInLocal)
046            throws ScmException {
047        LocalScmProviderRepository repo = (LocalScmProviderRepository) repository;
048        List<ScmFile> createdDirs = new ArrayList<>();
049
050        // create/commit the directory directly in the repository
051        if (!createInLocal) {
052            File file = fileSet.getFileList().get(0);
053            File modulePath = new File(repo.getRoot(), repo.getModule());
054            File dir = new File(modulePath, file.getName());
055
056            if (dir.exists()) {
057                return new MkdirScmResult(null, "Directory already exists!", "Directory already exists.", false);
058            } else {
059                if (logger.isInfoEnabled()) {
060                    logger.info("Creating directory in '" + modulePath.getAbsolutePath() + "'");
061                }
062
063                FileUtils.mkdir(dir.getAbsolutePath());
064                createdDirs.add(new ScmFile(dir.getPath(), ScmFileStatus.ADDED));
065            }
066        } else {
067            // add the directory, but not commit
068            LocalAddCommand addCmd = new LocalAddCommand();
069
070            CommandParameters parameters = new CommandParameters();
071            parameters.setString(CommandParameter.MESSAGE, message);
072            parameters.setString(CommandParameter.BINARY, "false");
073
074            String path = (fileSet.getFileList().get(0)).getPath();
075            if (repo.isFileAdded(path)) {
076                return new MkdirScmResult(null, "Directory already exists!", "Directory already exists.", false);
077            }
078
079            AddScmResult result = (AddScmResult) addCmd.execute(repository, fileSet, parameters);
080            createdDirs.addAll(result.getAddedFiles());
081        }
082
083        return new MkdirScmResult(null, createdDirs);
084    }
085}