From dd559b0f69628277c868dcf3deeb17948e3e9b19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Pluchino?= Date: Thu, 7 Sep 2017 11:01:09 +0200 Subject: [PATCH] Split complex methods --- Converter/PackageUtil.php | 32 ++++++++++++----- Repository/Vcs/GitDriver.php | 70 ++++++++++++++++++++++-------------- 2 files changed, 67 insertions(+), 35 deletions(-) diff --git a/Converter/PackageUtil.php b/Converter/PackageUtil.php index 916d62f5..4ca2f5c6 100644 --- a/Converter/PackageUtil.php +++ b/Converter/PackageUtil.php @@ -139,10 +139,9 @@ public static function checkAliasVersion(AssetTypeInterface $assetType, $depende */ public static function convertDependencyVersion(AssetTypeInterface $assetType, $dependency, $version) { - $containsHash = strpos($version, '#') !== false; + $containsHash = false !== strpos($version, '#'); $version = str_replace('#', '', $version); - $version = empty($version) ? '*' : $version; - $version = trim($version); + $version = empty($version) ? '*' : trim($version); $searchVersion = str_replace(array(' ', '<', '>', '=', '^', '~'), '', $version); // sha version or branch version @@ -150,12 +149,7 @@ public static function convertDependencyVersion(AssetTypeInterface $assetType, $ if ($containsHash && preg_match('{^[0-9a-f]{4,40}$}', $version)) { $version = 'dev-default#'.$version; } elseif ('*' !== $version && !Validator::validateTag($searchVersion, $assetType) && !static::depIsRange($version)) { - $oldVersion = $version; - $version = 'dev-'.$assetType->getVersionConverter()->convertVersion($version); - - if (!Validator::validateBranch($oldVersion)) { - $version .= ' || '.$oldVersion; - } + $version = static::convertBrachVersion($assetType, $version); } return array($dependency, $version); @@ -304,4 +298,24 @@ protected static function depIsRange($version) return (bool) preg_match('/[\<\>\=\^\~\ ]/', $version); } + + /** + * Convert the dependency branch version. + * + * @param AssetTypeInterface $assetType The asset type + * @param string $version The version + * + * @return string + */ + protected static function convertBrachVersion(AssetTypeInterface $assetType, $version) + { + $oldVersion = $version; + $version = 'dev-'.$assetType->getVersionConverter()->convertVersion($version); + + if (!Validator::validateBranch($oldVersion)) { + $version .= ' || '.$oldVersion; + } + + return $version; + } } diff --git a/Repository/Vcs/GitDriver.php b/Repository/Vcs/GitDriver.php index 421e8561..67e79115 100644 --- a/Repository/Vcs/GitDriver.php +++ b/Repository/Vcs/GitDriver.php @@ -16,6 +16,7 @@ use Composer\Repository\Vcs\GitDriver as BaseGitDriver; use Composer\Util\Filesystem; use Composer\Util\Git as GitUtil; +use Fxp\Composer\AssetPlugin\Repository\AssetRepositoryManager; /** * Git vcs driver. @@ -46,8 +47,8 @@ public function initialize() { /* @var AssetRepositoryManager $arm */ $arm = $this->repoConfig['asset-repository-manager']; - $skipSync = false; + if (null !== ($skip = $arm->getConfig()->get('git-skip-update'))) { $localUrl = $this->config->get('cache-vcs-dir').'/'.preg_replace('{[^a-z0-9.]}i', '-', $this->url).'/'; // check if local copy exists and if it is a git repository and that modification time is within threshold @@ -57,39 +58,56 @@ public function initialize() } } - // copied from parent::initialize() - if (Filesystem::isLocalPath($this->url)) { - $this->url = preg_replace('{[\\/]\.git/?$}', '', $this->url); - $this->repoDir = $this->url; - $cacheUrl = realpath($this->url); - } else { - $this->repoDir = $this->config->get('cache-vcs-dir') . '/' . preg_replace('{[^a-z0-9.]}i', '-', $this->url) . '/'; + $cacheUrl = Filesystem::isLocalPath($this->url) + ? $this->initializeLocalPath() : $this->initializeRemotePath($skipSync); + $this->getTags(); + $this->getBranches(); + $this->cache = new Cache($this->io, $this->config->get('cache-repo-dir').'/'.preg_replace('{[^a-z0-9.]}i', '-', $cacheUrl)); + } - GitUtil::cleanEnv(); + /** + * Initialize the local path. + * + * @return string + */ + private function initializeLocalPath() + { + $this->url = preg_replace('{[\\/]\.git/?$}', '', $this->url); + $this->repoDir = $this->url; - $fs = new Filesystem(); - $fs->ensureDirectoryExists(dirname($this->repoDir)); + return realpath($this->url); + } - if (!is_writable(dirname($this->repoDir))) { - throw new \RuntimeException('Can not clone '.$this->url.' to access package information. The "'.dirname($this->repoDir).'" directory is not writable by the current user.'); - } + /** + * Initialize the remote path. + * + * @param bool $skipSync Check if sync must be skipped + * + * @return string + */ + private function initializeRemotePath($skipSync) + { + $this->repoDir = $this->config->get('cache-vcs-dir').'/'.preg_replace('{[^a-z0-9.]}i', '-', $this->url).'/'; - if (preg_match('{^ssh://[^@]+@[^:]+:[^0-9]+}', $this->url)) { - throw new \InvalidArgumentException('The source URL '.$this->url.' is invalid, ssh URLs should have a port number after ":".'."\n".'Use ssh://git@example.com:22/path or just git@example.com:path if you do not want to provide a password or custom port.'); - } + GitUtil::cleanEnv(); - $gitUtil = new GitUtil($this->io, $this->config, $this->process, $fs); - // patched line, sync from local dir without modifying url - if (!$skipSync && !$gitUtil->syncMirror($this->url, $this->repoDir)) { - $this->io->writeError('Failed to update '.$this->url.', package information from this repository may be outdated'); - } + $fs = new Filesystem(); + $fs->ensureDirectoryExists(dirname($this->repoDir)); - $cacheUrl = $this->url; + if (!is_writable(dirname($this->repoDir))) { + throw new \RuntimeException('Can not clone '.$this->url.' to access package information. The "'.dirname($this->repoDir).'" directory is not writable by the current user.'); } - $this->getTags(); - $this->getBranches(); + if (preg_match('{^ssh://[^@]+@[^:]+:[^0-9]+}', $this->url)) { + throw new \InvalidArgumentException('The source URL '.$this->url.' is invalid, ssh URLs should have a port number after ":".'."\n".'Use ssh://git@example.com:22/path or just git@example.com:path if you do not want to provide a password or custom port.'); + } - $this->cache = new Cache($this->io, $this->config->get('cache-repo-dir').'/'.preg_replace('{[^a-z0-9.]}i', '-', $cacheUrl)); + $gitUtil = new GitUtil($this->io, $this->config, $this->process, $fs); + // patched line, sync from local dir without modifying url + if (!$skipSync && !$gitUtil->syncMirror($this->url, $this->repoDir)) { + $this->io->writeError('Failed to update '.$this->url.', package information from this repository may be outdated'); + } + + return $this->url; } }