Skip to content

Commit 0d7d48e

Browse files
authored
Merge pull request #43 from BrainMaestro/feature/handles-scripts-with-multiple-commands
Feature/handles scripts with multiple commands
2 parents 0ce5e37 + 70f6cf4 commit 0d7d48e

10 files changed

+124
-47
lines changed

Diff for: CONTRIBUTING.md

+10-8
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,23 @@ We accept contributions via Pull Requests on [Github](https://github.com/BrainMa
66

77
## Pull Requests
88

9-
- **[PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)** - The easiest way to apply the conventions is run command:
10-
```sh
9+
- **[PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)** - We use git hooks to enforce the style :). The easiest way to apply the conventions is run command:
10+
11+
```sh
1112
composer fix-style
1213
```
13-
- **Add tests!** - Your patch won't be accepted if it doesn't have tests.
1414

15-
- **Document any change in behaviour** - Make sure the [README.md](README.md) and any other relevant documentation are kept up-to-date.
15+
- **Add tests!** - Your patch won't be accepted if it doesn't have tests.
16+
17+
- **Document any change in behaviour** - Make sure the [README.md](README.md) and any other relevant documentation are kept up-to-date.
1618

17-
- **Consider our release cycle** - We try to follow [SemVer v2.0.0](http://semver.org/). Randomly breaking public APIs is not an option.
19+
- **Consider our release cycle** - We try to follow [SemVer v2.0.0](http://semver.org/). Randomly breaking public APIs is not an option.
1820

19-
- **Create feature branches** - Don't ask us to pull from your master branch.
21+
- **Create feature branches** - Don't ask us to pull from your master branch.
2022

21-
- **One pull request per feature** - If you want to do more than one thing, send multiple pull requests.
23+
- **One pull request per feature** - If you want to do more than one thing, send multiple pull requests.
2224

23-
- **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please [squash them](http://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting.
25+
- **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please [squash them](http://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting.
2426

2527
## Running Tests
2628

Diff for: Dockerfile

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@ WORKDIR /app
44

55
COPY ./composer.json ./composer.lock /app/
66

7+
# Remove any scripts that have cghooks since it is not yet present in the container
8+
RUN sed -iE '/\.\/cghooks .*/d' composer.json
9+
710
RUN composer install
811

912
COPY . /app/
1013

11-
RUN ./vendor/bin/phpunit
14+
RUN composer fix-style && composer test && ./cghooks add

Diff for: Makefile

+3
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,6 @@
22

33
test:
44
docker build --rm -t cghooks .
5+
6+
exec:
7+
docker run --rm -it cghooks sh

Diff for: README.md

+32-32
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,19 @@ Add a `hooks` section to the `extra` section of your `composer.json` and add the
1313

1414
```json
1515
{
16-
"extra": {
17-
"hooks": {
18-
"pre-commit": "phpunit",
19-
"post-commit": "echo committed",
20-
"pre-push": "phpunit && echo pushing!",
21-
"...": "..."
16+
"extra": {
17+
"hooks": {
18+
"pre-commit": "phpunit",
19+
"post-commit": "echo committed",
20+
"pre-push": ["phpunit", "echo pushing!"],
21+
"...": "..."
22+
}
2223
}
23-
}
2424
}
2525
```
2626

2727
Then install the library with
28+
2829
```sh
2930
composer require --dev brainmaestro/composer-git-hooks
3031
```
@@ -39,10 +40,10 @@ Add a `cghooks` script to the `scripts` section of your `composer.json` file. Th
3940

4041
```json
4142
{
42-
"scripts": {
43-
"cghooks": "vendor/bin/cghooks",
44-
"...": "..."
45-
}
43+
"scripts": {
44+
"cghooks": "vendor/bin/cghooks",
45+
"...": "..."
46+
}
4647
}
4748
```
4849

@@ -52,11 +53,11 @@ Add the following events to your `composer.json` file. The `cghooks` commands wi
5253

5354
```json
5455
{
55-
"scripts": {
56-
"post-install-cmd": "vendor/bin/cghooks add --ignore-lock",
57-
"post-update-cmd": "vendor/bin/cghooks update",
58-
"...": "..."
59-
}
56+
"scripts": {
57+
"post-install-cmd": "vendor/bin/cghooks add --ignore-lock",
58+
"post-update-cmd": "vendor/bin/cghooks update",
59+
"...": "..."
60+
}
6061
}
6162
```
6263

@@ -69,11 +70,11 @@ All the following commands have to be run in the same folder as your `composer.j
6970
After installation is complete, run `cghooks add`
7071
to add all the valid git hooks that have been specified in the composer config.
7172

72-
Option | Description | Command
73-
------ | ----------- | -------
74-
`no-lock` | Do not create a lock file | `cghooks add --no-lock`
75-
`ignore-lock` | Add the lock file to .gitignore | `cghooks add --ignore-lock`
76-
`force-win` | Force windows bash compatibility | `cghooks add --force-win`
73+
| Option | Description | Command |
74+
| ------------- | -------------------------------- | --------------------------- |
75+
| `no-lock` | Do not create a lock file | `cghooks add --no-lock` |
76+
| `ignore-lock` | Add the lock file to .gitignore | `cghooks add --ignore-lock` |
77+
| `force-win` | Force windows bash compatibility | `cghooks add --force-win` |
7778

7879
The `lock` file contains a list of all added hooks.
7980

@@ -87,13 +88,12 @@ Hooks can be easily removed with `cghooks remove`. This will remove all the hook
8788

8889
Hooks can also be removed by passing them as arguments. The command `cghooks remove pre-commit post-commit` which will remove the `pre-commit` and `post-commit` hooks.
8990

90-
Option | Description | Command
91-
------ | ----------- | -------
92-
`force` | Delete hooks without checking the lock file | `cghooks remove --force`
91+
| Option | Description | Command |
92+
| ------- | ------------------------------------------- | ------------------------ |
93+
| `force` | Delete hooks without checking the lock file | `cghooks remove --force` |
9394

9495
**CAREFUL**: If the lock file was tampered with or the force option was used, hooks that already existed before using this package, but were specified in the composer scripts config will be removed as well. That is, if you had a previous `pre-commit` hook, but your current composer config also has a `pre-commit` hook, this option will cause the command to remove your initial hook.
9596

96-
9797
### Listing hooks
9898

9999
Hooks can be listed with the `cghooks list-hooks` command. This basically checks composer config and list the hooks that actually have files.
@@ -102,9 +102,9 @@ Hooks can be listed with the `cghooks list-hooks` command. This basically checks
102102

103103
The following options are common to all commands.
104104

105-
Option | Description | Command
106-
------ | ----------- | -------
107-
`git-dir` | Path to git directory | `cghooks ${command} --git-dir='/path/to/.git'`
105+
| Option | Description | Command |
106+
| --------- | --------------------- | ---------------------------------------------- |
107+
| `git-dir` | Path to git directory | `cghooks ${command} --git-dir='/path/to/.git'` |
108108

109109
### Testing Hooks
110110

@@ -116,11 +116,12 @@ Please see [CONTRIBUTING](CONTRIBUTING.md) for details.
116116

117117
## Credits
118118

119-
- [Ezinwa Okpoechi][link-author]
120-
- [All Contributors][link-contributors]
119+
- [Ezinwa Okpoechi][link-author]
120+
- [All Contributors][link-contributors]
121121

122122
## Related
123-
- [husky][link-husky]
123+
124+
- [husky][link-husky]
124125

125126
## License
126127

@@ -131,7 +132,6 @@ The MIT License (MIT). Please see [License File](LICENSE) for more information.
131132
[badge-packagist]: https://img.shields.io/packagist/v/brainmaestro/composer-git-hooks.svg?style=flat-square
132133
[badge-stable]: https://poser.pugx.org/your-app-rocks/eloquent-uuid/v/stable
133134
[badge-travis]: https://img.shields.io/travis/BrainMaestro/composer-git-hooks.svg?style=flat-square
134-
135135
[link-author]: https://github.com/BrainMaestro
136136
[link-composer-events]: https://getcomposer.org/doc/articles/scripts.md#command-events
137137
[link-contributors]: ../../contributors

Diff for: composer.json

+12-4
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,20 @@
3636
"BrainMaestro\\GitHooks\\Tests\\": "tests/"
3737
}
3838
},
39-
"bin": ["cghooks"],
39+
"bin": [
40+
"cghooks"
41+
],
4042
"scripts": {
4143
"test": "vendor/bin/phpunit",
44+
"post-install-cmd": "./cghooks add --ignore-lock",
45+
"post-update-cmd": "./cghooks update",
4246
"check-style": "php-cs-fixer fix --using-cache=no --diff --dry-run .",
43-
"fix-style": "php-cs-fixer fix --using-cache=no .",
44-
"pre-commit": "echo about to commit",
45-
"post-commit": "echo just committed"
47+
"fix-style": "php-cs-fixer fix --using-cache=no ."
48+
},
49+
"extra": {
50+
"hooks": {
51+
"pre-commit": "composer check-style",
52+
"pre-push": "composer test"
53+
}
4654
}
4755
}

Diff for: src/Commands/AddCommand.php

+2
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
4545
$filename = "{$gitDir}/hooks/{$hook}";
4646
$fileExists = file_exists($filename);
4747

48+
$script = is_array($script) ? implode(PHP_EOL, $script) : $script;
49+
4850
if (! $force && $fileExists) {
4951
$output->writeln("<comment>{$hook} already exists</comment>");
5052
} else {

Diff for: src/Commands/HookCommand.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ protected function configure()
3131

3232
protected function execute(InputInterface $input, OutputInterface $output)
3333
{
34-
$output->writeln(shell_exec($this->script));
34+
$script = is_array($this->script) ? implode(PHP_EOL, $this->script) : $this->script;
35+
$output->writeln(shell_exec($script));
3536
}
3637
}

Diff for: src/Commands/UpdateCommand.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,16 @@ protected function execute(InputInterface $input, OutputInterface $output)
3333
{
3434
$gitDir = $input->getOption('git-dir');
3535
$forceWindows = $input->getOption('force-win');
36-
36+
3737
create_hooks_dir($gitDir);
3838

3939
foreach ($this->hooks as $hook => $script) {
4040
$filename = "{$gitDir}/hooks/{$hook}";
4141

4242
$operation = file_exists($filename) ? 'Updated' : 'Added';
4343

44+
$script = is_array($script) ? implode(PHP_EOL, $script) : $script;
45+
4446
if ($forceWindows || is_windows()) {
4547
// On windows we need to add a SHEBANG
4648
// See: https://github.com/BrainMaestro/composer-git-hooks/issues/7

Diff for: tests/AddCommandTest.php

+26
Original file line numberDiff line numberDiff line change
@@ -192,4 +192,30 @@ public function it_adds_win_bash_compat_if_the_force_windows_option_is_passed()
192192
$this->assertEquals(strpos($content, "#!/bin/bash"), 0);
193193
}
194194
}
195+
196+
/**
197+
* @test
198+
*/
199+
public function it_handles_commands_defined_in_an_array()
200+
{
201+
$hooks = [
202+
'test-pre-commit' => [
203+
'echo pre-commit first',
204+
'echo pre-commit second',
205+
'echo pre-commit third',
206+
],
207+
];
208+
209+
$command = new AddCommand($hooks);
210+
$commandTester = new CommandTester($command);
211+
212+
$commandTester->execute([]);
213+
214+
foreach ($hooks as $hook => $scripts) {
215+
$this->assertContains("Added {$hook} hook", $commandTester->getDisplay());
216+
217+
$content = file_get_contents(".git/hooks/" . $hook);
218+
$this->assertContains(implode(PHP_EOL, $scripts), $content);
219+
}
220+
}
195221
}

Diff for: tests/UpdateCommandTest.php

+30
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ public function it_updates_hooks_that_already_exist()
4646
}
4747
}
4848

49+
/**
50+
* @test
51+
*/
4952
public function it_adds_shebang_to_hooks_on_windows()
5053
{
5154
if (! is_windows()) {
@@ -120,4 +123,31 @@ public function it_adds_win_bash_compat_if_the_force_windows_option_is_passed()
120123
$this->assertEquals(strpos($content, "#!/bin/bash"), 0);
121124
}
122125
}
126+
127+
/**
128+
* @test
129+
*/
130+
public function it_handles_commands_defined_in_an_array()
131+
{
132+
self::createHooks();
133+
$hooks = [
134+
'test-pre-commit' => [
135+
'echo pre-commit first',
136+
'echo pre-commit second',
137+
'echo pre-commit third',
138+
],
139+
];
140+
141+
$command = new UpdateCommand($hooks);
142+
$commandTester = new CommandTester($command);
143+
144+
$commandTester->execute([]);
145+
146+
foreach ($hooks as $hook => $scripts) {
147+
$this->assertContains("Updated {$hook} hook", $commandTester->getDisplay());
148+
149+
$content = file_get_contents(".git/hooks/" . $hook);
150+
$this->assertContains(implode(PHP_EOL, $scripts), $content);
151+
}
152+
}
123153
}

0 commit comments

Comments
 (0)