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