Skip to content

Commit

Permalink
Merge pull request netz98#333 from fooman/install-from-cached-vcs
Browse files Browse the repository at this point in the history
Install from cached vcs
  • Loading branch information
cmuench committed May 21, 2014
2 parents e715b9c + 20eeb01 commit bcb3419
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 7 deletions.
69 changes: 65 additions & 4 deletions src/N98/Magento/Command/AbstractMagentoCommand.php
Expand Up @@ -2,6 +2,7 @@

namespace N98\Magento\Command;

use N98\Util\Console\Helper\MagentoHelper;
use Composer\Package\PackageInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
Expand Down Expand Up @@ -229,9 +230,7 @@ protected function getCoreHelper()
*/
protected function getComposerDownloadManager($input, $output)
{
$io = new ConsoleIO($input, $output, $this->getHelperSet());
$composer = ComposerFactory::create($io, array());
return $composer->getDownloadManager();
return $this->getComposer($input, $output)->getDownloadManager();
}

/**
Expand Down Expand Up @@ -261,10 +260,67 @@ protected function downloadByComposerConfig(InputInterface $input, OutputInterfa
} else {
$package = $config;
}
$dm->download($package, $targetFolder, $preferSource);

$helper = new \N98\Util\Console\Helper\MagentoHelper();
$helper->detect($targetFolder);
if ($this->isSourceTypeRepository($package->getSourceType()) && $helper->getRootFolder() == $targetFolder) {
$package->setInstallationSource('source');
$this->checkRepository($package, $targetFolder);
$dm->update($package, $package, $targetFolder);
} else {
$dm->download($package, $targetFolder, $preferSource);
}

return $package;
}

/**
* brings locally cached repository up to date if it is missing the requested tag
*
* @param $package
* @param $targetFolder
*/
protected function checkRepository($package, $targetFolder)
{
if ($package->getSourceType() == 'git') {
$command = sprintf(
'cd %s && git rev-parse refs/tags/%s',
escapeshellarg($targetFolder),
escapeshellarg($package->getSourceReference())
);
$existingTags = shell_exec($command);
if (!$existingTags) {
$command = sprintf('cd %s && git fetch', escapeshellarg($targetFolder));
shell_exec($command);
}
} elseif ($package->getSourceType() == 'hg') {
$command = sprintf(
'cd %s && hg log --template "{tags}" -r %s',
escapeshellarg($targetFolder),
escapeshellarg($package->getSourceReference())
);
$existingTag = shell_exec($command);
if ($existingTag === $package->getSourceReference()) {
$command = sprintf('cd %s && hg pull', escapeshellarg($targetFolder));
shell_exec($command);
}
}
}

/**
* obtain composer
*
* @param InputInterface $input
* @param OutputInterface $output
*
* @return \Composer\Composer
*/
protected function getComposer(InputInterface $input, OutputInterface $output)
{
$io = new ConsoleIO($input, $output, $this->getHelperSet());
return ComposerFactory::create($io, array());
}

/**
* @param string $alias
* @param string $message
Expand Down Expand Up @@ -443,4 +499,9 @@ protected function chooseInstallationFolder(InputInterface $input, OutputInterfa
$this->config['installationFolder'] = realpath($installationFolder);
\chdir($this->config['installationFolder']);
}

protected function isSourceTypeRepository($type)
{
return in_array($type, array('git', 'hg'));
}
}
45 changes: 42 additions & 3 deletions src/N98/Magento/Command/Installer/InstallCommand.php
Expand Up @@ -227,16 +227,25 @@ public function downloadMagento(InputInterface $input, OutputInterface $output)
return false;
}

$composer = $this->getComposer($input, $output);
$targetFolder = $this->getTargetFolderByType($composer, $package, $this->config['installationFolder']);
$this->config['magentoPackage'] = $this->downloadByComposerConfig(
$input,
$output,
$package,
$this->config['installationFolder'] . '/_n98_magerun_download',
$targetFolder,
true
);

$filesystem = new \Composer\Util\Filesystem();
$filesystem->copyThenRemove($this->config['installationFolder'] . '/_n98_magerun_download', $this->config['installationFolder']);
if ($this->isSourceTypeRepository($package->getSourceType())) {
$filesystem = new \N98\Util\Filesystem;
$filesystem->recursiveCopy($targetFolder, $this->config['installationFolder'], array('.git', '.hg'));
} else {
$filesystem = new \Composer\Util\Filesystem();
$filesystem->copyThenRemove(
$this->config['installationFolder'] . '/_n98_magerun_download', $this->config['installationFolder']
);
}

if (version_compare(PHP_VERSION, '5.4.0') >= 0) {
// Patch installer
Expand All @@ -250,6 +259,36 @@ public function downloadMagento(InputInterface $input, OutputInterface $output)
return true;
}

/**
* construct a folder to where magerun will download the source to, cache git/hg repositories under COMPOSER_HOME
*
* @param $composer
* @param $package
* @param $installationFolder
*
* @return string
*/
protected function getTargetFolderByType($composer, $package, $installationFolder)
{
$type = $package->getSourceType();
if ($this->isSourceTypeRepository($type)) {
$targetPath = sprintf(
'%s/%s/%s/%s',
$composer->getConfig()->get('cache-dir'),
'_n98_magerun_download',
$type,
preg_replace('{[^a-z0-9.]}i', '-', $package->getSourceUrl())
);
} else {
$targetPath = sprintf(
'%s/%s',
$installationFolder,
'_n98_magerun_download'
);
}
return $targetPath;
}

/**
* @param string $magentoFolder
*/
Expand Down

0 comments on commit bcb3419

Please sign in to comment.