View Javadoc
1   package org.apache.maven.scm.provider.starteam.command.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.starteam.command.StarteamCommand;
30  import org.apache.maven.scm.provider.starteam.command.StarteamCommandLineUtils;
31  import org.apache.maven.scm.provider.starteam.repository.StarteamScmProviderRepository;
32  import org.codehaus.plexus.util.cli.CommandLineUtils;
33  import org.codehaus.plexus.util.cli.Commandline;
34  
35  import java.io.File;
36  
37  /**
38   * @author <a href="mailto:dantran@gmail.com">Dan T. Tran</a>
39   * @author Olivier Lamy
40   *
41   */
42  public class StarteamTagCommand
43      extends AbstractTagCommand
44      implements StarteamCommand
45  {
46      // ----------------------------------------------------------------------
47      // AbstractTagCommand Implementation
48      // ----------------------------------------------------------------------
49  
50      protected ScmResult executeTagCommand( ScmProviderRepository repo, ScmFileSet fileSet, String tag, String message )
51          throws ScmException
52      {
53          return executeTagCommand( repo, fileSet, tag, new ScmTagParameters( message ) );
54      }
55      
56      /** {@inheritDoc} */
57      protected ScmResult executeTagCommand( ScmProviderRepository repo, ScmFileSet fileSet, String tag,
58                                             ScmTagParameters scmTagParameters )
59          throws ScmException
60      {
61          if ( fileSet.getFileList().isEmpty() )
62          {
63              throw new ScmException( "This provider doesn't support tagging subsets of a directory" );
64          }
65  
66          if ( tag == null || tag.trim().length() == 0 )
67          {
68              throw new ScmException( "tag must be specified" );
69          }
70  
71          if ( getLogger().isInfoEnabled() )
72          {
73              getLogger().info( "Working directory: " + fileSet.getBasedir().getAbsolutePath() );
74          }
75  
76          StarteamScmProviderRepository repository = (StarteamScmProviderRepository) repo;
77  
78          StarteamTagConsumer consumer = new StarteamTagConsumer( getLogger() );
79  
80          CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
81  
82          Commandline cl = createCommandLine( repository, fileSet.getBasedir(), tag );
83  
84          int exitCode = StarteamCommandLineUtils.executeCommandline( cl, consumer, stderr, getLogger() );
85  
86          if ( exitCode != 0 )
87          {
88              return new TagScmResult( cl.toString(), "The starteam command failed.", stderr.getOutput(), false );
89          }
90  
91          return new TagScmResult( cl.toString(), consumer.getTaggedFiles() );
92      }
93  
94      // ----------------------------------------------------------------------
95      //
96      // ----------------------------------------------------------------------
97  
98      public static Commandline createCommandLine( StarteamScmProviderRepository repo, File workingDirectory, String tag )
99          throws ScmException
100     {
101         Commandline cl = StarteamCommandLineUtils.createStarteamBaseCommandLine( "label", repo );
102 
103         cl.createArg().setValue( "-p" );
104 
105         cl.createArg().setValue( repo.getFullUrl() );
106 
107         cl.createArg().setValue( "-nl" );
108 
109         cl.createArg().setValue( tag );
110 
111         cl.createArg().setValue( "-b" );
112 
113         return cl;
114     }
115 }