Coverage Report - org.apache.maven.scm.client.cli.MavenScmCli
 
Classes in this File Line Coverage Branch Coverage Complexity
MavenScmCli
0 %
0/112
0 %
0/42
5,75
 
 1  
 package org.apache.maven.scm.client.cli;
 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 java.io.File;
 23  
 import java.util.List;
 24  
 
 25  
 import org.apache.maven.scm.ScmBranch;
 26  
 import org.apache.maven.scm.ScmException;
 27  
 import org.apache.maven.scm.ScmFile;
 28  
 import org.apache.maven.scm.ScmFileSet;
 29  
 import org.apache.maven.scm.ScmResult;
 30  
 import org.apache.maven.scm.ScmRevision;
 31  
 import org.apache.maven.scm.ScmTag;
 32  
 import org.apache.maven.scm.ScmVersion;
 33  
 import org.apache.maven.scm.command.checkin.CheckInScmResult;
 34  
 import org.apache.maven.scm.command.checkout.CheckOutScmResult;
 35  
 import org.apache.maven.scm.command.update.UpdateScmResult;
 36  
 import org.apache.maven.scm.manager.NoSuchScmProviderException;
 37  
 import org.apache.maven.scm.manager.ScmManager;
 38  
 import org.apache.maven.scm.repository.ScmRepository;
 39  
 import org.apache.maven.scm.repository.ScmRepositoryException;
 40  
 import org.codehaus.plexus.embed.Embedder;
 41  
 import org.codehaus.plexus.util.StringUtils;
 42  
 
 43  
 /**
 44  
  * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
 45  
  * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
 46  
  * @version $Id: MavenScmCli.java 1242108 2012-02-08 21:29:06Z rfscholte $
 47  
  */
 48  
 public class MavenScmCli
 49  
 {
 50  
     private Embedder plexus;
 51  
 
 52  
     private ScmManager scmManager;
 53  
 
 54  
     // ----------------------------------------------------------------------
 55  
     // Lifecycle
 56  
     // ----------------------------------------------------------------------
 57  
 
 58  
     public MavenScmCli()
 59  
         throws Exception
 60  0
     {
 61  0
         plexus = new Embedder();
 62  
 
 63  0
         plexus.start();
 64  
 
 65  0
         scmManager = (ScmManager) plexus.lookup( ScmManager.ROLE );
 66  0
     }
 67  
 
 68  
     public void stop()
 69  
     {
 70  
         try
 71  
         {
 72  0
             plexus.stop();
 73  
         }
 74  0
         catch ( Exception ex )
 75  
         {
 76  
             // ignore
 77  0
         }
 78  0
     }
 79  
 
 80  
     // ----------------------------------------------------------------------
 81  
     //
 82  
     // ----------------------------------------------------------------------
 83  
 
 84  
     public static void main( String[] args )
 85  
     {
 86  
         MavenScmCli cli;
 87  
 
 88  
         try
 89  
         {
 90  0
             cli = new MavenScmCli();
 91  
         }
 92  0
         catch ( Exception ex )
 93  
         {
 94  0
             System.err.println( "Error while starting Maven Scm." );
 95  
 
 96  0
             ex.printStackTrace( System.err );
 97  
 
 98  0
             return;
 99  0
         }
 100  
 
 101  
         String scmUrl;
 102  
 
 103  
         String command;
 104  
 
 105  0
         if ( args.length != 3 )
 106  
         {
 107  0
             System.err.println(
 108  
                 "Usage: maven-scm-client <command> <working directory> <scm url> [<scmVersion> [<scmVersionType>]]" );
 109  0
             System.err.println( "scmVersion is a branch name/tag name/revision number." );
 110  0
             System.err.println( "scmVersionType can be 'branch', 'tag', 'revision'. "
 111  
                 + "The default value is 'revision'." );
 112  
 
 113  0
             return;
 114  
         }
 115  
 
 116  0
         command = args[0];
 117  
 
 118  
         // SCM-641
 119  0
         File workingDirectory = new File( args[1] ).getAbsoluteFile();
 120  
 
 121  0
         scmUrl = args[2];
 122  
 
 123  0
         ScmVersion scmVersion = null;
 124  0
         if ( args.length > 3 )
 125  
         {
 126  0
             String version = args[3];
 127  
 
 128  0
             if ( args.length > 4 )
 129  
             {
 130  0
                 String type = args[4];
 131  
 
 132  0
                 if ( "tag".equals( type ) )
 133  
                 {
 134  0
                     scmVersion = new ScmTag( version );
 135  
                 }
 136  0
                 else if ( "branch".equals( type ) )
 137  
                 {
 138  0
                     scmVersion = new ScmBranch( version );
 139  
                 }
 140  0
                 else if ( "revision".equals( type ) )
 141  
                 {
 142  0
                     scmVersion = new ScmRevision( version );
 143  
                 }
 144  
                 else
 145  
                 {
 146  0
                     throw new IllegalArgumentException( "'" + type + "' version type isn't known." );
 147  
                 }
 148  0
             }
 149  
             else
 150  
             {
 151  0
                 scmVersion = new ScmRevision( args[3] );
 152  
             }
 153  
         }
 154  
 
 155  0
         cli.execute( scmUrl, command, workingDirectory, scmVersion );
 156  
 
 157  0
         cli.stop();
 158  0
     }
 159  
 
 160  
     // ----------------------------------------------------------------------
 161  
     //
 162  
     // ----------------------------------------------------------------------
 163  
 
 164  
     public void execute( String scmUrl, String command, File workingDirectory, ScmVersion version )
 165  
     {
 166  
         ScmRepository repository;
 167  
 
 168  
         try
 169  
         {
 170  0
             repository = scmManager.makeScmRepository( scmUrl );
 171  
         }
 172  0
         catch ( NoSuchScmProviderException ex )
 173  
         {
 174  0
             System.err.println( "Could not find a provider." );
 175  
 
 176  0
             return;
 177  
         }
 178  0
         catch ( ScmRepositoryException ex )
 179  
         {
 180  0
             System.err.println( "Error while connecting to the repository" );
 181  
 
 182  0
             ex.printStackTrace( System.err );
 183  
 
 184  0
             return;
 185  0
         }
 186  
 
 187  
         try
 188  
         {
 189  0
             if ( command.equals( "checkout" ) )
 190  
             {
 191  0
                 checkOut( repository, workingDirectory, version );
 192  
             }
 193  0
             else if ( command.equals( "checkin" ) )
 194  
             {
 195  0
                 checkIn( repository, workingDirectory, version );
 196  
             }
 197  0
             else if ( command.equals( "update" ) )
 198  
             {
 199  0
                 update( repository, workingDirectory, version );
 200  
             }
 201  
             else
 202  
             {
 203  0
                 System.err.println( "Unknown SCM command '" + command + "'." );
 204  
             }
 205  
         }
 206  0
         catch ( ScmException ex )
 207  
         {
 208  0
             System.err.println( "Error while executing the SCM command." );
 209  
 
 210  0
             ex.printStackTrace( System.err );
 211  
 
 212  0
             return;
 213  0
         }
 214  0
     }
 215  
 
 216  
     // ----------------------------------------------------------------------
 217  
     //
 218  
     // ----------------------------------------------------------------------
 219  
 
 220  
     private void checkOut( ScmRepository scmRepository, File workingDirectory, ScmVersion version )
 221  
         throws ScmException
 222  
     {
 223  0
         if ( workingDirectory.exists() )
 224  
         {
 225  0
             System.err.println( "The working directory already exist: '" + workingDirectory.getAbsolutePath()
 226  
                 + "'." );
 227  
 
 228  0
             return;
 229  
         }
 230  
 
 231  0
         if ( !workingDirectory.mkdirs() )
 232  
         {
 233  0
             System.err.println(
 234  
                 "Error while making the working directory: '" + workingDirectory.getAbsolutePath() + "'." );
 235  
 
 236  0
             return;
 237  
         }
 238  
 
 239  0
         CheckOutScmResult result = scmManager.checkOut( scmRepository, new ScmFileSet( workingDirectory ), version );
 240  
 
 241  0
         if ( !result.isSuccess() )
 242  
         {
 243  0
             showError( result );
 244  
 
 245  0
             return;
 246  
         }
 247  
 
 248  0
         List<ScmFile> checkedOutFiles = result.getCheckedOutFiles();
 249  
 
 250  0
         System.out.println( "Checked out these files: " );
 251  
 
 252  0
         for ( ScmFile file : checkedOutFiles )
 253  
         {
 254  0
             System.out.println( " " + file.getPath() );
 255  
         }
 256  0
     }
 257  
 
 258  
     private void checkIn( ScmRepository scmRepository, File workingDirectory, ScmVersion version )
 259  
         throws ScmException
 260  
     {
 261  0
         if ( !workingDirectory.exists() )
 262  
         {
 263  0
             System.err.println( "The working directory doesn't exist: '" + workingDirectory.getAbsolutePath()
 264  
                 + "'." );
 265  
 
 266  0
             return;
 267  
         }
 268  
 
 269  0
         String message = "";
 270  
 
 271  0
         CheckInScmResult result =
 272  
             scmManager.checkIn( scmRepository, new ScmFileSet( workingDirectory ), version, message );
 273  
 
 274  0
         if ( !result.isSuccess() )
 275  
         {
 276  0
             showError( result );
 277  
 
 278  0
             return;
 279  
         }
 280  
 
 281  0
         List<ScmFile> checkedInFiles = result.getCheckedInFiles();
 282  
 
 283  0
         System.out.println( "Checked in these files: " );
 284  
 
 285  0
         for ( ScmFile file : checkedInFiles )
 286  
         {
 287  0
             System.out.println( " " + file.getPath() );
 288  
         }
 289  0
     }
 290  
 
 291  
     private void update( ScmRepository scmRepository, File workingDirectory, ScmVersion version )
 292  
         throws ScmException
 293  
     {
 294  0
         if ( !workingDirectory.exists() )
 295  
         {
 296  0
             System.err.println( "The working directory doesn't exist: '" + workingDirectory.getAbsolutePath()
 297  
                 + "'." );
 298  
 
 299  0
             return;
 300  
         }
 301  
 
 302  0
         UpdateScmResult result = scmManager.update( scmRepository, new ScmFileSet( workingDirectory ), version );
 303  
 
 304  0
         if ( !result.isSuccess() )
 305  
         {
 306  0
             showError( result );
 307  
 
 308  0
             return;
 309  
         }
 310  
 
 311  0
         List<ScmFile> updatedFiles = result.getUpdatedFiles();
 312  
 
 313  0
         System.out.println( "Updated these files: " );
 314  
 
 315  0
         for ( ScmFile file : updatedFiles )
 316  
         {
 317  0
             System.out.println( " " + file.getPath() );
 318  
         }
 319  0
     }
 320  
 
 321  
     // ----------------------------------------------------------------------
 322  
     //
 323  
     // ----------------------------------------------------------------------
 324  
 
 325  
     private void showError( ScmResult result )
 326  
     {
 327  0
         System.err.println( "There was a error while executing the SCM command." );
 328  
 
 329  0
         String providerMessage = result.getProviderMessage();
 330  
 
 331  0
         if ( !StringUtils.isEmpty( providerMessage ) )
 332  
         {
 333  0
             System.err.println( "Error message from the provider: " + providerMessage );
 334  
         }
 335  
         else
 336  
         {
 337  0
             System.err.println( "The provider didn't give a error message." );
 338  
         }
 339  
 
 340  0
         String output = result.getCommandOutput();
 341  
 
 342  0
         if ( !StringUtils.isEmpty( output ) )
 343  
         {
 344  0
             System.err.println( "Command output:" );
 345  
 
 346  0
             System.err.println( output );
 347  
         }
 348  0
     }
 349  
 }