From 82415fdd705aa7a4130154476c83b9e5591c69b1 Mon Sep 17 00:00:00 2001 From: Teppo Koivula Date: Sun, 16 Jun 2019 14:37:15 +0300 Subject: [PATCH] Automatically PascalCase module directory name and attempt to resolve site/site/ issues when installing modules from site directory --- CHANGELOG.md | 6 +++ src/ComposerInstaller/BaseInstaller.php | 8 +++- src/ComposerInstaller/ModuleInstaller.php | 38 +++++++++++++++++++ .../SiteProfileInstaller.php | 2 +- 4 files changed, 52 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5a7581a..d118c27 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [0.0.5] - 2019-06-16 + +### Changed +- Convert installed module directories automatically to Pascal Case. +- Modify base path when installing from the site directory to avoid nested site/site/ directories. + ## [0.0.4] - 2019-06-10 ### Fixed diff --git a/src/ComposerInstaller/BaseInstaller.php b/src/ComposerInstaller/BaseInstaller.php index 99cf07f..48872f8 100644 --- a/src/ComposerInstaller/BaseInstaller.php +++ b/src/ComposerInstaller/BaseInstaller.php @@ -43,6 +43,12 @@ public function supports($packageType) * @return string Base path. */ protected function getBasePath($defaultBasePath) { + // if we're already in the site directory, remove "site/" from the beginning + // of the default base path to avoid creating nested /site/site/ directories + if (basename(\getcwd()) === 'site' && substr($defaultBasePath, 0, 5) === 'site/') { + $defaultBasePath = substr($defaultBasePath, 5); + } + // get the extra configuration of the top-level package $extra = []; if ($rootPackage = $this->composer->getPackage()) { @@ -70,7 +76,7 @@ protected function getBasePath($defaultBasePath) { * "installer-name" in the composer.json of the package in question. * * @param PackageInterface $package - * @return string Module or site profile name. + * @return string Module or site profile directory name. */ protected function getName(PackageInterface $package) { // determine the directory name from its package name diff --git a/src/ComposerInstaller/ModuleInstaller.php b/src/ComposerInstaller/ModuleInstaller.php index 73a416e..5ae8ce3 100644 --- a/src/ComposerInstaller/ModuleInstaller.php +++ b/src/ComposerInstaller/ModuleInstaller.php @@ -35,4 +35,42 @@ public function getInstallPath(PackageInterface $package) { return $this->getFullPath($package, static::BASE_PATH); } + + /** + * Get the directory name from package name + * + * Default directory name can be overridden by specifying extra argument + * "installer-name" in the composer.json of the package in question. + * + * @param PackageInterface $package + * @return string Module directory name. + */ + protected function getName(PackageInterface $package) { + // fetch initial directory name + $name = parent::getName($package); + + // make sure that the directory name is in Pascal Case + $name = $this->pascalCase($name); + + return $name; + } + + /** + * Convert string to Pascal Case + * + * Typically used for ProcessWire module directory names, which are loosely + * expected to follow a PascalCase type naming convention. + * + * @param string $string + * @return string String in PascalCase + */ + protected function pascalCase($string) { + $replace_chars = [ + '_', + '-', + '/', + ' ' + ]; + return str_replace($replace_chars, '', ucwords($string, implode($replace_chars))); + } } diff --git a/src/ComposerInstaller/SiteProfileInstaller.php b/src/ComposerInstaller/SiteProfileInstaller.php index b0ebfc4..c9c182b 100644 --- a/src/ComposerInstaller/SiteProfileInstaller.php +++ b/src/ComposerInstaller/SiteProfileInstaller.php @@ -67,7 +67,7 @@ public function getInstallPath(PackageInterface $package) * "installer-name" in the composer.json of the package in question. * * @param PackageInterface $package - * @return string Module or site profile name. + * @return string Site profile directory name. */ protected function getName(PackageInterface $package) { // fetch initial directory name