source: other-projects/trunk/gs3-release-maker/apache-ant-1.6.5/src/main/org/apache/tools/ant/taskdefs/Patch.java@ 14627

Last change on this file since 14627 was 14627, checked in by oranfry, 17 years ago

initial import of the gs3-release-maker

File size: 5.8 KB
Line 
1/*
2 * Copyright 2000,2002-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
18package org.apache.tools.ant.taskdefs;
19
20import java.io.File;
21import java.io.IOException;
22import org.apache.tools.ant.BuildException;
23import org.apache.tools.ant.Project;
24import org.apache.tools.ant.Task;
25import org.apache.tools.ant.types.Commandline;
26
27/**
28 * Patches a file by applying a 'diff' file to it; requires "patch" to be
29 * on the execution path.
30 *
31 * @since Ant 1.1
32 *
33 * @ant.task category="utility"
34 */
35public class Patch extends Task {
36
37 private File originalFile;
38 private File directory;
39 private boolean havePatchfile = false;
40 private Commandline cmd = new Commandline();
41
42 /**
43 * The file to patch; optional if it can be inferred from
44 * the diff file
45 * @param file the file to patch
46 */
47 public void setOriginalfile(File file) {
48 originalFile = file;
49 }
50
51 /**
52 * The name of a file to send the output to, instead of patching
53 * the file(s) in place; optional.
54 * @param file the file to send the output to
55 * @since Ant 1.6
56 */
57 public void setDestfile(File file) {
58 if (file != null) {
59 cmd.createArgument().setValue("-o");
60 cmd.createArgument().setFile(file);
61 }
62 }
63
64 /**
65 * The file containing the diff output; required.
66 * @param file the file containing the diff output
67 */
68 public void setPatchfile(File file) {
69 if (!file.exists()) {
70 throw new BuildException("patchfile " + file + " doesn\'t exist",
71 getLocation());
72 }
73 cmd.createArgument().setValue("-i");
74 cmd.createArgument().setFile(file);
75 havePatchfile = true;
76 }
77
78 /**
79 * flag to create backups; optional, default=false
80 * @param backups if true create backups
81 */
82 public void setBackups(boolean backups) {
83 if (backups) {
84 cmd.createArgument().setValue("-b");
85 }
86 }
87
88 /**
89 * flag to ignore whitespace differences; default=false
90 * @param ignore if true ignore whitespace differences
91 */
92 public void setIgnorewhitespace(boolean ignore) {
93 if (ignore) {
94 cmd.createArgument().setValue("-l");
95 }
96 }
97
98 /**
99 * Strip the smallest prefix containing <i>num</i> leading slashes
100 * from filenames.
101 *
102 * <p>patch's <i>-p</i> option.
103 * @param num number of lines to strip
104 * @exception BuildException if num is < 0, or other errors
105 */
106 public void setStrip(int num) throws BuildException {
107 if (num < 0) {
108 throw new BuildException("strip has to be >= 0", getLocation());
109 }
110 cmd.createArgument().setValue("-p" + num);
111 }
112
113 /**
114 * Work silently unless an error occurs; optional, default=false
115 * @param q if true suppress set the -s option on the patch command
116 */
117 public void setQuiet(boolean q) {
118 if (q) {
119 cmd.createArgument().setValue("-s");
120 }
121 }
122
123 /**
124 * Assume patch was created with old and new files swapped; optional,
125 * default=false
126 * @param r if true set the -R option on the patch command
127 */
128 public void setReverse(boolean r) {
129 if (r) {
130 cmd.createArgument().setValue("-R");
131 }
132 }
133
134 /**
135 * The directory to run the patch command in, defaults to the
136 * project's base directory.
137 * @param directory the directory to run the patch command in
138 * @since Ant 1.5
139 */
140 public void setDir(File directory) {
141 this.directory = directory;
142 }
143
144 /**
145 * execute patch
146 * @throws BuildException when it all goes a bit pear shaped
147 */
148 public void execute() throws BuildException {
149 if (!havePatchfile) {
150 throw new BuildException("patchfile argument is required",
151 getLocation());
152 }
153 Commandline toExecute = (Commandline) cmd.clone();
154 toExecute.setExecutable("patch");
155
156 if (originalFile != null) {
157 toExecute.createArgument().setFile(originalFile);
158 }
159
160 Execute exe = new Execute(new LogStreamHandler(this, Project.MSG_INFO,
161 Project.MSG_WARN),
162 null);
163 exe.setCommandline(toExecute.getCommandline());
164
165 if (directory != null) {
166 if (directory.exists() && directory.isDirectory()) {
167 exe.setWorkingDirectory(directory);
168 } else if (!directory.isDirectory()) {
169 throw new BuildException(directory + " is not a directory.",
170 getLocation());
171 } else {
172 throw new BuildException("directory " + directory
173 + " doesn\'t exist", getLocation());
174 }
175 } else {
176 exe.setWorkingDirectory(getProject().getBaseDir());
177 }
178
179 log(toExecute.describeCommand(), Project.MSG_VERBOSE);
180 try {
181 exe.execute();
182 } catch (IOException e) {
183 throw new BuildException(e, getLocation());
184 }
185 }
186}
Note: See TracBrowser for help on using the repository browser.