Debian Codegraph @

src/main/java/net/sf/antcontrib/platform/ShellScriptTask.java

raw symbols SCIP
1 /*
2 * Copyright (c) 2001-2004 Ant-Contrib project. All rights reserved.
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 package net.sf.antcontrib.platform;
17
18
19 import java.io.File;
20 import java.io.FileOutputStream;
21
22 import org.apache.tools.ant.BuildException;
23 import org.apache.tools.ant.taskdefs.ExecTask;
24 import org.apache.tools.ant.types.Commandline;
25 import org.apache.tools.ant.util.FileUtils;
26
27 /**
28 * A generic front-end for passing "shell lines" to any application which can
29 * accept a filename containing script input (bash, perl, csh, tcsh, etc.).
30 * see antcontrib doc for useage
31 *
32 * @author stephan beal
33 *@author peter reilly
34 */
35
36 public class ShellScriptTask extends ExecTask {
37
38 private StringBuffer script = new StringBuffer();
39 private String shell = null;
40 private File tmpFile;
41 private String tmpSuffix = null;
42
43 /**
44 * Adds s to the lines of script code.
45 */
46 public void addText(String s) {
47 script.append(getProject().replaceProperties(s));
48 }
49
50 /**
51 * Sets script code to s.
52 */
53 public void setInputString(String s) {
54 script.append(s);
55 }
56
57 /**
58 * Sets the shell used to run the script.
59 * @param shell the shell to use (bash is default)
60 */
61 public void setShell(String shell) {
62 this.shell = shell;
63 }
64
65 /**
66 * Sets the shell used to run the script.
67 * @param shell the shell to use (bash is default)
68 */
69 public void setExecutable(String shell) {
70 this.shell = shell;
71 }
72
73 /**
74 * Disallow the command attribute of parent class ExecTask.
75 * ant.attribute ignore="true"
76 * @param notUsed not used
77 * @throws BuildException if called
78 */
79 public void setCommand(Commandline notUsed) {
80 throw new BuildException("Attribute command is not supported");
81 }
82
83
84 /**
85 * Sets the suffix for the tmp file used to
86 * contain the script.
87 * This is useful for cmd.exe as one can
88 * use cmd /c call x.bat
89 * @param tmpSuffix the suffix to use
90 */
91
92 public void setTmpSuffix(String tmpSuffix) {
93 this.tmpSuffix = tmpSuffix;
94 }
95
96 /**
97 * execute the task
98 */
99 public void execute() throws BuildException {
100 // Remove per peter's comments. Makes sense.
101 /*
102 if (shell == null)
103 {
104 // Get the default shell
105 shell = Platform.getDefaultShell();
106
107 // Get the default shell arguments
108 String args[] = Platform.getDefaultShellArguments();
109 for (int i=args.length-1;i>=0;i--)
110 this.cmdl.createArgument(true).setValue(args[i]);
111
112 // Get the default script suffix
113 if (tmpSuffix == null)
114 tmpSuffix = Platform.getDefaultScriptSuffix();
115
116 }
117 */
118 if (shell == null)
119 throw new BuildException("You must specify a shell to run.");
120
121 try {
122 /* // The following may be used when ant 1.6 is used.
123 if (tmpSuffix == null)
124 super.setInputString(script.toString());
125 else
126 */
127 {
128 writeScript();
129 super.createArg().setValue(tmpFile.getAbsolutePath());
130 }
131 super.setExecutable(shell);
132 super.execute();
133 }
134 finally {
135 if (tmpFile != null) {
136 if (! tmpFile.delete()) {
137 log("Non-fatal error: could not delete temporary file " +
138 tmpFile.getAbsolutePath());
139 }
140 }
141 }
142 }
143
144 /**
145 * Writes the script lines to a temp file.
146 */
147 protected void writeScript() throws BuildException {
148 FileOutputStream os = null;
149 try {
150 FileUtils fileUtils = FileUtils.newFileUtils();
151 // NB: use File.io.createTempFile whenever jdk 1.2 is allowed
152 tmpFile = fileUtils.createTempFile("script", tmpSuffix, null);
153 os = new java.io.FileOutputStream(tmpFile);
154 String string = script.toString();
155 os.write(string.getBytes(), 0, string.length());
156 os.close();
157 }
158 catch (Exception e) {
159 throw new BuildException(e);
160 }
161 finally {
162 try {os.close();} catch (Throwable t) {}
163 }
164 }
165
166 }
167