1 package org.apache.maven.scm.provider.clearcase.command.add;
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.ScmResult;
25 import org.apache.maven.scm.command.add.AbstractAddCommand;
26 import org.apache.maven.scm.command.status.StatusScmResult;
27 import org.apache.maven.scm.provider.ScmProviderRepository;
28 import org.apache.maven.scm.provider.clearcase.command.ClearCaseCommand;
29 import org.apache.maven.scm.provider.clearcase.command.edit.ClearCaseEditCommand;
30 import org.codehaus.plexus.util.cli.CommandLineException;
31 import org.codehaus.plexus.util.cli.CommandLineUtils;
32 import org.codehaus.plexus.util.cli.Commandline;
33
34 import java.io.File;
35
36
37
38
39
40 public class ClearCaseAddCommand
41 extends AbstractAddCommand
42 implements ClearCaseCommand
43 {
44
45 protected ScmResult executeAddCommand( ScmProviderRepository scmProviderRepository, ScmFileSet scmFileSet,
46 String string, boolean b )
47 throws ScmException
48 {
49 if ( getLogger().isDebugEnabled() )
50 {
51 getLogger().debug( "executing add command..." );
52 }
53 Commandline cl = createCommandLine( scmFileSet );
54
55 ClearCaseAddConsumer consumer = new ClearCaseAddConsumer( getLogger() );
56
57 CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
58
59 int exitCode;
60
61 try
62 {
63
64 Commandline checkoutCurrentDirCommandLine =
65 ClearCaseEditCommand.createCheckoutCurrentDirCommandLine( scmFileSet );
66 if ( getLogger().isDebugEnabled() )
67 {
68 getLogger().debug(
69 "Executing: "
70 + checkoutCurrentDirCommandLine.getWorkingDirectory().getAbsolutePath()
71 + ">>" + checkoutCurrentDirCommandLine.toString() );
72 }
73 exitCode = CommandLineUtils.executeCommandLine( checkoutCurrentDirCommandLine,
74 new CommandLineUtils.StringStreamConsumer(), stderr );
75
76 if ( exitCode == 0 )
77 {
78
79 if ( getLogger().isDebugEnabled() )
80 {
81 getLogger().debug(
82 "Executing: " + cl.getWorkingDirectory().getAbsolutePath() + ">>"
83 + cl.toString() );
84 }
85 exitCode = CommandLineUtils.executeCommandLine( cl, consumer, stderr );
86
87 if ( exitCode == 0 )
88 {
89
90 Commandline checkinCurrentDirCommandLine =
91 ClearCaseEditCommand.createCheckinCurrentDirCommandLine( scmFileSet );
92 if ( getLogger().isDebugEnabled() )
93 {
94 getLogger().debug(
95 "Executing: "
96 + checkinCurrentDirCommandLine.getWorkingDirectory().getAbsolutePath()
97 + ">>" + checkinCurrentDirCommandLine.toString() );
98 }
99 exitCode = CommandLineUtils.executeCommandLine( checkinCurrentDirCommandLine,
100 new CommandLineUtils.StringStreamConsumer(),
101 stderr );
102 }
103 }
104 }
105 catch ( CommandLineException ex )
106 {
107 throw new ScmException( "Error while executing clearcase command.", ex );
108 }
109
110 if ( exitCode != 0 )
111 {
112 return new StatusScmResult( cl.toString(), "The cleartool command failed.", stderr.getOutput(), false );
113 }
114
115 return new StatusScmResult( cl.toString(), consumer.getAddedFiles() );
116 }
117
118
119
120
121
122 public static Commandline createCommandLine( ScmFileSet scmFileSet )
123 {
124 Commandline command = new Commandline();
125
126 File workingDirectory = scmFileSet.getBasedir();
127
128 command.setWorkingDirectory( workingDirectory.getAbsolutePath() );
129
130 command.setExecutable( "cleartool" );
131
132 command.createArg().setValue( "mkelem" );
133
134 command.createArg().setValue( "-c" );
135
136 command.createArg().setValue( "new file" );
137
138 command.createArg().setValue( "-nco" );
139
140 for ( File file : scmFileSet.getFileList() )
141 {
142 command.createArg().setValue( file.getName() );
143 }
144
145 return command;
146 }
147
148 }