|
1 | 1 | <?php
|
2 | 2 |
|
3 |
| -use Sami\RemoteRepository\GitHubRemoteRepository; |
4 |
| -use Sami\Sami; |
5 |
| -use Symfony\Component\Finder\Finder; |
6 |
| -use Symfony\Component\Process\Process; |
7 |
| - |
8 |
| -return (new class() { |
9 |
| - public function buildSami() { |
10 |
| - $iterator = $this->getSourceIterator(); |
11 |
| - $themeSettings = $this->getThemeSettings(); |
12 |
| - $title = $this->getTitle(); |
13 |
| - $version = $this->getLatestStableVersion(); |
14 |
| - $remoteUrl = $this->getRemoteUrl(); |
15 |
| - |
16 |
| - $buildDir = __DIR__ . '/build/doc'; |
17 |
| - $cacheDir = __DIR__ . '/build/cache'; |
18 |
| - |
19 |
| - $this->clearDirectories([$buildDir, $cacheDir]); |
20 |
| - $this->checkout($version); |
21 |
| - |
22 |
| - register_shutdown_function(function () { |
23 |
| - $this->checkout('-'); |
24 |
| - }); |
25 |
| - |
26 |
| - return new Sami($iterator, $themeSettings + [ |
27 |
| - 'title' => $title, |
28 |
| - 'version' => $version, |
29 |
| - 'build_dir' => $buildDir, |
30 |
| - 'cache_dir' => $cacheDir, |
31 |
| - 'remote_repository' => new GitHubRemoteRepository($remoteUrl, __DIR__), |
32 |
| - 'default_opened_level' => 2, |
33 |
| - ]); |
34 |
| - } |
35 |
| - |
36 |
| - private function getSourceIterator(): Traversable { |
37 |
| - return Finder::create() |
38 |
| - ->files() |
39 |
| - ->name('*.php') |
40 |
| - ->in(__DIR__ . '/src'); |
41 |
| - } |
42 |
| - |
43 |
| - private function getThemeSettings(): array { |
44 |
| - $theme = getenv('SAMI_THEME'); |
45 |
| - $settings = []; |
46 |
| - |
47 |
| - if ($theme) { |
48 |
| - $settings['theme'] = basename($theme); |
49 |
| - $settings['template_dirs'] = [dirname($theme)]; |
50 |
| - } |
51 |
| - |
52 |
| - return $settings; |
53 |
| - } |
54 |
| - |
55 |
| - private function getTitle(): string { |
56 |
| - $readme = file_get_contents(__DIR__ . '/README.md'); |
57 |
| - |
58 |
| - if (!preg_match('/^#([^#\r\n]++)#?(\R|$)/', $readme, $match)) { |
59 |
| - throw new RuntimeException('Could not parse a title from the README.md'); |
60 |
| - } |
61 |
| - |
62 |
| - return trim($match[1]). ' API'; |
63 |
| - } |
64 |
| - |
65 |
| - private function getLatestStableVersion(): string { |
66 |
| - $process = new Process('git tag', __DIR__); |
67 |
| - $process->mustRun(); |
68 |
| - |
69 |
| - $tags = []; |
70 |
| - |
71 |
| - foreach (preg_split('/\R/', $process->getOutput()) as $tag) { |
72 |
| - if (preg_match('/^v?\d+\.\d+\.\d+$/', $tag)) { |
73 |
| - $tags[] = $tag; |
74 |
| - } |
75 |
| - } |
76 |
| - |
77 |
| - if (empty($tags)) { |
78 |
| - throw new RuntimeException('No stable versions exist to create documentation'); |
79 |
| - } |
80 |
| - |
81 |
| - usort($tags, function ($a, $b) { |
82 |
| - return version_compare($a, $b); |
83 |
| - }); |
84 |
| - |
85 |
| - return array_pop($tags); |
86 |
| - } |
87 |
| - |
88 |
| - private function getRemoteUrl(): string { |
89 |
| - $process = new Process('git remote get-url origin', __DIR__); |
90 |
| - $process->mustRun(); |
91 |
| - |
92 |
| - $url = trim($process->getOutput()); |
93 |
| - |
94 |
| - if (!preg_match('#^https://github.com/([^/.]++/[^/.]++)\.git#', $url, $match)) { |
95 |
| - throw new RuntimeException("The remote url '$url' for origin is not a valid github url"); |
96 |
| - } |
97 |
| - |
98 |
| - return $match[1]; |
99 |
| - } |
100 |
| - |
101 |
| - private function clearDirectories(array $paths) { |
102 |
| - foreach ($paths as $path) { |
103 |
| - if (file_exists($path)) { |
104 |
| - if (!is_dir($path)) { |
105 |
| - throw new RuntimeException("The directory path '$path' is not a directory"); |
106 |
| - } |
107 |
| - |
108 |
| - $process = new Process(['rm', '-rf', $path], __DIR__); |
109 |
| - $process->mustRun(); |
110 |
| - } |
111 |
| - } |
112 |
| - } |
113 |
| - |
114 |
| - private function checkout(string $name) { |
115 |
| - $process = new Process('git status --porcelain --untracked-files=no', __DIR__); |
116 |
| - $process->mustRun(); |
117 |
| - |
118 |
| - if (trim($process->getOutput())) { |
119 |
| - throw new RuntimeException("Cannot checkout '$name', because the directory is not clean"); |
120 |
| - } |
121 |
| - |
122 |
| - $process = new Process(['git', 'checkout', $name], __DIR__); |
123 |
| - $process->mustRun(); |
124 |
| - } |
125 |
| -})->buildSami(); |
| 3 | +return require __DIR__ . '/vendor/riimu/sami-config/config.php'; |
0 commit comments