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