Skip to content

Commit

Permalink
Merge pull request #1 from ArtARTs36/ci
Browse files Browse the repository at this point in the history
Using in CI
  • Loading branch information
ArtARTs36 authored Nov 16, 2021
2 parents b878441 + f3818a1 commit c67dbea
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 6 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
12 changes: 11 additions & 1 deletion config/artisan_documentator.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,14 @@
// 'app:' => 'App Commands',
// 'make:' => 'Laravel make Commands',
],
];
'git' => [
'dir' => base_path(),
'remote' => [
'login' => 'my-name',
'token' => env('ARTISAN_DOCUMENTATOR_GIT_REMOTE_TOKEN'),
],
'commit' => [
'message' => '[DOCS] auto-build console documentation',
],
],
];
9 changes: 8 additions & 1 deletion src/Generators/DocGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
13 changes: 9 additions & 4 deletions src/Ports/Console/GenerateDocCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
13 changes: 13 additions & 0 deletions src/Providers/ArtisanDocumentatorServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
32 changes: 32 additions & 0 deletions src/Support/Repo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace ArtARTs36\ArtisanDocumentator\Support;

use ArtARTs36\GitHandler\Contracts\Handler\GitHandler;
use ArtARTs36\GitHandler\Making\MakingPush;
use Illuminate\Config\Repository;
use Psr\Http\Message\UriInterface;

class Repo
{
public function __construct(
private GitHandler $git,
private string $login,
private string $token,
private string $message,
) {
//
}

public function commitAndPush(string $path): void
{
$this->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);
});
});
}
}

0 comments on commit c67dbea

Please sign in to comment.