Skip to content

Commit

Permalink
Update embedded related extension standards
Browse files Browse the repository at this point in the history
  • Loading branch information
billtomczak committed Oct 27, 2021
1 parent 88461c5 commit 817acff
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 77 deletions.
2 changes: 1 addition & 1 deletion src/build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ Missing path to Free version: project.${composer.extra.name}.path property
</target>

<target name="standards" description="Check formatting/settigns in composer/manifest files">
<standardsFixes/>
<standardsFixes manifest="${project.manifest}"/>
</target>

<!-- Load sub-projects -->
Expand Down
75 changes: 61 additions & 14 deletions src/tasks/StandardsFixesTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ class StandardsFixesTask extends Task
{
use TraitShack;

/**
* @var string
*/
protected $manifest = null;

/**
* Preferred order of manifest headers
*
Expand All @@ -47,19 +52,28 @@ class StandardsFixesTask extends Task
'variant' => null
];

/**
* @param string $path
*
* @return void
*/
public function setManifest(string $path)
{
$this->manifest = $path;
}

/**
* @inheritDoc
*/
public function main()
{
$manifestPath = $this->getProperty('project.manifest');
if (is_file($manifestPath)) {
$this->updateManifest($manifestPath);

} else {
$this->throwError('Manifest not found: ' . $manifestPath);
if (is_file($this->manifest) == false) {
$this->throwError('Manifest not found: ' . $this->manifest);
}

$this->updateManifest($this->manifest);
$this->updateRelatedManifests($this->manifest);

$composerPath = $this->getProperty('project.path') . '/composer.json';
if (is_file($composerPath)) {
$this->updateComposer($composerPath);
Expand Down Expand Up @@ -88,11 +102,11 @@ protected function updateComposer(string $composerPath)
$composer = str_replace($match[0], $fixed, $composer);
}
if (preg_match('#"php"\s*:\s*"(.*)"#', $composer, $match)) {
$fixed = str_replace($match[1], '>=7.2.5', $match[0]);
$fixed = str_replace($match[1], '>=7.2.5', $match[0]);
$composer = str_replace($match[0], $fixed, $composer);
}
if (preg_match('#"target-platform"\s*:\s*"(.*)"#', $composer, $match)) {
$fixed = str_replace($match[1], '.*', $match[0]);
$fixed = str_replace($match[1], '.*', $match[0]);
$composer = str_replace($match[0], $fixed, $composer);
}

Expand All @@ -106,15 +120,12 @@ protected function updateComposer(string $composerPath)
}

/**
* @param string $manifestPath
*
* @return void
*/
protected function updateManifest($manifestPath = null)
protected function updateManifest(string $manifestPath)
{
$manifestPath = $manifestPath ?: $this->getProperty('project.manifest');
if (is_file($manifestPath) == false) {
$this->throwError('Manifest not found: ' . $manifestPath);
}

$manifestString = file_get_contents($manifestPath);
$sha1 = sha1($manifestString);

Expand Down Expand Up @@ -182,4 +193,40 @@ function ($tag) {
);
}
}

/**
* @param string $manifestPath
*
* @return void
*/
protected function updateRelatedManifests(string $manifestPath)
{
/** @var SimpleXMLElement $manifest */
$manifest = $this->tryXmlFunctions(function () use ($manifestPath) {
return simplexml_load_file($manifestPath);
});

$basePath = $this->getProperty('project.source.path');

$relatedExtensions = explode(',', $this->getProperty('project.relatedExtensions'));
foreach ($relatedExtensions as $relatedExtension) {
$path = $this->getProperty('project.' . $relatedExtension . '.path');
if (empty($path)) {
$path = $basePath . '/extensions/' . $relatedExtension;
if (is_dir($path)) {
$xpath = sprintf("alledia/relatedExtensions/extension[text()='%s']", $relatedExtension);
$extension = $manifest->xpath($xpath);
$extension = reset($extension);

$type = (string)$extension['type'];
$element = (string)$extension['element'];
if ($extension instanceof SimpleXMLElement && $type && $element) {
if ($manifestPath = $this->findManifestFile($type, $element, $path)) {
$this->updateManifest($manifestPath);
}
}
}
}
}
}
}
75 changes: 13 additions & 62 deletions src/tasks/TraitShack.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ protected function getProperty(string $name, $default = null)
}

/**
* @param string $name
* @param string $name
* @param ?string $value
*
* @return ?string
Expand Down Expand Up @@ -171,21 +171,13 @@ protected function tryXmlFunctions(callable $function)

/**
* @param string $type Type code or full element name
* @param string $element Short element or base path
* @param ?string $path Optional base path
* @param string $element Short element
* @param string $path Optional base path
*
* @return string
*/
protected function findManifestFile(string $type, string $element, string $path = null): string
protected function findManifestFile(string $type, string $element, string $path): string
{
if (func_num_args() == 2) {
$elementParts = $this->getElementFromLong($type);

$path = $element;
$type = $elementParts->type;
$element = $elementParts->element;
}

if (!is_dir($path)) {
$this->throwError('Invalid Manifest path: ' . $path);
}
Expand All @@ -196,7 +188,15 @@ protected function findManifestFile(string $type, string $element, string $path

switch ($type) {
case 'component':
$baseName = '/' . substr($element, 4);
if (strpos($element, 'com_') === 0) {
$baseName = '/' . substr($element, 4);
}
break;

case 'module':
if (strpos($element, 'mod_') !== 0) {
$baseName = '/mod_' . $element;
}
break;

case 'template':
Expand All @@ -216,55 +216,6 @@ protected function findManifestFile(string $type, string $element, string $path
return $manifestPath;
}

/**
* @param string $elementLong
*
* @return Element
*/
protected function getElementFromLong(string $elementLong): Element
{
$parts = explode('_', $elementLong);

$typeId = array_shift($parts);
$type = array_search($typeId, $this->extensionTypes);
if (empty($type)) {
$this->throwError('Extension type unrecognized: ' . $typeId);
}

$folder = null;
switch ($type) {
case 'component':
case 'module':
case 'package':
// Long and short element are the same
$element = $elementLong;
break;

case 'library':
case 'file':
case 'template':
// short element is whatever is left
$element = join('_', $parts);
break;

case 'plugin':
// Folder is next item in the array
$folder = array_shift($parts);
$element = join('_', $parts);
break;

default:
$this->throwError(sprintf('\'%s\' extensions are not recognized', $type));
die;
}

return new Element([
'type' => $type,
'folder' => $folder,
'element' => $element
]);
}

/**
* @param string[] $strings
* @param string $subject
Expand Down

0 comments on commit 817acff

Please sign in to comment.