ResolvePathTask.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. /*
  3. * $Id: ResolvePathTask.php 905 2010-10-05 16:28:03Z 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. require_once 'phing/Task.php';
  22. /**
  23. * Task for resolving relative paths and setting absolute path in property value.
  24. *
  25. * This task was created to address a need for resolving absolute paths of files / directories.
  26. * In many cases a relative directory (e.g. "./build") is specified, but it needs to be treated
  27. * as an absolute path since other build files (e.g. in subdirs) should all be using the same
  28. * path -- and not treating it as a relative path to their own directory.
  29. *
  30. * <code>
  31. * <property name="relative_path" value="./dirname"/>
  32. * <resolvepath propertyName="absolute_path" file="${relative_path}"/>
  33. * <echo>Resolved [absolute] path: ${absolute_path}</echo>
  34. * </code>
  35. *
  36. * TODO:
  37. * - Possibly integrate this with PackageAsPath, for handling/resolving dot-path paths.
  38. *
  39. * @author Hans Lellelid <hans@xmpl.org>
  40. * @version $Revision: 905 $
  41. * @package phing.tasks.system
  42. */
  43. class ResolvePathTask extends Task {
  44. /** Name of property to set. */
  45. private $propertyName;
  46. /** The [possibly] relative file/path that needs to be resolved. */
  47. private $file;
  48. /** Base directory used for resolution. */
  49. private $dir;
  50. /**
  51. * Set the name of the property to set.
  52. * @param string $v Property name
  53. * @return void
  54. */
  55. public function setPropertyName($v) {
  56. $this->propertyName = $v;
  57. }
  58. /**
  59. * Sets a base dir to use for resolution.
  60. * @param PhingFile $d
  61. */
  62. function setDir(PhingFile $d) {
  63. $this->dir = $d;
  64. }
  65. /**
  66. * Sets a path (file or directory) that we want to resolve.
  67. * This is the same as setFile() -- just more generic name so that it's
  68. * clear that you can also use it to set directory.
  69. * @param string $f
  70. * @see setFile()
  71. */
  72. function setPath($f) {
  73. $this->file = $f;
  74. }
  75. /**
  76. * Sets a file that we want to resolve.
  77. * @param string $f
  78. */
  79. function setFile($f) {
  80. $this->file = $f;
  81. }
  82. /**
  83. * Perform the resolution & set property.
  84. */
  85. public function main() {
  86. if (!$this->propertyName) {
  87. throw new BuildException("You must specify the propertyName attribute", $this->getLocation());
  88. }
  89. // Currently only files are supported
  90. if ($this->file === null) {
  91. throw new BuildException("You must specify a path to resolve", $this->getLocation());
  92. }
  93. $fs = FileSystem::getFileSystem();
  94. // if dir attribute was specified then we should
  95. // use that as basedir to which file was relative.
  96. // -- unless the file specified is an absolute path
  97. if ($this->dir !== null && !$fs->isAbsolute(new PhingFile($this->file))) {
  98. $resolved = new PhingFile($this->dir->getPath(), $this->file);
  99. } else {
  100. // otherwise just resolve it relative to project basedir
  101. $resolved = $this->project->resolveFile($this->file);
  102. }
  103. $this->log("Resolved " . $this->file . " to " . $resolved->getAbsolutePath(), Project::MSG_INFO);
  104. $this->project->setProperty($this->propertyName, $resolved->getAbsolutePath());
  105. }
  106. }