1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package: chapi |
4
|
|
|
* |
5
|
|
|
* @author: msiebeneicher |
6
|
|
|
* @since: 2015-07-28 |
7
|
|
|
* |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Chapi\Commands; |
11
|
|
|
|
12
|
|
|
use Chapi\BusinessCase\Comparison\JobComparisonInterface; |
13
|
|
|
use Chapi\Service\JobIndex\JobIndexServiceInterface; |
14
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
15
|
|
|
use Symfony\Component\Console\Input\InputOption; |
16
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
17
|
|
|
|
18
|
|
|
class StatusCommand extends AbstractCommand |
19
|
|
|
{ |
20
|
|
|
const LABEL_CHRONOS = 'chronos'; |
21
|
|
|
const LABEL_MARATHON = 'marathon'; |
22
|
|
|
|
23
|
|
|
/** @var JobIndexServiceInterface */ |
24
|
|
|
private $jobIndexService; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Configures the current command. |
28
|
|
|
*/ |
29
|
|
|
protected function configure() |
30
|
|
|
{ |
31
|
|
|
$this->setName('status') |
32
|
|
|
->setDescription('Show the working tree status') |
33
|
|
|
->addOption( |
34
|
|
|
'strict', |
35
|
|
|
null, |
36
|
|
|
InputOption::VALUE_NONE, |
37
|
|
|
"Return a non-zero exit code when there are changes", |
|
|
|
|
38
|
|
|
null |
39
|
|
|
); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @inheritdoc |
44
|
|
|
*/ |
45
|
|
|
protected function initialize(InputInterface $input, OutputInterface $output) |
46
|
|
|
{ |
47
|
|
|
parent::initialize($input, $output); |
48
|
|
|
|
49
|
|
|
$this->jobIndexService = $this->getContainer()->get(JobIndexServiceInterface::DIC_NAME); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return int |
54
|
|
|
*/ |
55
|
|
|
protected function process() |
56
|
|
|
{ |
57
|
|
|
$changedJobs = $this->getChangedAppJobs(); |
58
|
|
|
|
59
|
|
|
// tracked jobs |
60
|
|
|
$this->output->writeln("\nChanges to be committed"); |
61
|
|
|
$this->output->writeln(" (use 'chapi reset <job>...' to unstage)"); |
62
|
|
|
$this->output->writeln(''); |
63
|
|
|
|
64
|
|
|
$this->printStatusView($changedJobs, true); |
65
|
|
|
|
66
|
|
|
// untracked jobs |
67
|
|
|
$this->output->writeln("\nChanges not staged for commit"); |
68
|
|
|
$this->output->writeln(" (use 'chapi add <job>...' to update what will be committed)"); |
69
|
|
|
$this->output->writeln(" (use 'chapi checkout <job>...' to discard changes in local repository)"); |
70
|
|
|
$this->output->writeln(''); |
71
|
|
|
|
72
|
|
|
$this->printStatusView($changedJobs, false); |
73
|
|
|
|
74
|
|
|
if ($this->input->getOption('strict') && !empty($changedJobs)) { |
75
|
|
|
return 1; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return 0; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return array<string,array<string,array>> |
83
|
|
|
*/ |
84
|
|
|
private function getChangedAppJobs() |
85
|
|
|
{ |
86
|
|
|
/** @var JobComparisonInterface $jobComparisonBusinessCaseChronos */ |
87
|
|
|
/** @var JobComparisonInterface $jobComparisonBusinessCaseMarathon */ |
88
|
|
|
$jobComparisonBusinessCaseChronos = $this->getContainer()->get(JobComparisonInterface::DIC_NAME_CHRONOS); |
89
|
|
|
$jobComparisonBusinessCaseMarathon = $this->getContainer()->get(JobComparisonInterface::DIC_NAME_MARATHON); |
90
|
|
|
|
91
|
|
|
$result = [ |
92
|
|
|
'new' => [ |
93
|
|
|
self::LABEL_CHRONOS => $jobComparisonBusinessCaseChronos->getRemoteMissingJobs(), |
94
|
|
|
self::LABEL_MARATHON => $jobComparisonBusinessCaseMarathon->getRemoteMissingJobs(), |
95
|
|
|
], |
96
|
|
|
'missing' => [ |
97
|
|
|
self::LABEL_CHRONOS => $jobComparisonBusinessCaseChronos->getLocalMissingJobs(), |
98
|
|
|
self::LABEL_MARATHON => $jobComparisonBusinessCaseMarathon->getLocalMissingJobs(), |
99
|
|
|
], |
100
|
|
|
'updates' => [ |
101
|
|
|
self::LABEL_CHRONOS => $jobComparisonBusinessCaseChronos->getLocalJobUpdates(), |
102
|
|
|
self::LABEL_MARATHON => $jobComparisonBusinessCaseMarathon->getLocalJobUpdates(), |
103
|
|
|
], |
104
|
|
|
]; |
105
|
|
|
|
106
|
|
|
return $result; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param array $changedJobs |
111
|
|
|
* @param bool $filterIsInIndex |
112
|
|
|
*/ |
113
|
|
|
private function printStatusView($changedJobs, $filterIsInIndex) |
114
|
|
|
{ |
115
|
|
|
$formatMap = [ |
116
|
|
|
'new' => ['title' => 'New jobs in local repository', 'format' => "\t<comment>new %s job:\t%s</comment>"], |
117
|
|
|
'missing' => ['title' => 'Missing jobs in local repository', 'format' => "\t<fg=red>delete %s job:\t%s</>"], |
118
|
|
|
'updates' => ['title' => 'Updated jobs in local repository', 'format' => "\t<info>modified %s job:\t%s</info>"] |
119
|
|
|
]; |
120
|
|
|
|
121
|
|
|
foreach ($changedJobs as $jobStatus => $jobList) { |
122
|
|
|
$filteredJobList = $this->filterJobListWithIndex($jobList, $filterIsInIndex); |
123
|
|
|
if (!empty($filteredJobList)) { |
124
|
|
|
$this->printJobList($formatMap[$jobStatus]['title'], $filteredJobList, $formatMap[$jobStatus]['format']); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param array $jobLists |
131
|
|
|
* @param bool $filterIsInIndex |
132
|
|
|
* @return array |
133
|
|
|
*/ |
134
|
|
|
private function filterJobListWithIndex($jobLists, $filterIsInIndex) |
135
|
|
|
{ |
136
|
|
|
$filteredJobList = []; |
137
|
|
|
|
138
|
|
|
foreach ($jobLists as $appLabel => $jobList) { |
139
|
|
|
foreach ($jobList as $jobName) { |
140
|
|
|
if ($filterIsInIndex == $this->jobIndexService->isJobInIndex($jobName)) { |
141
|
|
|
$filteredJobList[$appLabel][] = $jobName; |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
return $filteredJobList; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @param string $title |
151
|
|
|
* @param array $jobLists |
152
|
|
|
* @param string $listFormat |
153
|
|
|
*/ |
154
|
|
|
private function printJobList($title, $jobLists, $listFormat) |
155
|
|
|
{ |
156
|
|
|
$this->output->writeln(sprintf(' %s:', $title)); |
157
|
|
|
|
158
|
|
|
foreach ($jobLists as $label => $jobList) { |
159
|
|
|
foreach ($jobList as $jobName) { |
160
|
|
|
$this->output->writeln( |
161
|
|
|
sprintf($listFormat, $label, $jobName) |
162
|
|
|
); |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
$this->output->writeln("\n"); |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
PHP provides two ways to mark string literals. Either with single quotes
'literal'
or with double quotes"literal"
. The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (
\'
) and the backslash (\\
). Every other character is displayed as is.Double quoted string literals may contain other variables or more complex escape sequences.
will print an indented:
Single is Value
If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.
For more information on PHP string literals and available escape sequences see the PHP core documentation.