Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: json files to use correct author field #49

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 33 additions & 14 deletions src/Command/UpdateLicensesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

namespace PrestaShop\HeaderStamp\Command;

use Exception;
use PhpParser\Node\Stmt;
use PhpParser\ParserFactory;
use PrestaShop\HeaderStamp\LicenseHeader;
Expand Down Expand Up @@ -399,32 +400,50 @@ private function addLicenseToHtmlFile(SplFileInfo $file): void
$this->addLicenseToFile($file, '<!--', '-->');
}

private function addLicenseToJsonFile(SplFileInfo $file): bool
/**
* @throws Exception
*/
private function addLicenseToJsonFile(SplFileInfo $file): void
{
if (!in_array($file->getFilename(), ['composer.json', 'package.json'])) {
return false;
return;
}

$content = json_decode($file->getContents(), true);
$oldContent = $content;
$content['author'] = 'PrestaShop';

$authorDetails = [
'name' => 'PrestaShop SA',
'email' => '[email protected]',
];

// update author information depending of file
if ('composer.json' === $file->getFilename()) {
$content['authors'] = [$authorDetails];
} else { // package.json
$content['author'] = $authorDetails;
}

$content['license'] = (false !== strpos($this->license, 'afl')) ? 'AFL-3.0' : 'OSL-3.0';

$encodedContent = json_encode($content, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT);

if (!$encodedContent) {
throw new Exception('File can not be encoded to JSON format');
}

// add blank line in end of file if not exist
if (substr($encodedContent, -1) !== "\n") {
$encodedContent .= "\n";
}

if (!$this->runAsDry) {
$result = file_put_contents(
file_put_contents(
$this->targetDirectory . '/' . $file->getRelativePathname(),
json_encode($content, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT)
$encodedContent
);
} else {
$result = true;
}

$newFileContent = (string) json_encode($content);
$oldFileContent = (string) json_encode($oldContent);

$this->reportOperationResult($newFileContent, $oldFileContent, $file->getFilename());

return false !== $result;
$this->reportOperationResult($encodedContent, $file->getContents(), $file->getFilename());
}

/**
Expand Down
5 changes: 2 additions & 3 deletions tests/integration/expected/dashproducts/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,5 @@
"config": {
"preferred-install": "dist"
},
"type": "prestashop-module",
"author": "PrestaShop"
}
"type": "prestashop-module"
}
15 changes: 7 additions & 8 deletions tests/integration/expected/gsitemap/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,17 @@
"description": "PrestaShop module gsitemap",
"homepage": "https://github.com/PrestaShop/gsitemap",
"license": "AFL-3.0",
"authors": [
{
"name": "PrestaShop SA",
"email": "[email protected]"
}
],
"require": {
"php": ">=5.6.0"
},
"config": {
"preferred-install": "dist"
},
"type": "prestashop-module",
"author": "PrestaShop"
}
"authors": [
{
"name": "PrestaShop SA",
"email": "[email protected]"
}
]
}
5 changes: 2 additions & 3 deletions tests/integration/module-samples/dashproducts/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,5 @@
"config": {
"preferred-install": "dist"
},
"type": "prestashop-module",
"author": "PrestaShop"
}
"type": "prestashop-module"
}
11 changes: 2 additions & 9 deletions tests/integration/module-samples/gsitemap/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,11 @@
"description": "PrestaShop module gsitemap",
"homepage": "https://github.com/PrestaShop/gsitemap",
"license": "AFL-3.0",
"authors": [
{
"name": "PrestaShop SA",
"email": "[email protected]"
}
],
"require": {
"php": ">=5.6.0"
},
"config": {
"preferred-install": "dist"
},
"type": "prestashop-module",
"author": "PrestaShop"
}
"type": "prestashop-module"
}
1 change: 0 additions & 1 deletion tests/integration/runner/FolderComparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ class FolderComparator
'autoload_static.php',
'autoload_real.php',
'ClassLoader.php',
'composer.json',
];

/**
Expand Down
Loading