Skip to content

Commit f7cf9d2

Browse files
committed
Merge pull request hechoendrupal#612 from sethfischer/composer.json
Add composer.json to ModuleGenerator
2 parents 7646671 + 4b02e22 commit f7cf9d2

6 files changed

Lines changed: 52 additions & 11 deletions

File tree

Test/Command/GeneratorModuleCommandTest.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ class GeneratorModuleCommandTest extends GenerateCommandTest
1515
*/
1616
public function testInteractive($options, $expected, $input)
1717
{
18-
list($module, $machine_name, $dir, $description, $core, $package, $controller, $dependencies, $test) = $expected;
18+
list($module, $machine_name, $dir, $description, $core, $package, $controller, $composer, $dependencies, $test) = $expected;
1919

2020
$generator = $this->getGenerator();
2121

2222
$generator
2323
->expects($this->once())
2424
->method('generate')
25-
->with($module, $machine_name, $dir, $description, $core, $package, $controller, $dependencies, $test);
25+
->with($module, $machine_name, $dir, $description, $core, $package, $controller, $composer, $dependencies, $test);
2626

2727
$command = $this->getCommand($generator, $input);
2828

@@ -38,7 +38,7 @@ public function getInteractiveData()
3838
// case one basic options
3939
[
4040
[],
41-
['foo', 'foo', $dir, 'My Awesome Module', '8.x', 'Other', false, [], false],
41+
['foo', 'foo', $dir, 'My Awesome Module', '8.x', 'Other', false, false, [], false],
4242
"foo\nfoo\n$dir\n"
4343
],
4444
];
@@ -49,14 +49,14 @@ public function getInteractiveData()
4949
*/
5050
public function testNoInteractive($options, $expected)
5151
{
52-
list($module, $machine_name, $dir, $description, $core, $package, $controller, $dependencies, $test) = $expected;
52+
list($module, $machine_name, $dir, $description, $core, $package, $controller, $composer, $dependencies, $test) = $expected;
5353

5454
$generator = $this->getGenerator();
5555

5656
$generator
5757
->expects($this->once())
5858
->method('generate')
59-
->with($module, $machine_name, $dir, $description, $core, $package, $controller, $dependencies, $test);
59+
->with($module, $machine_name, $dir, $description, $core, $package, $controller, $composer, $dependencies, $test);
6060

6161
$cmd = new CommandTester($this->getCommand($generator, ''));
6262
$cmd->execute($options, ['interactive' => false]);
@@ -75,10 +75,11 @@ public function getNoInteractiveData()
7575
'--description' => 'My Awesome Module',
7676
'--core' => '8.x',
7777
'--package' => 'Other',
78+
'--composer' => false,
7879
'--controller' => true,
7980
'--test' => true
8081
],
81-
["foo", "foo", $dir, "My Awesome Module", '8.x', 'Other', true, [], true],
82+
["foo", "foo", $dir, "My Awesome Module", '8.x', 'Other', true, false, [], true],
8283
],
8384
[
8485
[
@@ -88,10 +89,11 @@ public function getNoInteractiveData()
8889
'--description' => 'My Awesome Module',
8990
'--core' => '8.x',
9091
'--package' => 'Other',
92+
'--composer' => false,
9193
'--controller' => true,
9294
'--test' => true
9395
],
94-
["foo", 'foo', $dir, "My Awesome Module", '8.x', 'Other', true, [], true],
96+
["foo", 'foo', $dir, "My Awesome Module", '8.x', 'Other', true, false, [], true],
9597
]
9698
];
9799
}

Test/Generator/ModuleGeneratorTest.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public function testGenerateModule($parameters)
2525
$core,
2626
$package,
2727
$controller,
28+
$composer,
2829
$dependencies,
2930
$tests
3031
) = $parameters;
@@ -37,6 +38,7 @@ public function testGenerateModule($parameters)
3738
$core,
3839
$package,
3940
$controller,
41+
$composer,
4042
$dependencies,
4143
$tests
4244
);
@@ -84,16 +86,16 @@ public function commandData()
8486

8587
return [
8688
[
87-
['Foo', 'foo' . rand(), $this->dir, 'Description', '8.x', 'Other', false, null, false],
89+
['Foo', 'foo' . rand(), $this->dir, 'Description', '8.x', 'Other', false, false, null, false],
8890
],
8991
[
90-
['Foo', 'foo' . rand(), $this->dir, 'Description', '8.x', 'Other', false, null, true],
92+
['Foo', 'foo' . rand(), $this->dir, 'Description', '8.x', 'Other', false, false, null, true],
9193
],
9294
[
93-
['Foo', 'foo' . rand(), $this->dir, 'Description', '8.x', 'Other', false, null, false],
95+
['Foo', 'foo' . rand(), $this->dir, 'Description', '8.x', 'Other', false, false, null, false],
9496
],
9597
[
96-
['Foo', 'foo' . rand(), $this->dir, 'Description', '8.x', 'Other', true, null, true],
98+
['Foo', 'foo' . rand(), $this->dir, 'Description', '8.x', 'Other', true, true, null, true],
9799
],
98100
];
99101
}

config/translations/console.en.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ commands:
201201
core: Core version
202202
package: Module package
203203
controller: Default Controller
204+
composer: Add a composer.json file
204205
dependencies: Module dependencies separated by commas (i.e. context, panels)
205206
test: Generate a test class
206207
questions:
@@ -211,6 +212,7 @@ commands:
211212
core: Enter Drupal Core version
212213
package: Enter package name
213214
controller: Do you want to generate a default Controller
215+
composer: Do you want to add a composer.json file to your module
214216
dependencies: Would you like to add module depencencies
215217
test: Do you want to generate a unit test class
216218
warnings:

src/Command/GeneratorModuleCommand.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ protected function configure()
3838
$this->trans('commands.generate.module.options.package'))
3939
->addOption('controller', '', InputOption::VALUE_NONE,
4040
$this->trans('commands.generate.module.options.controller'))
41+
->addOption('composer', '', InputOption::VALUE_NONE,
42+
$this->trans('commands.generate.module.options.composer'))
4143
->addOption('dependencies', '', InputOption::VALUE_OPTIONAL,
4244
$this->trans('commands.generate.module.options.dependencies'))
4345
->addOption('test', '', InputOption::VALUE_NONE, $this->trans('commands.generate.module.options.test'));
@@ -68,6 +70,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
6870
$core = $input->getOption('core');
6971
$package = $input->getOption('package');
7072
$controller = $input->getOption('controller');
73+
$composer = $input->getOption('composer');
7174
/**
7275
* Modules Dependencies
7376
*
@@ -100,6 +103,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
100103
$core,
101104
$package,
102105
$controller,
106+
$composer,
103107
$dependencies,
104108
$test
105109
);
@@ -240,6 +244,14 @@ function ($module_path) use ($drupal_root, $machine_name) {
240244
}
241245
$input->setOption('controller', $controller);
242246

247+
$composer = $input->getOption('composer');
248+
if (!$composer && $dialog->askConfirmation($output,
249+
$dialog->getQuestion($this->trans('commands.generate.module.questions.composer'), 'no', '?'), false)
250+
) {
251+
$composer = true;
252+
}
253+
$input->setOption('composer', $composer);
254+
243255
$dependencies = $input->getOption('dependencies');
244256
if (!$dependencies) {
245257
if ($dialog->askConfirmation($output,

src/Generator/ModuleGenerator.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public function generate(
1717
$core,
1818
$package,
1919
$controller,
20+
$composer,
2021
$dependencies,
2122
$tests
2223
) {
@@ -59,6 +60,14 @@ public function generate(
5960
$parameters
6061
);
6162

63+
if ($composer) {
64+
$this->renderFile(
65+
'module/composer.json.twig',
66+
$dir . '/' . 'composer.json',
67+
$parameters
68+
);
69+
}
70+
6271
if ($controller) {
6372
$class_name = 'DefaultController';
6473
$parameters = array(
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "drupal/{{ machine_name }}",
3+
"type": "{{ type }}",
4+
"description": "{{ description }}",
5+
"keywords": ["Drupal"],
6+
"license": "GPL-2.0+",
7+
"homepage": "http://drupal.org/project/{{ machine_name }}",
8+
"minimum-stability": "dev",
9+
"support": {
10+
"issues": "http://drupal.org/project/{{ machine_name }}",
11+
"source": "http://cgit.drupalcode.org/{{ machine_name }}"
12+
},
13+
"require": { }
14+
}

0 commit comments

Comments
 (0)