From cc8c3068fe65361973cc466058868b633138616f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Huet?= Date: Fri, 7 Aug 2015 17:56:47 +0200 Subject: [PATCH] A task to create a simple symbolic link --- Mage/Task/BuiltIn/Filesystem/SymlinkTask.php | 139 +++++++++++++++++++ 1 file changed, 139 insertions(+) create mode 100644 Mage/Task/BuiltIn/Filesystem/SymlinkTask.php diff --git a/Mage/Task/BuiltIn/Filesystem/SymlinkTask.php b/Mage/Task/BuiltIn/Filesystem/SymlinkTask.php new file mode 100644 index 0000000..28911a7 --- /dev/null +++ b/Mage/Task/BuiltIn/Filesystem/SymlinkTask.php @@ -0,0 +1,139 @@ + + */ +class SymlinkTask extends AbstractTask +{ + /** + * The name of the symlink including full path to it. + * + * If the stage is on local host you should give full paths. If on remote + * you may give full or relative to the current release directory paths. + * + * @var string + */ + private $link; + + /** + * The target to wich the symlink should point to including full path to it. + * + * If the stage is on local host you should give full paths. If on remote + * you may give full or relative to the current release directory paths. + * + * @var string + */ + private $target; + + /** + * Initialize parameters. + * + * @throws RequiredConfigNotFoundException + */ + public function init() + { + parent::init(); + + if (! $this->getParameter('target')) { + throw new RequiredConfigNotFoundException('Missing required target link.'); + } + + $this->setTarget($this->getParameter('target')); + if (!is_null($this->getParameter('link'))) { + $this->setLink($this->getParameter('link')); + } + } + + /** + * @return string + */ + public function getName() + { + return "Creating symbolic link [built-in]"; + } + + /** + * @return boolean + */ + public function run() + { + $command = 'ln -fs ' . $this->getAbsolutPath($this->getTarget()); + if ($this->getLink()) { + $command .= ' ' . $this->getAbsolutPath($this->getLink()); + } + + $result = $this->runCommand($command); + + return $result; + } + + /** + * @param string $path + * @return string + */ + public function getAbsolutPath($path) + { + // For release + if ($this->getStage() != 'pre-deploy' && $path[0] != '/' && $this->getConfig()->deployment('to')) { + $releasesDirectory = trim($this->getConfig()->release('directory', 'releases'), '/') . '/' . $this->getConfig()->getReleaseId(); + return rtrim($this->getConfig()->deployment('to'), '/') . '/' . $releasesDirectory . '/' . ltrim($path, '/'); + } + + return $path; + } + + /** + * Set link. + * + * @param string $link + * @return SymlinkTask + */ + protected function setLink($link) + { + $this->link = $link; + + return $this; + } + + /** + * @return string + */ + protected function getLink() + { + return $this->link; + } + + /** + * Set target. + * + * @param string $target + * @return SymlinkTask + */ + protected function setTarget($target) + { + $this->target = $target; + + return $this; + } + + /** + * @return string + */ + protected function getTarget() + { + return $this->target; + } +}