From fe18f407098c361380d24df8298ed4cdf389263c Mon Sep 17 00:00:00 2001 From: Maxime Rainville Date: Wed, 24 Oct 2018 18:37:45 +1300 Subject: [PATCH 01/13] NEW Make the resources dir configurable. --- composer.json | 3 ++- src/Library.php | 11 ++++++----- src/VendorModule.php | 5 +++-- src/VendorPlugin.php | 41 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 52 insertions(+), 8 deletions(-) diff --git a/composer.json b/composer.json index 0d73b0b..9223e35 100644 --- a/composer.json +++ b/composer.json @@ -28,7 +28,8 @@ "minimum-stability": "dev", "require": { "composer/installers": "^1.4", - "composer-plugin-api": "^1.1" + "composer-plugin-api": "^1.1", + "m1/env": "^2.1" }, "require-dev": { "composer/composer": "^1.5", diff --git a/src/Library.php b/src/Library.php index f4e6a42..248b9ea 100644 --- a/src/Library.php +++ b/src/Library.php @@ -20,8 +20,9 @@ class Library /** * Subfolder to map within public webroot + * @deprecated 1.4.0:2.0.0 Use global constant RESOURCES_DIR instead. */ - const RESOURCES_PATH = 'resources'; + const RESOURCES_PATH = RESOURCES_DIR; /** * Project root @@ -104,14 +105,14 @@ public function getBasePath() /** * Get base path to expose all libraries to * - * @return string Path with no trailing slash E.g. /var/www/public/resources + * @return string Path with no trailing slash E.g. /var/www/public/_resources */ public function getBasePublicPath() { $projectPath = $this->getBasePath(); $publicPath = $this->publicPathExists() - ? Util::joinPaths($projectPath, self::PUBLIC_PATH, self::RESOURCES_PATH) - : Util::joinPaths($projectPath, self::RESOURCES_PATH); + ? Util::joinPaths($projectPath, self::PUBLIC_PATH, RESOURCES_DIR) + : Util::joinPaths($projectPath, RESOURCES_DIR); return $publicPath; } @@ -140,7 +141,7 @@ public function getRelativePath() /** * Get base path to map resources for this module * - * @return string Path with trimmed slashes. E.g. /var/www/public/resources/vendor/silverstripe/module + * @return string Path with trimmed slashes. E.g. /var/www/public/_resources/vendor/silverstripe/module */ public function getPublicPath() { diff --git a/src/VendorModule.php b/src/VendorModule.php index 6e4b2e6..2be180d 100644 --- a/src/VendorModule.php +++ b/src/VendorModule.php @@ -16,8 +16,9 @@ class VendorModule extends Library /** * Default replacement folder for 'vendor' + * @deprecated 1.4.0:2.0.0 Use global RESOURCES_DIR instead. */ - const DEFAULT_TARGET = 'resources'; + const DEFAULT_TARGET = RESOURCES_DIR; /** * Build a vendor module library @@ -42,7 +43,7 @@ public function __construct($basePath, $name) * @param string $base Rewrite root (or 'vendor' for actual module path) * @return string Path for this module */ - public function getModulePath($base = self::DEFAULT_SOURCE) + public function getModulePath($base = RESOURCES_DIR) { if ($base === self::DEFAULT_TARGET) { return $this->getPublicPath(); diff --git a/src/VendorPlugin.php b/src/VendorPlugin.php index 438e8e7..b619100 100644 --- a/src/VendorPlugin.php +++ b/src/VendorPlugin.php @@ -15,7 +15,9 @@ use Composer\Plugin\Capable; use Composer\Plugin\PluginInterface; use Composer\Script\Event; +use Composer\Semver\Comparator; use Composer\Util\Filesystem; +use M1\Env\Parser; use SilverStripe\VendorPlugin\Console\VendorCommandProvider; /** @@ -80,6 +82,7 @@ public function __construct() */ public function activate(Composer $composer, IOInterface $io) { + $this->definedResourcesDir($composer); } public static function getSubscribedEvents() @@ -248,4 +251,42 @@ protected function installLibrary(IOInterface $IO, Library $library) ); $task->process($IO, [$library]); } + + private function getDotEnvVar($key) + { + $path = $this->getProjectPath() . DIRECTORY_SEPARATOR . '.env'; + + // Not readable + if (!file_exists($path) || !is_readable($path)) { + return null; + } + + // Parse and cleanup content + $result = []; + $variables = Parser::parse(file_get_contents($path)); + return isset($variables[$key]) ? $variables[$key] : null; + } + + private function definedResourcesDir(Composer $composer) + { + if (defined('RESOURCES_DIR')) { + return; + } + + $framework = $composer + ->getRepositoryManager() + ->getLocalRepository() + ->findPackage('silverstripe/framework', '*'); + + if ($framework && Comparator::greaterThanOrEqualTo($framework->getVersion(), '4.3')) { + $resourcesDir = $this->getDotEnvVar('SS_RESOURCES_DIR'); + if (!preg_match('/[_\-a-z0-9]+/i', $resourcesDir)) { + $resourcesDir = '_resources'; + } + } else { + $resourcesDir = 'resources'; + } + + define('RESOURCES_DIR', $resourcesDir); + } } From b59fcb08cb469accde2f27735e268e3636f7f5cd Mon Sep 17 00:00:00 2001 From: Maxime Rainville Date: Thu, 25 Oct 2018 12:29:32 +1300 Subject: [PATCH 02/13] Add a method to get the version alias from the lock file. --- src/VendorPlugin.php | 44 +++++++++++++++++++++++++++++++++++++++----- 1 file changed, 39 insertions(+), 5 deletions(-) diff --git a/src/VendorPlugin.php b/src/VendorPlugin.php index b619100..e9b4646 100644 --- a/src/VendorPlugin.php +++ b/src/VendorPlugin.php @@ -10,10 +10,13 @@ use Composer\Factory; use Composer\Installer\PackageEvent; use Composer\IO\IOInterface; +use Composer\Json\JsonFile; +use Composer\Package\Locker; use Composer\Package\PackageInterface; use Composer\Plugin\Capability\CommandProvider; use Composer\Plugin\Capable; use Composer\Plugin\PluginInterface; +use Composer\Repository\RepositoryInterface; use Composer\Script\Event; use Composer\Semver\Comparator; use Composer\Util\Filesystem; @@ -82,7 +85,7 @@ public function __construct() */ public function activate(Composer $composer, IOInterface $io) { - $this->definedResourcesDir($composer); + $this->definedResourcesDir($composer, $io); } public static function getSubscribedEvents() @@ -267,17 +270,16 @@ private function getDotEnvVar($key) return isset($variables[$key]) ? $variables[$key] : null; } - private function definedResourcesDir(Composer $composer) + private function definedResourcesDir(Composer $composer, IOInterface $io) { if (defined('RESOURCES_DIR')) { return; } - $framework = $composer - ->getRepositoryManager() - ->getLocalRepository() + $framework = $this->getRepository($composer, $io) ->findPackage('silverstripe/framework', '*'); + if ($framework && Comparator::greaterThanOrEqualTo($framework->getVersion(), '4.3')) { $resourcesDir = $this->getDotEnvVar('SS_RESOURCES_DIR'); if (!preg_match('/[_\-a-z0-9]+/i', $resourcesDir)) { @@ -289,4 +291,36 @@ private function definedResourcesDir(Composer $composer) define('RESOURCES_DIR', $resourcesDir); } + + /** + * Find a Repository to interogate for our package versions. Tries to get it from the locker file first because + * this one understand version alias. Fallsback to the local repository + * @param Composer $composer + * @param IOInterface $io + * @return RepositoryInterface + */ + private function getRepository(Composer $composer, IOInterface $io) + { + // Some times getLocker will return null, so we can't rely on this + if ($locker = $composer->getLocker()) { + return $locker->getLockedRepository(); + } + + // Let's build our own locker from the lock file + $lockFile = $this->getProjectPath() . DIRECTORY_SEPARATOR . 'composer.lock'; + if (is_readable($lockFile)) { + $locker = new Locker( + $io, + new JsonFile($lockFile, null, $io), + $composer->getRepositoryManager(), + $composer->getInstallationManager(), + file_get_contents($lockFile) + ); + return $locker->getLockedRepository(); + } + + // Fallback to LocalRepository + return $composer->getRepositoryManager()->getLocalRepository(); + + } } From e687c177fb2dbc6cc4a9ff46e6c33f1819c898e3 Mon Sep 17 00:00:00 2001 From: Maxime Rainville Date: Thu, 25 Oct 2018 14:49:56 +1300 Subject: [PATCH 03/13] Move logic to identify resource path to Library. --- src/Console/VendorExposeCommand.php | 2 +- src/Library.php | 126 ++++++++++++++++++++++++++-- src/VendorPlugin.php | 70 ---------------- 3 files changed, 122 insertions(+), 76 deletions(-) diff --git a/src/Console/VendorExposeCommand.php b/src/Console/VendorExposeCommand.php index 4630633..2c489ed 100644 --- a/src/Console/VendorExposeCommand.php +++ b/src/Console/VendorExposeCommand.php @@ -74,7 +74,7 @@ protected function getAllLibraries() } // Ensure this library should be exposed, and has at least one folder - $module = new Library($basePath, $modulePath); + $module = new Library($basePath, $modulePath, null, $this->getComposer(), $this->getIO()); if (!$module->requiresExpose() || !$module->getExposedFolders()) { continue; } diff --git a/src/Library.php b/src/Library.php index 248b9ea..9d284a9 100644 --- a/src/Library.php +++ b/src/Library.php @@ -2,8 +2,13 @@ namespace SilverStripe\VendorPlugin; +use Composer\Composer; +use Composer\IO\IOInterface; use Composer\Json\JsonFile; +use Composer\Package\Locker; +use Composer\Semver\Comparator; use LogicException; +use M1\Env\Parser; use SilverStripe\VendorPlugin\Methods\ExposeMethod; /** @@ -18,11 +23,21 @@ class Library */ const PUBLIC_PATH = 'public'; + /** + * Default folder where vendor resources will be exposed. + */ + const DEFAULT_RESOURCES_DIR = '_resources'; + + /** + * Default folder where vendor resources will be exposed if using pre-4.3 framework. + */ + const LEGACY_DEFAULT_RESOURCES_DIR = 'resources'; + /** * Subfolder to map within public webroot - * @deprecated 1.4.0:2.0.0 Use global constant RESOURCES_DIR instead. + * @deprecated 1.4.0:2.0.0 Use Library::getResourceDir() instead. */ - const RESOURCES_PATH = RESOURCES_DIR; + const RESOURCES_PATH = self::LEGACY_DEFAULT_RESOURCES_DIR; /** * Project root @@ -38,6 +53,16 @@ class Library */ protected $path = null; + /** + * @var Composer + */ + protected $composer = null; + + /** + * @var IOInterface + */ + protected $io = null; + /** * Build a vendor module library * @@ -45,11 +70,19 @@ class Library * @param string $libraryPath Path to this library * @param string $name Composer name of this library */ - public function __construct($basePath, $libraryPath, $name = null) + public function __construct( + $basePath, + $libraryPath, + $name = null, + Composer $composer=null, + IOInterface $io = null + ) { $this->basePath = realpath($basePath); $this->path = realpath($libraryPath); $this->name = $name; + $this->composer = $composer; + $this->io = $io; } /** @@ -110,9 +143,10 @@ public function getBasePath() public function getBasePublicPath() { $projectPath = $this->getBasePath(); + $resourceDir = $this->getResourcesDir(); $publicPath = $this->publicPathExists() - ? Util::joinPaths($projectPath, self::PUBLIC_PATH, RESOURCES_DIR) - : Util::joinPaths($projectPath, RESOURCES_DIR); + ? Util::joinPaths($projectPath, self::PUBLIC_PATH, $resourceDir) + : Util::joinPaths($projectPath, $resourceDir); return $publicPath; } @@ -281,4 +315,86 @@ protected function installedIntoVendor() { return preg_match('#^vendor[/\\\\]#', $this->getRelativePath()); } + + public function getResourcesDir() + { + $locker = $this->getLocker(); + + if (!$locker) { + return self::LEGACY_DEFAULT_RESOURCES_DIR; + } + $framework = $locker->getLockedRepository()->findPackage('silverstripe/framework', '*'); + $frameworVersion = $framework->getPrettyVersion(); + $aliases = $locker->getAliases(); + foreach ($aliases as $alias) { + if ($alias['package'] === 'silverstripe/framework' && $alias['version'] === $frameworVersion) { + $frameworVersion = isset($alias['alias_normalized']) ? $alias['alias_normalized'] : $alias['alias']; + break; + } + } + + if ($framework && Comparator::greaterThanOrEqualTo($frameworVersion, '4.3')) { + $resourcesDir = $this->getDotEnvVar('SS_RESOURCES_DIR'); + if (!preg_match('/[_\-a-z0-9]+/i', $resourcesDir)) { + $resourcesDir = self::DEFAULT_RESOURCES_DIR; + } + } else { + $resourcesDir = self::LEGACY_DEFAULT_RESOURCES_DIR; + } + + return $resourcesDir; + } + + private function getDotEnvVar($key) + { + if ($env = getenv($key)) { + return $env; + } + + $path = $this->getBasePath() . DIRECTORY_SEPARATOR . '.env'; + + // Not readable + if (!file_exists($path) || !is_readable($path)) { + return null; + } + + // Parse and cleanup content + $result = []; + $variables = Parser::parse(file_get_contents($path)); + return isset($variables[$key]) ? $variables[$key] : null; + } + + /** + * Find a Repository to interogate for our package versions. Tries to get it from the locker file first because + * this one understand version alias. Fallsback to the local repository + * @param Composer $composer + * @param IOInterface $io + * @return Locker + */ + private function getLocker() + { + if (!$this->composer) { + return null; + } + + // Some times getLocker will return null, so we can't rely on this + if ($locker = $this->composer->getLocker()) { + return $locker; + } + + // Let's build our own locker from the lock file + $lockFile = $this->getBasePath() . DIRECTORY_SEPARATOR . 'composer.lock'; + if ($this->io && is_readable($lockFile)) { + $locker = new Locker( + $this->io, + new JsonFile($lockFile, null, $this->io), + $this->composer->getRepositoryManager(), + $this->composer->getInstallationManager(), + file_get_contents($lockFile) + ); + return $locker; + } + + return null; + } } diff --git a/src/VendorPlugin.php b/src/VendorPlugin.php index e9b4646..2342405 100644 --- a/src/VendorPlugin.php +++ b/src/VendorPlugin.php @@ -85,7 +85,6 @@ public function __construct() */ public function activate(Composer $composer, IOInterface $io) { - $this->definedResourcesDir($composer, $io); } public static function getSubscribedEvents() @@ -254,73 +253,4 @@ protected function installLibrary(IOInterface $IO, Library $library) ); $task->process($IO, [$library]); } - - private function getDotEnvVar($key) - { - $path = $this->getProjectPath() . DIRECTORY_SEPARATOR . '.env'; - - // Not readable - if (!file_exists($path) || !is_readable($path)) { - return null; - } - - // Parse and cleanup content - $result = []; - $variables = Parser::parse(file_get_contents($path)); - return isset($variables[$key]) ? $variables[$key] : null; - } - - private function definedResourcesDir(Composer $composer, IOInterface $io) - { - if (defined('RESOURCES_DIR')) { - return; - } - - $framework = $this->getRepository($composer, $io) - ->findPackage('silverstripe/framework', '*'); - - - if ($framework && Comparator::greaterThanOrEqualTo($framework->getVersion(), '4.3')) { - $resourcesDir = $this->getDotEnvVar('SS_RESOURCES_DIR'); - if (!preg_match('/[_\-a-z0-9]+/i', $resourcesDir)) { - $resourcesDir = '_resources'; - } - } else { - $resourcesDir = 'resources'; - } - - define('RESOURCES_DIR', $resourcesDir); - } - - /** - * Find a Repository to interogate for our package versions. Tries to get it from the locker file first because - * this one understand version alias. Fallsback to the local repository - * @param Composer $composer - * @param IOInterface $io - * @return RepositoryInterface - */ - private function getRepository(Composer $composer, IOInterface $io) - { - // Some times getLocker will return null, so we can't rely on this - if ($locker = $composer->getLocker()) { - return $locker->getLockedRepository(); - } - - // Let's build our own locker from the lock file - $lockFile = $this->getProjectPath() . DIRECTORY_SEPARATOR . 'composer.lock'; - if (is_readable($lockFile)) { - $locker = new Locker( - $io, - new JsonFile($lockFile, null, $io), - $composer->getRepositoryManager(), - $composer->getInstallationManager(), - file_get_contents($lockFile) - ); - return $locker->getLockedRepository(); - } - - // Fallback to LocalRepository - return $composer->getRepositoryManager()->getLocalRepository(); - - } } From eded214417ade5f36a03740eadb93d7ca04e4bfd Mon Sep 17 00:00:00 2001 From: Maxime Rainville Date: Thu, 25 Oct 2018 15:51:37 +1300 Subject: [PATCH 04/13] Add logic to handle initial install. --- src/Library.php | 43 +++++++++++++++++++++++++++++++------------ src/VendorPlugin.php | 4 ++-- 2 files changed, 33 insertions(+), 14 deletions(-) diff --git a/src/Library.php b/src/Library.php index 9d284a9..ea2d5f2 100644 --- a/src/Library.php +++ b/src/Library.php @@ -6,6 +6,7 @@ use Composer\IO\IOInterface; use Composer\Json\JsonFile; use Composer\Package\Locker; +use Composer\Package\Package; use Composer\Semver\Comparator; use LogicException; use M1\Env\Parser; @@ -104,6 +105,7 @@ public function getName() } // Get from composer $json = $this->getJson(); + if (isset($json['name'])) { $this->name = $json['name']; } @@ -318,28 +320,49 @@ protected function installedIntoVendor() public function getResourcesDir() { + if (!$this->composer) { + throw new LogicException('Could not find the targeted resource dir'); + } + $locker = $this->getLocker(); + $ss_resources_dir = $this->getDotEnvVar('SS_RESOURCES_DIR'); if (!$locker) { return self::LEGACY_DEFAULT_RESOURCES_DIR; } - $framework = $locker->getLockedRepository()->findPackage('silverstripe/framework', '*'); - $frameworVersion = $framework->getPrettyVersion(); - $aliases = $locker->getAliases(); - foreach ($aliases as $alias) { - if ($alias['package'] === 'silverstripe/framework' && $alias['version'] === $frameworVersion) { + + try { + $framework = $locker->getLockedRepository()->findPackage('silverstripe/framework', '*'); + $frameworkVersion = $framework->getVersion(); + $aliases = $locker->getAliases(); + } catch (LogicException $ex) { + $framework = $this->composer->getRepositoryManager()->getLocalRepository()->findPackage('silverstripe/framework', '*'); + $frameworkVersion = $framework->getVersion(); + $aliases = []; + } + + foreach ($aliases as $alias) { + if ($alias['package'] === 'silverstripe/framework' && $alias['version'] === $frameworkVersion) { $frameworVersion = isset($alias['alias_normalized']) ? $alias['alias_normalized'] : $alias['alias']; break; } } - if ($framework && Comparator::greaterThanOrEqualTo($frameworVersion, '4.3')) { - $resourcesDir = $this->getDotEnvVar('SS_RESOURCES_DIR'); + if (Comparator::greaterThanOrEqualTo($frameworkVersion, '4.3')) { + // We're definitively running 4.3 or above + $resourcesDir = $ss_resources_dir; if (!preg_match('/[_\-a-z0-9]+/i', $resourcesDir)) { $resourcesDir = self::DEFAULT_RESOURCES_DIR; } - } else { + } elseif (Comparator::lessThan($frameworkVersion, '4.3')) { + // We're definitively running something below 4.3 $resourcesDir = self::LEGACY_DEFAULT_RESOURCES_DIR; + } else ($ss_resources_dir) { + // We're confused and will use the value provided by the environement if we can + $resourcesDir = $ss_resources_dir; + if (!preg_match('/[_\-a-z0-9]+/i', $resourcesDir)) { + $resourcesDir = self::LEGACY_DEFAULT_RESOURCES_DIR; + } } return $resourcesDir; @@ -373,10 +396,6 @@ private function getDotEnvVar($key) */ private function getLocker() { - if (!$this->composer) { - return null; - } - // Some times getLocker will return null, so we can't rely on this if ($locker = $this->composer->getLocker()) { return $locker; diff --git a/src/VendorPlugin.php b/src/VendorPlugin.php index 2342405..7a97c54 100644 --- a/src/VendorPlugin.php +++ b/src/VendorPlugin.php @@ -129,7 +129,7 @@ public function getLibrary(PackageEvent $event) $path = $installer->getInstallPath($package); // Build module - return new Library($this->getProjectPath(), $path); + return new Library($this->getProjectPath(), $path, null, $event->getComposer(), $event->getIO()); } /** @@ -158,7 +158,7 @@ public function installRootPackage(Event $event) { // Build library in base path $basePath = $this->getProjectPath(); - $library = new Library($basePath, $basePath); + $library = new Library($basePath, $basePath, null, $event->getComposer(), $event->getIO()); // Pass to library installer $this->installLibrary($event->getIO(), $library); From 7f832a2c7881a92e9ea9398dcdde520f4a57666c Mon Sep 17 00:00:00 2001 From: Maxime Rainville Date: Thu, 25 Oct 2018 15:54:30 +1300 Subject: [PATCH 05/13] Coorect syntax error. --- src/Library.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Library.php b/src/Library.php index ea2d5f2..3f510cb 100644 --- a/src/Library.php +++ b/src/Library.php @@ -357,7 +357,7 @@ public function getResourcesDir() } elseif (Comparator::lessThan($frameworkVersion, '4.3')) { // We're definitively running something below 4.3 $resourcesDir = self::LEGACY_DEFAULT_RESOURCES_DIR; - } else ($ss_resources_dir) { + } else { // We're confused and will use the value provided by the environement if we can $resourcesDir = $ss_resources_dir; if (!preg_match('/[_\-a-z0-9]+/i', $resourcesDir)) { From 3495ef98f68b84baf623b09f248969c4478c8d0f Mon Sep 17 00:00:00 2001 From: Maxime Rainville Date: Thu, 25 Oct 2018 16:16:12 +1300 Subject: [PATCH 06/13] Add some refinements to getRessourceDir --- src/Library.php | 57 +++++++++++++++++++++++++++++++++---------------- 1 file changed, 39 insertions(+), 18 deletions(-) diff --git a/src/Library.php b/src/Library.php index 3f510cb..2959f65 100644 --- a/src/Library.php +++ b/src/Library.php @@ -318,33 +318,48 @@ protected function installedIntoVendor() return preg_match('#^vendor[/\\\\]#', $this->getRelativePath()); } + /** + * Determine the name of the folder where vendor module's resources will be exposed. e.g. `_resources` + * @throws LogicException + * @return string + */ public function getResourcesDir() { if (!$this->composer) { - throw new LogicException('Could not find the targeted resource dir'); + // We need a composer instance for this to work. + throw new LogicException('Could not find the targeted resource dir.'); } - $locker = $this->getLocker(); + // Try to get our resource dir from our .env file $ss_resources_dir = $this->getDotEnvVar('SS_RESOURCES_DIR'); - if (!$locker) { - return self::LEGACY_DEFAULT_RESOURCES_DIR; - } + $frameworkVersion = ''; + + if ($locker = $this->getLocker()) { + try { + // Try to get our package info from the locker + $framework = $locker + ->getLockedRepository() + ->findPackage('silverstripe/framework', '*'); + $aliases = $locker->getAliases(); + } catch (LogicException $ex) { + // Fallback to the local repo, this won't allow us to get our aliases however. + $framework = $this->composer + ->getRepositoryManager() + ->getLocalRepository() + ->findPackage('silverstripe/framework', '*'); + $aliases = []; + } - try { - $framework = $locker->getLockedRepository()->findPackage('silverstripe/framework', '*'); $frameworkVersion = $framework->getVersion(); - $aliases = $locker->getAliases(); - } catch (LogicException $ex) { - $framework = $this->composer->getRepositoryManager()->getLocalRepository()->findPackage('silverstripe/framework', '*'); - $frameworkVersion = $framework->getVersion(); - $aliases = []; - } - foreach ($aliases as $alias) { - if ($alias['package'] === 'silverstripe/framework' && $alias['version'] === $frameworkVersion) { - $frameworVersion = isset($alias['alias_normalized']) ? $alias['alias_normalized'] : $alias['alias']; - break; + // If we're running off a dev branch of framework, we might not get a clean version number. + // So we'll try to match it to an alias + foreach ($aliases as $alias) { + if ($alias['package'] === 'silverstripe/framework' && $alias['version'] === $frameworkVersion) { + $frameworkVersion = isset($alias['alias_normalized']) ? $alias['alias_normalized'] : $alias['alias']; + break; + } } } @@ -358,7 +373,7 @@ public function getResourcesDir() // We're definitively running something below 4.3 $resourcesDir = self::LEGACY_DEFAULT_RESOURCES_DIR; } else { - // We're confused and will use the value provided by the environement if we can + // We're confused ... if we'll use the value from the .env file or we'll default to legacy. $resourcesDir = $ss_resources_dir; if (!preg_match('/[_\-a-z0-9]+/i', $resourcesDir)) { $resourcesDir = self::LEGACY_DEFAULT_RESOURCES_DIR; @@ -368,6 +383,12 @@ public function getResourcesDir() return $resourcesDir; } + /** + * Find a value from the environment. + * + * @param $key + * @return string|null + */ private function getDotEnvVar($key) { if ($env = getenv($key)) { From fc32d66bf1ef067297735b874460b88ff967a0b9 Mon Sep 17 00:00:00 2001 From: Maxime Rainville Date: Fri, 26 Oct 2018 15:42:17 +1300 Subject: [PATCH 07/13] Remove explicit reference to 4.3 and use a constant instead. --- src/Library.php | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/Library.php b/src/Library.php index 2959f65..47174e9 100644 --- a/src/Library.php +++ b/src/Library.php @@ -30,7 +30,7 @@ class Library const DEFAULT_RESOURCES_DIR = '_resources'; /** - * Default folder where vendor resources will be exposed if using pre-4.3 framework. + * Default folder where vendor resources will be exposed if using a non-configurable framework */ const LEGACY_DEFAULT_RESOURCES_DIR = 'resources'; @@ -40,6 +40,11 @@ class Library */ const RESOURCES_PATH = self::LEGACY_DEFAULT_RESOURCES_DIR; + /** + * Version of `silverstripe/framework` from which + */ + const CONFIGURABLE_FRAMEWORK_VERSION = "4.3.0"; + /** * Project root * @@ -363,17 +368,17 @@ public function getResourcesDir() } } - if (Comparator::greaterThanOrEqualTo($frameworkVersion, '4.3')) { - // We're definitively running 4.3 or above + if (Comparator::greaterThanOrEqualTo($frameworkVersion, self::CONFIGURABLE_FRAMEWORK_VERSION)) { + // We're definitively running a framework that supports a configurable resources folder $resourcesDir = $ss_resources_dir; if (!preg_match('/[_\-a-z0-9]+/i', $resourcesDir)) { $resourcesDir = self::DEFAULT_RESOURCES_DIR; } - } elseif (Comparator::lessThan($frameworkVersion, '4.3')) { - // We're definitively running something below 4.3 + } elseif (Comparator::lessThan($frameworkVersion, self::CONFIGURABLE_FRAMEWORK_VERSION)) { + // We're definitively running a framework that DOES NOT supports a configurable resources folder $resourcesDir = self::LEGACY_DEFAULT_RESOURCES_DIR; } else { - // We're confused ... if we'll use the value from the .env file or we'll default to legacy. + // We're confused ... we'll try using the value from the .env file or we'll default to legacy. $resourcesDir = $ss_resources_dir; if (!preg_match('/[_\-a-z0-9]+/i', $resourcesDir)) { $resourcesDir = self::LEGACY_DEFAULT_RESOURCES_DIR; From 4b19d93158622a978e75b5358219d0d375cd9db3 Mon Sep 17 00:00:00 2001 From: Maxime Rainville Date: Mon, 29 Oct 2018 17:50:11 +1300 Subject: [PATCH 08/13] Add test for Library::getResourcesDir() --- src/Library.php | 15 ++- tests/LibraryTest.php | 95 +++++++++++++++++++ tests/fixtures/projects/ss43/composer.json | 28 ++++++ tests/fixtures/projects/ss44/composer.json | 28 ++++++ tests/fixtures/projects/ss44WithEnv/.env | 1 + .../projects/ss44WithEnv/composer.json | 28 ++++++ 6 files changed, 193 insertions(+), 2 deletions(-) create mode 100644 tests/LibraryTest.php create mode 100644 tests/fixtures/projects/ss43/composer.json create mode 100644 tests/fixtures/projects/ss44/composer.json create mode 100644 tests/fixtures/projects/ss44WithEnv/.env create mode 100644 tests/fixtures/projects/ss44WithEnv/composer.json diff --git a/src/Library.php b/src/Library.php index 47174e9..2123d5b 100644 --- a/src/Library.php +++ b/src/Library.php @@ -43,7 +43,7 @@ class Library /** * Version of `silverstripe/framework` from which */ - const CONFIGURABLE_FRAMEWORK_VERSION = "4.3.0"; + const CONFIGURABLE_FRAMEWORK_VERSION = "4.4.0"; /** * Project root @@ -331,7 +331,7 @@ protected function installedIntoVendor() public function getResourcesDir() { if (!$this->composer) { - // We need a composer instance for this to work. + // We need a composer instance for this to work. This should never happen. throw new LogicException('Could not find the targeted resource dir.'); } @@ -366,6 +366,17 @@ public function getResourcesDir() break; } } + } elseif ($repo = $this->composer->getRepositoryManager()->getLocalRepository()) { + $framework = $repo->findPackage('silverstripe/framework', '*'); + $frameworkVersion = $framework->getVersion(); + } + + if (!$frameworkVersion) { + throw new LogicException( + 'Could not find the targeted resource dir. SilverStripe Framework does not appear to be' . + 'installed. Try running a `composer update`. If the error persist, please report this issue at ' . + 'https://github.com/silverstripe/vendor-plugin/issues/new' + ); } if (Comparator::greaterThanOrEqualTo($frameworkVersion, self::CONFIGURABLE_FRAMEWORK_VERSION)) { diff --git a/tests/LibraryTest.php b/tests/LibraryTest.php new file mode 100644 index 0000000..bbb5452 --- /dev/null +++ b/tests/LibraryTest.php @@ -0,0 +1,95 @@ +cwd = getcwd(); + } + + protected function tearDown() + { + parent::tearDown(); + chdir($this->cwd); + + } + + /** + * @dataProvider resourcesDirProvider + */ + public function testResourcesDir($expected, $projectPath, $env, $preloadLock) + { + putenv('SS_RESOURCES_DIR=' . $env); + + $lib = $this->getLib($projectPath, $preloadLock); + $this->assertEquals($expected, $lib->getResourcesDir()); + } + + public function resourcesDirProvider() + { + return [ + ['resources', 'ss43', '', false], + ['_resources', 'ss44', '', false], + ['etc', 'ss44', 'etc', false], + ['resources', 'ss43', 'etc', false], + ['resources', 'ss43', '', true], + ['_resources', 'ss44', '', true], + ['etc', 'ss44', 'etc', true], + ['environement-defined', 'ss44WithEnv', '', true], + ]; + } + + private function getLib($project, $preloadLock) + { + $path = __DIR__ . '/fixtures/projects/' . $project; + chdir($path); + $factory = new Factory(); + + + $io = new NullIO(); + $composer = $factory->createComposer($io,null, false, $path); + $preloadLock && $composer->setLocker(new Locker( + $io, + new JsonFile($path . '/composer.lock', null, $io), + $composer->getRepositoryManager(), + $composer->getInstallationManager(), + file_get_contents($path . '/composer.lock') + )); + + $lib = new Library( + $path, + 'vendor/silverstripe/skynet', + null, + $composer, + $io + ); + + return $lib; + } + + +} diff --git a/tests/fixtures/projects/ss43/composer.json b/tests/fixtures/projects/ss43/composer.json new file mode 100644 index 0000000..70318cf --- /dev/null +++ b/tests/fixtures/projects/ss43/composer.json @@ -0,0 +1,28 @@ +{ + "name": "silverstripe/ss42", + "type": "silverstripe-project", + "description": "Fake project using SS 4.3", + "homepage": "https://www.silverstripe.org", + "license": "BSD-3-Clause", + "require": { + "silverstripe/recipe-cms": "4.3.x-dev as 4.3.0" + }, + "extra": { + "project-files-installed": [ + "app/.htaccess", + "app/_config.php", + "app/_config/mysite.yml", + "app/src/Page.php", + "app/src/PageController.php" + ], + "public-files-installed": [ + ".htaccess", + "index.php", + "install-frameworkmissing.html", + "install.php", + "web.config" + ] + }, + "prefer-stable": true, + "minimum-stability": "dev" +} diff --git a/tests/fixtures/projects/ss44/composer.json b/tests/fixtures/projects/ss44/composer.json new file mode 100644 index 0000000..7f9bd60 --- /dev/null +++ b/tests/fixtures/projects/ss44/composer.json @@ -0,0 +1,28 @@ +{ + "name": "silverstripe/ss44", + "type": "silverstripe-project", + "description": "Fake project using SS 4.4", + "homepage": "https://www.silverstripe.org", + "license": "BSD-3-Clause", + "require": { + "silverstripe/recipe-cms": "4.4.x-dev as 4.4.0" + }, + "extra": { + "project-files-installed": [ + "app/.htaccess", + "app/_config.php", + "app/_config/mysite.yml", + "app/src/Page.php", + "app/src/PageController.php" + ], + "public-files-installed": [ + ".htaccess", + "index.php", + "install-frameworkmissing.html", + "install.php", + "web.config" + ] + }, + "prefer-stable": true, + "minimum-stability": "dev" +} diff --git a/tests/fixtures/projects/ss44WithEnv/.env b/tests/fixtures/projects/ss44WithEnv/.env new file mode 100644 index 0000000..f6bd06a --- /dev/null +++ b/tests/fixtures/projects/ss44WithEnv/.env @@ -0,0 +1 @@ +SS_RESOURCES_DIR=environement-defined diff --git a/tests/fixtures/projects/ss44WithEnv/composer.json b/tests/fixtures/projects/ss44WithEnv/composer.json new file mode 100644 index 0000000..7f9bd60 --- /dev/null +++ b/tests/fixtures/projects/ss44WithEnv/composer.json @@ -0,0 +1,28 @@ +{ + "name": "silverstripe/ss44", + "type": "silverstripe-project", + "description": "Fake project using SS 4.4", + "homepage": "https://www.silverstripe.org", + "license": "BSD-3-Clause", + "require": { + "silverstripe/recipe-cms": "4.4.x-dev as 4.4.0" + }, + "extra": { + "project-files-installed": [ + "app/.htaccess", + "app/_config.php", + "app/_config/mysite.yml", + "app/src/Page.php", + "app/src/PageController.php" + ], + "public-files-installed": [ + ".htaccess", + "index.php", + "install-frameworkmissing.html", + "install.php", + "web.config" + ] + }, + "prefer-stable": true, + "minimum-stability": "dev" +} From 0cd42ff5ae30e88394a3f696655fa58c1c50a630 Mon Sep 17 00:00:00 2001 From: Maxime Rainville Date: Mon, 29 Oct 2018 17:51:37 +1300 Subject: [PATCH 09/13] Fix linting. --- src/Library.php | 11 ++++++----- tests/LibraryTest.php | 5 +---- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/src/Library.php b/src/Library.php index 2123d5b..835f07a 100644 --- a/src/Library.php +++ b/src/Library.php @@ -80,10 +80,9 @@ public function __construct( $basePath, $libraryPath, $name = null, - Composer $composer=null, + Composer $composer = null, IOInterface $io = null - ) - { + ) { $this->basePath = realpath($basePath); $this->path = realpath($libraryPath); $this->name = $name; @@ -362,7 +361,9 @@ public function getResourcesDir() // So we'll try to match it to an alias foreach ($aliases as $alias) { if ($alias['package'] === 'silverstripe/framework' && $alias['version'] === $frameworkVersion) { - $frameworkVersion = isset($alias['alias_normalized']) ? $alias['alias_normalized'] : $alias['alias']; + $frameworkVersion = isset($alias['alias_normalized']) + ? $alias['alias_normalized'] + : $alias['alias']; break; } } @@ -385,7 +386,7 @@ public function getResourcesDir() if (!preg_match('/[_\-a-z0-9]+/i', $resourcesDir)) { $resourcesDir = self::DEFAULT_RESOURCES_DIR; } - } elseif (Comparator::lessThan($frameworkVersion, self::CONFIGURABLE_FRAMEWORK_VERSION)) { + } elseif (Comparator::lessThan($frameworkVersion, self::CONFIGURABLE_FRAMEWORK_VERSION)) { // We're definitively running a framework that DOES NOT supports a configurable resources folder $resourcesDir = self::LEGACY_DEFAULT_RESOURCES_DIR; } else { diff --git a/tests/LibraryTest.php b/tests/LibraryTest.php index bbb5452..0e5b841 100644 --- a/tests/LibraryTest.php +++ b/tests/LibraryTest.php @@ -35,7 +35,6 @@ protected function tearDown() { parent::tearDown(); chdir($this->cwd); - } /** @@ -71,7 +70,7 @@ private function getLib($project, $preloadLock) $io = new NullIO(); - $composer = $factory->createComposer($io,null, false, $path); + $composer = $factory->createComposer($io, null, false, $path); $preloadLock && $composer->setLocker(new Locker( $io, new JsonFile($path . '/composer.lock', null, $io), @@ -90,6 +89,4 @@ private function getLib($project, $preloadLock) return $lib; } - - } From 5857f59782fd3940bd77b50c741cbb8878e2e7c9 Mon Sep 17 00:00:00 2001 From: Maxime Rainville Date: Tue, 30 Oct 2018 16:58:41 +1300 Subject: [PATCH 10/13] Use the composer file instead of the .env file --- composer.json | 3 +- src/Library.php | 36 ++++--------------- tests/LibraryTest.php | 24 +++++++------ .../composer.json | 3 +- tests/fixtures/projects/ss44WithEnv/.env | 1 - 5 files changed, 22 insertions(+), 45 deletions(-) rename tests/fixtures/projects/{ss44WithEnv => ss44WithCustomResourcesDir}/composer.json (92%) delete mode 100644 tests/fixtures/projects/ss44WithEnv/.env diff --git a/composer.json b/composer.json index 9223e35..0d73b0b 100644 --- a/composer.json +++ b/composer.json @@ -28,8 +28,7 @@ "minimum-stability": "dev", "require": { "composer/installers": "^1.4", - "composer-plugin-api": "^1.1", - "m1/env": "^2.1" + "composer-plugin-api": "^1.1" }, "require-dev": { "composer/composer": "^1.5", diff --git a/src/Library.php b/src/Library.php index 835f07a..d751eb8 100644 --- a/src/Library.php +++ b/src/Library.php @@ -335,7 +335,8 @@ public function getResourcesDir() } // Try to get our resource dir from our .env file - $ss_resources_dir = $this->getDotEnvVar('SS_RESOURCES_DIR'); + $extras = $this->composer->getPackage()->getExtra(); + $resourcesDirOverride = isset($extras['resources-dir']) ? $extras['resources-dir'] : ''; $frameworkVersion = ''; @@ -347,12 +348,12 @@ public function getResourcesDir() ->findPackage('silverstripe/framework', '*'); $aliases = $locker->getAliases(); } catch (LogicException $ex) { - // Fallback to the local repo, this won't allow us to get our aliases however. + // Fallback to the local repo $framework = $this->composer ->getRepositoryManager() ->getLocalRepository() ->findPackage('silverstripe/framework', '*'); - $aliases = []; + $aliases = $this->composer->getPackage()->getAliases(); } $frameworkVersion = $framework->getVersion(); @@ -382,7 +383,7 @@ public function getResourcesDir() if (Comparator::greaterThanOrEqualTo($frameworkVersion, self::CONFIGURABLE_FRAMEWORK_VERSION)) { // We're definitively running a framework that supports a configurable resources folder - $resourcesDir = $ss_resources_dir; + $resourcesDir = $resourcesDirOverride; if (!preg_match('/[_\-a-z0-9]+/i', $resourcesDir)) { $resourcesDir = self::DEFAULT_RESOURCES_DIR; } @@ -391,7 +392,7 @@ public function getResourcesDir() $resourcesDir = self::LEGACY_DEFAULT_RESOURCES_DIR; } else { // We're confused ... we'll try using the value from the .env file or we'll default to legacy. - $resourcesDir = $ss_resources_dir; + $resourcesDir = $resourcesDirOverride; if (!preg_match('/[_\-a-z0-9]+/i', $resourcesDir)) { $resourcesDir = self::LEGACY_DEFAULT_RESOURCES_DIR; } @@ -400,31 +401,6 @@ public function getResourcesDir() return $resourcesDir; } - /** - * Find a value from the environment. - * - * @param $key - * @return string|null - */ - private function getDotEnvVar($key) - { - if ($env = getenv($key)) { - return $env; - } - - $path = $this->getBasePath() . DIRECTORY_SEPARATOR . '.env'; - - // Not readable - if (!file_exists($path) || !is_readable($path)) { - return null; - } - - // Parse and cleanup content - $result = []; - $variables = Parser::parse(file_get_contents($path)); - return isset($variables[$key]) ? $variables[$key] : null; - } - /** * Find a Repository to interogate for our package versions. Tries to get it from the locker file first because * this one understand version alias. Fallsback to the local repository diff --git a/tests/LibraryTest.php b/tests/LibraryTest.php index 0e5b841..901055e 100644 --- a/tests/LibraryTest.php +++ b/tests/LibraryTest.php @@ -40,10 +40,8 @@ protected function tearDown() /** * @dataProvider resourcesDirProvider */ - public function testResourcesDir($expected, $projectPath, $env, $preloadLock) + public function testResourcesDir($expected, $projectPath, $preloadLock) { - putenv('SS_RESOURCES_DIR=' . $env); - $lib = $this->getLib($projectPath, $preloadLock); $this->assertEquals($expected, $lib->getResourcesDir()); } @@ -51,17 +49,21 @@ public function testResourcesDir($expected, $projectPath, $env, $preloadLock) public function resourcesDirProvider() { return [ - ['resources', 'ss43', '', false], - ['_resources', 'ss44', '', false], - ['etc', 'ss44', 'etc', false], - ['resources', 'ss43', 'etc', false], - ['resources', 'ss43', '', true], - ['_resources', 'ss44', '', true], - ['etc', 'ss44', 'etc', true], - ['environement-defined', 'ss44WithEnv', '', true], + ['resources', 'ss43', false], + ['_resources', 'ss44', false], + ['customised-resources-dir', 'ss44WithCustomResourcesDir', false], + ['resources', 'ss43', true], + ['_resources', 'ss44', true], + ['customised-resources-dir', 'ss44WithCustomResourcesDir', true], ]; } + /** + * Get a library for the provided project + * @param string $project name of the project folder in the fixtures directory + * @param bool $preloadLock Whatever to preload the lock file or let Library do that for us + * @return Library + */ private function getLib($project, $preloadLock) { $path = __DIR__ . '/fixtures/projects/' . $project; diff --git a/tests/fixtures/projects/ss44WithEnv/composer.json b/tests/fixtures/projects/ss44WithCustomResourcesDir/composer.json similarity index 92% rename from tests/fixtures/projects/ss44WithEnv/composer.json rename to tests/fixtures/projects/ss44WithCustomResourcesDir/composer.json index 7f9bd60..ee9e3ac 100644 --- a/tests/fixtures/projects/ss44WithEnv/composer.json +++ b/tests/fixtures/projects/ss44WithCustomResourcesDir/composer.json @@ -21,7 +21,8 @@ "install-frameworkmissing.html", "install.php", "web.config" - ] + ], + "resources-dir": "customised-resources-dir" }, "prefer-stable": true, "minimum-stability": "dev" diff --git a/tests/fixtures/projects/ss44WithEnv/.env b/tests/fixtures/projects/ss44WithEnv/.env deleted file mode 100644 index f6bd06a..0000000 --- a/tests/fixtures/projects/ss44WithEnv/.env +++ /dev/null @@ -1 +0,0 @@ -SS_RESOURCES_DIR=environement-defined From aa721cc0fe2db882612f26f2e5188077e1e08aa4 Mon Sep 17 00:00:00 2001 From: Maxime Rainville Date: Tue, 30 Oct 2018 18:35:15 +1300 Subject: [PATCH 11/13] Restore some of the old value of VendorModule --- src/VendorModule.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/VendorModule.php b/src/VendorModule.php index 2be180d..430d7cb 100644 --- a/src/VendorModule.php +++ b/src/VendorModule.php @@ -16,9 +16,9 @@ class VendorModule extends Library /** * Default replacement folder for 'vendor' - * @deprecated 1.4.0:2.0.0 Use global RESOURCES_DIR instead. + * @deprecated 1.4.0:2.0.0 Use global `Library::getResourcesDir()` instead. */ - const DEFAULT_TARGET = RESOURCES_DIR; + const DEFAULT_TARGET = 'resources'; /** * Build a vendor module library @@ -43,7 +43,7 @@ public function __construct($basePath, $name) * @param string $base Rewrite root (or 'vendor' for actual module path) * @return string Path for this module */ - public function getModulePath($base = RESOURCES_DIR) + public function getModulePath($base = self::DEFAULT_SOURCE) { if ($base === self::DEFAULT_TARGET) { return $this->getPublicPath(); From bb1c1bc19889ab80bf38278698a2a220afcc843e Mon Sep 17 00:00:00 2001 From: Maxime Rainville Date: Wed, 31 Oct 2018 15:09:59 +1300 Subject: [PATCH 12/13] Move away from framework detetection. --- src/Console/VendorExposeCommand.php | 2 +- src/Library.php | 133 +++--------------- src/VendorPlugin.php | 4 +- tests/LibraryTest.php | 74 ++-------- tests/fixtures/projects/ss44/composer.json | 3 +- .../ss44InvalidResourcesDir/composer.json | 29 ++++ 6 files changed, 64 insertions(+), 181 deletions(-) create mode 100644 tests/fixtures/projects/ss44InvalidResourcesDir/composer.json diff --git a/src/Console/VendorExposeCommand.php b/src/Console/VendorExposeCommand.php index 2c489ed..4630633 100644 --- a/src/Console/VendorExposeCommand.php +++ b/src/Console/VendorExposeCommand.php @@ -74,7 +74,7 @@ protected function getAllLibraries() } // Ensure this library should be exposed, and has at least one folder - $module = new Library($basePath, $modulePath, null, $this->getComposer(), $this->getIO()); + $module = new Library($basePath, $modulePath); if (!$module->requiresExpose() || !$module->getExposedFolders()) { continue; } diff --git a/src/Library.php b/src/Library.php index d751eb8..e2a4748 100644 --- a/src/Library.php +++ b/src/Library.php @@ -4,12 +4,12 @@ use Composer\Composer; use Composer\IO\IOInterface; +use Composer\IO\NullIO; use Composer\Json\JsonFile; use Composer\Package\Locker; use Composer\Package\Package; use Composer\Semver\Comparator; use LogicException; -use M1\Env\Parser; use SilverStripe\VendorPlugin\Methods\ExposeMethod; /** @@ -27,23 +27,13 @@ class Library /** * Default folder where vendor resources will be exposed. */ - const DEFAULT_RESOURCES_DIR = '_resources'; - - /** - * Default folder where vendor resources will be exposed if using a non-configurable framework - */ - const LEGACY_DEFAULT_RESOURCES_DIR = 'resources'; + const DEFAULT_RESOURCES_DIR = 'resources'; /** * Subfolder to map within public webroot - * @deprecated 1.4.0:2.0.0 Use Library::getResourceDir() instead. - */ - const RESOURCES_PATH = self::LEGACY_DEFAULT_RESOURCES_DIR; - - /** - * Version of `silverstripe/framework` from which + * @deprecated 1.4.0..2.0.0 Use Library::getResourcesDir() instead */ - const CONFIGURABLE_FRAMEWORK_VERSION = "4.4.0"; + const RESOURCES_PATH = self::DEFAULT_RESOURCES_DIR; /** * Project root @@ -64,11 +54,6 @@ class Library */ protected $composer = null; - /** - * @var IOInterface - */ - protected $io = null; - /** * Build a vendor module library * @@ -86,8 +71,6 @@ public function __construct( $this->basePath = realpath($basePath); $this->path = realpath($libraryPath); $this->name = $name; - $this->composer = $composer; - $this->io = $io; } /** @@ -329,105 +312,27 @@ protected function installedIntoVendor() */ public function getResourcesDir() { - if (!$this->composer) { - // We need a composer instance for this to work. This should never happen. - throw new LogicException('Could not find the targeted resource dir.'); - } - - // Try to get our resource dir from our .env file - $extras = $this->composer->getPackage()->getExtra(); - $resourcesDirOverride = isset($extras['resources-dir']) ? $extras['resources-dir'] : ''; - - $frameworkVersion = ''; - - if ($locker = $this->getLocker()) { - try { - // Try to get our package info from the locker - $framework = $locker - ->getLockedRepository() - ->findPackage('silverstripe/framework', '*'); - $aliases = $locker->getAliases(); - } catch (LogicException $ex) { - // Fallback to the local repo - $framework = $this->composer - ->getRepositoryManager() - ->getLocalRepository() - ->findPackage('silverstripe/framework', '*'); - $aliases = $this->composer->getPackage()->getAliases(); - } + $rootComposerFile = $this->getBasePath() . '/composer.json'; + $rootProject = new JsonFile($rootComposerFile, null, new NullIO()); - $frameworkVersion = $framework->getVersion(); - - // If we're running off a dev branch of framework, we might not get a clean version number. - // So we'll try to match it to an alias - foreach ($aliases as $alias) { - if ($alias['package'] === 'silverstripe/framework' && $alias['version'] === $frameworkVersion) { - $frameworkVersion = isset($alias['alias_normalized']) - ? $alias['alias_normalized'] - : $alias['alias']; - break; - } - } - } elseif ($repo = $this->composer->getRepositoryManager()->getLocalRepository()) { - $framework = $repo->findPackage('silverstripe/framework', '*'); - $frameworkVersion = $framework->getVersion(); - } - - if (!$frameworkVersion) { - throw new LogicException( - 'Could not find the targeted resource dir. SilverStripe Framework does not appear to be' . - 'installed. Try running a `composer update`. If the error persist, please report this issue at ' . - 'https://github.com/silverstripe/vendor-plugin/issues/new' - ); - } - - if (Comparator::greaterThanOrEqualTo($frameworkVersion, self::CONFIGURABLE_FRAMEWORK_VERSION)) { - // We're definitively running a framework that supports a configurable resources folder - $resourcesDir = $resourcesDirOverride; - if (!preg_match('/[_\-a-z0-9]+/i', $resourcesDir)) { - $resourcesDir = self::DEFAULT_RESOURCES_DIR; - } - } elseif (Comparator::lessThan($frameworkVersion, self::CONFIGURABLE_FRAMEWORK_VERSION)) { - // We're definitively running a framework that DOES NOT supports a configurable resources folder - $resourcesDir = self::LEGACY_DEFAULT_RESOURCES_DIR; - } else { - // We're confused ... we'll try using the value from the .env file or we'll default to legacy. - $resourcesDir = $resourcesDirOverride; - if (!preg_match('/[_\-a-z0-9]+/i', $resourcesDir)) { - $resourcesDir = self::LEGACY_DEFAULT_RESOURCES_DIR; - } + if (!$rootProject->exists()) { + return self::DEFAULT_RESOURCES_DIR; } - return $resourcesDir; - } + $rootProjectData = $rootProject->read(); + $resourcesDir = isset($rootProjectData['extra']['resources-dir']) + ? $rootProjectData['extra']['resources-dir'] + : self::DEFAULT_RESOURCES_DIR; - /** - * Find a Repository to interogate for our package versions. Tries to get it from the locker file first because - * this one understand version alias. Fallsback to the local repository - * @param Composer $composer - * @param IOInterface $io - * @return Locker - */ - private function getLocker() - { - // Some times getLocker will return null, so we can't rely on this - if ($locker = $this->composer->getLocker()) { - return $locker; - } - // Let's build our own locker from the lock file - $lockFile = $this->getBasePath() . DIRECTORY_SEPARATOR . 'composer.lock'; - if ($this->io && is_readable($lockFile)) { - $locker = new Locker( - $this->io, - new JsonFile($lockFile, null, $this->io), - $this->composer->getRepositoryManager(), - $this->composer->getInstallationManager(), - file_get_contents($lockFile) - ); - return $locker; + if (preg_match('/^[_\-a-z0-9]+$/i', $resourcesDir)) { + return $resourcesDir; } - return null; + throw new LogicException(sprintf( + 'Resources dir error: "%s" is not a valid resources directory name. Update the ' . + '`extra.resources-dir` key in your composer.json file', + $resourcesDir + )); } } diff --git a/src/VendorPlugin.php b/src/VendorPlugin.php index 7a97c54..2342405 100644 --- a/src/VendorPlugin.php +++ b/src/VendorPlugin.php @@ -129,7 +129,7 @@ public function getLibrary(PackageEvent $event) $path = $installer->getInstallPath($package); // Build module - return new Library($this->getProjectPath(), $path, null, $event->getComposer(), $event->getIO()); + return new Library($this->getProjectPath(), $path); } /** @@ -158,7 +158,7 @@ public function installRootPackage(Event $event) { // Build library in base path $basePath = $this->getProjectPath(); - $library = new Library($basePath, $basePath, null, $event->getComposer(), $event->getIO()); + $library = new Library($basePath, $basePath); // Pass to library installer $this->installLibrary($event->getIO(), $library); diff --git a/tests/LibraryTest.php b/tests/LibraryTest.php index 901055e..4a7d670 100644 --- a/tests/LibraryTest.php +++ b/tests/LibraryTest.php @@ -13,82 +13,30 @@ class LibraryTest extends TestCase { - /** - * @var Filesystem - */ - protected $filesystem = null; - - /** - * @var string app base path - */ - protected $root = null; - - protected $cwd; - - protected function setUp() - { - parent::setUp(); - $this->cwd = getcwd(); - } - - protected function tearDown() - { - parent::tearDown(); - chdir($this->cwd); - } - /** * @dataProvider resourcesDirProvider */ - public function testResourcesDir($expected, $projectPath, $preloadLock) + public function testResourcesDir($expected, $projectPath) { - $lib = $this->getLib($projectPath, $preloadLock); + $path = __DIR__ . '/fixtures/projects/' . $projectPath; + $lib = new Library($path,'vendor/silverstripe/skynet'); $this->assertEquals($expected, $lib->getResourcesDir()); } public function resourcesDirProvider() { return [ - ['resources', 'ss43', false], - ['_resources', 'ss44', false], - ['customised-resources-dir', 'ss44WithCustomResourcesDir', false], - ['resources', 'ss43', true], - ['_resources', 'ss44', true], - ['customised-resources-dir', 'ss44WithCustomResourcesDir', true], + ['resources', 'ss43'], + ['_resources', 'ss44'], + ['customised-resources-dir', 'ss44WithCustomResourcesDir'] ]; } - /** - * Get a library for the provided project - * @param string $project name of the project folder in the fixtures directory - * @param bool $preloadLock Whatever to preload the lock file or let Library do that for us - * @return Library - */ - private function getLib($project, $preloadLock) + public function testInvalidResourceDir() { - $path = __DIR__ . '/fixtures/projects/' . $project; - chdir($path); - $factory = new Factory(); - - - $io = new NullIO(); - $composer = $factory->createComposer($io, null, false, $path); - $preloadLock && $composer->setLocker(new Locker( - $io, - new JsonFile($path . '/composer.lock', null, $io), - $composer->getRepositoryManager(), - $composer->getInstallationManager(), - file_get_contents($path . '/composer.lock') - )); - - $lib = new Library( - $path, - 'vendor/silverstripe/skynet', - null, - $composer, - $io - ); - - return $lib; + $this->expectException(\LogicException::class); + $path = __DIR__ . '/fixtures/projects/ss44InvalidResourcesDir'; + $lib = new Library($path,'vendor/silverstripe/skynet'); + $lib->getResourcesDir(); } } diff --git a/tests/fixtures/projects/ss44/composer.json b/tests/fixtures/projects/ss44/composer.json index 7f9bd60..02288d6 100644 --- a/tests/fixtures/projects/ss44/composer.json +++ b/tests/fixtures/projects/ss44/composer.json @@ -21,7 +21,8 @@ "install-frameworkmissing.html", "install.php", "web.config" - ] + ], + "resources-dir": "_resources" }, "prefer-stable": true, "minimum-stability": "dev" diff --git a/tests/fixtures/projects/ss44InvalidResourcesDir/composer.json b/tests/fixtures/projects/ss44InvalidResourcesDir/composer.json new file mode 100644 index 0000000..37b967f --- /dev/null +++ b/tests/fixtures/projects/ss44InvalidResourcesDir/composer.json @@ -0,0 +1,29 @@ +{ + "name": "silverstripe/ss44", + "type": "silverstripe-project", + "description": "Fake project using SS 4.4", + "homepage": "https://www.silverstripe.org", + "license": "BSD-3-Clause", + "require": { + "silverstripe/recipe-cms": "4.4.x-dev as 4.4.0" + }, + "extra": { + "project-files-installed": [ + "app/.htaccess", + "app/_config.php", + "app/_config/mysite.yml", + "app/src/Page.php", + "app/src/PageController.php" + ], + "public-files-installed": [ + ".htaccess", + "index.php", + "install-frameworkmissing.html", + "install.php", + "web.config" + ], + "resources-dir": "Vendor plugin will throw a !$%^#$!#$!&^# fit!!!" + }, + "prefer-stable": true, + "minimum-stability": "dev" +} From 0957d8e92d6f1bbbdb105fcc3b4a9c35c6d2b00b Mon Sep 17 00:00:00 2001 From: Maxime Rainville Date: Wed, 31 Oct 2018 15:48:15 +1300 Subject: [PATCH 13/13] Remove unused use statemeent. --- src/Library.php | 14 +------------- src/VendorModule.php | 1 - src/VendorPlugin.php | 5 ----- tests/LibraryTest.php | 10 ++-------- 4 files changed, 3 insertions(+), 27 deletions(-) diff --git a/src/Library.php b/src/Library.php index e2a4748..152d075 100644 --- a/src/Library.php +++ b/src/Library.php @@ -2,13 +2,8 @@ namespace SilverStripe\VendorPlugin; -use Composer\Composer; -use Composer\IO\IOInterface; use Composer\IO\NullIO; use Composer\Json\JsonFile; -use Composer\Package\Locker; -use Composer\Package\Package; -use Composer\Semver\Comparator; use LogicException; use SilverStripe\VendorPlugin\Methods\ExposeMethod; @@ -49,11 +44,6 @@ class Library */ protected $path = null; - /** - * @var Composer - */ - protected $composer = null; - /** * Build a vendor module library * @@ -64,9 +54,7 @@ class Library public function __construct( $basePath, $libraryPath, - $name = null, - Composer $composer = null, - IOInterface $io = null + $name = null ) { $this->basePath = realpath($basePath); $this->path = realpath($libraryPath); diff --git a/src/VendorModule.php b/src/VendorModule.php index 430d7cb..6e4b2e6 100644 --- a/src/VendorModule.php +++ b/src/VendorModule.php @@ -16,7 +16,6 @@ class VendorModule extends Library /** * Default replacement folder for 'vendor' - * @deprecated 1.4.0:2.0.0 Use global `Library::getResourcesDir()` instead. */ const DEFAULT_TARGET = 'resources'; diff --git a/src/VendorPlugin.php b/src/VendorPlugin.php index 2342405..438e8e7 100644 --- a/src/VendorPlugin.php +++ b/src/VendorPlugin.php @@ -10,17 +10,12 @@ use Composer\Factory; use Composer\Installer\PackageEvent; use Composer\IO\IOInterface; -use Composer\Json\JsonFile; -use Composer\Package\Locker; use Composer\Package\PackageInterface; use Composer\Plugin\Capability\CommandProvider; use Composer\Plugin\Capable; use Composer\Plugin\PluginInterface; -use Composer\Repository\RepositoryInterface; use Composer\Script\Event; -use Composer\Semver\Comparator; use Composer\Util\Filesystem; -use M1\Env\Parser; use SilverStripe\VendorPlugin\Console\VendorCommandProvider; /** diff --git a/tests/LibraryTest.php b/tests/LibraryTest.php index 4a7d670..f375e39 100644 --- a/tests/LibraryTest.php +++ b/tests/LibraryTest.php @@ -2,12 +2,6 @@ namespace SilverStripe\VendorPlugin\Tests\Methods; -use Composer\Composer; -use Composer\Factory; -use Composer\IO\NullIO; -use Composer\Json\JsonFile; -use Composer\Package\Locker; -use Composer\Repository\RepositoryManager; use PHPUnit\Framework\TestCase; use SilverStripe\VendorPlugin\Library; @@ -19,7 +13,7 @@ class LibraryTest extends TestCase public function testResourcesDir($expected, $projectPath) { $path = __DIR__ . '/fixtures/projects/' . $projectPath; - $lib = new Library($path,'vendor/silverstripe/skynet'); + $lib = new Library($path, 'vendor/silverstripe/skynet'); $this->assertEquals($expected, $lib->getResourcesDir()); } @@ -36,7 +30,7 @@ public function testInvalidResourceDir() { $this->expectException(\LogicException::class); $path = __DIR__ . '/fixtures/projects/ss44InvalidResourcesDir'; - $lib = new Library($path,'vendor/silverstripe/skynet'); + $lib = new Library($path, 'vendor/silverstripe/skynet'); $lib->getResourcesDir(); } }