View Javadoc
1   package org.apache.maven.scm.provider.vss.commands.tag;
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.ScmResult;
25  import org.apache.maven.scm.ScmTagParameters;
26  import org.apache.maven.scm.command.tag.AbstractTagCommand;
27  import org.apache.maven.scm.command.tag.TagScmResult;
28  import org.apache.maven.scm.provider.ScmProviderRepository;
29  import org.apache.maven.scm.provider.vss.commands.VssCommandLineUtils;
30  import org.apache.maven.scm.provider.vss.commands.VssConstants;
31  import org.apache.maven.scm.provider.vss.repository.VssScmProviderRepository;
32  import org.codehaus.plexus.util.cli.CommandLineUtils;
33  import org.codehaus.plexus.util.cli.Commandline;
34  
35  /**
36   * @author <a href="mailto:matpimenta@gmail.com">Mateus Pimenta</a>
37   * @since 1.3
38   */
39  public class VssTagCommand
40      extends AbstractTagCommand
41  {
42  
43      protected ScmResult executeTagCommand( ScmProviderRepository repository, ScmFileSet fileSet, String tagName,
44                                             ScmTagParameters scmTagParameters )
45          throws ScmException
46      {
47          return executeTagCommand( repository, fileSet, tagName, scmTagParameters == null ? "" : scmTagParameters
48              .getMessage() );
49      }
50  
51  
52      /**
53       * @see org.apache.maven.scm.command.tag.AbstractTagCommand#executeTagCommand(org.apache.maven.scm.provider.ScmProviderRepository, org.apache.maven.scm.ScmFileSet, java.lang.String, java.lang.String)
54       */
55      protected ScmResult executeTagCommand( ScmProviderRepository repository, ScmFileSet fileSet, String tagName,
56                                             String message )
57          throws ScmException
58      {
59          if ( getLogger().isDebugEnabled() )
60          {
61              getLogger().debug( "executing tag command..." );
62          }
63  
64          VssScmProviderRepository repo = (VssScmProviderRepository) repository;
65  
66          Commandline cl = buildCmdLine( repo, fileSet, tagName, message );
67  
68          VssTagConsumer consumer = new VssTagConsumer( repo, getLogger() );
69  
70          //      TODO handle deleted files from VSS
71          CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
72  
73          int exitCode;
74  
75          if ( getLogger().isDebugEnabled() )
76          {
77              getLogger().debug( "Executing: " + cl.getWorkingDirectory().getAbsolutePath() + ">>" + cl.toString() );
78          }
79  
80          exitCode = VssCommandLineUtils.executeCommandline( cl, consumer, stderr, getLogger() );
81  
82          if ( exitCode != 0 )
83          {
84              String error = stderr.getOutput();
85  
86              if ( getLogger().isDebugEnabled() )
87              {
88                  getLogger().debug( "VSS returns error: [" + error + "] return code: [" + exitCode + "]" );
89              }
90              if ( error.indexOf( "A writable copy of" ) < 0 )
91              {
92                  return new TagScmResult( cl.toString(), "The vss command failed.", error, false );
93              }
94              // print out the writable copy for manual handling
95              if ( getLogger().isWarnEnabled() )
96              {
97                  getLogger().warn( error );
98              }
99          }
100 
101         return new TagScmResult( cl.toString(), consumer.getUpdatedFiles() );
102     }
103 
104     public Commandline buildCmdLine( VssScmProviderRepository repo, ScmFileSet fileSet, String tagName, String message )
105         throws ScmException
106     {
107 
108         Commandline command = new Commandline();
109 
110         command.setWorkingDirectory( fileSet.getBasedir().getAbsolutePath() );
111 
112         try
113         {
114             command.addSystemEnvironment();
115         }
116         catch ( Exception e )
117         {
118             throw new ScmException( "Can't add system environment.", e );
119         }
120 
121         command.addEnvironment( "SSDIR", repo.getVssdir() );
122 
123         String ssDir = VssCommandLineUtils.getSsDir();
124 
125         command.setExecutable( ssDir + VssConstants.SS_EXE );
126 
127         command.createArg().setValue( VssConstants.COMMAND_LABEL );
128 
129         command.createArg().setValue( VssConstants.PROJECT_PREFIX + repo.getProject() );
130 
131         // User identification to get access to vss repository
132         if ( repo.getUserPassword() != null )
133         {
134             command.createArg().setValue( VssConstants.FLAG_LOGIN + repo.getUserPassword() );
135         }
136 
137         // Ignore: Do not ask for input under any circumstances.
138         command.createArg().setValue( VssConstants.FLAG_AUTORESPONSE_DEF );
139 
140         command.createArg().setValue( VssConstants.FLAG_LABEL + tagName );
141 
142         return command;
143     }
144 
145 }