Repository navigation
Expand file tree
/
Copy pathDocumentationImporter.php
More file actions
100 lines (76 loc) · 2.83 KB
/
Copy pathDocumentationImporter.php
File metadata and controls
100 lines (76 loc) · 2.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
declare(strict_types=1);
namespace App\Extend;
use GuzzleHttp\Client;
use Symfony\Component\Process\Process;
use TightenCo\Jigsaw\Jigsaw;
class DocumentationImporter
{
public const GIT_REPO_URL = 'https://github.com/geocoder-php/Geocoder.git';
public const CLONE_LOCATION = '/tmp/geocoder-build';
/** @var GithubEmojiReplacer */
private $emojiReplacer;
public function __construct()
{
$this->emojiReplacer = new GithubEmojiReplacer(new Client());
}
public function handle(Jigsaw $jigsaw): void
{
if ($jigsaw->getEnvironment() !== 'production' && file_exists($jigsaw->getSourcePath() . '/docs/index.md')) {
return;
}
echo 'Updating geocoder repo' . PHP_EOL;
file_exists(self::CLONE_LOCATION)
? $this->updateRepo()
: $this->cloneRepo();
echo 'Updating documentation files' . PHP_EOL;
$this->updateMainDocumentation($jigsaw);
$this->updateProvidersDocumentation($jigsaw);
}
protected function cloneRepo(): bool
{
$updateCommand = new Process(['git', 'clone', self::GIT_REPO_URL, self::CLONE_LOCATION, '--branch', 'master']);
$updateCommand->run();
return $updateCommand->isSuccessful();
}
protected function updateRepo(): bool
{
$updateCommand = new Process(['git', 'pull', 'origin', 'master'], self::CLONE_LOCATION);
$updateCommand->run();
return $updateCommand->isSuccessful();
}
public function updateMainDocumentation(Jigsaw $jigsaw): void
{
$content = $this->withHeader(
file_get_contents(self::CLONE_LOCATION . '/README.md'),
'Geocoder'
);
$content = $this->emojiReplacer->replace($content);
$jigsaw->writeSourceFile('docs/index.md', $content);
}
public function updateProvidersDocumentation(Jigsaw $jigsaw): void
{
$providerFiles = glob(sprintf('%s/src/Provider/*/*.md', self::CLONE_LOCATION));
$providerReadmeFiles = preg_grep('/README.md$/i', $providerFiles);
$providerDirectory = $jigsaw->getSourcePath() . '/docs/providers';
if (!file_exists($providerDirectory)) {
mkdir($providerDirectory);
}
foreach ($providerReadmeFiles as $fileName) {
$provider = basename(dirname($fileName));
$file = $this->emojiReplacer->replace(file_get_contents($fileName));
$jigsaw->writeSourceFile(sprintf('docs/providers/%s.md', $provider), $this->withHeader($file, $provider));
}
}
protected function withHeader(string $content, string $title, string $description = ''): string
{
$headerTemplate = '---
title: %s
description: %s
extends: _layouts.documentation
section: content
---
%s';
return sprintf($headerTemplate, $title, $description, $content);
}
}