Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into experimental-refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
ProjektGopher committed Aug 9, 2023
2 parents 1b1e75d + fa93dd7 commit 46ec297
Show file tree
Hide file tree
Showing 9 changed files with 452 additions and 9 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,18 @@ chmod +x ./scripts/*

## Testing
```bash
# Run test suite
composer test

# Test hook without having to make a dummy commit
git hook run pre-commit
```


## Contributing
Please see [CONTRIBUTING](CONTRIBUTING.md) for details.
> **Note** Don't build the binary when contributing. The binary will be built when a release is tagged.
Please see [CONTRIBUTING](CONTRIBUTING.md) for more details.


## Security Vulnerabilities
Expand Down
119 changes: 119 additions & 0 deletions app/FileJson.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?php

namespace ProjektGopher\Whisky;

use Illuminate\Support\Facades\File;
use Swaggest\JsonSchema\Context;
use Swaggest\JsonSchema\Schema;

class FileJson
{
public function __construct(
public string $path,
) {
//
}

public static function make(string $path): FileJson
{
return new FileJson($path);
}

public function read(bool $validate = true): string|array|null
{
$content = json_decode(File::get($this->path), true);

if (json_last_error() !== JSON_ERROR_NONE) {
$msg = 'Invalid JSON';
if (function_exists('json_last_error_msg')) {
$msg .= ': '.json_last_error_msg();
$msg .= ' in '.$this->path;
}
throw new \Exception($msg);
}

if ($validate) {
try {
$contentJson = json_decode(File::get($this->path));
$this->validate($contentJson);
} catch (\Exception $e) {
$msg = 'Invalid JSON schema';
$msg .= ': '.$e->getMessage();
$msg .= ' in '.$this->path;
throw new \Exception($msg);
}
}

return $content;
}

/**
* @param \stdClass|array|string|int|float|bool|null $content
*
* @throws \Exception
*/
protected function validate(mixed $content): void
{
$options = new Context();
// $options->version = 7;

$schema = Schema::import($this->getSchemaValidation(), $options);

$schema->in($content);
}

protected function getSchemaValidation(): ?\stdClass
{
// return json_decode(File::get(Whisky::base_path('resources/schemas/whisky.json')), false);
$schema = [
'$schema' => 'http://json-schema.org/draft-07/schema#',
'type' => 'object',
'properties' => [
'disabled' => [
'type' => 'array',
'items' => [
'type' => 'string',
],
],
'hooks' => [
'type' => 'object',
'properties' => [],
'additionalProperties' => false,
],
],
'required' => ['hooks'],
'additionalProperties' => false,
];

// TODO: Move this into const on the Hooks class.
$availableHooks = [
'pre-commit',
'prepare-commit-msg',
'commit-msg',
'post-commit',
'applypatch-msg',
'pre-applypatch',
'post-applypatch',
'pre-rebase',
'post-rewrite',
'post-checkout',
'post-merge',
'pre-push',
'pre-auto-gc',
];
$schema['properties']['disabled']['items']['enum'] = $availableHooks;
foreach ($availableHooks as $hook) {
$schema['properties']['hooks']['properties'][$hook] = [
'type' => 'array',
'items' => [
'type' => 'string',
'minLength' => 1,
'pattern' => '^(?!\s*$).+',
],
'minItems' => 1,
];
}

return json_decode(json_encode($schema), false);
}
}
3 changes: 1 addition & 2 deletions app/Whisky.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace ProjektGopher\Whisky;

use Illuminate\Support\Facades\File;
use Phar;

class Whisky
Expand Down Expand Up @@ -60,7 +59,7 @@ public static function base_path(string $path = ''): string

public static function readConfig(string $key): string|array|null
{
$cfg = File::json(static::cwd('whisky.json'));
$cfg = FileJson::make(static::cwd('whisky.json'))->read();

return data_get($cfg, $key);
}
Expand Down
13 changes: 10 additions & 3 deletions bin/run-hook
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
#!/bin/bash
#########################
# DO NOT EDIT THIS FILE #
#########################

# Get the hook name from the first argument
hook=$1
Expand All @@ -15,7 +12,17 @@ SCRIPTS=$($bin scripts "$hook")
# Make newlines the only separator
IFS=$'\n'

# Set the exit code to 0. If any script fails,
# this will then be set to that exit code
exit_code=0

# Run each script
for SCRIPT in $SCRIPTS; do
eval "$SCRIPT"
last_exit_code=$?
if [ $last_exit_code -ne 0 ]; then
exit_code=$last_exit_code
fi
done

exit $exit_code
Binary file modified builds/whisky
Binary file not shown.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"mockery/mockery": "^1.5.1",
"nunomaduro/larastan": "^2.0",
"nunomaduro/termwind": "^1.15.1",
"swaggest/json-schema": "^0.12.41",
"pestphp/pest": "^2.5",
"pestphp/pest-plugin-type-coverage": "^2.0"
},
Expand Down Expand Up @@ -67,7 +68,7 @@
"@php whisky test"
],
"types": [
"vendor/bin/pest --type-coverage --min=100"
"php -d memory_limit=1G vendor/bin/pest --type-coverage --min=100"
]
},
"minimum-stability": "stable",
Expand Down
144 changes: 143 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
|
*/

'version' => '0.4.0',
'version' => '0.4.1',

/*
|--------------------------------------------------------------------------
Expand Down
Loading

0 comments on commit 46ec297

Please sign in to comment.