View Javadoc
1   package org.apache.maven.scm.provider.cvslib.command;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.scm.ScmException;
23  import org.apache.maven.scm.ScmFileSet;
24  import org.apache.maven.scm.provider.cvslib.repository.CvsScmProviderRepository;
25  import org.apache.maven.scm.provider.cvslib.util.CvsUtil;
26  import org.apache.maven.scm.providers.cvslib.settings.Settings;
27  import org.codehaus.plexus.util.StringUtils;
28  import org.codehaus.plexus.util.cli.CommandLineException;
29  import org.codehaus.plexus.util.cli.CommandLineUtils;
30  import org.codehaus.plexus.util.cli.Commandline;
31  
32  import java.io.File;
33  import java.util.Enumeration;
34  
35  /**
36   * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
37   *
38   */
39  public class CvsCommandUtils
40  {
41      private CvsCommandUtils()
42      {
43          // noop
44      }
45  
46      public static boolean isCvsNT()
47          throws ScmException
48      {
49          Commandline cl = new Commandline();
50  
51          cl.setExecutable( "cvs" );
52  
53          cl.createArg().setValue( "-v" );
54  
55          CommandLineUtils.StringStreamConsumer stdout = new CommandLineUtils.StringStreamConsumer();
56  
57          CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
58  
59          try
60          {
61              CommandLineUtils.executeCommandLine( cl, stdout, stderr );
62          }
63          catch ( CommandLineException e )
64          {
65              throw new ScmException( "Error while executing command.", e );
66          }
67  
68          return stdout.getOutput().indexOf( "CVSNT" ) >= 0;
69      }
70  
71      public static Commandline getBaseCommand( String commandName, CvsScmProviderRepository repo, ScmFileSet fileSet )
72      {
73          return getBaseCommand( commandName, repo, fileSet, null, true );
74      }
75  
76      public static Commandline getBaseCommand( String commandName, CvsScmProviderRepository repo, ScmFileSet fileSet,
77                                                boolean addCvsRoot )
78      {
79          return getBaseCommand( commandName, repo, fileSet, null, addCvsRoot );
80      }
81  
82      public static Commandline getBaseCommand( String commandName, CvsScmProviderRepository repo, ScmFileSet fileSet,
83                                                String options )
84      {
85          return getBaseCommand( commandName, repo, fileSet, options, true );
86      }
87  
88      public static Commandline getBaseCommand( String commandName, CvsScmProviderRepository repo, ScmFileSet fileSet,
89                                                String options, boolean addCvsRoot )
90      {
91          Settings settings = CvsUtil.getSettings();
92  
93          Commandline cl = new Commandline();
94  
95          cl.setExecutable( "cvs" );
96  
97          cl.setWorkingDirectory( fileSet.getBasedir().getAbsolutePath() );
98  
99          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 }