Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolves #20: Filter package name based on Packagist requirements #21

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 34 additions & 9 deletions customize/Customizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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\\']);
Expand Down Expand Up @@ -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();
Expand Down
38 changes: 38 additions & 0 deletions tests/unit/Customizer/CustomizerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace ExampleProject\unit\Customizer;

use CustomizeProject\Customizer;
use PHPUnit\Framework\TestCase;

class CustomizerTest extends TestCase
{

/**
* @test
*/
public function itEnsuresValidComposerProjectNames()
{
$customizer = new Customizer();
$pascalExample = 'CoolCorp/LeetNinja!';
$this->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)
);
}
}