Skip to content

Commit fbb0e6c

Browse files
committed
Fix composer command
1 parent 3508f0b commit fbb0e6c

File tree

5 files changed

+72
-20
lines changed

5 files changed

+72
-20
lines changed

scripts/run-sut-tests.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ if [[ $TARGET_TEST = all || $TARGET_TEST = module_component ]]; then
132132
cp -R $SELF_PATH/$MODULE_MACHINE_NAME $MODULE_DIR
133133
cd $MODULE_DIR
134134

135-
$DCG composer -a bar -a 'Bar project' -a 'drupal-module' -a Yes
135+
$DCG composer -a drupal/bar -a 'Bar project' -a 'drupal-module' -a Yes
136136
$DCG controller -a Bar -a bar -a BarController -a No -a Yes -a bar.example -a /bar/example -a Example -a 'access content'
137137
$DCG install -a Bar -a bar
138138
$DCG javascript -a Bar -a bar -a heavy-metal.js -a Yes -a heavy_metal

src/Command/Composer.php

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace DrupalCodeGenerator\Command;
44

55
use DrupalCodeGenerator\Application;
6-
use Symfony\Component\Console\Question\Question;
76

87
/**
98
* Implements composer command.
@@ -15,19 +14,24 @@ final class Composer extends DrupalGenerator {
1514
protected string $alias = 'composer.json';
1615
protected string $label = 'composer.json';
1716
protected string $templatePath = Application::TEMPLATE_PATH . '/composer';
18-
protected ?string $nameQuestion = NULL;
19-
protected ?string $machineNameQuestion = 'Project machine name';
2017

2118
/**
2219
* {@inheritdoc}
2320
*/
2421
protected function generate(array &$vars): void {
25-
$this->collectDefault($vars);
22+
// @see https://getcomposer.org/doc/04-schema.md#name
23+
$validator = static function (string $input): string {
24+
if (!\preg_match('#^[a-z0-9]([_.-]?[a-z0-9]+)*/[a-z0-9](([_.]?|-{0,2})[a-z0-9]+)*$#', $input)) {
25+
throw new \UnexpectedValueException('The package name sdsdf is invalid, it should be lowercase and have a vendor name, a forward slash, and a package name.');
26+
}
27+
return $input;
28+
};
29+
$vars['project_name'] = $this->ask('Project name', 'drupal/example', $validator);
30+
[, $vars['machine_name']] = \explode('/', $vars['project_name']);
31+
2632
$vars['description'] = $this->ask('Description');
2733

28-
$type_question = new Question('Type', 'drupal-module');
29-
$type_question->setValidator([self::class, 'validateRequired']);
30-
$type_question->setAutocompleterValues([
34+
$type_choices = [
3135
'drupal-module',
3236
'drupal-custom-module',
3337
'drupal-theme',
@@ -36,20 +40,20 @@ protected function generate(array &$vars): void {
3640
'drupal-profile',
3741
'drupal-custom-profile',
3842
'drupal-drush',
39-
]);
40-
$vars['type'] = $this->io->askQuestion($type_question);
43+
];
44+
$vars['type'] = $this->choice('Project type', \array_combine($type_choices, $type_choices));
4145

4246
$custom_types = [
4347
'drupal-custom-module',
4448
'drupal-custom-theme',
4549
'drupal-custom-profile',
4650
];
47-
if (!\in_array($vars['type'], $custom_types)) {
48-
// If project type is custom, there is no reason to ask this.
49-
$vars['drupal_org'] = $this->confirm('Is this project hosted on drupal.org?', FALSE);
51+
if (\in_array($vars['type'], $custom_types)) {
52+
$vars['drupal_org'] = FALSE;
5053
}
5154
else {
52-
$vars['drupal_org'] = FALSE;
55+
// If project type is custom, there is no reason to ask this.
56+
$vars['drupal_org'] = $this->confirm('Is this project hosted on drupal.org?', FALSE);
5357
}
5458

5559
$this->addFile('composer.json', 'composer');

templates/composer/composer.twig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "drupal/{{ machine_name }}",
2+
"name": "{{ project_name }}",
33
"type": "{{ type }}",
44
"description": "{{ description }}",
55
"keywords": ["Drupal"]{{ drupal_org ? ',' }}
@@ -9,7 +9,7 @@
99
"authors": [
1010
{
1111
"name": "Your name here",
12-
"homepage": "https://www.drupal.org/u/your_name_here",
12+
"homepage": "https://www.drupal.org/u/YOUR_NAME_HERE",
1313
"role": "Maintainer"
1414
},
1515
{

tests/dcg/Generator/ComposerTest.php

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,70 @@
22

33
namespace DrupalCodeGenerator\Tests\Generator;
44

5+
use DrupalCodeGenerator\Command\Composer;
6+
use DrupalCodeGenerator\Test\GeneratorTest;
7+
58
/**
69
* Test for composer command.
710
*/
8-
final class ComposerTest extends BaseGeneratorTest {
11+
final class ComposerTest extends GeneratorTest {
912

1013
protected string $class = 'Composer';
1114

1215
protected array $interaction = [
1316
'Project machine name [%default_machine_name%]:' => 'example',
1417
'Description:' => 'Example description.',
15-
'Type [drupal-module]:' => 'drupal-module',
1618
'Is this project hosted on drupal.org? [No]:' => 'Yes',
1719
];
1820

1921
protected array $fixtures = [
2022
'composer.json' => '/_composer.json',
2123
];
2224

25+
protected string $fixtureDir = __DIR__;
26+
27+
/**
28+
* Test callback.
29+
*/
30+
public function testGenerator(): void {
31+
32+
$user_input = ['drupal/example', 'Some description.', 'drupal-module', 'Yes', 'Yes'];
33+
$this->execute(new Composer(), $user_input);
34+
35+
$expected_display = <<< 'TXT'
36+
37+
Welcome to composer.json generator!
38+
–––––––––––––––––––––––––––––––––––––
39+
40+
Project name [drupal/example]:
41+
42+
43+
Description:
44+
45+
46+
Project type:
47+
[1] drupal-module
48+
[2] drupal-custom-module
49+
[3] drupal-theme
50+
[4] drupal-custom-theme
51+
[5] drupal-library
52+
[6] drupal-profile
53+
[7] drupal-custom-profile
54+
[8] drupal-drush
55+
56+
57+
Is this project hosted on drupal.org? [No]:
58+
59+
60+
The following directories and files have been created or updated:
61+
–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
62+
• composer.json
63+
64+
65+
TXT;
66+
$this->assertDisplay($expected_display);
67+
68+
$this->assertGeneratedFile('composer.json', '_composer.json');
69+
}
70+
2371
}

tests/dcg/Generator/_composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
{
22
"name": "drupal/example",
33
"type": "drupal-module",
4-
"description": "Example description.",
4+
"description": "Some description.",
55
"keywords": ["Drupal"],
66
"license": "GPL-2.0+",
77
"homepage": "https://www.drupal.org/project/example",
88
"authors": [
99
{
1010
"name": "Your name here",
11-
"homepage": "https://www.drupal.org/u/your_name_here",
11+
"homepage": "https://www.drupal.org/u/YOUR_NAME_HERE",
1212
"role": "Maintainer"
1313
},
1414
{

0 commit comments

Comments
 (0)