1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package: chapi |
4
|
|
|
* |
5
|
|
|
* @author: msiebeneicher |
6
|
|
|
* @since: 2015-07-30 |
7
|
|
|
* |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Chapi\Commands; |
11
|
|
|
|
12
|
|
|
use Chapi\BusinessCase\Comparison\JobComparisonInterface; |
13
|
|
|
use Chapi\Service\JobRepository\JobRepository; |
14
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
15
|
|
|
|
16
|
|
|
class DiffCommand extends AbstractCommand |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Configures the current command. |
20
|
|
|
*/ |
21
|
|
|
protected function configure() |
22
|
|
|
{ |
23
|
|
|
$this->setName('diff') |
24
|
|
|
->setDescription('Show changes between jobs and working tree, etc') |
25
|
|
|
->addArgument('jobName', InputArgument::OPTIONAL, 'Show changes for specific job') |
26
|
|
|
; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @return int |
31
|
|
|
*/ |
32
|
|
|
protected function process() |
33
|
|
|
{ |
34
|
|
|
/** @var JobComparisonInterface $jobComparisonBusinessCase */ |
35
|
|
|
$jobComparisonBusinessCase = $this->getContainer()->get(JobComparisonInterface::DIC_NAME); |
36
|
|
|
$jobName = $this->input->getArgument('jobName'); |
37
|
|
|
|
38
|
|
|
if (!empty($jobName)) { |
39
|
|
|
$this->printJobDiff($jobName); |
40
|
|
|
} else { |
41
|
|
|
$localJobUpdates = $jobComparisonBusinessCase->getLocalJobUpdates(); |
42
|
|
|
if (!empty($localJobUpdates)) { |
43
|
|
|
foreach ($localJobUpdates as $jobName) { |
44
|
|
|
$this->printJobDiff($jobName); |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return 0; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param string $jobName |
54
|
|
|
*/ |
55
|
|
|
private function printJobDiff($jobName) |
56
|
|
|
{ |
57
|
|
|
/** @var JobComparisonInterface $jobComparisonBusinessCase */ |
58
|
|
|
$jobComparisonBusinessCase = $this->getContainer()->get(JobComparisonInterface::DIC_NAME); |
59
|
|
|
|
60
|
|
|
$jobs = [ $jobName ]; |
61
|
|
|
|
62
|
|
|
if (strpos($jobName, '*') !== false) { |
63
|
|
|
$jobs = $this->getJobsMatchingWildcard($jobName); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
foreach ($jobs as $jobName) { |
67
|
|
|
$this->printSingleJobDiff($jobComparisonBusinessCase, $jobName); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param JobComparisonInterface $jobComparisonBusinessCase |
73
|
|
|
* @param string $jobName |
74
|
|
|
*/ |
75
|
|
|
private function printSingleJobDiff(JobComparisonInterface $jobComparisonBusinessCase, $jobName) |
76
|
|
|
{ |
77
|
|
|
$this->output->writeln(sprintf("\n<comment>diff %s</comment>", $jobName)); |
78
|
|
|
|
79
|
|
|
$jobDiff = $jobComparisonBusinessCase->getJobDiff($jobName); |
80
|
|
|
|
81
|
|
|
foreach ($jobDiff as $property => $diff) { |
82
|
|
|
$diffLines = explode(PHP_EOL, $diff); |
83
|
|
|
|
84
|
|
|
// the first line might be missing some leading whitespace |
85
|
|
|
if (count($diffLines) > 1) { |
86
|
|
|
$lastLine = $diffLines[count($diffLines) - 1]; |
87
|
|
|
|
88
|
|
|
if (strpos($lastLine, ' ') === 0) { |
89
|
|
|
$length = strspn($lastLine, ' '); |
90
|
|
|
|
91
|
|
|
$diffLines[0] = substr($lastLine, 0, $length) . $diffLines[0]; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
foreach ($diffLines as $diffLine) { |
96
|
|
|
$diffSign = substr($diffLine, 0, 1); |
97
|
|
|
|
98
|
|
|
if ($diffSign == '+') { |
99
|
|
|
$this->output->writeln(sprintf("<info>%s\t%s: %s</info>", $diffSign, $property, ' ' . substr($diffLine, 1))); |
100
|
|
|
} elseif ($diffSign == '-') { |
101
|
|
|
$this->output->writeln(sprintf("<fg=red>%s\t%s: %s</>", $diffSign, $property, ' ' . substr($diffLine, 1))); |
102
|
|
|
} else { |
103
|
|
|
$this->output->writeln(sprintf(" \t%s: %s", $property, $diffLine)); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$this->output->writeln("\n"); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param string $jobName |
113
|
|
|
* @return string[] |
114
|
|
|
*/ |
115
|
|
|
private function getJobsMatchingWildcard($jobName) |
116
|
|
|
{ |
117
|
|
|
/** @var JobRepository[] $jobRepositories */ |
118
|
|
|
$jobRepositories = [ |
119
|
|
|
$this->getContainer()->get(JobRepository::DIC_NAME_CHRONOS), |
120
|
|
|
$this->getContainer()->get(JobRepository::DIC_NAME_FILESYSTEM_CHRONOS), |
121
|
|
|
$this->getContainer()->get(JobRepository::DIC_NAME_FILESYSTEM_MARATHON), |
122
|
|
|
$this->getContainer()->get(JobRepository::DIC_NAME_MARATHON) |
123
|
|
|
]; |
124
|
|
|
|
125
|
|
|
$jobNames = []; |
126
|
|
|
|
127
|
|
|
foreach ($jobRepositories as $jobRepository) { |
128
|
|
|
foreach ($jobRepository->getJobs() as $job) { |
129
|
|
|
if (fnmatch($jobName, $job->getKey())) { |
130
|
|
|
$jobNames[$job->getKey()] = true; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
ksort($jobNames); |
136
|
|
|
|
137
|
|
|
return array_keys($jobNames); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|