Skip to content

Commit

Permalink
enhanced config transformation + added json schema generation
Browse files Browse the repository at this point in the history
  • Loading branch information
fchastanet committed May 25, 2024
1 parent 51d19b5 commit 2d0de5d
Show file tree
Hide file tree
Showing 19 changed files with 1,268 additions and 582 deletions.
3 changes: 2 additions & 1 deletion .pre-commit-config-github.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ repos:
# x modifier: extended. Spaces and text after a # in the pattern are ignored
exclude: |
(?x)(
^.vscode\/.*\.json$
.vscode\/.*\.json$
)
- repo: https://github.com/rhysd/actionlint
Expand Down Expand Up @@ -125,6 +125,7 @@ repos:
exclude: |
(?x)(
\.md$|
\.vscode/.*\.json$|
^\.vscode/.*\.code-snippets$
)
Expand Down
3 changes: 2 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ repos:
# x modifier: extended. Spaces and text after a # in the pattern are ignored
exclude: |
(?x)(
^.vscode\/.*\.json$
.vscode\/.*\.json$
)
- repo: https://github.com/rhysd/actionlint
Expand Down Expand Up @@ -120,6 +120,7 @@ repos:
exclude: |
(?x)(
\.md$|
\.vscode/.*\.json$|
^\.vscode/.*\.code-snippets$
)
Expand Down
13 changes: 13 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,19 @@
"name": "manualTests/Array::wrap.sh",
"program": "manualTests/Array::wrap.sh",
"args": []
},
{
"name": "Listen for Xdebug",
"type": "php",
"request": "launch",
"port": 9012
},
{
"name": "Launch currently open script",
"type": "php",
"request": "launch",
"program": "${file}",
"cwd": "${fileDirname}"
}
]
}
21 changes: 21 additions & 0 deletions command-configurator/.vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Listen for Xdebug",
"type": "php",
"request": "launch",
"port": 9012
},
{
"name": "Launch currently open script",
"type": "php",
"request": "launch",
"program": "${file}",
"cwd": "${fileDirname}"
}
]
}
18 changes: 18 additions & 0 deletions command-configurator/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"search.useIgnoreFiles": true,
"search.exclude": {
"vendor": true,
},
"explorer.autoReveal": false,
"files.exclude": {
"**/.git": true,
},
"editor.detectIndentation": false,
"editor.insertSpaces": true,
"editor.indentSize": "tabSize",
"editor.tabSize": 2,
"files.trimTrailingWhitespace": true,
"workbench.editorAssociations": {
"*.md": "default"
},
}
8 changes: 8 additions & 0 deletions command-configurator/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,13 @@
"symfony/yaml": "^5.4",
"monolog/monolog": "^2.9",
"opis/json-schema": "^2.3"
},
"autoload": {
"psr-4": {
"CommandConfigurator\\": "src/CommandConfigurator/"
},
"classmap": [
"src/CommandConfigurator/"
]
}
}
30 changes: 17 additions & 13 deletions command-configurator/config/default.php
Original file line number Diff line number Diff line change
@@ -1,27 +1,31 @@
<?php

return [
'author' => '[François Chastanet](https://github.com/fchastanet)',
'sourceFile' => '${REPOSITORY_URL}/tree/master/${SRC_FILE_PATH}',
'license' => 'MIT License',
'copyright' => 'copyrightCallback',
'optionGroups' => [
[
'title' => 'GLOBAL OPTIONS:',
'functionName' => 'zzzGroupGlobalOptionsFunction',
'defaultCommandConfig' => [
'author' => '[François Chastanet](https://github.com/fchastanet)',
'sourceFile' => '${REPOSITORY_URL}/tree/master/${SRC_FILE_PATH}',
'license' => 'MIT License',
'copyright' => 'copyrightCallback',
'optionGroups' => [
[
'title' => 'GLOBAL OPTIONS:',
'functionName' => 'zzzGroupGlobalOptionsFunction',
],
],
'includes' => [
'default.options.tpl' => true,
],
],
'includes' => [
'default.options.tpl' => true,
],
'options' => [
'defaultCommandOptions' => [
[
'variableName' => 'optionHelp',
'group' => 'zzzGroupGlobalOptionsFunction',
'type' => 'Boolean',
'help' => 'Display this command help',
'alts' => ['--help','-h',],
'callback' => 'optionHelpCallback',
'callbacks' => [
'optionHelpCallback',
],
],
],
];
68 changes: 45 additions & 23 deletions command-configurator/config/shellcheckLint.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,37 @@
<?php

$defaultCommandConfig = require(__DIR__."/default.php");
$defaultConfig = require(__DIR__."/default.php");

$options = array_merge(
$defaultConfig['defaultCommandOptions'],
[
[
'variableName' => 'optionFormat',
'type' => 'String',
'help' => 'define output format of this command',
'alts' => ['--format', '-f',],
"group" => "shellcheckLintOptionsFunction",
'defaultValue' => 'tty',
'authorizedValues' => ['checkstyle', 'diff', 'gcc', 'json', 'json1', 'quiet', 'tty',],
],
[
'variableName' => 'optionStaged',
'functionName' => 'optionStagedFunction',
'help' => 'lint only staged git files(files added to file list to be committed) and which are beginning with a bash shebang.',
'alts' => ['--staged',],
],
[
'variableName' => 'optionXargs',
'functionName' => 'optionXargsFunction',
'type' => 'Boolean',
'help' => 'uses parallelization(using xargs command) only if tty format',
'alts' => ['--xargs',],
],
]
);

$shellcheckLintCommand = array_merge(
$defaultCommandConfig,
$defaultConfig['defaultCommandConfig'],
[
'functionName' => 'shellcheckLintCommand',
'commandName' => 'shellcheckLint',
Expand All @@ -21,30 +49,12 @@
'title' => 'GLOBAL OPTIONS:',
'functionName' => 'zzzGroupGlobalOptionsFunction',
],
],
'options' => [
[
'variableName' => 'optionFormat',
'type' => 'String',
'help' => 'define output format of this command',
'alts' => ['--format', '-f',],
'defaultValue' => 'tty',
'authorizedValues' => ['checkstyle', 'diff', 'gcc', 'json', 'json1', 'quiet', 'tty',],
],
[
'variableName' => 'optionStaged',
'functionName' => 'optionStagedFunction',
'help' => 'lint only staged git files(files added to file list to be committed) and which are beginning with a bash shebang.',
'alts' => ['--staged',],
],
[
'variableName' => 'optionXargs',
'functionName' => 'optionXargsFunction',
'type' => 'Boolean',
'help' => 'uses parallelization(using xargs command) only if tty format',
'alts' => ['--xargs',],
'title' => 'OPTIONS:',
'functionName' => 'shellcheckLintOptionsFunction',
],
],
'options' => $options,
'args' => [
[
'variableName' => 'argShellcheckFiles',
Expand All @@ -61,6 +71,18 @@

return [
'binFile' => [
'targetFile' => '${FRAMEWORK_ROOT_DIR}/bin/shellcheckLint',
'relativeRootDirBasedOnTargetDir' => '..',
'templateFile' => 'binFile.gtpl',
'templateName' => 'binFile',
'srcDirs' => [
'/home/wsl/fchastanet/bash-dev-env/vendor/bash-tools-framework/src',
],
'templateDirs' => [
"templates-examples",
],
],
'binData' => [
'commands' => [
$shellcheckLintCommand,
],
Expand Down
83 changes: 42 additions & 41 deletions command-configurator/convertConfigFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@
use Symfony\Component\Yaml\Yaml;
use Opis\JsonSchema\{
Validator,
ValidationResult,
Errors\ErrorFormatter,
};
use Opis\JsonSchema\Errors\ValidationError;
use Opis\JsonSchema\Exceptions\InvalidKeywordException;
use Opis\JsonSchema\Exceptions\ParseException;
use CommandConfigurator\Transformers\TransformConfigDefault;

const YAML_INDENTATION = 2;
const YAML_DUMP_LEVEL = PHP_INT_MAX;
Expand Down Expand Up @@ -46,41 +45,6 @@ function getConfig(Logger $logger, string $configFile): array {
return require($configFile);
}

function transformConfig(Logger $logger, array &$config): void {
$logger->info("Transform config");
// check if each option has type set
if (is_array($config['binFile']['commands'])) {
foreach($config['binFile']['commands'] as &$command) {
if (is_array($command['options'])) {
foreach($command['options'] as &$option) {
if (!isset($option['type'])) {
$option['type'] = 'Boolean';
}
if (!isset($option['callbacks'])) {
$option['callbacks'] = [];
}
if (!isset($option['min'])) {
$option['min'] = 0;
}
if (!isset($option['max'])) {
$option['max'] = ($option['type'] === 'StringArray') ? -1 : 1;
}
}
unset($option);
}
if (is_array($command['args'])) {
foreach($command['args'] as &$option) {
if (!isset($option['callbacks'])) {
$option['callbacks'] = [];
}
}
unset($option);
}
}
unset($command);
}
}

function generateConfig(Logger $logger, string $configFile, array $config, string $targetConfigFile) {
$value = Yaml::dump(
$config,
Expand All @@ -101,7 +65,7 @@ function formatJsonSchemaParseError(Logger $logger, ValidationError $validationE
}
}

function validateJson($jsonValidator, array $config, object $jsonSchema): bool {
function validateJson(Logger $logger, $jsonValidator, array $config, object $jsonSchema): bool {
$config = json_decode(json_encode($config));
$result = $jsonValidator->validate($config, $jsonSchema);
if (!$result->isValid()) {
Expand All @@ -112,22 +76,59 @@ function validateJson($jsonValidator, array $config, object $jsonSchema): bool {
return true;
}

function generateJsonSchema(Logger $logger) {
$base = loadJsonFile($logger, __DIR__ . "/src/jsonSchema/commands.schema.template.json");
$args = loadJsonFile($logger, __DIR__ . "/src/jsonSchema/args.json");
$binFile = loadJsonFile($logger, __DIR__ . "/src/jsonSchema/binFile.json");
$commands = loadJsonFile($logger, __DIR__ . "/src/jsonSchema/commands.json");
$options = loadJsonFile($logger, __DIR__ . "/src/jsonSchema/options.json");
$parameter = loadJsonFile($logger, __DIR__ . "/src/jsonSchema/parameter.json");
$base['properties']['binFile'] = $binFile;
$base['properties']['binData']['properties']['commands'] = $commands;
$base['properties']['binData']['properties']['commands']['items']['properties']['options']['items'] = array_merge_recursive(
$parameter, $options
);
$base['properties']['binData']['properties']['commands']['items']['properties']['args']['items'] = array_merge_recursive(
$parameter, $args
);
file_put_contents(
__DIR__ . '/src/commands.schema.json',
json_encode($base, JSON_PRETTY_PRINT)
);
}

function loadJsonFile(Logger $logger, string $file) {
$logger->debug("Loading $file");
return json_decode(file_get_contents($file), true);
}

$logger = initLogger();
$configFile = __DIR__ . "/config/shellcheckLint.php";
$config = getConfig($logger, $configFile);
print_r(
$config);
$intermediateConfigFile = __DIR__ . "/config-generated/shellcheckLint.beforeTransform.json";
file_put_contents(
$intermediateConfigFile,
json_encode($config, JSON_PRETTY_PRINT)
);
$logger->info("Written intermediate json file before transform $intermediateConfigFile");
transformConfig($logger, $config);

$intermediateConfigFile = __DIR__ . "/config-generated/shellcheckLint.afterTransform.json";
$transformConfig = new TransformConfigDefault($logger);
$transformConfig->transformConfig($config);
file_put_contents(
$intermediateConfigFile,
json_encode($config, JSON_PRETTY_PRINT)
);
$logger->info("Written intermediate json file after transform $intermediateConfigFile");

generateJsonSchema($logger);
$jsonValidator = initJsonSchemaValidator();
$jsonSchema = json_decode(
file_get_contents(__DIR__.'/schema/commands.schema.json')
file_get_contents(__DIR__.'/src/commands.schema.json')
);
if (!validateJson($jsonValidator, $config, $jsonSchema)) {
if (!validateJson($logger, $jsonValidator, $config, $jsonSchema)) {
exit(1);
}
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
namespace CommandConfigurator\Exceptions;

use Exception;

class TransformException extends Exception {
}
Loading

0 comments on commit 2d0de5d

Please sign in to comment.