diff --git a/README.md b/README.md index f280824..46b76b7 100644 --- a/README.md +++ b/README.md @@ -24,3 +24,23 @@ You can set command's namespaces in the file: `config/artisan_documentator.php` // 'make:' => 'Laravel make Commands', ], ``` + +## Using in CI + +In order to use generation in CI you need to specify your login and token in the file in the file: `config/artisan_documentator.php` in section 'git.remotes': +```php + 'git' => [ + 'dir' => base_path(), + 'remote' => [ + 'login' => 'my-name', + 'token' => env('ARTISAN_DOCUMENTATOR_GIT_REMOTE_TOKEN'), + ], + 'commit' => [ + 'message' => '[DOCS] auto-build console documentation', + ], + ], +``` + +Command call in your CI conf file: + +`php artisan command:doc docs/command.md --ci` \ No newline at end of file diff --git a/composer.json b/composer.json index 478da3c..0a0b190 100644 --- a/composer.json +++ b/composer.json @@ -13,6 +13,7 @@ "minimum-stability": "dev", "prefer-stable":true, "require": { + "artarts36/git-handler": "^1.1", "illuminate/config": "^8.0", "illuminate/console": "^8.0", "illuminate/filesystem": "^8.69", diff --git a/config/artisan_documentator.php b/config/artisan_documentator.php index 6062afa..169e3cc 100644 --- a/config/artisan_documentator.php +++ b/config/artisan_documentator.php @@ -17,4 +17,14 @@ // 'app:' => 'App Commands', // 'make:' => 'Laravel make Commands', ], -]; \ No newline at end of file + 'git' => [ + 'dir' => base_path(), + 'remote' => [ + 'login' => 'my-name', + 'token' => env('ARTISAN_DOCUMENTATOR_GIT_REMOTE_TOKEN'), + ], + 'commit' => [ + 'message' => '[DOCS] auto-build console documentation', + ], + ], +]; diff --git a/src/Generators/DocGenerator.php b/src/Generators/DocGenerator.php index 6ee98ce..35a01f5 100644 --- a/src/Generators/DocGenerator.php +++ b/src/Generators/DocGenerator.php @@ -17,13 +17,20 @@ public function __construct( ) { } - public function generate(string $documentator, string $path): void + /** + * @return bool - is file modified + */ + public function generate(string $documentator, string $path): bool { $content = $this ->documentators ->create($documentator) ->generate($this->commands->fetch($this->context->getNamespaces()), $this->context->namespacesGroups); + $prevHash = $this->files->exists($path) ? $this->files->hash($path) : ''; + $this->files->put($path, $content); + + return $prevHash !== $this->files->hash($path); } } diff --git a/src/Ports/Console/GenerateDocCommand.php b/src/Ports/Console/GenerateDocCommand.php index 9964ec2..344c966 100644 --- a/src/Ports/Console/GenerateDocCommand.php +++ b/src/Ports/Console/GenerateDocCommand.php @@ -3,20 +3,25 @@ namespace ArtARTs36\ArtisanDocumentator\Ports\Console; use ArtARTs36\ArtisanDocumentator\Generators\DocGenerator; +use ArtARTs36\ArtisanDocumentator\Support\Repo; use Illuminate\Console\Command; use Illuminate\Contracts\Config\Repository; class GenerateDocCommand extends Command { - protected $signature = 'command:doc {path} {--documentator=}'; + protected $signature = 'command:doc {path} {--documentator=} {--ci}'; protected $description = 'Generate commands documentation'; - public function handle(DocGenerator $generator, Repository $config) + public function handle(DocGenerator $generator, Repository $config, Repo $repo) { - $generator->generate( + $modified = $generator->generate( $this->option('documentator') ?? $config->get('artisan_documentator.documentators.default'), - $this->argument('path') + $path = $this->argument('path') ); + + if ($this->option('ci') && $modified) { + $repo->commitAndPush($path); + } } } diff --git a/src/Providers/ArtisanDocumentatorServiceProvider.php b/src/Providers/ArtisanDocumentatorServiceProvider.php index be948dd..6ecbde1 100644 --- a/src/Providers/ArtisanDocumentatorServiceProvider.php +++ b/src/Providers/ArtisanDocumentatorServiceProvider.php @@ -9,6 +9,8 @@ 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 Illuminate\Contracts\Container\Container; use Illuminate\Contracts\View\Factory; use Illuminate\Support\ServiceProvider; @@ -40,6 +42,17 @@ 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'); + + return new Repo( + (new LocalGitFactory())->factory($config['dir']), + $config['remote']['login'], + $config['remote']['token'], + $config['commit']['message'], + ); + }); } private function registerDocGenerateContext(): void diff --git a/src/Support/Repo.php b/src/Support/Repo.php new file mode 100644 index 0000000..5a0ee8d --- /dev/null +++ b/src/Support/Repo.php @@ -0,0 +1,32 @@ +git->index()->add($path); + $this->git->commits()->commit($this->message); + + $this->git->pushes()->send(function (MakingPush $push) { + $push->onRemote(function (UriInterface $uri) { + return $uri->withUserInfo($this->login, $this->token); + }); + }); + } +}