diff --git a/customize/Customizer.php b/customize/Customizer.php index 52f7619..170d918 100644 --- a/customize/Customizer.php +++ b/customize/Customizer.php @@ -31,14 +31,11 @@ public static function customize() static::loadGuzzleFunctions(); $customizer = new self(); - try - { + try { $customizer->run(); - } - catch (\Exception $e) - { + } catch (\Exception $e) { print $e->getMessage() . "\n"; - exit ($e->getCode()); + exit($e->getCode()); } } @@ -97,8 +94,7 @@ public function run() $this->passthru('git add .'); $this->passthru('git rm -r --cached customize'); $this->passthru('git commit -m "Initial commit of unmodified template project [ci skip]."'); - } - else { + } else { // If we are re-using an existing repo, make sure that the // origin is set correctly. If there is no origin, then // 'hub' will set the origin. @@ -230,10 +226,29 @@ protected function readComposerJson($composer_path) return json_decode($composer_contents, true); } + /** + * Format the name of the project for Composer/Packagist compatibility. + * + * @param string $name + * The Incoming name, which could contain incompatible characters. + * + * @return string + * The name formatted for Composer/Packagist. + * + * @see https://packagist.org/about + */ + public function formatNameForPackagist(string $name = ''): string + { + return array_reduce(explode('/', $name), function ($carry, $segment) { + return $carry === "" ? $this->formatNameSegment($segment) : + $carry . "/" . $this->formatNameSegment($segment); + }, ""); + } + protected function adjustComposerJson($composer_path, $composer_data) { // Fix the name - $composer_data['name'] = $this->project_name_and_org; + $composer_data['name'] = $this->formatNameForPackagist($this->project_name_and_org); // Remove parts of autoloader that are no longer going to be used. unset($composer_data['autoload']['psr-4']['CustomizeProject\\']); @@ -304,6 +319,16 @@ protected function operateOnAllProjectFiles($replacements, $fn, $parameter = fal } } + /** + * Remove characters not allowed by Packagist. + * @param string $name + * @return string + */ + public function formatNameSegment(string $name): string + { + return preg_replace('/[^a-z0-9.\-_]/i', '', strtolower($name)); + } + protected function replaceProjectFileOrTemplate($replacements, $file, $template_dir) { $source_file = $file->getRealPath(); diff --git a/tests/unit/Customizer/CustomizerTest.php b/tests/unit/Customizer/CustomizerTest.php new file mode 100644 index 0000000..eeb86f3 --- /dev/null +++ b/tests/unit/Customizer/CustomizerTest.php @@ -0,0 +1,38 @@ +assertEquals( + 'coolcorp/leetninja', + $customizer->formatNameForPackagist($pascalExample) + ); + $camelExample = 'coolCorp/LeetNinja!'; + $this->assertEquals( + 'coolcorp/leetninja', + $customizer->formatNameForPackagist($camelExample) + ); + $kebabExample = 'cool-corp@/leet-ninja!'; + $this->assertEquals( + 'cool-corp/leet-ninja', + $customizer->formatNameForPackagist($kebabExample) + ); + $numericExample = 'cool-corp1/leet-ninja4'; + $this->assertEquals( + 'cool-corp1/leet-ninja4', + $customizer->formatNameForPackagist($numericExample) + ); + } +}