1 | <?php |
||
14 | class InitCommand extends Command |
||
15 | { |
||
16 | const TYPE_INIT = 'template.init'; |
||
17 | const TYPE_GIT = 'template.git'; |
||
18 | const TYPE_COMPOSER = 'template.composer'; |
||
19 | const TYPE_TEST = 'template.test'; |
||
20 | const TYPE_CI = 'template.ci'; |
||
21 | |||
22 | const OUTPUT_LEVEL_SPACE = ' '; |
||
23 | |||
24 | /** |
||
25 | * {@inheritdoc} |
||
26 | */ |
||
27 | protected function configure() |
||
53 | |||
54 | /** |
||
55 | * {@inheritdoc} |
||
56 | */ |
||
57 | protected function execute(InputInterface $input, OutputInterface $output) |
||
58 | { |
||
59 | $mode = $input->getArgument('type'); |
||
60 | |||
61 | if (!$this->modeIsValid($output, $mode)) { |
||
62 | return 1; |
||
63 | } |
||
64 | |||
65 | $skipExistingFile = false === $input->getOption('ask-before-override'); |
||
66 | $forceOverride = $input->getOption('force-override'); |
||
67 | if (true === $forceOverride) { |
||
68 | $skipExistingFile = false; |
||
69 | } |
||
70 | |||
71 | $variableBag = (new VariableBagFactory())->load($mode); |
||
72 | $templatePathList = (new TemplatePathBagFactory())->load($mode); |
||
73 | |||
74 | $commandTemplateHelper = new CommandTemplateHelper( |
||
75 | $this->getHelper('question'), |
||
76 | $input, |
||
77 | $output, |
||
78 | $variableBag->all(), |
||
79 | $skipExistingFile, |
||
80 | $forceOverride |
||
81 | ); |
||
82 | $commandProcessor = new CommandTemplateProcessor($commandTemplateHelper); |
||
83 | |||
84 | $output->writeln(sprintf('<comment>Creating default files for : </comment><info>%s</info>', ucwords($mode))); |
||
85 | if (true === $forceOverride) { |
||
86 | $output->writeln('<fg=red>WARNING : Existing files will be overriden by default</fg=red>'); |
||
87 | } elseif (true === $skipExistingFile) { |
||
88 | $output->writeln('<comment>INFO : Existing files will be skipped !</comment>'); |
||
89 | } |
||
90 | try { |
||
91 | $currentType = null; |
||
92 | foreach ($templatePathList as $templateKey => $templatePath) { |
||
93 | if (count($input->getOption('id')) && !in_array($templateKey, $input->getOption('id'))) { |
||
94 | continue; |
||
95 | } |
||
96 | |||
97 | if (null === $currentType || !preg_match(sprintf('#%s#', preg_quote($currentType)), $templateKey)) { |
||
98 | preg_match('#(template\.[^\.]+)#', $templateKey, $matches); |
||
99 | $currentType = isset($matches[1]) ? $matches[1] : $templateKey; |
||
100 | $header = ucwords(str_replace('template.', '', $currentType)); |
||
101 | if ('Init' === $header) { |
||
102 | $header = 'Init repository'; |
||
103 | } elseif ('Ci' === $header) { |
||
104 | $header = 'Continuous integration'; |
||
105 | } |
||
106 | $output->writeln(sprintf('<info>%s%s</info>', self::OUTPUT_LEVEL_SPACE, $header)); |
||
107 | } |
||
108 | |||
109 | $output->writeln(sprintf( |
||
110 | '%s* %s : ', |
||
111 | str_repeat(self::OUTPUT_LEVEL_SPACE, 2), |
||
112 | ucwords(str_replace('template.', '', str_replace($currentType.'.', '', $templateKey))) |
||
113 | )); |
||
114 | if (true === $input->getOption('list')) { |
||
115 | $output->writeln(sprintf( |
||
116 | '%s<comment>Id : </comment><info>%s</info>', |
||
117 | str_repeat(self::OUTPUT_LEVEL_SPACE, 3), |
||
118 | $templateKey |
||
119 | )); |
||
120 | $output->writeln(sprintf( |
||
121 | '%s<comment>File : </comment><info>%s</info>', |
||
122 | str_repeat(self::OUTPUT_LEVEL_SPACE, 3), |
||
123 | $templatePath |
||
124 | )); |
||
125 | } else { |
||
126 | $commandProcessor->process($templatePath); |
||
127 | } |
||
128 | } |
||
129 | return 0; |
||
130 | } catch (\Exception $e) { |
||
131 | $output->writeln(sprintf('<error>Error -> %s</error>', $e->getMessage())); |
||
132 | throw $e; |
||
133 | } |
||
134 | } |
||
135 | |||
136 | protected function modeIsValid(OutputInterface $output, $mode) |
||
159 | } |
||
160 |