View Javadoc
1   package org.apache.maven.scm.provider.perforce.command.checkout;
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.ScmFile;
23  import org.apache.maven.scm.ScmFileStatus;
24  import org.apache.maven.scm.provider.perforce.command.AbstractPerforceConsumer;
25  import org.apache.maven.scm.provider.perforce.command.PerforceVerbMapper;
26  import org.codehaus.plexus.util.cli.StreamConsumer;
27  
28  import java.util.ArrayList;
29  import java.util.List;
30  import java.util.regex.Matcher;
31  import java.util.regex.Pattern;
32  
33  /**
34   * @author Mike Perham
35   */
36  public class PerforceCheckOutConsumer
37      extends AbstractPerforceConsumer
38      implements StreamConsumer
39  {
40      public static final int STATE_CLIENTSPEC = 0;
41  
42      public static final int STATE_NORMAL = 1;
43  
44      public static final int STATE_ERROR = 2;
45  
46      private int currentState = STATE_CLIENTSPEC;
47  
48      private Pattern fileRegexp = Pattern.compile( "([^#]+)#\\d+ - ([a-z]+)" );
49  
50      private List<ScmFile> checkedout = new ArrayList<ScmFile>();
51  
52      private String repo = null;
53  
54      private String specname = null;
55  
56      public PerforceCheckOutConsumer( String clientspec, String repoPath )
57      {
58          repo = repoPath;
59          specname = clientspec;
60      }
61  
62      /*
63       * Client mperham-mikeperham-dt-maven saved.
64       */
65      /*
66       * //depot/modules/cordoba/runtime-ear/.j2ee#1 - deleted as
67       * d:\perforce\depot\modules\cordoba\runtime-ear\.j2ee
68       * //depot/modules/cordoba/runtime-ear/.project#1 - deleted as
69       * d:\perforce\depot\modules\cordoba\runtime-ear\.project
70       * //depot/modules/cordoba/runtime-ear/.runtime#1 - deleted as
71       * d:\perforce\depot\modules\cordoba\runtime-ear\.runtime
72       * //depot/modules/cordoba/runtime-ear/Foo.java#1 - deleted as
73       * d:\perforce\depot\modules\cordoba\runtime-ear\Foo.java
74       * //depot/modules/cordoba/runtime-ear/META-INF/.modulemaps#1 - deleted as
75       * d:\perforce\depot\modules\cordoba\runtime-ear\META-INF\.modulemaps
76       * //depot/modules/cordoba/runtime-ear/META-INF/application.xml#1 - deleted
77       * as d:\perforce\depot\modules\cordoba\runtime-ear\META-INF\application.xml
78       * //depot/modules/cordoba/runtime-ear/pom.xml#4 - deleted as
79       * d:\perforce\depot\modules\cordoba\runtime-ear\pom.xml
80       */
81      /*
82       * Invalid changelist/client/label/date '@somelabel'.
83       */
84      /** {@inheritDoc} */
85      public void consumeLine( String line )
86      {
87          if ( currentState == STATE_CLIENTSPEC
88              && ( line.startsWith( "Client " + specname + " saved." ) || line.startsWith( "Client " + specname
89                  + " not changed." ) ) )
90          {
91              currentState = STATE_NORMAL;
92              return;
93          }
94  
95          // Handle case where the clientspec is current
96          if ( currentState == STATE_NORMAL && line.indexOf( "ile(s) up-to-date" ) != -1 )
97          {
98              return;
99          }
100 
101         Matcher matcher;
102         if ( currentState != STATE_ERROR && ( matcher = fileRegexp.matcher( line ) ).find() )
103         {
104             String location = matcher.group( 1 );
105             if ( location.startsWith( repo ) )
106             {
107                 location = location.substring( repo.length() + 1 );
108             }
109             ScmFileStatus status = PerforceVerbMapper.toStatus( matcher.group( 2 ) );
110             if ( status != null )
111             {
112                 // there are cases where Perforce prints out something but the file did not
113                 // actually change (especially when force syncing).  Those files will have
114                 // a null status.
115                 checkedout.add( new ScmFile( location, status ) );
116             }
117             return;
118         }
119 
120         error( line );
121     }
122 
123     private void error( String line )
124     {
125         currentState = STATE_ERROR;
126         output.println( line );
127     }
128 
129     public boolean isSuccess()
130     {
131         return currentState == STATE_NORMAL;
132     }
133 
134     public List<ScmFile> getCheckedout()
135     {
136         return checkedout;
137     }
138 }