-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Tim Clifford
committed
Sep 9, 2020
1 parent
2bc13e8
commit bdc2e72
Showing
4 changed files
with
153 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
namespace Drutiny\algm\Audit; | ||
|
||
use Drutiny\Audit; | ||
use Drutiny\Sandbox\Sandbox; | ||
use Drutiny\Annotation\Token; | ||
use Drutiny\algm\Utils\MarkdownTableGenerator; | ||
|
||
/** | ||
* Look for contrib modules with available updates. | ||
* @Token( | ||
* name = "updates", | ||
* type = "array", | ||
* description = "Description of module updates available." | ||
* ) | ||
*/ | ||
class D9ModuleUpdateStatus extends Audit { | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function audit(Sandbox $sandbox) { | ||
$output = $sandbox->exec('COMPOSER_MEMORY_LIMIT=-1 composer show "drupal/*" -o --no-cache --format=json 2> /dev/null && echo \'\''); | ||
|
||
$modules = json_decode($output, TRUE); | ||
if ($modules === null) { | ||
// If null, then the json cannot be decoded | ||
return AUDIT::ERROR; | ||
} | ||
|
||
// Pass check if there are no updates. | ||
if (empty($modules["installed"])) { | ||
return Audit::SUCCESS; | ||
} | ||
|
||
$num_modules = count($modules["installed"]); | ||
$columns = ['Module', 'Version', 'Status', 'Latest']; | ||
$rows = []; | ||
foreach ($modules["installed"] as $key => $module) { | ||
$rows[] = [ $module["name"], $module["version"], $module["latest-status"], $module["latest"] ]; | ||
} | ||
|
||
$md_table = new MarkdownTableGenerator($columns, $rows); | ||
$rendered_table_markdown = $md_table->render(); | ||
|
||
$sandbox->setParameter('updates', $rendered_table_markdown); | ||
|
||
return Audit::FAIL; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
<?php | ||
|
||
namespace Drutiny\algm\Utils; | ||
|
||
/** | ||
* Generate markdown table as output from php array | ||
*/ | ||
class MarkdownTableGenerator { | ||
private $columns = []; | ||
private $length = []; | ||
private $data = []; | ||
|
||
const MAX_STRING_LENGTH = 75; | ||
|
||
/** | ||
* Generate columns and populate rows with given data | ||
*/ | ||
public function __construct($columns = null, $rows = []) { | ||
if ($columns) { | ||
$this->columns = $columns; | ||
} | ||
elseif ($rows) { | ||
foreach ($rows[0] as $key => $value) { | ||
$this->columns[$key] = $key; | ||
} | ||
} | ||
|
||
foreach ($this->columns as $key => $header) { | ||
$this->length[$key] = strlen($header); | ||
} | ||
|
||
foreach ($rows as &$row) { | ||
foreach ($this->columns as $key => $value) { | ||
if (!isset($row[$key])) { | ||
$row[$key] = '-'; | ||
} | ||
elseif (strlen($row[$key]) > self::MAX_STRING_LENGTH) { | ||
$this->length[$key] = self::MAX_STRING_LENGTH; | ||
// Add an ellipsis if string reaches max. | ||
$row[$key] = substr($row[$key], 0, self::MAX_STRING_LENGTH - 3).'...'; | ||
} | ||
elseif (strlen($row[$key]) > $this->length[$key]) { | ||
$this->length[$key] = strlen($row[$key]); | ||
} | ||
} | ||
} | ||
|
||
$this->data = $rows; | ||
} | ||
|
||
private function renderColumnRow() { | ||
$res = '|'; | ||
|
||
foreach ($this->length as $key => $l) { | ||
$res .= ' ' . str_repeat('-', $l) . ' ' . '|'; | ||
} | ||
|
||
return $res."\r\n"; | ||
} | ||
|
||
private function renderLineRow() { | ||
$res = ' '; | ||
|
||
$sum = 0; | ||
foreach ($this->length as $key => $l) { | ||
$sum += $l+3; | ||
} | ||
|
||
$res .= str_repeat('-', $sum); | ||
|
||
return $res."\r\n"; | ||
} | ||
|
||
private function renderRow($row) { | ||
$res = '|'; | ||
foreach ($this->length as $key => $l) { | ||
$res .= ' '.$row[$key].($l > strlen($row[$key]) ? str_repeat(' ', $l - strlen($row[$key])) : '').' |'; | ||
} | ||
|
||
return $res."\r\n"; | ||
} | ||
|
||
public function render() { | ||
$res = $this->renderLineRow(); | ||
$res .= $this->renderRow($this->columns); | ||
$res .= $this->renderColumnRow(); | ||
|
||
foreach ($this->data as $row) { | ||
$res .= $this->renderRow($row); | ||
} | ||
|
||
$res .= $this->renderLineRow(); | ||
|
||
return $res; | ||
} | ||
} |