Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
 
3
/*
4
 *  $Id: ExecTask.php 334 2008-01-04 14:25:20Z hans $
5
 *
6
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
7
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
8
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
9
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
10
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
11
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
12
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
13
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
14
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
15
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
16
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
17
 *
18
 * This software consists of voluntary contributions made by many individuals
19
 * and is licensed under the LGPL. For more information please see
20
 * <http://phing.info>.
21
 */
22
 
23
require_once 'phing/Task.php';
24
 
25
/**
26
 * Executes a command on the shell.
27
 *
28
 * @author   Andreas Aderhold <andi@binarycloud.com>
29
 * @author   Hans Lellelid <hans@xmpl.org>
30
 * @version  $Revision: 1.17 $
31
 * @package  phing.tasks.system
32
 */
33
class ExecTask extends Task {
34
 
35
	/**
36
	 * Command to execute.
37
	 * @var string
38
	 */
39
	protected $command;
40
 
41
	/**
42
	 * Working directory.
43
	 * @var File
44
	 */
45
	protected $dir;
46
 
47
	/**
48
	 * Operating system.
49
	 * @var string
50
	 */
51
	protected $os;
52
 
53
	/**
54
	 * Whether to escape shell command using escapeshellcmd().
55
	 * @var boolean
56
	 */
57
	protected $escape = false;
58
 
59
	/**
60
	 * Where to direct output.
61
	 * @var File
62
	 */
63
	protected $output;
64
 
65
	/**
66
	 * Whether to passthru the output
67
	 * @var boolean
68
	 */
69
	protected $passthru = false;
70
 
71
	/**
72
	 * Where to direct error output.
73
	 * @var File
74
	 */
75
	protected $error;
76
 
77
	/**
78
	 * If spawn is set then [unix] programs will redirect stdout and add '&'.
79
	 * @var boolean
80
	 */
81
	protected $spawn = false;
82
 
83
	/**
84
	 * Property name to set with return value from exec call.
85
	 *
86
	 * @var string
87
	 */
88
	protected $returnProperty;
89
 
90
	/**
91
	 * Whether to check the return code.
92
	 * @var boolean
93
	 */
94
	protected $checkreturn = false;
95
 
96
	/**
97
	 * Main method: wraps execute() command.
98
	 * @return void
99
	 */
100
	public function main() {
101
		$this->execute();
102
	}
103
 
104
	/**
105
	 * Executes a program and returns the return code.
106
	 * Output from command is logged at INFO level.
107
	 * @return int Return code from execution.
108
	 */
109
	public function execute() {
110
 
111
		// test if os match
112
		$myos = Phing::getProperty("os.name");
113
		$this->log("Myos = " . $myos, Project::MSG_VERBOSE);
114
		if (($this->os !== null) && (strpos($this->os, $myos) === false)) {
115
			// this command will be executed only on the specified OS
116
			$this->log("Not found in " . $this->os, Project::MSG_VERBOSE);
117
			return 0;
118
		}
119
 
120
		if ($this->dir !== null) {
121
			if ($this->dir->isDirectory()) {
122
				$currdir = getcwd();
123
				@chdir($this->dir->getPath());
124
			} else {
125
				throw new BuildException("Can't chdir to:" . $this->dir->__toString());
126
			}
127
		}
128
 
129
 
130
		if ($this->escape == true) {
131
			// FIXME - figure out whether this is correct behavior
132
			$this->command = escapeshellcmd($this->command);
133
		}
134
 
135
		if ($this->error !== null) {
136
			$this->command .= ' 2> ' . $this->error->getPath();
137
			$this->log("Writing error output to: " . $this->error->getPath());
138
		}
139
 
140
		if ($this->output !== null) {
141
			$this->command .= ' 1> ' . $this->output->getPath();
142
			$this->log("Writing standard output to: " . $this->output->getPath());
143
		} elseif ($this->spawn) {
144
			$this->command .= ' 1>/dev/null';
145
			$this->log("Sending ouptut to /dev/null");
146
		}
147
 
148
		// If neither output nor error are being written to file
149
		// then we'll redirect error to stdout so that we can dump
150
		// it to screen below.
151
 
152
		if ($this->output === null && $this->error === null) {
153
			$this->command .= ' 2>&1';
154
		}
155
 
156
		// we ignore the spawn boolean for windows
157
		if ($this->spawn) {
158
			$this->command .= ' &';
159
		}
160
 
161
		$this->log("Executing command: " . $this->command);
162
 
163
		$output = array();
164
		$return = null;
165
		exec($this->command, $output, $return);
166
 
167
		if ($this->dir !== null) {
168
			@chdir($currdir);
169
		}
170
 
171
		foreach($output as $line) {
172
			$this->log($line,  ($this->passthru ? Project::MSG_INFO : Project::MSG_VERBOSE));
173
		}
174
 
175
		if ($this->returnProperty) {
176
			$this->project->setProperty($this->returnProperty, $return);
177
		}
178
 
179
		if($return != 0 && $this->checkreturn) {
180
			throw new BuildException("Task exited with code $return");
181
		}
182
 
183
		return $return;
184
	}
185
 
186
	/**
187
	 * The command to use.
188
	 * @param mixed $command String or string-compatible (e.g. w/ __toString()).
189
	 */
190
	function setCommand($command) {
191
		$this->command = "" . $command;
192
	}
193
 
194
	/**
195
	 * Whether to use escapeshellcmd() to escape command.
196
	 * @param boolean $escape
197
	 */
198
	function setEscape($escape) {
199
		$this->escape = (bool) $escape;
200
	}
201
 
202
	/**
203
	 * Specify the working directory for executing this command.
204
	 * @param PhingFile $dir
205
	 */
206
	function setDir(PhingFile $dir) {
207
		$this->dir = $dir;
208
	}
209
 
210
	/**
211
	 * Specify OS (or muliple OS) that must match in order to execute this command.
212
	 * @param string $os
213
	 */
214
	function setOs($os) {
215
		$this->os = (string) $os;
216
	}
217
 
218
	/**
219
	 * File to which output should be written.
220
	 * @param PhingFile $output
221
	 */
222
	function setOutput(PhingFile $f) {
223
		$this->output = $f;
224
	}
225
 
226
	/**
227
	 * File to which error output should be written.
228
	 * @param PhingFile $output
229
	 */
230
	function setError(PhingFile $f) {
231
		$this->error = $f;
232
	}
233
 
234
	/**
235
	 * Whether to use passthru the output.
236
	 * @param boolean $passthru
237
	 */
238
	function setPassthru($passthru) {
239
		$this->passthru = (bool) $passthru;
240
	}
241
 
242
	/**
243
	 * Whether to suppress all output and run in the background.
244
	 * @param boolean $spawn
245
	 */
246
	function setSpawn($spawn) {
247
		$this->spawn  = (bool) $spawn;
248
	}
249
 
250
	/**
251
	 * Whether to check the return code.
252
	 * @param boolean $checkreturn
253
	 */
254
	function setCheckreturn($checkreturn) {
255
		$this->checkreturn = (bool) $checkreturn;
256
	}
257
 
258
	/**
259
	 * The name of property to set to return value from exec() call.
260
	 * @param string $prop
261
	 */
262
	function setReturnProperty($prop) {
263
		$this->returnProperty = $prop;
264
	}
265
}
266