001    package org.apache.maven.scm.provider.tfs.command;
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    
022    import org.apache.maven.scm.ScmException;
023    import org.apache.maven.scm.ScmFileSet;
024    import org.apache.maven.scm.ScmVersion;
025    import org.apache.maven.scm.command.checkout.AbstractCheckOutCommand;
026    import org.apache.maven.scm.command.checkout.CheckOutScmResult;
027    import org.apache.maven.scm.provider.ScmProviderRepository;
028    import org.apache.maven.scm.provider.tfs.TfsScmProviderRepository;
029    import org.apache.maven.scm.provider.tfs.command.consumer.ErrorStreamConsumer;
030    import org.apache.maven.scm.provider.tfs.command.consumer.FileListConsumer;
031    
032    // Usage: mvn scm:checkout -DcheckoutDirectory=<dir>
033    public class TfsCheckOutCommand
034        extends AbstractCheckOutCommand
035    {
036    
037        protected CheckOutScmResult executeCheckOutCommand( ScmProviderRepository r, ScmFileSet f, ScmVersion v,
038                                                            boolean recursive )
039            throws ScmException
040        {
041            TfsScmProviderRepository tfsRepo = (TfsScmProviderRepository) r;
042            String url = tfsRepo.getServerPath();
043            String tfsUrl = tfsRepo.getTfsUrl();
044            String workspace = tfsRepo.getWorkspace();
045    
046            // Try creating workspace
047            boolean workspaceProvided = workspace != null && !workspace.trim().equals( "" );
048            if ( workspaceProvided )
049            {
050                createWorkspace( r, f, workspace, tfsUrl );
051            }
052    
053            TfsCommand command;
054            int status;
055    
056            if ( workspaceProvided )
057            {
058                status = executeUnmapCommand( r, f );
059            }
060            
061            ErrorStreamConsumer out = new ErrorStreamConsumer();
062            ErrorStreamConsumer err = new ErrorStreamConsumer();
063            if ( workspaceProvided )
064            {
065                command = new TfsCommand( "workfold", r, null, getLogger() );
066                command.addArgument( "-workspace:" + workspace );
067                command.addArgument( "-map" );
068                command.addArgument( url );
069                command.addArgument( f.getBasedir().getAbsolutePath() );
070                status = command.execute( out, err );
071                if ( status != 0 || err.hasBeenFed() )
072                {
073                    return new CheckOutScmResult( command.getCommandString(),
074                                                  "Error code for TFS checkout (workfold map) command - " + status,
075                                                  err.getOutput(), false );
076                }
077            }
078            FileListConsumer fileConsumer = new FileListConsumer();
079            err = new ErrorStreamConsumer();
080            command = createGetCommand( r, f, v, recursive );
081            status = command.execute( fileConsumer, err );
082            if ( status != 0 || err.hasBeenFed() )
083            {
084                return new CheckOutScmResult( command.getCommandString(), "Error code for TFS checkout (get) command - "
085                    + status, err.getOutput(), false );
086            }
087            
088            return new CheckOutScmResult( command.getCommandString(), fileConsumer.getFiles() );
089        }
090    
091        public TfsCommand createGetCommand( ScmProviderRepository r, ScmFileSet f, ScmVersion v, boolean recursive )
092        {
093            TfsCommand command = new TfsCommand( "get", r, f, getLogger() );
094            if ( recursive )
095            {
096                command.addArgument( "-recursive" );
097            }
098            
099            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    }