001package org.apache.maven.scm.provider.starteam.command.add;
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 org.apache.maven.scm.ScmException;
023import org.apache.maven.scm.ScmFileSet;
024import org.apache.maven.scm.ScmResult;
025import org.apache.maven.scm.command.add.AbstractAddCommand;
026import org.apache.maven.scm.command.add.AddScmResult;
027import org.apache.maven.scm.provider.ScmProviderRepository;
028import org.apache.maven.scm.provider.starteam.command.StarteamCommand;
029import org.apache.maven.scm.provider.starteam.command.StarteamCommandLineUtils;
030import org.apache.maven.scm.provider.starteam.repository.StarteamScmProviderRepository;
031import org.codehaus.plexus.util.cli.CommandLineUtils;
032import org.codehaus.plexus.util.cli.Commandline;
033
034import java.io.File;
035import java.util.ArrayList;
036import java.util.List;
037
038/**
039 * @author <a href="mailto:dantran@gmail.com">Dan T. Tran</a>
040 *
041 */
042public class StarteamAddCommand
043    extends AbstractAddCommand
044    implements StarteamCommand
045{
046    /** {@inheritDoc} */
047    protected ScmResult executeAddCommand( ScmProviderRepository repo, ScmFileSet fileSet, String message,
048                                           boolean binary )
049        throws ScmException
050    {
051
052        //work around until maven-scm-api allow this
053        String issue = System.getProperty( "maven.scm.issue" );
054
055        if ( getLogger().isInfoEnabled() )
056        {
057            getLogger().info( "Working directory: " + fileSet.getBasedir().getAbsolutePath() );
058        }
059
060        StarteamScmProviderRepository repository = (StarteamScmProviderRepository) repo;
061
062        StarteamAddConsumer consumer = new StarteamAddConsumer( getLogger(), fileSet.getBasedir() );
063
064        CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
065
066        for ( File fileToBeAdded : fileSet.getFileList() )
067        {
068            ScmFileSet scmFile = new ScmFileSet( fileSet.getBasedir(), fileToBeAdded );
069
070            Commandline cl = createCommandLine( repository, scmFile, issue );
071
072            int exitCode = StarteamCommandLineUtils.executeCommandline( cl, consumer, stderr, getLogger() );
073
074            if ( exitCode != 0 )
075            {
076                return new AddScmResult( cl.toString(), "The starteam command failed.", stderr.getOutput(), false );
077            }
078        }
079
080        return new AddScmResult( null, consumer.getAddedFiles() );
081    }
082
083    static Commandline createCommandLine( StarteamScmProviderRepository repo, ScmFileSet scmFileSet, String issue )
084    {
085        List<String> args = new ArrayList<String>();
086
087        if ( issue != null && issue.length() != 0 )
088        {
089            args.add( "-cr" );
090            args.add( issue );
091        }
092
093        StarteamCommandLineUtils.addEOLOption( args );
094
095        return StarteamCommandLineUtils.createStarteamCommandLine( "add", args, scmFileSet, repo );
096    }
097}