1 | /*
|
---|
2 | * Copyright 2000-2004 The Apache Software Foundation
|
---|
3 | *
|
---|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
5 | * you may not use this file except in compliance with the License.
|
---|
6 | * You may obtain a copy of the License at
|
---|
7 | *
|
---|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
9 | *
|
---|
10 | * Unless required by applicable law or agreed to in writing, software
|
---|
11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
13 | * See the License for the specific language governing permissions and
|
---|
14 | * limitations under the License.
|
---|
15 | *
|
---|
16 | */
|
---|
17 |
|
---|
18 | package org.apache.tools.ant.taskdefs.optional.clearcase;
|
---|
19 |
|
---|
20 | import java.io.File;
|
---|
21 | import org.apache.tools.ant.BuildException;
|
---|
22 | import org.apache.tools.ant.Project;
|
---|
23 | import org.apache.tools.ant.Task;
|
---|
24 | import org.apache.tools.ant.taskdefs.ExecTask;
|
---|
25 | import org.apache.tools.ant.taskdefs.Execute;
|
---|
26 | import org.apache.tools.ant.taskdefs.LogStreamHandler;
|
---|
27 | import org.apache.tools.ant.types.Commandline;
|
---|
28 |
|
---|
29 |
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * A base class for creating tasks for executing commands on ClearCase.
|
---|
33 | * <p>
|
---|
34 | * The class extends the 'exec' task as it operates by executing the cleartool program
|
---|
35 | * supplied with ClearCase. By default the task expects the cleartool executable to be
|
---|
36 | * in the path, * you can override this be specifying the cleartooldir attribute.
|
---|
37 | * </p>
|
---|
38 | * <p>
|
---|
39 | * This class provides set and get methods for the 'viewpath' and 'objselect'
|
---|
40 | * attribute. It also contains constants for the flags that can be passed to
|
---|
41 | * cleartool.
|
---|
42 | * </p>
|
---|
43 | *
|
---|
44 | */
|
---|
45 | public abstract class ClearCase extends Task {
|
---|
46 | private String mClearToolDir = "";
|
---|
47 | private String mviewPath = null;
|
---|
48 | private String mobjSelect = null;
|
---|
49 | private static int pcnt = 0;
|
---|
50 | private boolean mFailonerr = true;
|
---|
51 | /**
|
---|
52 | * Set the directory where the cleartool executable is located.
|
---|
53 | *
|
---|
54 | * @param dir the directory containing the cleartool executable
|
---|
55 | */
|
---|
56 | public final void setClearToolDir(String dir) {
|
---|
57 | mClearToolDir = getProject().translatePath(dir);
|
---|
58 | }
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * Builds and returns the command string to execute cleartool
|
---|
62 | *
|
---|
63 | * @return String containing path to the executable
|
---|
64 | */
|
---|
65 | protected final String getClearToolCommand() {
|
---|
66 | String toReturn = mClearToolDir;
|
---|
67 | if (!toReturn.equals("") && !toReturn.endsWith("/")) {
|
---|
68 | toReturn += "/";
|
---|
69 | }
|
---|
70 |
|
---|
71 | toReturn += CLEARTOOL_EXE;
|
---|
72 |
|
---|
73 | return toReturn;
|
---|
74 | }
|
---|
75 |
|
---|
76 | /**
|
---|
77 | * Set the path to the item in a ClearCase view to operate on.
|
---|
78 | *
|
---|
79 | * @param viewPath Path to the view directory or file
|
---|
80 | */
|
---|
81 | public final void setViewPath(String viewPath) {
|
---|
82 | mviewPath = viewPath;
|
---|
83 | }
|
---|
84 |
|
---|
85 | /**
|
---|
86 | * Get the path to the item in a clearcase view
|
---|
87 | *
|
---|
88 | * @return mviewPath
|
---|
89 | */
|
---|
90 | public String getViewPath() {
|
---|
91 | return mviewPath;
|
---|
92 | }
|
---|
93 |
|
---|
94 | /**
|
---|
95 | * Get the basename path of the item in a clearcase view
|
---|
96 | *
|
---|
97 | * @return basename
|
---|
98 | */
|
---|
99 | public String getViewPathBasename() {
|
---|
100 | return (new File(mviewPath)).getName();
|
---|
101 | }
|
---|
102 |
|
---|
103 | /**
|
---|
104 | * Set the object to operate on.
|
---|
105 | *
|
---|
106 | * @param objSelect object to operate on
|
---|
107 | */
|
---|
108 | public final void setObjSelect(String objSelect) {
|
---|
109 | mobjSelect = objSelect;
|
---|
110 | }
|
---|
111 |
|
---|
112 | /**
|
---|
113 | * Get the object to operate on
|
---|
114 | *
|
---|
115 | * @return mobjSelect
|
---|
116 | */
|
---|
117 | public String getObjSelect() {
|
---|
118 | return mobjSelect;
|
---|
119 | }
|
---|
120 |
|
---|
121 | /**
|
---|
122 | * Execute the given command are return success or failure
|
---|
123 | * @param cmd command line to execute
|
---|
124 | * @return the exit status of the subprocess or <code>INVALID</code>
|
---|
125 | */
|
---|
126 | protected int run(Commandline cmd) {
|
---|
127 | try {
|
---|
128 | Project aProj = getProject();
|
---|
129 | Execute exe
|
---|
130 | = new Execute(new LogStreamHandler(this, Project.MSG_INFO, Project.MSG_WARN));
|
---|
131 | exe.setAntRun(aProj);
|
---|
132 | exe.setWorkingDirectory(aProj.getBaseDir());
|
---|
133 | exe.setCommandline(cmd.getCommandline());
|
---|
134 | return exe.execute();
|
---|
135 | } catch (java.io.IOException e) {
|
---|
136 | throw new BuildException(e, getLocation());
|
---|
137 | }
|
---|
138 | }
|
---|
139 |
|
---|
140 | /**
|
---|
141 | * Execute the given command, and return it's output
|
---|
142 | * @param cmdline command line to execute
|
---|
143 | * @return output of the command line
|
---|
144 | */
|
---|
145 | protected String runS(Commandline cmdline) {
|
---|
146 | String outV = "opts.cc.runS.output" + pcnt++;
|
---|
147 | Project aProj = getProject();
|
---|
148 | ExecTask exe = (ExecTask) aProj.createTask("exec");
|
---|
149 | Commandline.Argument arg = exe.createArg();
|
---|
150 |
|
---|
151 | exe.setExecutable(cmdline.getExecutable());
|
---|
152 | arg.setLine(cmdline.toString(cmdline.getArguments()));
|
---|
153 | exe.setOutputproperty(outV);
|
---|
154 | exe.execute();
|
---|
155 | // System.out.println( "runS: " + outV + " : " + aProj.getProperty( outV ));
|
---|
156 |
|
---|
157 | return aProj.getProperty(outV);
|
---|
158 | }
|
---|
159 | /**
|
---|
160 | * If true, command will throw an exception on failure.
|
---|
161 | *
|
---|
162 | * @param failonerr the status to set the flag to
|
---|
163 | * @since ant 1.6.1
|
---|
164 | */
|
---|
165 | public void setFailOnErr(boolean failonerr) {
|
---|
166 | mFailonerr = failonerr;
|
---|
167 | }
|
---|
168 |
|
---|
169 | /**
|
---|
170 | * Get failonerr flag status
|
---|
171 | *
|
---|
172 | * @return boolean containing status of failonerr flag
|
---|
173 | * @since ant 1.6.1
|
---|
174 | */
|
---|
175 | public boolean getFailOnErr() {
|
---|
176 | return mFailonerr;
|
---|
177 | }
|
---|
178 |
|
---|
179 | /**
|
---|
180 | * Constant for the thing to execute
|
---|
181 | */
|
---|
182 | private static final String CLEARTOOL_EXE = "cleartool";
|
---|
183 | /**
|
---|
184 | * The 'Update' command
|
---|
185 | */
|
---|
186 | public static final String COMMAND_UPDATE = "update";
|
---|
187 | /**
|
---|
188 | * The 'Checkout' command
|
---|
189 | */
|
---|
190 | public static final String COMMAND_CHECKOUT = "checkout";
|
---|
191 | /**
|
---|
192 | * The 'Checkin' command
|
---|
193 | */
|
---|
194 | public static final String COMMAND_CHECKIN = "checkin";
|
---|
195 | /**
|
---|
196 | * The 'UndoCheckout' command
|
---|
197 | */
|
---|
198 | public static final String COMMAND_UNCHECKOUT = "uncheckout";
|
---|
199 | /**
|
---|
200 | * The 'Lock' command
|
---|
201 | */
|
---|
202 | public static final String COMMAND_LOCK = "lock";
|
---|
203 | /**
|
---|
204 | * The 'Unlock' command
|
---|
205 | */
|
---|
206 | public static final String COMMAND_UNLOCK = "unlock";
|
---|
207 | /**
|
---|
208 | * The 'Mkbl' command
|
---|
209 | */
|
---|
210 | public static final String COMMAND_MKBL = "mkbl";
|
---|
211 | /**
|
---|
212 | * The 'Mklabel' command
|
---|
213 | */
|
---|
214 | public static final String COMMAND_MKLABEL = "mklabel";
|
---|
215 | /**
|
---|
216 | * The 'Mklbtype' command
|
---|
217 | */
|
---|
218 | public static final String COMMAND_MKLBTYPE = "mklbtype";
|
---|
219 | /**
|
---|
220 | * The 'Rmtype' command
|
---|
221 | */
|
---|
222 | public static final String COMMAND_RMTYPE = "rmtype";
|
---|
223 | /**
|
---|
224 | * The 'LsCheckout' command
|
---|
225 | */
|
---|
226 | public static final String COMMAND_LSCO = "lsco";
|
---|
227 | /**
|
---|
228 | * The 'Mkelem' command
|
---|
229 | */
|
---|
230 | public static final String COMMAND_MKELEM = "mkelem";
|
---|
231 | /**
|
---|
232 | * The 'Mkattr' command
|
---|
233 | */
|
---|
234 | public static final String COMMAND_MKATTR = "mkattr";
|
---|
235 | /**
|
---|
236 | * The 'Mkdir' command
|
---|
237 | */
|
---|
238 | public static final String COMMAND_MKDIR = "mkdir";
|
---|
239 |
|
---|
240 | }
|
---|
241 |
|
---|