Skip to content

Commit

Permalink
Merge pull request #2 from ArtARTs36/0.3
Browse files Browse the repository at this point in the history
0.3
  • Loading branch information
ArtARTs36 authored Nov 16, 2021
2 parents a2cc80d + b0b1bd7 commit e108afd
Show file tree
Hide file tree
Showing 19 changed files with 315 additions and 64 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
/tests export-ignore
/.editorconfig export-ignore
.phpunit.result.cache export-ignore
phpunit.xml export-ignore
37 changes: 37 additions & 0 deletions .github/workflows/testing.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: PHP Composer

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2

- name: Validate composer.json
run: composer validate

- name: Cache Composer packages
id: composer-cache
uses: actions/cache@v2
with:
path: vendor
key: ${{ runner.os }}-php-$GITHUB_REF-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-php-
- name: Install dependencies
if: steps.composer-cache.outputs.cache-hit != 'true'
run: composer install --prefer-dist --no-progress --no-suggest && composer dump-autoload -o

- name: Lint
run: composer lint

- name: Run Tests
run: composer test
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
/.idea
/.php_cs.cache
/.phpunit.result.cache
/tests/reports/
19 changes: 15 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,41 @@
}
],
"minimum-stability": "dev",
"prefer-stable":true,
"prefer-stable": true,
"require": {
"artarts36/git-handler": "^1.1.5",
"artarts36/ci-git-sender": "^0.1.0",
"illuminate/config": "^8.0",
"illuminate/console": "^8.0",
"illuminate/filesystem": "^8.69",
"illuminate/support": "^8.0",
"ondram/ci-detector": "^4.1"
"illuminate/support": "^8.0"
},
"require-dev": {
"mockery/mockery": "^1.4",
"orchestra/testbench": "^6.23",
"phpunit/phpunit": "^9.5",
"squizlabs/php_codesniffer": "*"
},
"autoload": {
"psr-4": {
"ArtARTs36\\ArtisanDocumentator\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"ArtARTs36\\ArtisanDocumentator\\Tests\\": "tests"
}
},
"config": {
"preferred-install": "dist",
"sort-packages": true
},
"scripts": {
"lint": [
"./vendor/bin/phpcs --standard=PSR2 src/"
],
"test": [
"echo 'Run tests'",
"XDEBUG_MODE=coverage ./vendor/bin/phpunit -v --coverage-text --configuration phpunit.xml --coverage-clover=tests/reports/logs/clover.xml --coverage-xml=tests/reports/logs/coverage-xml --log-junit=tests/reports/logs/junit.xml"
]
}
}
4 changes: 2 additions & 2 deletions config/artisan_documentator.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
'dir' => base_path(),
'remote' => [
'login' => 'my-name',
'token' => env('ARTISAN_DOCUMENTATOR_GIT_REMOTE_TOKEN'),
'token' => env('ARTISAN_DOCUMENTATOR_GIT_REMOTE_TOKEN', ''),
],
'commit' => [
'message' => '[DOCS] auto-build console documentation',
'message' => '[DOCS] auto-build console documentation in ${FILE}',
],
],
];
16 changes: 16 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd" backupGlobals="false" backupStaticAttributes="false" bootstrap="./vendor/autoload.php" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" executionOrder="random">
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">./src</directory>
</include>
</coverage>
<testsuites>
<testsuite name="Unit">
<directory suffix="Test.php">./tests/Unit</directory>
</testsuite>
<testsuite name="Feature">
<directory suffix="Test.php">./tests/Feature</directory>
</testsuite>
</testsuites>
</phpunit>
1 change: 1 addition & 0 deletions src/Contracts/ArtisanDocumentator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
interface ArtisanDocumentator
{
/**
* Generate documentation content
* @param array<string, array<Command>> $commands
* @param array<string, string> $namespaceGroups
*/
Expand Down
1 change: 1 addition & 0 deletions src/Contracts/CommandsFetcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
interface CommandsFetcher
{
/**
* @param array<string> $namespaces
* @return array<string, array<Command>>
*/
public function fetch(array $namespaces = []): array;
Expand Down
20 changes: 13 additions & 7 deletions src/Documentators/SignatureBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace ArtARTs36\ArtisanDocumentator\Documentators;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputOption;

class SignatureBuilder
{
Expand All @@ -14,20 +15,25 @@ public function build(Command $command): string
$signature .= ' {' . $argument->getName() . '}';
}

foreach ($command->getDefinition()->getOptions() as $option) {
$signature .= ' {--' . $option->getName();
$signature .= $this->buildOptions($command);

return $signature;
}

protected function buildOptions(Command $command): string
{
return implode("\n", array_map(function (InputOption $option) {
$signature = ' {--' . $option->getName();

if ($option->acceptValue()) {
$signature .= ' =';
$signature .= '=';
}

if (! empty($option->getDescription())) {
$signature .= ': ' . $option->getDescription();
}

$signature .= "}\n";
}

return $signature;
return $signature . '}';
}, $command->getDefinition()->getOptions()));
}
}
2 changes: 0 additions & 2 deletions src/Fetchers/AppCommandsFetcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

use ArtARTs36\ArtisanDocumentator\Contracts\CommandsFetcher;
use Illuminate\Contracts\Console\Kernel;
use Illuminate\Support\Arr;
use Illuminate\Console\Application;
use Symfony\Component\Console\Command\Command;

class AppCommandsFetcher implements CommandsFetcher
Expand Down
1 change: 1 addition & 0 deletions src/Generators/DocGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public function __construct(
}

/**
* Generate and save documentation to path
* @return bool - is file modified
*/
public function generate(string $documentator, string $path): bool
Expand Down
18 changes: 12 additions & 6 deletions src/Ports/Console/GenerateDocCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,33 @@
namespace ArtARTs36\ArtisanDocumentator\Ports\Console;

use ArtARTs36\ArtisanDocumentator\Generators\DocGenerator;
use ArtARTs36\ArtisanDocumentator\Support\Repo;
use ArtARTs36\ArtisanDocumentator\Support\Ci;
use Illuminate\Console\Command;
use Illuminate\Contracts\Config\Repository;
use OndraM\CiDetector\CiDetector;
use OndraM\CiDetector\CiDetectorInterface;

class GenerateDocCommand extends Command
{
protected $signature = 'command:doc {path} {--documentator=} {--ci}';

protected $description = 'Generate commands documentation';

public function handle(DocGenerator $generator, Repository $config, Repo $repo, CiDetector $ciDetector)
public function handle(DocGenerator $generator, Repository $config, Ci $ci)
{
$modified = $generator->generate(
$this->option('documentator') ?? $config->get('artisan_documentator.documentators.default'),
$path = $this->argument('path')
);

if ($this->option('ci') && $ciDetector->isCiDetected() && $modified) {
$repo->commitAndPush($path, $ciDetector->detect()->getBranch());
if ($modified) {
$this->info('Documentation updated');

if ($this->option('ci')) {
$ci->sendDoc($path);

$this->info('Documentation was send to remote git');
}
} else {
$this->info('Documentation already is actually');
}
}
}
37 changes: 26 additions & 11 deletions src/Providers/ArtisanDocumentatorServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@
use ArtARTs36\ArtisanDocumentator\Documentators\TemplateDocumentator;
use ArtARTs36\ArtisanDocumentator\Fetchers\AppCommandsFetcher;
use ArtARTs36\ArtisanDocumentator\Ports\Console\GenerateDocCommand;
use ArtARTs36\ArtisanDocumentator\Support\Repo;
use ArtARTs36\GitHandler\Factory\LocalGitFactory;
use ArtARTs36\ArtisanDocumentator\Support\Ci;
use ArtARTs36\CiGitSender\Commit\Message;
use ArtARTs36\CiGitSender\Contracts\Sender;
use ArtARTs36\CiGitSender\Remote\Credentials;
use ArtARTs36\CiGitSender\Factory\SenderFactory;
use Illuminate\Contracts\Container\Container;
use Illuminate\Contracts\View\Factory;
use Illuminate\Support\ServiceProvider;
Expand Down Expand Up @@ -43,16 +46,28 @@ public function boot(): void
return new ConfigDocumentatorFactory($container, $map);
});

$this->app->bind(Repo::class, static function (Container $container) {
$config = $container->get('config')->get('artisan_documentator.git');
$this->registerCi();
}

return new Repo(
(new LocalGitFactory())->factory($config['dir']),
$config['remote']['login'],
$config['remote']['token'],
$config['commit']['message'],
);
});
protected function registerCi(): void
{
$git = $this->app->get('config')->get('artisan_documentator.git');

$this
->app
->when(Ci::class)
->needs(Sender::class)
->give(static function () use ($git) {
return SenderFactory::local()->create($git['dir'], Credentials::fromArray($git['remote']));
});

$this
->app
->when(Ci::class)
->needs(Message::class)
->give(static function () use ($git) {
return new Message($git['commit']['message']);
});
}

private function registerDocGenerateContext(): void
Expand Down
21 changes: 21 additions & 0 deletions src/Support/Ci.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace ArtARTs36\ArtisanDocumentator\Support;

use ArtARTs36\CiGitSender\Commit\Message;
use ArtARTs36\CiGitSender\Contracts\Sender;

class Ci
{
public function __construct(
protected Sender $sender,
protected Message $message,
) {
//
}

public function sendDoc(string $file): void
{
$this->sender->send($file, $this->message);
}
}
32 changes: 0 additions & 32 deletions src/Support/Repo.php

This file was deleted.

23 changes: 23 additions & 0 deletions tests/Feature/GenerateDocCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace ArtARTs36\ArtisanDocumentator\Tests\Feature;

use ArtARTs36\ArtisanDocumentator\Ports\Console\GenerateDocCommand;
use ArtARTs36\ArtisanDocumentator\Tests\TestCase;
use Illuminate\Console\Command;

final class GenerateDocCommandTest extends TestCase
{
/**
* @covers \ArtARTs36\ArtisanDocumentator\Ports\Console\GenerateDocCommand::handle
*/
public function testHandleOnDocumentationUpdated(): void
{
$this
->artisan(GenerateDocCommand::class, [
'path' => 'commands.md',
])
->assertSuccessful()
->run();
}
}
18 changes: 18 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace ArtARTs36\ArtisanDocumentator\Tests;

use ArtARTs36\ArtisanDocumentator\Providers\ArtisanDocumentatorServiceProvider;
use Illuminate\Foundation\Application;
use Illuminate\Support\Facades\Facade;
use Illuminate\Contracts\Console\Kernel;

abstract class TestCase extends \Orchestra\Testbench\TestCase
{
protected function getPackageProviders($app)
{
return [
ArtisanDocumentatorServiceProvider::class,
];
}
}
Loading

0 comments on commit e108afd

Please sign in to comment.