1 | /*
|
---|
2 | * Copyright 2001-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.ccm;
|
---|
19 |
|
---|
20 |
|
---|
21 | import java.io.File;
|
---|
22 | import java.util.Vector;
|
---|
23 | import org.apache.tools.ant.BuildException;
|
---|
24 | import org.apache.tools.ant.DirectoryScanner;
|
---|
25 | import org.apache.tools.ant.Project;
|
---|
26 | import org.apache.tools.ant.taskdefs.Execute;
|
---|
27 | import org.apache.tools.ant.types.Commandline;
|
---|
28 | import org.apache.tools.ant.types.FileSet;
|
---|
29 |
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * Class common to all check commands (checkout, checkin,checkin default task);
|
---|
33 | * @ant.task ignore="true"
|
---|
34 | */
|
---|
35 | public class CCMCheck extends Continuus {
|
---|
36 |
|
---|
37 | private File file = null;
|
---|
38 | private String comment = null;
|
---|
39 | private String task = null;
|
---|
40 |
|
---|
41 | protected Vector filesets = new Vector();
|
---|
42 |
|
---|
43 | public CCMCheck() {
|
---|
44 | super();
|
---|
45 | }
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * Get the value of file.
|
---|
49 | * @return value of file.
|
---|
50 | */
|
---|
51 | public File getFile() {
|
---|
52 | return file;
|
---|
53 | }
|
---|
54 |
|
---|
55 | /**
|
---|
56 | * Sets the path to the file that the command will operate on.
|
---|
57 | * @param v Value to assign to file.
|
---|
58 | */
|
---|
59 | public void setFile(File v) {
|
---|
60 | log("working file " + v, Project.MSG_VERBOSE);
|
---|
61 | this.file = v;
|
---|
62 | }
|
---|
63 |
|
---|
64 | /**
|
---|
65 | * Get the value of comment.
|
---|
66 | * @return value of comment.
|
---|
67 | */
|
---|
68 | public String getComment() {
|
---|
69 | return comment;
|
---|
70 | }
|
---|
71 |
|
---|
72 | /**
|
---|
73 | * Specifies a comment.
|
---|
74 | * @param v Value to assign to comment.
|
---|
75 | */
|
---|
76 | public void setComment(String v) {
|
---|
77 | this.comment = v;
|
---|
78 | }
|
---|
79 |
|
---|
80 |
|
---|
81 | /**
|
---|
82 | * Get the value of task.
|
---|
83 | * @return value of task.
|
---|
84 | */
|
---|
85 | public String getTask() {
|
---|
86 | return task;
|
---|
87 | }
|
---|
88 |
|
---|
89 | /**
|
---|
90 | * Specifies the task number used to check
|
---|
91 | * in the file (may use 'default').
|
---|
92 | * @param v Value to assign to task.
|
---|
93 | */
|
---|
94 | public void setTask(String v) {
|
---|
95 | this.task = v;
|
---|
96 | }
|
---|
97 |
|
---|
98 |
|
---|
99 | /**
|
---|
100 | * Adds a set of files to copy.
|
---|
101 | */
|
---|
102 | public void addFileset(FileSet set) {
|
---|
103 | filesets.addElement(set);
|
---|
104 | }
|
---|
105 |
|
---|
106 |
|
---|
107 | /**
|
---|
108 | * Executes the task.
|
---|
109 | * <p>
|
---|
110 | * Builds a command line to execute ccm and then calls Exec's run method
|
---|
111 | * to execute the command line.
|
---|
112 | * </p>
|
---|
113 | */
|
---|
114 | public void execute() throws BuildException {
|
---|
115 |
|
---|
116 | if (file == null && filesets.size() == 0) {
|
---|
117 | throw new BuildException(
|
---|
118 | "Specify at least one source - a file or a fileset.");
|
---|
119 | }
|
---|
120 |
|
---|
121 | if (file != null && file.exists() && file.isDirectory()) {
|
---|
122 | throw new BuildException("CCMCheck cannot be generated for directories");
|
---|
123 | }
|
---|
124 |
|
---|
125 | if (file != null && filesets.size() > 0) {
|
---|
126 | throw new BuildException("Choose between file and fileset !");
|
---|
127 | }
|
---|
128 |
|
---|
129 | if (getFile() != null) {
|
---|
130 | doit();
|
---|
131 | return;
|
---|
132 | }
|
---|
133 |
|
---|
134 | int sizeofFileSet = filesets.size();
|
---|
135 | for (int i = 0; i < sizeofFileSet; i++) {
|
---|
136 | FileSet fs = (FileSet) filesets.elementAt(i);
|
---|
137 | DirectoryScanner ds = fs.getDirectoryScanner(getProject());
|
---|
138 | String[] srcFiles = ds.getIncludedFiles();
|
---|
139 | for (int j = 0; j < srcFiles.length; j++) {
|
---|
140 | File src = new File(fs.getDir(getProject()), srcFiles[j]);
|
---|
141 | setFile(src);
|
---|
142 | doit();
|
---|
143 | }
|
---|
144 | }
|
---|
145 | }
|
---|
146 |
|
---|
147 | /**
|
---|
148 | * check the file given by getFile().
|
---|
149 | */
|
---|
150 | private void doit() {
|
---|
151 | Commandline commandLine = new Commandline();
|
---|
152 |
|
---|
153 | // build the command line from what we got the format is
|
---|
154 | // ccm co /t .. files
|
---|
155 | // as specified in the CCM.EXE help
|
---|
156 |
|
---|
157 | commandLine.setExecutable(getCcmCommand());
|
---|
158 | commandLine.createArgument().setValue(getCcmAction());
|
---|
159 |
|
---|
160 | checkOptions(commandLine);
|
---|
161 |
|
---|
162 | int result = run(commandLine);
|
---|
163 | if (Execute.isFailure(result)) {
|
---|
164 | String msg = "Failed executing: " + commandLine.toString();
|
---|
165 | throw new BuildException(msg, getLocation());
|
---|
166 | }
|
---|
167 | }
|
---|
168 |
|
---|
169 |
|
---|
170 | /**
|
---|
171 | * Check the command line options.
|
---|
172 | */
|
---|
173 | private void checkOptions(Commandline cmd) {
|
---|
174 | if (getComment() != null) {
|
---|
175 | cmd.createArgument().setValue(FLAG_COMMENT);
|
---|
176 | cmd.createArgument().setValue(getComment());
|
---|
177 | }
|
---|
178 |
|
---|
179 | if (getTask() != null) {
|
---|
180 | cmd.createArgument().setValue(FLAG_TASK);
|
---|
181 | cmd.createArgument().setValue(getTask());
|
---|
182 | }
|
---|
183 |
|
---|
184 | if (getFile() != null) {
|
---|
185 | cmd.createArgument().setValue(file.getAbsolutePath());
|
---|
186 | }
|
---|
187 | }
|
---|
188 |
|
---|
189 | /**
|
---|
190 | * -comment flag -- comment to attach to the file
|
---|
191 | */
|
---|
192 | public static final String FLAG_COMMENT = "/comment";
|
---|
193 |
|
---|
194 | /**
|
---|
195 | * -task flag -- associate checkout task with task
|
---|
196 | */
|
---|
197 | public static final String FLAG_TASK = "/task";
|
---|
198 | }
|
---|
199 |
|
---|