Module: Introduce new metadata field `Requires`

This is the successor of `Depends` and allows to
define dependencies for libraries and modules:

`Requires: <lib-name>[ (<ver>)][, <lib-name>[ (<ver>)]] ...`

or

```
Requires:
  Libraries: <name>[ (<ver>)][, <name>[ (<ver>)]] ...
  Modules: <name>[ (<ver>)][, <name>[ (<ver>)]] ...
```
This commit is contained in:
Johannes Meyer 2020-11-13 11:07:03 +01:00
parent 99b620983a
commit baaf663db3
1 changed files with 65 additions and 18 deletions

View File

@ -842,12 +842,33 @@ class Module
* Get the module dependencies * Get the module dependencies
* *
* @return array * @return array
* @deprecated Use method getRequiredModules() instead
*/ */
public function getDependencies() public function getDependencies()
{ {
return $this->metadata()->depends; return $this->metadata()->depends;
} }
/**
* Get required libraries
*
* @return array
*/
public function getRequiredLibraries()
{
return $this->metadata()->libraries;
}
/**
* Get required modules
*
* @return array
*/
public function getRequiredModules()
{
return $this->metadata()->modules ?: $this->metadata()->depends;
}
/** /**
* Fetch module metadata * Fetch module metadata
* *
@ -856,16 +877,19 @@ class Module
protected function metadata() protected function metadata()
{ {
if ($this->metadata === null) { if ($this->metadata === null) {
$metadata = (object) array( $metadata = (object) [
'name' => $this->getName(), 'name' => $this->getName(),
'version' => '0.0.0', 'version' => '0.0.0',
'title' => null, 'title' => null,
'description' => '', 'description' => '',
'depends' => array(), 'depends' => [],
); 'libraries' => [],
'modules' => []
];
if (file_exists($this->metadataFile)) { if (file_exists($this->metadataFile)) {
$key = null; $key = null;
$simpleRequires = false;
$file = new File($this->metadataFile, 'r'); $file = new File($this->metadataFile, 'r');
foreach ($file as $lineno => $line) { foreach ($file as $lineno => $line) {
$line = rtrim($line); $line = rtrim($line);
@ -884,10 +908,8 @@ class Module
if (strpos($line, ':') === false) { if (strpos($line, ':') === false) {
Logger::debug( Logger::debug(
$this->translate( "Can't process line %d in %s: Line does not specify a key:value pair"
"Can't process line %d in %s: Line does not specify a key:value pair" . " nor is it part of the description (indented with a single space)",
. " nor is it part of the description (indented with a single space)"
),
$lineno, $lineno,
$this->metadataFile $this->metadataFile
); );
@ -895,27 +917,54 @@ class Module
break; break;
} }
list($key, $val) = preg_split('/:\s+/', $line, 2); $parts = preg_split('/:\s+/', $line, 2);
$key = lcfirst($key); if (count($parts) === 1) {
$parts[] = '';
}
list($key, $val) = $parts;
$key = strtolower($key);
switch ($key) { switch ($key) {
case 'requires':
if ($val) {
$simpleRequires = true;
$key = 'libraries';
} else {
break;
}
// Shares the syntax with `Depends`
case ' libraries':
case ' modules':
if ($simpleRequires && $key[0] === ' ') {
Logger::debug(
'Can\'t process line %d in %s: Requirements already registered by a previous line',
$lineno,
$this->metadataFile
);
break;
}
$key = ltrim($key);
// Shares the syntax with `Depends`
case 'depends': case 'depends':
if (strpos($val, ' ') === false) { if (strpos($val, ' ') === false) {
$metadata->depends[$val] = true; $metadata->{$key}[$val] = true;
continue 2; continue 2;
} }
$parts = preg_split('/,\s+/', $val); $parts = preg_split('/,\s+/', $val);
foreach ($parts as $part) { foreach ($parts as $part) {
if (preg_match('/^(\w+)\s+\((.+)\)$/', $part, $m)) { if (preg_match('/^(\w+)\s+\((.+)\)$/', $part, $m)) {
$metadata->depends[$m[1]] = $m[2]; $metadata->{$key}[$m[1]] = $m[2];
} else { } else {
// TODO: FAIL? // TODO: FAIL?
continue; continue;
} }
} }
break;
break;
case 'description': case 'description':
if ($metadata->title === null) { if ($metadata->title === null) {
$metadata->title = $val; $metadata->title = $val;
@ -935,8 +984,6 @@ class Module
} }
if ($metadata->description === '') { if ($metadata->description === '') {
// TODO: Check whether the translation module is able to
// extract this
$metadata->description = t( $metadata->description = t(
'This module has no description' 'This module has no description'
); );