|
|
@ -5,21 +5,47 @@ use Mage\Task\AbstractTask; |
|
|
|
use Mage\Task\Releases\IsReleaseAware; |
|
|
|
use Mage\Task\Releases\IsReleaseAware; |
|
|
|
use Mage\Task\SkipException; |
|
|
|
use Mage\Task\SkipException; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Class LinkSharedFilesTask |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @package Mage\Task\BuiltIn\Filesystem |
|
|
|
|
|
|
|
* @author Andrey Kolchenko <andrey@kolchenko.me> |
|
|
|
|
|
|
|
*/ |
|
|
|
class LinkSharedFilesTask extends AbstractTask implements IsReleaseAware |
|
|
|
class LinkSharedFilesTask extends AbstractTask implements IsReleaseAware |
|
|
|
{ |
|
|
|
{ |
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Linked folders parameter name |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
const LINKED_FILES = 'linked_files'; |
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Linked folders parameter name |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
const LINKED_FOLDERS = 'linked_folders'; |
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Linking strategy parameter name |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
const LINKED_STRATEGY = 'linking_strategy'; |
|
|
|
|
|
|
|
|
|
|
|
const LINKED_FOLDERS = 'linked_folders'; |
|
|
|
/** |
|
|
|
const LINKED_STRATEGY = 'linking_strategy'; |
|
|
|
* Absolute linked strategy |
|
|
|
|
|
|
|
*/ |
|
|
|
const ABSOLUTE_LINKING = 'absolute'; |
|
|
|
const ABSOLUTE_LINKING = 'absolute'; |
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Relative linked strategy |
|
|
|
|
|
|
|
*/ |
|
|
|
const RELATIVE_LINKING = 'relative'; |
|
|
|
const RELATIVE_LINKING = 'relative'; |
|
|
|
|
|
|
|
|
|
|
|
public $linkingStrategies = array( |
|
|
|
/** |
|
|
|
|
|
|
|
* @var array |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
private static $linkingStrategies = array( |
|
|
|
self::ABSOLUTE_LINKING, |
|
|
|
self::ABSOLUTE_LINKING, |
|
|
|
self::RELATIVE_LINKING |
|
|
|
self::RELATIVE_LINKING |
|
|
|
); |
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Returns the Title of the Task |
|
|
|
* Returns the Title of the Task |
|
|
|
|
|
|
|
* |
|
|
|
* @return string |
|
|
|
* @return string |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public function getName() |
|
|
|
public function getName() |
|
|
@ -35,41 +61,92 @@ class LinkSharedFilesTask extends AbstractTask implements IsReleaseAware |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public function run() |
|
|
|
public function run() |
|
|
|
{ |
|
|
|
{ |
|
|
|
$linkedFiles = $this->getParameter('linked_files', []); |
|
|
|
$linkedEntities = array_merge( |
|
|
|
$linkedFolders = $this->getParameter(self::LINKED_FOLDERS, []); |
|
|
|
$this->getParameter(self::LINKED_FILES, array()), |
|
|
|
$linkingStrategy = $this->getParameter(self::LINKED_STRATEGY, self::ABSOLUTE_LINKING); |
|
|
|
$this->getParameter(self::LINKED_FOLDERS, array()) |
|
|
|
|
|
|
|
); |
|
|
|
$linkedEntities = array_merge($linkedFiles,$linkedFolders); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (sizeof($linkedFiles) == 0 && sizeof($linkedFolders) == 0) { |
|
|
|
if (empty($linkedEntities)) { |
|
|
|
throw new SkipException('No files and folders configured for sym-linking.'); |
|
|
|
throw new SkipException('No files and folders configured for sym-linking.'); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
$sharedFolderName = $this->getParameter('shared', 'shared'); |
|
|
|
$remoteDirectory = rtrim($this->getConfig()->deployment('to'), '/') . '/'; |
|
|
|
$sharedFolderPath = rtrim($this->getConfig()->deployment('to'), '/') . '/' . $sharedFolderName; |
|
|
|
$sharedFolderPath = $remoteDirectory . $this->getParameter('shared', 'shared'); |
|
|
|
$releasesDirectory = $this->getConfig()->release('directory', 'releases'); |
|
|
|
$releasesDirectoryPath = $remoteDirectory . $this->getConfig()->release('directory', 'releases'); |
|
|
|
$releasesDirectoryPath = rtrim($this->getConfig()->deployment('to'), '/') . '/' . $releasesDirectory; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$currentCopy = $releasesDirectoryPath . '/' . $this->getConfig()->getReleaseId(); |
|
|
|
$currentCopy = $releasesDirectoryPath . '/' . $this->getConfig()->getReleaseId(); |
|
|
|
$relativeDiffPath = str_replace($this->getConfig()->deployment('to'),'',$currentCopy) . '/'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
foreach ($linkedEntities as $ePath) { |
|
|
|
foreach ($linkedEntities as $ePath) { |
|
|
|
if(is_array($ePath) && in_array($strategy = reset($ePath), $this->linkingStrategies ) ) { |
|
|
|
list($entityPath, $strategy) = $this->getPath($ePath); |
|
|
|
$entityPath = key($ePath); |
|
|
|
if ($strategy === self::RELATIVE_LINKING) { |
|
|
|
|
|
|
|
$dirName = dirname($currentCopy . '/' . $entityPath); |
|
|
|
|
|
|
|
$target = $this->makePathRelative($sharedFolderPath, $dirName) . $entityPath; |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
$strategy = $linkingStrategy; |
|
|
|
$target = $sharedFolderPath . '/' . $entityPath; |
|
|
|
$entityPath = $ePath; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
$sharedEntityLinkedPath = "$sharedFolderPath/$entityPath"; |
|
|
|
|
|
|
|
if($strategy==self::RELATIVE_LINKING) { |
|
|
|
|
|
|
|
$parentFolderPath = dirname($entityPath); |
|
|
|
|
|
|
|
$relativePath = $parentFolderPath=='.'?$relativeDiffPath:$relativeDiffPath.$parentFolderPath.'/'; |
|
|
|
|
|
|
|
$sharedEntityLinkedPath = ltrim(preg_replace('/(\w+\/)/', '../', $relativePath),'/').$sharedFolderName .'/'. $entityPath; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
$command = "ln -nfs $sharedEntityLinkedPath $currentCopy/$entityPath"; |
|
|
|
$command = 'mkdir -p ' . escapeshellarg(dirname($target)); |
|
|
|
|
|
|
|
$this->runCommandRemote($command); |
|
|
|
|
|
|
|
$command = 'ln -nfs ' . escapeshellarg($target) . ' ' . escapeshellarg($currentCopy . '/' . $entityPath); |
|
|
|
$this->runCommandRemote($command); |
|
|
|
$this->runCommandRemote($command); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return true; |
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Given an existing path, convert it to a path relative to a given starting path |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @param string $endPath Absolute path of target |
|
|
|
|
|
|
|
* @param string $startPath Absolute path where traversal begins |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @return string Path of target relative to starting path |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @author Fabien Potencier <fabien@symfony.com> |
|
|
|
|
|
|
|
* @see https://github.com/symfony/Filesystem/blob/v2.6.1/Filesystem.php#L332 |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
private function makePathRelative($endPath, $startPath) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
// Normalize separators on Windows |
|
|
|
|
|
|
|
if (defined('PHP_WINDOWS_VERSION_MAJOR')) { |
|
|
|
|
|
|
|
$endPath = strtr($endPath, '\\', '/'); |
|
|
|
|
|
|
|
$startPath = strtr($startPath, '\\', '/'); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
// Split the paths into arrays |
|
|
|
|
|
|
|
$startPathArr = explode('/', trim($startPath, '/')); |
|
|
|
|
|
|
|
$endPathArr = explode('/', trim($endPath, '/')); |
|
|
|
|
|
|
|
// Find for which directory the common path stops |
|
|
|
|
|
|
|
$index = 0; |
|
|
|
|
|
|
|
while (isset($startPathArr[$index]) && isset($endPathArr[$index]) && $startPathArr[$index] === $endPathArr[$index]) { |
|
|
|
|
|
|
|
$index++; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
// Determine how deep the start path is relative to the common path (ie, "web/bundles" = 2 levels) |
|
|
|
|
|
|
|
$depth = count($startPathArr) - $index; |
|
|
|
|
|
|
|
// Repeated "../" for each level need to reach the common path |
|
|
|
|
|
|
|
$traverser = str_repeat('../', $depth); |
|
|
|
|
|
|
|
$endPathRemainder = implode('/', array_slice($endPathArr, $index)); |
|
|
|
|
|
|
|
// Construct $endPath from traversing to the common path, then to the remaining $endPath |
|
|
|
|
|
|
|
$relativePath = $traverser . (strlen($endPathRemainder) > 0 ? $endPathRemainder . '/' : ''); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return (strlen($relativePath) === 0) ? './' : $relativePath; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* @param array|string $linkedEntity |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @return array [$path, $strategy] |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
private function getPath($linkedEntity) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
$linkingStrategy = $this->getParameter(self::LINKED_STRATEGY, self::ABSOLUTE_LINKING); |
|
|
|
|
|
|
|
if (is_array($linkedEntity)) { |
|
|
|
|
|
|
|
list($path, $strategy) = each($linkedEntity); |
|
|
|
|
|
|
|
if (!in_array($strategy, self::$linkingStrategies)) { |
|
|
|
|
|
|
|
$strategy = $linkingStrategy; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
$strategy = $linkingStrategy; |
|
|
|
|
|
|
|
$path = $linkedEntity; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return [$path, $strategy]; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |