Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/*
3
 *  $Id: SvnBaseTask.php 329 2007-12-22 17:12:59Z mrook $
4
 *
5
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
6
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
7
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
8
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
9
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
10
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
11
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
12
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
13
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
14
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
15
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
16
 *
17
 * This software consists of voluntary contributions made by many individuals
18
 * and is licensed under the LGPL. For more information please see
19
 * <http://phing.info>.
20
 */
21
 
22
include_once 'phing/Task.php';
23
 
24
/**
25
 * Base class for Subversion tasks
26
 *
27
 * @author Michiel Rook <michiel.rook@gmail.com>
28
 * @author Andrew Eddie <andrew.eddie@jamboworks.com>
29
 * @version $Id: SvnBaseTask.php 329 2007-12-22 17:12:59Z mrook $
30
 * @package phing.tasks.ext.svn
31
 * @see VersionControl_SVN
32
 * @since 2.2.0
33
 */
34
abstract class SvnBaseTask extends Task
35
{
36
	private $workingCopy = "";
37
 
38
	private $repositoryUrl = "";
39
 
40
	private $svnPath = "/usr/bin/svn";
41
 
42
	private $svn = NULL;
43
 
44
	private $mode = "";
45
 
46
	private $svnArgs = array();
47
 
48
	private $svnSwitches = array();
49
 
50
	private $toDir = "";
51
 
52
	/**
53
	 * Initialize Task.
54
 	 * This method includes any necessary SVN libraries and triggers
55
	 * appropriate error if they cannot be found.  This is not done in header
56
	 * because we may want this class to be loaded w/o triggering an error.
57
	 */
58
	function init() {
59
		include_once 'VersionControl/SVN.php';
60
		if (!class_exists('VersionControl_SVN')) {
61
			throw new Exception("SvnLastRevisionTask depends on PEAR VersionControl_SVN package being installed.");
62
		}
63
	}
64
 
65
	/**
66
	 * Sets the path to the workingcopy
67
	 */
68
	function setWorkingCopy($workingCopy)
69
	{
70
		$this->workingCopy = $workingCopy;
71
	}
72
 
73
	/**
74
	 * Returns the path to the workingcopy
75
	 */
76
	function getWorkingCopy()
77
	{
78
		return $this->workingCopy;
79
	}
80
 
81
	/**
82
	 * Sets the path/URI to the repository
83
	 */
84
	function setRepositoryUrl($repositoryUrl)
85
	{
86
		$this->repositoryUrl = $repositoryUrl;
87
	}
88
 
89
	/**
90
	 * Returns the path/URI to the repository
91
	 */
92
	function getRepositoryUrl()
93
	{
94
		return $this->repositoryUrl;
95
	}
96
 
97
	/**
98
	 * Sets the path to the SVN executable
99
	 */
100
	function setSvnPath($svnPath)
101
	{
102
		$this->svnPath = $svnPath;
103
	}
104
 
105
	/**
106
	 * Returns the path to the SVN executable
107
	 */
108
	function getSvnPath()
109
	{
110
		return $this->svnPath;
111
	}
112
 
113
	//
114
	// Args
115
	//
116
 
117
	/**
118
	 * Sets the path to export/checkout to
119
	 */
120
	function setToDir($toDir)
121
	{
122
		$this->toDir = $toDir;
123
	}
124
 
125
	/**
126
	 * Returns the path to export/checkout to
127
	 */
128
	function getToDir()
129
	{
130
		return $this->toDir;
131
	}
132
 
133
	//
134
	// Switches
135
	//
136
 
137
	/**
138
	 * Sets the force switch
139
	 */
140
	function setForce($value)
141
	{
142
		$this->svnSwitches['force'] = $value;
143
	}
144
 
145
	/**
146
	 * Returns the force switch
147
	 */
148
	function getForce()
149
	{
150
		return isset( $this->svnSwitches['force'] ) ? $this->svnSwitches['force'] : '';
151
	}
152
 
153
	/**
154
	 * Sets the username of the user to export
155
	 */
156
	function setUsername($value)
157
	{
158
		$this->svnSwitches['username'] = $value;
159
	}
160
 
161
	/**
162
	 * Returns the username
163
	 */
164
	function getUsername()
165
	{
166
		return isset( $this->svnSwitches['username'] ) ? $this->svnSwitches['username'] : '';
167
	}
168
 
169
	/**
170
	 * Sets the password of the user to export
171
	 */
172
	function setPassword($value)
173
	{
174
		$this->svnSwitches['password'] = $value;
175
	}
176
 
177
	/**
178
	 * Returns the password
179
	 */
180
	function getPassword()
181
	{
182
		return isset( $this->svnSwitches['password'] ) ? $this->svnSwitches['password'] : '';
183
	}
184
 
185
	/**
186
	 * Sets the no-auth-cache switch
187
	 */
188
	function setNoCache($value)
189
	{
190
		$this->svnSwitches['no-auth-cache'] = $value;
191
	}
192
 
193
	/**
194
	 * Returns the no-auth-cache switch
195
	 */
196
	function getNoCache()
197
	{
198
		return isset( $this->svnSwitches['no-auth-cache'] ) ? $this->svnSwitches['no-auth-cache'] : '';
199
	}
200
 
201
	/**
202
	 * Sets the non-recursive switch
203
	 */
204
	function setRecursive($value)
205
	{
206
		$this->svnSwitches['non-recursive'] = is_bool($value) ? !$value : TRUE;
207
	}
208
 
209
	/**
210
	 * Returns the non-recursive switch
211
	 */
212
	function getRecursive()
213
	{
214
		return isset( $this->svnSwitches['non-recursive'] ) ? $this->svnSwitches['non-recursive'] : '';
215
	}
216
 
217
	/**
218
	 * Sets the ignore-externals switch
219
	 */
220
	function setIgnoreExternals($value)
221
	{
222
		$this->svnSwitches['ignore-externals'] = $value;
223
	}
224
 
225
	/**
226
	 * Returns the ignore-externals switch
227
	 */
228
	function getIgnoreExternals()
229
	{
230
		return isset( $this->svnSwitches['ignore-externals'] ) ? $this->svnSwitches['ignore-externals'] : '';
231
	}
232
 
233
	/**
234
	 * Creates a VersionControl_SVN class based on $mode
235
	 *
236
	 * @param string The SVN mode to use (info, export, checkout, ...)
237
	 * @throws BuildException
238
	 */
239
	protected function setup($mode)
240
	{
241
		$this->mode = $mode;
242
 
243
		// Set up runtime options. Will be passed to all
244
		// subclasses.
245
		$options = array('fetchmode' => VERSIONCONTROL_SVN_FETCHMODE_ASSOC, 'svn_path' => $this->getSvnPath());
246
 
247
		// Pass array of subcommands we need to factory
248
		$this->svn = VersionControl_SVN::factory($mode, $options);
249
 
250
		if (!empty($this->repositoryUrl))
251
		{
252
			$this->svnArgs = array($this->repositoryUrl);
253
		}
254
		else
255
		if (!empty($this->workingCopy))
256
		{
257
			if (is_dir($this->workingCopy))
258
			{
259
				if (in_array(".svn", scandir($this->workingCopy)))
260
				{
261
					$this->svnArgs = array($this->workingCopy);
262
				}
263
				else
264
				{
265
					throw new BuildException("'".$this->workingCopy."' doesn't seem to be a working copy");
266
				}
267
			}
268
			else
269
			if ($mode=='info' )
270
			{
271
				if (is_file($this->workingCopy))
272
				{
273
					$this->svnArgs = array($this->workingCopy);
274
				}
275
				else
276
				{
277
					throw new BuildException("'".$this->workingCopy."' is not a directory nor a file");
278
				}
279
			}
280
			else
281
			{
282
				throw new BuildException("'".$this->workingCopy."' is not a directory");
283
			}
284
		}
285
	}
286
 
287
	/**
288
	 * Executes the constructed VersionControl_SVN instance
289
	 *
290
	 * @param array Additional arguments to pass to SVN.
291
	 * @param array Switches to pass to SVN.
292
	 * @return string Output generated by SVN.
293
	 */
294
	protected function run($args = array(), $switches = array())
295
	{
296
		$svnstack = PEAR_ErrorStack::singleton('VersionControl_SVN');
297
 
298
		$tempArgs = $this->svnArgs;
299
 
300
		$tempArgs = array_merge($tempArgs, $args);
301
 
302
		$tempSwitches = $this->svnSwitches;
303
 
304
		$tempSwitches = array_merge($tempSwitches, $switches);
305
 
306
		if ($output = $this->svn->run($tempArgs, $tempSwitches))
307
		{
308
			return $output;
309
		}
310
		else
311
		{
312
			if (count($errs = $svnstack->getErrors()))
313
			{
314
				$err = current($errs);
315
 
316
				throw new BuildException("Failed to run the 'svn " . $this->mode . "' command: " . $err['message']);
317
			}
318
		}
319
	}
320
}
321