001package org.apache.maven.scm.provider.cvslib.command;
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.provider.cvslib.repository.CvsScmProviderRepository;
025import org.apache.maven.scm.provider.cvslib.util.CvsUtil;
026import org.apache.maven.scm.providers.cvslib.settings.Settings;
027import org.codehaus.plexus.util.StringUtils;
028import org.codehaus.plexus.util.cli.CommandLineException;
029import org.codehaus.plexus.util.cli.CommandLineUtils;
030import org.codehaus.plexus.util.cli.Commandline;
031
032import java.io.File;
033import java.util.Enumeration;
034
035/**
036 * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
037 *
038 */
039public class CvsCommandUtils
040{
041    private CvsCommandUtils()
042    {
043        // noop
044    }
045
046    public static boolean isCvsNT()
047        throws ScmException
048    {
049        Commandline cl = new Commandline();
050
051        cl.setExecutable( "cvs" );
052
053        cl.createArg().setValue( "-v" );
054
055        CommandLineUtils.StringStreamConsumer stdout = new CommandLineUtils.StringStreamConsumer();
056
057        CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
058
059        try
060        {
061            CommandLineUtils.executeCommandLine( cl, stdout, stderr );
062        }
063        catch ( CommandLineException e )
064        {
065            throw new ScmException( "Error while executing command.", e );
066        }
067
068        return stdout.getOutput().indexOf( "CVSNT" ) >= 0;
069    }
070
071    public static Commandline getBaseCommand( String commandName, CvsScmProviderRepository repo, ScmFileSet fileSet )
072    {
073        return getBaseCommand( commandName, repo, fileSet, null, true );
074    }
075
076    public static Commandline getBaseCommand( String commandName, CvsScmProviderRepository repo, ScmFileSet fileSet,
077                                              boolean addCvsRoot )
078    {
079        return getBaseCommand( commandName, repo, fileSet, null, addCvsRoot );
080    }
081
082    public static Commandline getBaseCommand( String commandName, CvsScmProviderRepository repo, ScmFileSet fileSet,
083                                              String options )
084    {
085        return getBaseCommand( commandName, repo, fileSet, options, true );
086    }
087
088    public static Commandline getBaseCommand( String commandName, CvsScmProviderRepository repo, ScmFileSet fileSet,
089                                              String options, boolean addCvsRoot )
090    {
091        Settings settings = CvsUtil.getSettings();
092
093        Commandline cl = new Commandline();
094
095        cl.setExecutable( "cvs" );
096
097        cl.setWorkingDirectory( fileSet.getBasedir().getAbsolutePath() );
098
099        if ( Boolean.getBoolean( "maven.scm.cvs.use_compression" ) )
100        {
101            cl.createArg().setValue( "-z" + System.getProperty( "maven.scm.cvs.compression_level", "3" ) );
102        }
103        else if ( settings.getCompressionLevel() > 0 )
104        {
105            cl.createArg().setValue( "-z" + settings.getCompressionLevel() );
106        }
107
108        if ( !settings.isUseCvsrc() )
109        {
110            cl.createArg().setValue( "-f" ); // don't use ~/.cvsrc
111        }
112
113        if ( settings.isTraceCvsCommand() )
114        {
115            cl.createArg().setValue( "-t" );
116        }
117
118        if ( !StringUtils.isEmpty( settings.getTemporaryFilesDirectory() ) )
119        {
120            File tempDir = new File( settings.getTemporaryFilesDirectory() );
121
122            if ( !tempDir.exists() )
123            {
124                tempDir.mkdirs();
125            }
126
127            cl.createArg().setValue( "-T" );
128
129            cl.createArg().setValue( tempDir.getAbsolutePath() );
130        }
131
132        if ( settings.getCvsVariables().size() > 0 )
133        {
134            for ( Enumeration<?> e = settings.getCvsVariables().propertyNames(); e.hasMoreElements(); )
135            {
136                String key = (String) e.nextElement();
137                String value = settings.getCvsVariables().getProperty( key );
138                cl.createArg().setValue( "-s" );
139                cl.createArg().setValue( key + "=" + value );
140            }
141        }
142
143        if ( addCvsRoot )
144        {
145            cl.createArg().setValue( "-d" );
146
147            cl.createArg().setValue( repo.getCvsRoot() );
148        }
149
150        cl.createArg().setLine( options );
151
152        cl.createArg().setValue( "-q" );
153
154        cl.createArg().setValue( commandName );
155
156        return cl;
157    }
158}