001package org.apache.maven.scm.client.cli;
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.List;
024
025import org.apache.maven.scm.ScmBranch;
026import org.apache.maven.scm.ScmException;
027import org.apache.maven.scm.ScmFile;
028import org.apache.maven.scm.ScmFileSet;
029import org.apache.maven.scm.ScmResult;
030import org.apache.maven.scm.ScmRevision;
031import org.apache.maven.scm.ScmTag;
032import org.apache.maven.scm.ScmVersion;
033import org.apache.maven.scm.command.checkin.CheckInScmResult;
034import org.apache.maven.scm.command.checkout.CheckOutScmResult;
035import org.apache.maven.scm.command.update.UpdateScmResult;
036import org.apache.maven.scm.manager.NoSuchScmProviderException;
037import org.apache.maven.scm.manager.ScmManager;
038import org.apache.maven.scm.repository.ScmRepository;
039import org.apache.maven.scm.repository.ScmRepositoryException;
040import org.codehaus.plexus.embed.Embedder;
041import org.codehaus.plexus.util.StringUtils;
042
043/**
044 * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
045 * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
046 *
047 */
048public class MavenScmCli
049{
050    private Embedder plexus;
051
052    private ScmManager scmManager;
053
054    // ----------------------------------------------------------------------
055    // Lifecycle
056    // ----------------------------------------------------------------------
057
058    public MavenScmCli()
059        throws Exception
060    {
061        plexus = new Embedder();
062
063        plexus.start();
064
065        scmManager = (ScmManager) plexus.lookup( ScmManager.ROLE );
066    }
067
068    public void stop()
069    {
070        try
071        {
072            plexus.stop();
073        }
074        catch ( Exception ex )
075        {
076            // ignore
077        }
078    }
079
080    // ----------------------------------------------------------------------
081    //
082    // ----------------------------------------------------------------------
083
084    public static void main( String[] args )
085    {
086        MavenScmCli cli;
087
088        try
089        {
090            cli = new MavenScmCli();
091        }
092        catch ( Exception ex )
093        {
094            System.err.println( "Error while starting Maven Scm." );
095
096            ex.printStackTrace( System.err );
097
098            return;
099        }
100
101        String scmUrl;
102
103        String command;
104
105        if ( args.length != 3 )
106        {
107            System.err.println(
108                "Usage: maven-scm-client <command> <working directory> <scm url> [<scmVersion> [<scmVersionType>]]" );
109            System.err.println( "scmVersion is a branch name/tag name/revision number." );
110            System.err.println( "scmVersionType can be 'branch', 'tag', 'revision'. "
111                + "The default value is 'revision'." );
112
113            return;
114        }
115
116        command = args[0];
117
118        // SCM-641
119        File workingDirectory = new File( args[1] ).getAbsoluteFile();
120
121        scmUrl = args[2];
122
123        ScmVersion scmVersion = null;
124        if ( args.length > 3 )
125        {
126            String version = args[3];
127
128            if ( args.length > 4 )
129            {
130                String type = args[4];
131
132                if ( "tag".equals( type ) )
133                {
134                    scmVersion = new ScmTag( version );
135                }
136                else if ( "branch".equals( type ) )
137                {
138                    scmVersion = new ScmBranch( version );
139                }
140                else if ( "revision".equals( type ) )
141                {
142                    scmVersion = new ScmRevision( version );
143                }
144                else
145                {
146                    throw new IllegalArgumentException( "'" + type + "' version type isn't known." );
147                }
148            }
149            else
150            {
151                scmVersion = new ScmRevision( args[3] );
152            }
153        }
154
155        cli.execute( scmUrl, command, workingDirectory, scmVersion );
156
157        cli.stop();
158    }
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            repository = scmManager.makeScmRepository( scmUrl );
171        }
172        catch ( NoSuchScmProviderException ex )
173        {
174            System.err.println( "Could not find a provider." );
175
176            return;
177        }
178        catch ( ScmRepositoryException ex )
179        {
180            System.err.println( "Error while connecting to the repository" );
181
182            ex.printStackTrace( System.err );
183
184            return;
185        }
186
187        try
188        {
189            if ( command.equals( "checkout" ) )
190            {
191                checkOut( repository, workingDirectory, version );
192            }
193            else if ( command.equals( "checkin" ) )
194            {
195                checkIn( repository, workingDirectory, version );
196            }
197            else if ( command.equals( "update" ) )
198            {
199                update( repository, workingDirectory, version );
200            }
201            else
202            {
203                System.err.println( "Unknown SCM command '" + command + "'." );
204            }
205        }
206        catch ( ScmException ex )
207        {
208            System.err.println( "Error while executing the SCM command." );
209
210            ex.printStackTrace( System.err );
211
212            return;
213        }
214    }
215
216    // ----------------------------------------------------------------------
217    //
218    // ----------------------------------------------------------------------
219
220    private void checkOut( ScmRepository scmRepository, File workingDirectory, ScmVersion version )
221        throws ScmException
222    {
223        if ( workingDirectory.exists() )
224        {
225            System.err.println( "The working directory already exist: '" + workingDirectory.getAbsolutePath()
226                + "'." );
227
228            return;
229        }
230
231        if ( !workingDirectory.mkdirs() )
232        {
233            System.err.println(
234                "Error while making the working directory: '" + workingDirectory.getAbsolutePath() + "'." );
235
236            return;
237        }
238
239        CheckOutScmResult result = scmManager.checkOut( scmRepository, new ScmFileSet( workingDirectory ), version );
240
241        if ( !result.isSuccess() )
242        {
243            showError( result );
244
245            return;
246        }
247
248        List<ScmFile> checkedOutFiles = result.getCheckedOutFiles();
249
250        System.out.println( "Checked out these files: " );
251
252        for ( ScmFile file : checkedOutFiles )
253        {
254            System.out.println( " " + file.getPath() );
255        }
256    }
257
258    private void checkIn( ScmRepository scmRepository, File workingDirectory, ScmVersion version )
259        throws ScmException
260    {
261        if ( !workingDirectory.exists() )
262        {
263            System.err.println( "The working directory doesn't exist: '" + workingDirectory.getAbsolutePath()
264                + "'." );
265
266            return;
267        }
268
269        String message = "";
270
271        CheckInScmResult result =
272            scmManager.checkIn( scmRepository, new ScmFileSet( workingDirectory ), version, message );
273
274        if ( !result.isSuccess() )
275        {
276            showError( result );
277
278            return;
279        }
280
281        List<ScmFile> checkedInFiles = result.getCheckedInFiles();
282
283        System.out.println( "Checked in these files: " );
284
285        for ( ScmFile file : checkedInFiles )
286        {
287            System.out.println( " " + file.getPath() );
288        }
289    }
290
291    private void update( ScmRepository scmRepository, File workingDirectory, ScmVersion version )
292        throws ScmException
293    {
294        if ( !workingDirectory.exists() )
295        {
296            System.err.println( "The working directory doesn't exist: '" + workingDirectory.getAbsolutePath()
297                + "'." );
298
299            return;
300        }
301
302        UpdateScmResult result = scmManager.update( scmRepository, new ScmFileSet( workingDirectory ), version );
303
304        if ( !result.isSuccess() )
305        {
306            showError( result );
307
308            return;
309        }
310
311        List<ScmFile> updatedFiles = result.getUpdatedFiles();
312
313        System.out.println( "Updated these files: " );
314
315        for ( ScmFile file : updatedFiles )
316        {
317            System.out.println( " " + file.getPath() );
318        }
319    }
320
321    // ----------------------------------------------------------------------
322    //
323    // ----------------------------------------------------------------------
324
325    private void showError( ScmResult result )
326    {
327        System.err.println( "There was a error while executing the SCM command." );
328
329        String providerMessage = result.getProviderMessage();
330
331        if ( !StringUtils.isEmpty( providerMessage ) )
332        {
333            System.err.println( "Error message from the provider: " + providerMessage );
334        }
335        else
336        {
337            System.err.println( "The provider didn't give a error message." );
338        }
339
340        String output = result.getCommandOutput();
341
342        if ( !StringUtils.isEmpty( output ) )
343        {
344            System.err.println( "Command output:" );
345
346            System.err.println( output );
347        }
348    }
349}