1 package org.apache.maven.scm.provider.starteam.command.update;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.scm.ScmException;
23 import org.apache.maven.scm.ScmFileSet;
24 import org.apache.maven.scm.ScmVersion;
25 import org.apache.maven.scm.command.changelog.ChangeLogCommand;
26 import org.apache.maven.scm.command.update.AbstractUpdateCommand;
27 import org.apache.maven.scm.command.update.UpdateScmResult;
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.command.changelog.StarteamChangeLogCommand;
32 import org.apache.maven.scm.provider.starteam.command.checkout.StarteamCheckOutConsumer;
33 import org.apache.maven.scm.provider.starteam.repository.StarteamScmProviderRepository;
34 import org.codehaus.plexus.util.StringUtils;
35 import org.codehaus.plexus.util.cli.CommandLineUtils;
36 import org.codehaus.plexus.util.cli.Commandline;
37 import org.codehaus.plexus.util.cli.DefaultConsumer;
38 import org.codehaus.plexus.util.cli.StreamConsumer;
39
40 import java.io.File;
41 import java.util.ArrayList;
42 import java.util.List;
43
44
45
46
47
48 public class StarteamUpdateCommand
49 extends AbstractUpdateCommand
50 implements StarteamCommand
51 {
52
53
54
55
56
57 protected UpdateScmResult executeUpdateCommand( ScmProviderRepository repo, ScmFileSet fileSet, ScmVersion version )
58 throws ScmException
59 {
60 if ( getLogger().isInfoEnabled() )
61 {
62 getLogger().info( "Working directory: " + fileSet.getBasedir().getAbsolutePath() );
63 }
64
65 StarteamScmProviderRepository repository = (StarteamScmProviderRepository) repo;
66
67 StarteamCheckOutConsumer consumer = new StarteamCheckOutConsumer( getLogger(), fileSet.getBasedir() );
68
69 CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
70
71 List<File> updateFiles = fileSet.getFileList();
72
73 if ( updateFiles.size() == 0 )
74 {
75
76 Commandline cl = createCommandLine( repository, fileSet, version );
77
78 int exitCode = StarteamCommandLineUtils.executeCommandline( cl, consumer, stderr, getLogger() );
79
80 if ( exitCode != 0 )
81 {
82 return new UpdateScmResult( cl.toString(), "The starteam command failed.", stderr.getOutput(), false );
83 }
84 else
85 {
86
87
88 String doDeleteLocal = System.getProperty( "maven.scm.starteam.deleteLocal" );
89
90 if ( "true".equalsIgnoreCase( doDeleteLocal ) )
91 {
92 this.deleteLocal( repository, fileSet, version );
93 }
94 }
95 }
96 else
97 {
98
99 for ( int i = 0; i < updateFiles.size(); ++i )
100 {
101 File updateFile = (File) updateFiles.get( i );
102 ScmFileSet scmFileSet = new ScmFileSet( fileSet.getBasedir(), updateFile );
103 Commandline cl = createCommandLine( repository, scmFileSet, version );
104
105 int exitCode = StarteamCommandLineUtils.executeCommandline( cl, consumer, stderr, getLogger() );
106
107 if ( exitCode != 0 )
108 {
109 return new UpdateScmResult( cl.toString(), "The starteam command failed.", stderr.getOutput(),
110 false );
111 }
112 }
113 }
114
115 return new UpdateScmResult( null, consumer.getCheckedOutFiles() );
116
117 }
118
119
120
121
122
123 public static Commandline createCommandLine( StarteamScmProviderRepository repo, ScmFileSet fileSet,
124 ScmVersion version )
125 {
126 List<String> args = new ArrayList<String>();
127
128 args.add( "-merge" );
129 args.add( "-neverprompt" );
130
131 if ( version != null && StringUtils.isNotEmpty( version.getName() ) )
132 {
133 args.add( "-vl" );
134 args.add( version.getName() );
135 }
136
137 StarteamCommandLineUtils.addEOLOption( args );
138
139 return StarteamCommandLineUtils.createStarteamCommandLine( "co", args, fileSet, repo );
140 }
141
142
143 protected ChangeLogCommand getChangeLogCommand()
144 {
145 StarteamChangeLogCommand command = new StarteamChangeLogCommand();
146
147 command.setLogger( getLogger() );
148
149 return command;
150 }
151
152 private void deleteLocal( StarteamScmProviderRepository repo, ScmFileSet fileSet, ScmVersion version )
153 throws ScmException
154 {
155 if ( fileSet.getFileList().size() != 0 )
156 {
157 return;
158 }
159
160 Commandline cl = createDeleteLocalCommand( repo, fileSet, version );
161
162 StreamConsumer consumer = new DefaultConsumer();
163
164 CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
165
166 int exitCode = StarteamCommandLineUtils.executeCommandline( cl, consumer, stderr, getLogger() );
167
168 if ( exitCode != 0 )
169 {
170 throw new ScmException( "Error executing delete-local: " + stderr.toString() );
171 }
172 }
173
174 public static Commandline createDeleteLocalCommand( StarteamScmProviderRepository repo, ScmFileSet dir,
175 ScmVersion version )
176 {
177 List<String> args = new ArrayList<String>();
178
179 if ( version != null && StringUtils.isNotEmpty( version.getName() ) )
180 {
181 args.add( "-cfgl" );
182 args.add( version.getName() );
183 }
184
185 args.add( "-filter" );
186 args.add( "N" );
187
188 return StarteamCommandLineUtils.createStarteamCommandLine( "delete-local", args, dir, repo );
189 }
190
191 }