33namespace BrainMaestro \GitHooks \Commands ;
44
55use BrainMaestro \GitHooks \Hook ;
6- use Symfony \ Component \ Console \ Command \Command ;
6+ use BrainMaestro \ GitHooks \ Commands \Command ;
77use Symfony \Component \Console \Input \InputInterface ;
88use Symfony \Component \Console \Input \InputOption ;
99use Symfony \Component \Console \Output \OutputInterface ;
1010
1111class AddCommand extends Command
1212{
13- private $ hooks ;
14-
15- public function __construct ($ hooks )
16- {
17- $ this ->hooks = $ hooks ;
18- parent ::__construct ();
19- }
13+ private $ force ;
14+ private $ windows ;
15+ private $ noLock ;
16+ private $ ignoreLock ;
17+ private $ addedHooks = [];
2018
2119 protected function configure ()
2220 {
@@ -32,75 +30,83 @@ protected function configure()
3230 ;
3331 }
3432
35- protected function execute ( InputInterface $ input, OutputInterface $ output )
33+ protected function init ( $ input )
3634 {
37- $ addedHooks = [];
38- $ gitDir = $ input ->getOption ('git-dir ' );
39- $ force = $ input ->getOption ('force ' );
40- $ forceWindows = $ input ->getOption ('force-win ' );
41-
42- create_hooks_dir ($ gitDir );
43-
44- foreach ($ this ->hooks as $ hook => $ script ) {
45- $ filename = "{$ gitDir }/hooks/ {$ hook }" ;
46- $ fileExists = file_exists ($ filename );
47-
48- $ script = is_array ($ script ) ? implode (PHP_EOL , $ script ) : $ script ;
49-
50- if (! $ force && $ fileExists ) {
51- $ output ->writeln ("<comment> {$ hook } already exists</comment> " );
52- } else {
53- if ($ forceWindows || is_windows ()) {
54- // On windows we need to add a SHEBANG
55- // See: https://github.com/BrainMaestro/composer-git-hooks/issues/7
56- $ script = '#!/bin/bash ' . PHP_EOL . $ script ;
57- }
58-
59- file_put_contents ($ filename , $ script );
60- chmod ($ filename , 0755 );
61- $ output ->writeln (
62- $ fileExists
63- ? "Overwrote <info> {$ hook }</info> hook "
64- : "Added <info> {$ hook }</info> hook "
65- );
66- $ addedHooks [] = $ hook ;
67- }
68- }
35+ $ this ->force = $ input ->getOption ('force ' );
36+ $ this ->windows = $ input ->getOption ('force-win ' ) || is_windows ();
37+ $ this ->noLock = $ input ->getOption ('no-lock ' );
38+ $ this ->ignoreLock = $ input ->getOption ('ignore-lock ' );
39+ }
6940
70- if (! count ($ addedHooks )) {
71- $ output ->writeln ('<error>No hooks were added. Try updating</error> ' );
72- return ;
41+ protected function command ()
42+ {
43+ create_hooks_dir ($ this ->gitDir );
44+
45+ foreach ($ this ->hooks as $ hook => $ contents ) {
46+ $ this ->addHook ($ hook , $ contents );
7347 }
7448
75- if ($ input -> getOption ( ' no-lock ' )) {
76- $ output -> writeln ( ' <comment>Skipped creating a ' . Hook:: LOCK_FILE . ' file</comment> ' );
49+ if (! count ( $ this -> addedHooks )) {
50+ $ this -> error ( ' No hooks were added. Try updating ' );
7751 return ;
7852 }
7953
80- $ this ->addLockFile ($ addedHooks , $ output );
54+ $ this ->addLockFile ();
55+ $ this ->ignoreLockFile ();
56+ }
57+
58+ private function addHook ($ hook , $ contents )
59+ {
60+ $ filename = "{$ this ->gitDir }/hooks/ {$ hook }" ;
61+ $ exists = file_exists ($ filename );
62+
63+ // On windows, the shebang needs to point to bash
64+ // See: https://github.com/BrainMaestro/composer-git-hooks/issues/7
65+ $ shebang = ($ this ->windows ? '#!/bin/bash ' : '#!/bin/sh ' ) . PHP_EOL . PHP_EOL ;
66+ $ contents = is_array ($ contents ) ? implode (PHP_EOL , $ contents ) : $ contents ;
8167
82- if (! $ input -> getOption ( ' ignore-lock ' ) ) {
83- $ output -> writeln ( ' < comment>Skipped adding ' . Hook:: LOCK_FILE . ' to .gitignore</comment> ' );
68+ if (! $ this -> force && $ exists ) {
69+ $ this -> comment ( "{ $ hook } already exists " );
8470 return ;
8571 }
8672
87- $ this ->ignoreLockFile ($ output );
73+ file_put_contents ($ filename , $ shebang . $ contents );
74+ chmod ($ filename , 0755 );
75+
76+ $ operation = $ exists ? 'Overwrote ' : 'Added ' ;
77+ $ this ->log ("{$ operation } <info> {$ hook }</info> hook " );
78+
79+ $ this ->addedHooks [] = $ hook ;
8880 }
8981
90- private function addLockFile ($ hooks , $ output )
82+ private function addLockFile ()
9183 {
92- file_put_contents (Hook::LOCK_FILE , json_encode ($ hooks ));
93- $ output ->writeln ('<comment>Created ' . Hook::LOCK_FILE . ' file</comment> ' );
84+ if ($ this ->noLock ) {
85+ $ this ->comment ('Skipped creating a ' . Hook::LOCK_FILE . ' file ' );
86+ return ;
87+ }
88+
89+ file_put_contents (Hook::LOCK_FILE , json_encode ($ this ->addedHooks ));
90+ $ this ->comment ('Created ' . Hook::LOCK_FILE . ' file ' );
9491 }
9592
96- private function ignoreLockFile ($ output )
93+ private function ignoreLockFile ()
9794 {
95+ if ($ this ->noLock ) {
96+ return ;
97+ }
98+
99+ if (! $ this ->ignoreLock ) {
100+ $ this ->comment ('Skipped adding ' . Hook::LOCK_FILE . ' to .gitignore ' );
101+ return ;
102+ }
103+
98104 $ contents = file_get_contents ('.gitignore ' );
99105 $ return = strpos ($ contents , Hook::LOCK_FILE );
100106
101107 if ($ return === false ) {
102108 file_put_contents ('.gitignore ' , Hook::LOCK_FILE . PHP_EOL , FILE_APPEND );
103- $ output -> writeln ( ' <comment> Added ' . Hook::LOCK_FILE . ' to .gitignore</comment> ' );
109+ $ this -> comment ( ' Added ' . Hook::LOCK_FILE . ' to .gitignore ' );
104110 }
105111 }
106112}
0 commit comments