001package org.apache.maven.scm.provider.local.command.mkdir;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 * http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.io.File;
023import java.util.ArrayList;
024import java.util.List;
025
026import org.apache.maven.scm.CommandParameter;
027import org.apache.maven.scm.CommandParameters;
028import org.apache.maven.scm.ScmException;
029import org.apache.maven.scm.ScmFile;
030import org.apache.maven.scm.ScmFileSet;
031import org.apache.maven.scm.ScmFileStatus;
032import org.apache.maven.scm.command.add.AddScmResult;
033import org.apache.maven.scm.command.mkdir.AbstractMkdirCommand;
034import org.apache.maven.scm.command.mkdir.MkdirScmResult;
035import org.apache.maven.scm.provider.ScmProviderRepository;
036import org.apache.maven.scm.provider.local.command.add.LocalAddCommand;
037import org.apache.maven.scm.provider.local.repository.LocalScmProviderRepository;
038import org.codehaus.plexus.util.FileUtils;
039
040/**
041 * @author <a href="mailto:oching@apache.org">Maria Odea Ching</a>
042 *
043 */
044public class LocalMkdirCommand
045    extends AbstractMkdirCommand
046{
047    protected MkdirScmResult executeMkdirCommand( ScmProviderRepository repository, ScmFileSet fileSet, String message,
048                                                  boolean createInLocal )
049        throws ScmException
050    {
051        LocalScmProviderRepository repo = (LocalScmProviderRepository) repository;
052        List<ScmFile> createdDirs = new ArrayList<ScmFile>();
053
054        // create/commit the directory directly in the repository
055        if ( !createInLocal )
056        {
057            File file = (File) fileSet.getFileList().get( 0 );
058            File modulePath = new File( repo.getRoot(), repo.getModule() );
059            File dir = new File( modulePath, file.getName() );
060
061            if ( dir.exists() )
062            {
063                return new MkdirScmResult( null, "Directory already exists!", "Directory already exists.", false );
064            }
065            else
066            {
067                if ( getLogger().isInfoEnabled() )
068                {
069                    getLogger().info( "Creating directory in '" + modulePath.getAbsolutePath() + "'" );
070                }
071
072                FileUtils.mkdir( dir.getAbsolutePath() );
073                createdDirs.add( new ScmFile( dir.getPath(), ScmFileStatus.ADDED ) );
074            }
075        }
076        else
077        {
078            // add the directory, but not commit
079            LocalAddCommand addCmd = new LocalAddCommand();
080            addCmd.setLogger( getLogger() );
081
082            CommandParameters parameters = new CommandParameters();
083            parameters.setString( CommandParameter.MESSAGE, message );
084            parameters.setString( CommandParameter.BINARY, "false" );
085
086            String path = ( (File) fileSet.getFileList().get( 0 ) ).getPath();
087            if ( repo.isFileAdded( path ) )
088            {
089                return new MkdirScmResult( null, "Directory already exists!", "Directory already exists.", false );
090            }
091
092            AddScmResult result = (AddScmResult) addCmd.execute( repository, fileSet, parameters );
093            createdDirs.addAll( result.getAddedFiles() );
094        }
095
096        return new MkdirScmResult( null, createdDirs );
097    }
098}