View Javadoc

1   package org.apache.maven.scm.provider.vss.commands.add;
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.command.add.AbstractAddCommand;
26  import org.apache.maven.scm.command.add.AddScmResult;
27  import org.apache.maven.scm.command.changelog.ChangeLogScmResult;
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:brett@apache.org">Brett Porter</a>
37   *
38   */
39  public class VssAddCommand
40      extends AbstractAddCommand
41  {
42      /** {@inheritDoc} */
43      protected ScmResult executeAddCommand( ScmProviderRepository repository, ScmFileSet fileSet, String message,
44                                             boolean binary )
45          throws ScmException
46      {
47          VssScmProviderRepository repo = (VssScmProviderRepository) repository;
48  
49          if ( fileSet.getFileList().isEmpty() )
50          {
51              throw new ScmException( "You must provide at least one file/directory to add" );
52          }
53  
54          Commandline cl = buildCmdLine( repo, fileSet );
55  
56          VssAddConsumer consumer = new VssAddConsumer( getLogger() );
57  
58          CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
59  
60          if ( getLogger().isInfoEnabled() )
61          {
62              getLogger().info( "Executing: " + cl );
63              getLogger().info( "Working directory: " + cl.getWorkingDirectory().getAbsolutePath() );
64          }
65  
66          int exitCode = VssCommandLineUtils.executeCommandline( cl, consumer, stderr, getLogger() );
67  
68          if ( exitCode != 0 )
69          {
70              return new ChangeLogScmResult( cl.toString(), "The vss command failed.", stderr.getOutput(), false );
71          }
72  
73          return new AddScmResult( cl.toString(), consumer.getAddedFiles() );
74      }
75  
76      public Commandline buildCmdLine( VssScmProviderRepository repo, ScmFileSet fileSet )
77          throws ScmException
78      {
79          Commandline command = new Commandline();
80  
81          command.setWorkingDirectory( fileSet.getBasedir().getAbsolutePath() );
82  
83          try
84          {
85              command.addSystemEnvironment();
86          }
87          catch ( Exception e )
88          {
89              throw new ScmException( "Can't add system environment.", e );
90          }
91  
92          command.addEnvironment( "SSDIR", repo.getVssdir() );
93  
94          String ssDir = VssCommandLineUtils.getSsDir();
95  
96          command.setExecutable( ssDir + VssConstants.SS_EXE );
97  
98          command.createArg().setValue( VssConstants.COMMAND_ADD );
99  
100         VssCommandLineUtils.addFiles( command, fileSet );
101 
102         //        command.createArg().setValue( VssConstants.PROJECT_PREFIX + repo.getProject() );
103 
104         //User identification to get access to vss repository
105         if ( repo.getUserPassword() != null )
106         {
107             command.createArg().setValue( VssConstants.FLAG_LOGIN + repo.getUserPassword() );
108         }
109 
110         //Ignore: Do not ask for input under any circumstances.
111         command.createArg().setValue( VssConstants.FLAG_AUTORESPONSE_DEF );
112 
113         return command;
114     }
115 
116     public Commandline buildSetCurrentProjectCmdLine( VssScmProviderRepository repo )
117         throws ScmException
118     {
119         Commandline command = new Commandline();
120 
121         try
122         {
123             command.addSystemEnvironment();
124         }
125         catch ( Exception e )
126         {
127             throw new ScmException( "Can't add system environment.", e );
128         }
129 
130         command.addEnvironment( "SSDIR", repo.getVssdir() );
131 
132         String ssDir = VssCommandLineUtils.getSsDir();
133 
134         command.setExecutable( ssDir + VssConstants.SS_EXE );
135 
136         command.createArg().setValue( VssConstants.COMMAND_CP );
137 
138         command.createArg().setValue( VssConstants.PROJECT_PREFIX + repo.getProject() );
139 
140         //User identification to get access to vss repository
141         if ( repo.getUserPassword() != null )
142         {
143             command.createArg().setValue( VssConstants.FLAG_LOGIN + repo.getUserPassword() );
144         }
145 
146         //Ignore: Do not ask for input under any circumstances.
147         command.createArg().setValue( VssConstants.FLAG_AUTORESPONSE_DEF );
148 
149         return command;
150     }
151 
152 }