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 */
042public class LocalMkdirCommand extends AbstractMkdirCommand {
043    protected MkdirScmResult executeMkdirCommand(
044            ScmProviderRepository repository, ScmFileSet fileSet, String message, boolean createInLocal)
045            throws ScmException {
046        LocalScmProviderRepository repo = (LocalScmProviderRepository) repository;
047        List<ScmFile> createdDirs = new ArrayList<>();
048
049        // create/commit the directory directly in the repository
050        if (!createInLocal) {
051            File file = fileSet.getFileList().get(0);
052            File modulePath = new File(repo.getRoot(), repo.getModule());
053            File dir = new File(modulePath, file.getName());
054
055            if (dir.exists()) {
056                return new MkdirScmResult(null, "Directory already exists!", "Directory already exists.", false);
057            } else {
058                if (logger.isInfoEnabled()) {
059                    logger.info("Creating directory in '" + modulePath.getAbsolutePath() + "'");
060                }
061
062                FileUtils.mkdir(dir.getAbsolutePath());
063                createdDirs.add(new ScmFile(dir.getPath(), ScmFileStatus.ADDED));
064            }
065        } else {
066            // add the directory, but not commit
067            LocalAddCommand addCmd = new LocalAddCommand();
068
069            CommandParameters parameters = new CommandParameters();
070            parameters.setString(CommandParameter.MESSAGE, message);
071            parameters.setString(CommandParameter.BINARY, "false");
072
073            String path = (fileSet.getFileList().get(0)).getPath();
074            if (repo.isFileAdded(path)) {
075                return new MkdirScmResult(null, "Directory already exists!", "Directory already exists.", false);
076            }
077
078            AddScmResult result = (AddScmResult) addCmd.execute(repository, fileSet, parameters);
079            createdDirs.addAll(result.getAddedFiles());
080        }
081
082        return new MkdirScmResult(null, createdDirs);
083    }
084}