Skip to content
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
86 changes: 86 additions & 0 deletions .github/scripts/validatePluginJson.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

/**
* Recursively find all `plugin.json` files in a directory.
*
* @param string $dir Directory to search in.
* @return array List of file paths.
*/
function findPluginJsonFiles(string $dir): array
{
$results = [];
$files = scandir($dir);

foreach ($files as $file) {
if ($file === '.' || $file === '..') {
continue;
}

$path = $dir . DIRECTORY_SEPARATOR . $file;

if (is_dir($path)) {
$results = array_merge($results, findPluginJsonFiles($path));
} elseif ($file === 'plugin.json') {
$results[] = $path;
}
}

return $results;
}

/**
* Validate a `plugin.json` file to ensure it does not contain a "meta" key.
*
* @param string $file Path to the JSON file.
* @param string $relativePath Relative path to the file for better output clarity.
* @return string|null Error message if invalid, or null if valid.
*/
function validateJsonFile(string $file, string $relativePath): ?string
{
$content = file_get_contents($file);

if ($content === false) {
return "Failed to read $relativePath.";
}

$json = json_decode($content, true);

if (json_last_error() !== JSON_ERROR_NONE) {
return "Invalid JSON in $relativePath: " . json_last_error_msg();
}

if (array_key_exists('meta', $json)) {
return "$relativePath contains a 'meta' key. Please remove it.";
}

return null;
}

$root = realpath(__DIR__ . '/../../');
$pluginJsonFiles = findPluginJsonFiles($root);

echo 'Found ' . count($pluginJsonFiles) . " plugin.json file(s) to validate.\n";

$errors = [];

foreach ($pluginJsonFiles as $file) {
$relativePath = str_replace($root . DIRECTORY_SEPARATOR, '', $file);

echo "Validating $relativePath...\n";
$error = validateJsonFile($file, $relativePath);

if ($error !== null) {
$errors[] = $error;
}
}

if (!empty($errors)) {
echo "\nErrors found in the following plugin.json files:\n";
foreach ($errors as $error) {
echo " - $error\n";
}
exit(1);
}

echo "All plugin.json files are valid!\n";
exit(0);
25 changes: 25 additions & 0 deletions .github/workflows/validate-plugin-json.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Validate plugin.json files

on:
push:
branches:
- main
pull_request:

jobs:
validate-plugin-json:
name: Validate plugin.json files
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Set up PHP
uses: shivammathur/setup-php@v2
with:
php-version: 8.4
coverage: none

- name: Validate plugin.json files
run: php .github/scripts/validatePluginJson.php