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 public class ClearCaseAddCommand
40 extends AbstractAddCommand
41 implements ClearCaseCommand
42 {
43 protected ScmResult executeAddCommand( ScmProviderRepository scmProviderRepository, ScmFileSet scmFileSet,
44 String string, boolean b )
45 throws ScmException
46 {
47 getLogger().debug( "executing add command..." );
48 Commandline cl = createCommandLine( scmFileSet );
49
50 ClearCaseAddConsumer consumer = new ClearCaseAddConsumer( getLogger() );
51
52 CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
53
54 int exitCode;
55
56 try
57 {
58
59 Commandline checkoutCurrentDirCommandLine =
60 ClearCaseEditCommand.createCheckoutCurrentDirCommandLine( scmFileSet );
61 getLogger().debug( "Executing: " + checkoutCurrentDirCommandLine.getWorkingDirectory().getAbsolutePath() +
62 ">>" + checkoutCurrentDirCommandLine.toString() );
63 exitCode = CommandLineUtils.executeCommandLine( checkoutCurrentDirCommandLine,
64 new CommandLineUtils.StringStreamConsumer(), stderr );
65
66 if ( exitCode == 0 )
67 {
68
69 getLogger().debug( "Executing: " + cl.getWorkingDirectory().getAbsolutePath() + ">>" + cl.toString() );
70 exitCode = CommandLineUtils.executeCommandLine( cl, consumer, stderr );
71
72 if ( exitCode == 0 )
73 {
74
75 Commandline checkinCurrentDirCommandLine =
76 ClearCaseEditCommand.createCheckinCurrentDirCommandLine( scmFileSet );
77 getLogger().debug( "Executing: " +
78 checkinCurrentDirCommandLine.getWorkingDirectory().getAbsolutePath() + ">>" +
79 checkinCurrentDirCommandLine.toString() );
80 exitCode = CommandLineUtils.executeCommandLine( checkinCurrentDirCommandLine,
81 new CommandLineUtils.StringStreamConsumer(),
82 stderr );
83 }
84 }
85 }
86 catch ( CommandLineException ex )
87 {
88 throw new ScmException( "Error while executing clearcase command.", ex );
89 }
90
91 if ( exitCode != 0 )
92 {
93 return new StatusScmResult( cl.toString(), "The cleartool command failed.", stderr.getOutput(), false );
94 }
95
96 return new StatusScmResult( cl.toString(), consumer.getAddedFiles() );
97 }
98
99
100
101
102
103 public static Commandline createCommandLine( ScmFileSet scmFileSet )
104 {
105 Commandline command = new Commandline();
106
107 File workingDirectory = scmFileSet.getBasedir();
108
109 command.setWorkingDirectory( workingDirectory.getAbsolutePath() );
110
111 command.setExecutable( "cleartool" );
112
113 command.createArgument().setValue( "mkelem" );
114
115 command.createArgument().setValue( "-c" );
116
117 command.createArgument().setValue( "new file" );
118
119 command.createArgument().setValue( "-nco" );
120
121 File[] files = scmFileSet.getFiles();
122 for ( int i = 0; i < files.length; i++ )
123 {
124 File file = files[i];
125 command.createArgument().setValue( file.getName() );
126 }
127
128 return command;
129 }
130
131 }