Skip to content
This repository was archived by the owner on Sep 12, 2018. It is now read-only.

Commit f125c41

Browse files
author
Tyler Mills
committed
Patch args added
1 parent 48b84c0 commit f125c41

File tree

5 files changed

+320
-9
lines changed

5 files changed

+320
-9
lines changed

n98-magerun.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ autoloaders:
33

44
commands:
55
customCommands:
6-
- Etre\Shell\Console\Commands\PatchCommand
6+
- Etre\Shell\Console\Commands\Patch\ListCommand
7+
- Etre\Shell\Console\Commands\Patch\DebugCommand

src/Console/Commands/PatchCommand.php renamed to src/Console/Commands/Patch/DebugCommand.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Etre\Shell\Console\Commands;
3+
namespace Etre\Shell\Console\Commands\Patch;
44

55
use Etre\Shell\Helper\DirectoryHelper;
66
use Etre\Shell\Helper\PatchesHelper;
@@ -14,7 +14,7 @@
1414
use Symfony\Component\Console\Output\OutputInterface;
1515
use Symfony\Component\Console\Question\Question;
1616

17-
class PatchCommand extends Command
17+
class DebugCommand extends Command
1818
{
1919
/** @var DirectoryHelper $directoryHelper */
2020
protected $directoryHelper;
@@ -37,9 +37,9 @@ public function __construct($name = null)
3737
public function configure()
3838
{
3939
$this
40-
->setName('patch:list')
41-
->setDescription('List applied patches')
42-
->addOption('sort', null, InputOption::VALUE_OPTIONAL, "Sort by patch installation date: ASC | DESC","DESC")
40+
->setName('etre:patch:review')
41+
->setDescription('Review code that could impacted by a patch.')
42+
->addArgument('patch-id', InputOption::VALUE_REQUIRED, "Accepted arguments: <comment>SUPEE-XXXX | XXXX | Path ID</comment>")
4343
->addOption('no-details', null, null, "Minimizes patch details shown")
4444
->setHelp("This command lists your applied patches");
4545
}
Lines changed: 232 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,232 @@
1+
<?php
2+
3+
namespace Etre\Shell\Console\Commands\Patch;
4+
5+
use Etre\Shell\Helper\DirectoryHelper;
6+
use Etre\Shell\Helper\PatchesHelper;
7+
use N98\Magento\Command\AbstractMagentoCommand;
8+
use Symfony\Component\Console\Helper\ProgressBar;
9+
use Symfony\Component\Console\Helper\Table as ConsoleTable;
10+
use Symfony\Component\Console\Helper\TableCell as ConsoleTableCell;
11+
use Symfony\Component\Console\Input\InputInterface;
12+
use Symfony\Component\Console\Input\InputOption;
13+
use Symfony\Component\Console\Output\OutputInterface;
14+
use Symfony\Component\Console\Question\Question;
15+
16+
class ListCommand extends AbstractMagentoCommand
17+
{
18+
protected $patchTableHeaders = [
19+
"Installed",
20+
"Name",
21+
"Applied Magento Version",
22+
"Version",
23+
"",
24+
"Release Date",
25+
"",
26+
"State",
27+
];
28+
/** @var DirectoryHelper $directoryHelper */
29+
protected $directoryHelper;
30+
31+
/** @var PatchesHelper $patchesHelper */
32+
protected $patchesHelper;
33+
34+
/**
35+
* PatchCommand constructor.
36+
* @param $patchHelper
37+
*/
38+
public function __construct($name = null)
39+
{
40+
$this->directoryHelper = new DirectoryHelper();
41+
$this->patchesHelper = new PatchesHelper($this->directoryHelper);
42+
43+
parent::__construct($name);
44+
}
45+
46+
public function configure()
47+
{
48+
$this
49+
->setName('etre:patch:list')
50+
->setDescription('List applied patches')
51+
->addArgument("id", null, "Optional, Numerical ID of patch: <comment>XXXX</comment> in SUPEE-<comment>XXXX</comment>")
52+
->addArgument("version", null, "Optional, Patch version: <comment>Y</comment> in SUPEE-<comment>XXXX</comment> v<comment>Y</comment>")
53+
->addOption('sort', null, InputOption::VALUE_OPTIONAL, "Sort by patch installation date: ASC | DESC", "DESC")
54+
->addOption('minimal', "m", null, "List patches by name and version.")
55+
->addOption('no-details', null, null, "Minimizes patch details shown")
56+
->setHelp("This command lists your applied patches");
57+
}
58+
59+
protected function execute(InputInterface $input, OutputInterface $output)
60+
{
61+
$this->detectMagento($output);
62+
if(!$this->initMagento()) {
63+
return;
64+
}
65+
66+
$lookupPatchId = $input->getArgument("id");
67+
$lookupPatchVersion = $input->getArgument("version");
68+
69+
$this->abortInvalidPatchId($output, $lookupPatchId);
70+
$this->abortInvalidVersion($output, $lookupPatchVersion);
71+
72+
$patchData = $this->patchesHelper->getPatches($input->getOption("sort"), $lookupPatchId, $lookupPatchVersion);
73+
$this->abortOnEmptyPatches($output, $patchData, $lookupPatchId, $lookupPatchVersion);
74+
75+
if($input->getOption("minimal")):
76+
$this->outputResponseMinimal($output, $patchData);
77+
endif;
78+
79+
$this->executeNonMinimalResponse($input, $output, $patchData);
80+
$output->writeln('');
81+
82+
}
83+
84+
/**
85+
* @param OutputInterface $output
86+
* @param $lookupPatchId
87+
* @param $patchesHelper
88+
*/
89+
protected function abortInvalidPatchId(OutputInterface $output, $lookupPatchId)
90+
{
91+
if($lookupPatchId && !$this->patchesHelper->isValidPatchId($lookupPatchId)):
92+
$this->abortWithMessage("Invalid patch Id provided.", $output);
93+
endif;
94+
}
95+
96+
/**
97+
* @param OutputInterface $output
98+
*/
99+
protected function abortWithMessage($message, OutputInterface $output)
100+
{
101+
$output->writeln("<comment>{$message}</comment>");
102+
exit;
103+
}
104+
105+
/**
106+
* @param OutputInterface $output
107+
* @param $lookupPatchVersion
108+
* @param $patchesHelper
109+
*/
110+
protected function abortInvalidVersion(OutputInterface $output, $lookupPatchVersion)
111+
{
112+
if($lookupPatchVersion && $lookupPatchVersion && !$this->patchesHelper->isValidVersion($lookupPatchVersion)):
113+
$this->abortWithMessage("Invalid patch version provided.", $output);
114+
endif;
115+
}
116+
117+
/**
118+
* @param OutputInterface $output
119+
* @param $patchData
120+
* @param $lookupPatchId
121+
* @param $lookupPatchVersion
122+
*/
123+
protected function abortOnEmptyPatches(OutputInterface $output, $patchData, $lookupPatchId, $lookupPatchVersion)
124+
{
125+
if(empty($patchData)):
126+
if($lookupPatchId && $lookupPatchVersion):
127+
$this->abortWithMessage("<comment>No patches found matching ID SUPEE-{$lookupPatchId} v{$lookupPatchVersion}</comment>", $output);
128+
elseif($lookupPatchId):
129+
$this->abortWithMessage("<comment>No patches found matching ID SUPEE-{$lookupPatchId} at any version.</comment>", $output);
130+
else:
131+
$this->abortWithMessage("<comment>No patches appear in applied.patches.list or this file could not be parsed.</comment>", $output);
132+
endif;
133+
endif;
134+
}
135+
136+
/**
137+
* @param OutputInterface $output
138+
* @param $patchData
139+
*/
140+
protected function outputResponseMinimal(OutputInterface $output, $patchData)
141+
{
142+
foreach($patchData as $patch):
143+
$this->outputPatchTitle($output, $patch);
144+
endforeach;
145+
exit;
146+
}
147+
148+
/**
149+
* @param OutputInterface $output
150+
* @param $patch
151+
*/
152+
protected function outputPatchTitle(OutputInterface $output, $patch)
153+
{
154+
$patchTitle = $this->getPatchTitle($patch);
155+
$output->writeln("<info>{$patchTitle}</info>");
156+
}
157+
158+
/**
159+
* @param $patch
160+
* @return string
161+
*/
162+
protected function getPatchTitle($patch)
163+
{
164+
$patchName = $patch['headers'][1];
165+
$patchVersion = $patch['headers'][3];
166+
$patchState = "[{$patch['headers'][7]}]";
167+
$patchTitle = "{$patchName} {$patchVersion} {$patchState}";
168+
return $patchTitle;
169+
}
170+
171+
/**
172+
* @param InputInterface $input
173+
* @param OutputInterface $output
174+
* @param $patchData
175+
* @param $progress
176+
*/
177+
protected function executeNonMinimalResponse(InputInterface $input, OutputInterface $output, $patchData)
178+
{
179+
$progress = new ProgressBar($output, count($patchData));
180+
$output->writeln("");
181+
$progress->setMessage("% List Reviewed");
182+
$question = new Question("Press any key to proceed to next patch.");
183+
$helper = $this->getHelper('question');
184+
185+
foreach($patchData as $patch):
186+
$this->outputPatchTitle($output, $patch);
187+
$this->outputPatchTable($input, $output, $patch);
188+
if(count($patchData) > 1):
189+
$progress->advance();
190+
$this->writeBlankLn($output);
191+
$helper->ask($input, $output, $question);
192+
endif;
193+
$output->writeln("");
194+
endforeach;
195+
196+
}
197+
198+
/**
199+
* @param InputInterface $input
200+
* @param OutputInterface $output
201+
* @param $patch
202+
*/
203+
protected function outputPatchTable(InputInterface $input, OutputInterface $output, $patch)
204+
{
205+
$patchTable = new ConsoleTable($output);
206+
$this->setTableHeaders($patch, $patchTable);
207+
if(!$input->getOption('no-details')):
208+
foreach($patch['details'] as $patchDetail):
209+
$patchTable->addRow([new ConsoleTableCell($patchDetail, ['colspan' => count($patch['headers'])])]);
210+
endforeach;
211+
endif;
212+
$patchTable->setColumnWidths([null, null, null, 1])->render();
213+
}
214+
215+
/**
216+
* @param $patch
217+
* @param $patchTable
218+
*/
219+
protected function setTableHeaders($patch, ConsoleTable $patchTable)
220+
{
221+
$patchTable->setHeaders($this->patchTableHeaders);
222+
$patchTable->addRow($patch['headers']);
223+
}
224+
225+
/**
226+
* @param OutputInterface $output
227+
*/
228+
protected function writeBlankLn(OutputInterface $output)
229+
{
230+
$output->writeln("");
231+
}
232+
}

src/Helper/DirectoryHelper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class DirectoryHelper
1313
function getApplicationDirectory()
1414
{
1515
$currentDirectory = $this->getCurrentDirectory();
16-
$directoryToFind = 'shell';
16+
$directoryToFind = 'lib';
1717
$shellSearch = $this->getDirectoryLiteralTerm($directoryToFind);
1818
$parentDirectoryPosition = $this->getParentDirectoryPosition($currentDirectory, $shellSearch);
1919
return substr($currentDirectory, 0, $parentDirectoryPosition);

src/Helper/PatchesHelper.php

Lines changed: 80 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
namespace Etre\Shell\Helper;
44

55
use Etre\Shell\Helper\DirectoryHelper;
6+
use N98\Util\Console\Helper\MagentoHelper;
7+
use Symfony\Component\Debug\Debug;
68

7-
class PatchesHelper
9+
class PatchesHelper extends MagentoHelper
810
{
911
protected $STATUS_REVERTED = "REVERTED";
1012
protected $STATUS_APPLIED = "APPLIED";
@@ -51,7 +53,46 @@ public function setDirectoryHelper($directoryHelper)
5153
return $this;
5254
}
5355

54-
public function getData($sort = "DESC")
56+
public function getPatches($sort = "DESC", $lookupPatchId = null, $lookupPatchVersion = null)
57+
{
58+
$searchByPatchId = $this->isValidPatchId($lookupPatchId) ? intval($lookupPatchId) : false;
59+
$hasVersionCriteria = $this->isValidVersion($lookupPatchVersion);
60+
$searchByVersion = $searchByPatchId && $hasVersionCriteria;
61+
$patchList = $this->getPatchList($sort);
62+
if($searchByPatchId):
63+
$patchList = $this->applyPatchIdFilter($lookupPatchId, $patchList);
64+
endif;
65+
if($searchByVersion):
66+
$patchList = $this->applyPatchVersionFilter($lookupPatchVersion, $patchList);
67+
endif;
68+
return $patchList;
69+
70+
}
71+
72+
/**
73+
* @param $lookupPatchId
74+
* @return bool
75+
*/
76+
public function isValidPatchId($lookupPatchId)
77+
{
78+
if(is_numeric($lookupPatchId)):
79+
return intval($lookupPatchId) > 0;
80+
endif;
81+
return false;
82+
}
83+
84+
/**
85+
* @param $lookupPatchVersion
86+
* @return bool
87+
*/
88+
public function isValidVersion($lookupPatchVersion)
89+
{
90+
if(is_numeric($lookupPatchVersion)):
91+
return intval($lookupPatchVersion) > 0;
92+
endif;
93+
}
94+
95+
public function getPatchList($sort = "DESC")
5596
{
5697
$parsedPatchContent = $this->getParsedPatchContent();
5798
$patches = [];
@@ -84,6 +125,7 @@ public function getData($sort = "DESC")
84125

85126
private function getParsedPatchContent()
86127
{
128+
$mage = \Mage::app();
87129
$csv = new \Varien_File_Csv();
88130

89131
$patchListArray = $csv->setDelimiter("|")->getData($this->pathToPatchesList());
@@ -106,4 +148,40 @@ private function removeNullItems($patchListArray)
106148
$patchListArray = array_filter($patchListArray);
107149
return $patchListArray;
108150
}
151+
152+
/**
153+
* @param $lookupPatchId
154+
* @param $searchByPatchId
155+
* @param $patchList
156+
*/
157+
protected function applyPatchIdFilter($lookupPatchId, $patchList)
158+
{
159+
foreach($patchList as $patchKey => $patch):
160+
$patchName = strtoupper($patch['headers'][1]);
161+
$patchNameParts = explode("-", $patchName);
162+
$currentPatchId = $patchNameParts[1];
163+
if($currentPatchId !== $lookupPatchId):
164+
unset($patchList[$patchKey]);
165+
endif;
166+
endforeach;
167+
return $patchList;
168+
}
169+
170+
/**
171+
* @param $lookupPatchVersion
172+
* @param $searchByPatchId
173+
* @param $patchList
174+
*/
175+
protected function applyPatchVersionFilter($lookupPatchVersion, $patchList)
176+
{
177+
foreach($patchList as $patchKey => $patch):
178+
$patchVersion = strtoupper($patch['headers'][3]);
179+
$patchVersionParts = explode("V", $patchVersion);
180+
$currentPatchVersion = $patchVersionParts[1];
181+
if($currentPatchVersion !== $lookupPatchVersion):
182+
unset($patchList[$patchKey]);
183+
endif;
184+
endforeach;
185+
return $patchList;
186+
}
109187
}

0 commit comments

Comments
 (0)