aboutsummaryrefslogtreecommitdiffstats
path: root/framework/src/ant/apache-ant-1.9.6/src/main/org/apache/tools/ant/taskdefs/optional/clearcase/ClearCase.java
blob: d81e505cd2c6422cbccb3fee79899c28c21bb2c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */

package org.apache.tools.ant.taskdefs.optional.clearcase;

import java.io.File;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.taskdefs.ExecTask;
import org.apache.tools.ant.taskdefs.Execute;
import org.apache.tools.ant.taskdefs.LogStreamHandler;
import org.apache.tools.ant.types.Commandline;
import org.apache.tools.ant.util.FileUtils;



/**
 * A base class for creating tasks for executing commands on ClearCase.
 * <p>
 * The class extends the 'exec' task as it operates by executing the cleartool program
 * supplied with ClearCase. By default the task expects the cleartool executable to be
 * in the path, * you can override this be specifying the cleartooldir attribute.
 * </p>
 * <p>
 * This class provides set and get methods for the 'viewpath' and 'objselect'
 * attribute. It also contains constants for the flags that can be passed to
 * cleartool.
 * </p>
 *
 */
public abstract class ClearCase extends Task {
    private String mClearToolDir = "";
    private String mviewPath = null;
    private String mobjSelect = null;
    private static int pcnt = 0;
    private boolean mFailonerr = true;
    /**
     * Set the directory where the cleartool executable is located.
     *
     * @param dir the directory containing the cleartool executable
     */
    public final void setClearToolDir(String dir) {
        mClearToolDir = FileUtils.translatePath(dir);
    }

    /**
     * Builds and returns the command string to execute cleartool
     *
     * @return String containing path to the executable
     */
    protected final String getClearToolCommand() {
        String toReturn = mClearToolDir;
        if (!toReturn.equals("") && !toReturn.endsWith("/")) {
            toReturn += "/";
        }

        toReturn += CLEARTOOL_EXE;

        return toReturn;
    }

    /**
     * Set the path to the item in a ClearCase view to operate on.
     *
     * @param viewPath Path to the view directory or file
     */
    public final void setViewPath(String viewPath) {
        mviewPath = viewPath;
    }

    /**
     * Get the path to the item in a clearcase view
     *
     * @return mviewPath
     */
    public String getViewPath() {
        return mviewPath;
    }

    /**
     * Get the basename path of the item in a clearcase view
     *
     * @return basename
     */
    public String getViewPathBasename() {
        return (new File(mviewPath)).getName();
    }

    /**
     * Set the object to operate on.
     *
     * @param objSelect object to operate on
     */
    public final void setObjSelect(String objSelect) {
        mobjSelect = objSelect;
    }

    /**
     * Get the object to operate on
     *
     * @return mobjSelect
     */
    public String getObjSelect() {
        return mobjSelect;
    }

    /**
     * Execute the given command are return success or failure
     * @param cmd command line to execute
     * @return the exit status of the subprocess or <code>INVALID</code>
     */
    protected int run(Commandline cmd) {
        try {
            Project aProj = getProject();
            Execute exe
                = new Execute(new LogStreamHandler(this, Project.MSG_INFO, Project.MSG_WARN));
            exe.setAntRun(aProj);
            exe.setWorkingDirectory(aProj.getBaseDir());
            exe.setCommandline(cmd.getCommandline());
            return exe.execute();
        } catch (java.io.IOException e) {
            throw new BuildException(e, getLocation());
        }
    }

    /**
     * Execute the given command, and return it's output
     * @param cmdline command line to execute
     * @return output of the command line
     */
    protected String runS(Commandline cmdline) {
        String   outV  = "opts.cc.runS.output" + pcnt++;
        ExecTask exe   = new ExecTask(this);
        Commandline.Argument arg = exe.createArg();

        exe.setExecutable(cmdline.getExecutable());
        arg.setLine(Commandline.toString(cmdline.getArguments()));
        exe.setOutputproperty(outV);
        exe.execute();

        return getProject().getProperty(outV);
    }
    /**
     * If true, command will throw an exception on failure.
     *
     * @param failonerr the status to set the flag to
     * @since ant 1.6.1
     */
    public void setFailOnErr(boolean failonerr) {
        mFailonerr = failonerr;
    }

    /**
     * Get failonerr flag status
     *
     * @return boolean containing status of failonerr flag
     * @since ant 1.6.1
     */
    public boolean getFailOnErr() {
        return mFailonerr;
    }

    /**
     * Constant for the thing to execute
     */
    private static final String CLEARTOOL_EXE = "cleartool";
    /**
     * The 'Update' command
     */
    public static final String COMMAND_UPDATE = "update";
    /**
     * The 'Checkout' command
     */
    public static final String COMMAND_CHECKOUT = "checkout";
    /**
     * The 'Checkin' command
     */
    public static final String COMMAND_CHECKIN = "checkin";
    /**
     * The 'UndoCheckout' command
     */
    public static final String COMMAND_UNCHECKOUT = "uncheckout";
    /**
     * The 'Lock' command
     */
    public static final String COMMAND_LOCK = "lock";
    /**
     * The 'Unlock' command
     */
    public static final String COMMAND_UNLOCK = "unlock";
    /**
     * The 'Mkbl' command
     */
    public static final String COMMAND_MKBL = "mkbl";
    /**
     * The 'Mklabel' command
     */
    public static final String COMMAND_MKLABEL = "mklabel";
    /**
     * The 'Mklbtype' command
     */
    public static final String COMMAND_MKLBTYPE = "mklbtype";
    /**
     * The 'Rmtype' command
     */
    public static final String COMMAND_RMTYPE = "rmtype";
    /**
     * The 'LsCheckout' command
     */
    public static final String COMMAND_LSCO = "lsco";
    /**
     * The 'Mkelem' command
     */
    public static final String COMMAND_MKELEM = "mkelem";
    /**
     * The 'Mkattr' command
     */
    public static final String COMMAND_MKATTR = "mkattr";
    /**
     * The 'Mkdir' command
     */
    public static final String COMMAND_MKDIR = "mkdir";

}