View Javadoc
1   package org.apache.maven.scm.provider.tfs.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.ScmVersion;
25  import org.apache.maven.scm.command.checkout.AbstractCheckOutCommand;
26  import org.apache.maven.scm.command.checkout.CheckOutScmResult;
27  import org.apache.maven.scm.provider.ScmProviderRepository;
28  import org.apache.maven.scm.provider.tfs.TfsScmProviderRepository;
29  import org.apache.maven.scm.provider.tfs.command.consumer.ErrorStreamConsumer;
30  import org.apache.maven.scm.provider.tfs.command.consumer.FileListConsumer;
31  
32  // Usage: mvn scm:checkout -DcheckoutDirectory=<dir>
33  public class TfsCheckOutCommand
34      extends AbstractCheckOutCommand
35  {
36  
37      protected CheckOutScmResult executeCheckOutCommand( ScmProviderRepository r, ScmFileSet f, ScmVersion v,
38                                                          boolean recursive )
39          throws ScmException
40      {
41          TfsScmProviderRepository tfsRepo = (TfsScmProviderRepository) r;
42          String url = tfsRepo.getServerPath();
43          String tfsUrl = tfsRepo.getTfsUrl();
44          String workspace = tfsRepo.getWorkspace();
45  
46          // Try creating workspace
47          boolean workspaceProvided = workspace != null && !workspace.trim().equals( "" );
48          if ( workspaceProvided )
49          {
50              createWorkspace( r, f, workspace, tfsUrl );
51          }
52  
53          TfsCommand command;
54          int status;
55  
56          if ( workspaceProvided )
57          {
58              status = executeUnmapCommand( r, f );
59          }
60          
61          ErrorStreamConsumer out = new ErrorStreamConsumer();
62          ErrorStreamConsumer err = new ErrorStreamConsumer();
63          if ( workspaceProvided )
64          {
65              command = new TfsCommand( "workfold", r, null, getLogger() );
66              command.addArgument( "-workspace:" + workspace );
67              command.addArgument( "-map" );
68              command.addArgument( url );
69              command.addArgument( f.getBasedir().getAbsolutePath() );
70              status = command.execute( out, err );
71              if ( status != 0 || err.hasBeenFed() )
72              {
73                  return new CheckOutScmResult( command.getCommandString(),
74                                                "Error code for TFS checkout (workfold map) command - " + status,
75                                                err.getOutput(), false );
76              }
77          }
78          FileListConsumer fileConsumer = new FileListConsumer();
79          err = new ErrorStreamConsumer();
80          command = createGetCommand( r, f, v, recursive );
81          status = command.execute( fileConsumer, err );
82          if ( status != 0 || err.hasBeenFed() )
83          {
84              return new CheckOutScmResult( command.getCommandString(), "Error code for TFS checkout (get) command - "
85                  + status, err.getOutput(), false );
86          }
87          
88          return new CheckOutScmResult( command.getCommandString(), fileConsumer.getFiles() );
89      }
90  
91      public TfsCommand createGetCommand( ScmProviderRepository r, ScmFileSet f, ScmVersion v, boolean recursive )
92      {
93          TfsCommand command = new TfsCommand( "get", r, f, getLogger() );
94          if ( recursive )
95          {
96              command.addArgument( "-recursive" );
97          }
98          
99          command.addArgument( "-force" );
100         
101         if ( v != null && !v.equals( "" ) )
102         {
103             String vType = "";
104             if ( v.getType().equals( "Tag" ) )
105             {
106                 vType = "L";
107             }
108             if ( v.getType().equals( "Revision" ) )
109             {
110                 vType = "C";
111             }
112             command.addArgument( "-version:" + vType + v.getName() );
113         }
114         
115         command.addArgument( f.getBasedir().getAbsolutePath() );
116         
117         return command;
118     }
119 
120     public int executeUnmapCommand( ScmProviderRepository r, ScmFileSet f )
121         throws ScmException
122     {
123         TfsScmProviderRepository tfsRepo = (TfsScmProviderRepository) r;
124         String url = tfsRepo.getServerPath();
125         String workspace = tfsRepo.getWorkspace();
126         ErrorStreamConsumer out = new ErrorStreamConsumer();
127         ErrorStreamConsumer err = new ErrorStreamConsumer();
128         
129         TfsCommand command = new TfsCommand( "workfold", r, null, getLogger() );
130         command.addArgument( "-workspace:" + workspace );
131         command.addArgument( "-unmap" );
132         command.addArgument( url );
133         
134         return command.execute( out, err );
135     }
136 
137     private void createWorkspace( ScmProviderRepository r, ScmFileSet f, String workspace, String url )
138         throws ScmException
139     {
140         ErrorStreamConsumer out = new ErrorStreamConsumer();
141         ErrorStreamConsumer err = new ErrorStreamConsumer();
142         // Checkout dir may not exist yet
143         TfsCommand command = new TfsCommand( "workspace", r, null, getLogger() );
144         command.addArgument( "-new" );
145         command.addArgument( "-comment:Creating workspace for maven command" );
146         command.addArgument( "-server:" + url );
147         command.addArgument( workspace );
148         
149         command.execute( out, err );
150     }
151 
152 }