|
11 | 11 | */ |
12 | 12 |
|
13 | 13 | /** |
14 | | - * Get CMake module header content. |
| 14 | + * Generate CMake module Markdown docs file from the file's header comment. |
15 | 15 | */ |
16 | 16 | function generateModuleDocs( |
17 | 17 | string $file, |
18 | 18 | string $namespace, |
19 | 19 | string $destination, |
20 | | - string $url = '', |
| 20 | + string $url, |
21 | 21 | ): void { |
22 | 22 | $content = file_get_contents($file); |
23 | 23 | preg_match('/^#\[===+\[\s*(.*?)\s*#\]===+\]/s', $content, $matches); |
24 | 24 |
|
25 | | - if (isset($matches[1])) { |
26 | | - $moduleName = basename($file, '.cmake'); |
| 25 | + if (!isset($matches[1])) { |
| 26 | + return; |
| 27 | + } |
27 | 28 |
|
28 | | - $content = ''; |
29 | | - $content .= "# $namespace" . "$moduleName\n\n"; |
30 | | - if ('' === $url) { |
31 | | - $url = 'https://github.com/petk/php-build-system/tree/master/cmake/cmake/modules/' . $namespace . $moduleName . '.cmake'; |
32 | | - } |
33 | | - $content .= "See: [$moduleName.cmake]($url)\n\n"; |
34 | | - $content .= $matches[1]; |
35 | | - $content .= "\n"; |
| 29 | + $moduleName = basename($file, '.cmake'); |
36 | 30 |
|
37 | | - if (!file_exists($destination . '/' . $namespace)) { |
38 | | - mkdir($destination . '/' . $namespace, 0777, true); |
39 | | - } |
| 31 | + $content = ''; |
| 32 | + $content .= "# $namespace" . "$moduleName\n\n"; |
| 33 | + $content .= "See: [$moduleName.cmake]($url)\n\n"; |
| 34 | + if ($namespace) { |
| 35 | + $content .= <<<EOT |
| 36 | + ## Basic usage |
40 | 37 |
|
41 | | - file_put_contents( |
42 | | - $destination . '/' . $namespace . $moduleName . '.md', |
43 | | - $content, |
44 | | - ); |
| 38 | + ```cmake |
| 39 | + include({$namespace}{$moduleName}) |
| 40 | + ``` |
| 41 | + EOT; |
| 42 | + } elseif (1 === preg_match('/^Find.+.cmake$/', $moduleName)) { |
| 43 | + $content .= <<<EOT |
| 44 | + ## Basic usage |
| 45 | +
|
| 46 | + ```cmake |
| 47 | + find_package($moduleName) |
| 48 | + ``` |
| 49 | + EOT; |
| 50 | + } else { |
| 51 | + $content .= <<<EOT |
| 52 | + ## Basic usage |
| 53 | +
|
| 54 | + ```cmake |
| 55 | + include(cmake/$moduleName.cmake) |
| 56 | + ``` |
| 57 | + EOT; |
45 | 58 | } |
46 | | -} |
| 59 | + $content .= "\n\n"; |
| 60 | + $content .= $matches[1]; |
| 61 | + $content .= "\n"; |
47 | 62 |
|
48 | | -$docs = __DIR__ . '/../docs/cmake/modules'; |
49 | | -if (!file_exists($docs)) { |
50 | | - mkdir($docs, 0777, true); |
| 63 | + if (!file_exists($destination)) { |
| 64 | + mkdir($destination, 0777, true); |
| 65 | + } |
| 66 | + |
| 67 | + $markdownFile = $destination . '/' . $moduleName . '.md'; |
| 68 | + if (file_exists($markdownFile)) { |
| 69 | + echo "\nWarning: $markdownFile already exists.\n"; |
| 70 | + $markdownFile = $destination . '/' . $moduleName . '_2.md'; |
| 71 | + echo "Module has been renamed to $markdownFile\n\n"; |
| 72 | + } |
| 73 | + |
| 74 | + file_put_contents($markdownFile, $content); |
51 | 75 | } |
52 | 76 |
|
53 | | -$docFiles = glob($docs . '/*{/*,*}', GLOB_BRACE); |
54 | | -foreach ($docFiles as $file) { |
55 | | - if (is_file($file)) { |
56 | | - unlink($file); |
| 77 | +/** |
| 78 | + * Remove directory contents recursively. |
| 79 | + */ |
| 80 | +function emptyDirectory(string $path): void |
| 81 | +{ |
| 82 | + $iterator = new RecursiveIteratorIterator( |
| 83 | + new RecursiveDirectoryIterator($path, RecursiveDirectoryIterator::SKIP_DOTS), |
| 84 | + RecursiveIteratorIterator::CHILD_FIRST, |
| 85 | + ); |
| 86 | + |
| 87 | + foreach ($iterator as $file) { |
| 88 | + if ($file->isDir()) { |
| 89 | + rmdir($file->getPathname()); |
| 90 | + } else { |
| 91 | + unlink($file->getPathname()); |
| 92 | + } |
57 | 93 | } |
58 | 94 | } |
59 | 95 |
|
60 | | -$modulesDirectory = realpath(__DIR__ . '/../cmake/cmake/modules'); |
61 | | -$files = glob($modulesDirectory . '/*{/*,*}.cmake', GLOB_BRACE); |
| 96 | +$modulesDocsDir = __DIR__ . '/../docs/cmake/modules'; |
62 | 97 |
|
63 | | -foreach ($files as $file) { |
64 | | - $relativeFilename = trim(str_replace($modulesDirectory, '', $file), '/'); |
65 | | - echo 'Processing ' . $relativeFilename . "\n"; |
| 98 | +if (!file_exists($modulesDocsDir)) { |
| 99 | + mkdir($modulesDocsDir, 0777, true); |
| 100 | +} else { |
| 101 | + emptyDirectory($modulesDocsDir); |
| 102 | +} |
| 103 | + |
| 104 | +$modules = [ |
| 105 | + 'cmake/modules', |
| 106 | + 'cmake/modules/Packages', |
| 107 | + 'cmake/modules/PHP', |
| 108 | + 'ext/opcache/cmake', |
| 109 | + 'ext/posix/cmake', |
| 110 | + 'ext/session/cmake', |
| 111 | + 'ext/skeleton/cmake/modules/FindPHP.cmake', |
| 112 | + 'ext/standard/cmake', |
| 113 | + 'sapi/fpm/cmake', |
| 114 | + 'sapi/phpdbg/cmake', |
| 115 | + 'Zend/cmake', |
| 116 | +]; |
66 | 117 |
|
67 | | - $namespace = trim(str_replace($modulesDirectory, '', dirname($file)), '/'); |
68 | | - $namespace = ('' == $namespace) ? '' : $namespace . '/'; |
| 118 | +$baseDir = __DIR__ . '/../cmake'; |
69 | 119 |
|
70 | | - generateModuleDocs($file, $namespace, $docs); |
| 120 | +$files = []; |
| 121 | +foreach ($modules as $module) { |
| 122 | + if (is_dir($baseDir . '/' . $module)) { |
| 123 | + $foundFiles = glob($baseDir . '/' . $module . '/*.cmake', GLOB_BRACE); |
| 124 | + $files = array_merge($files, $foundFiles); |
| 125 | + } else { |
| 126 | + $files[] = $baseDir . '/' . $module; |
| 127 | + } |
71 | 128 | } |
72 | 129 |
|
73 | | -// Add ext/skeleton/cmake/modules/FindPHP.cmake. |
74 | | -echo "Processing ext/skeleton/cmake/modules/FindPHP.cmake\n"; |
75 | | -generateModuleDocs( |
76 | | - __DIR__ . '/../cmake/ext/skeleton/cmake/modules/FindPHP.cmake', |
77 | | - '', |
78 | | - $docs, |
79 | | - 'https://github.com/petk/php-build-system/blob/master/cmake/ext/skeleton/cmake/modules/FindPHP.cmake', |
80 | | -); |
| 130 | +foreach ($files as $module) { |
| 131 | + $relativeFilename = trim(str_replace($baseDir, '', $module), '/'); |
| 132 | + |
| 133 | + if (str_starts_with($module, $baseDir . '/cmake/modules/')) { |
| 134 | + $namespace = trim(str_replace($baseDir . '/cmake/modules', '', dirname($module)), '/'); |
| 135 | + $namespace = ('' == $namespace) ? '' : $namespace . '/'; |
| 136 | + $subdir = $namespace; |
| 137 | + } else { |
| 138 | + $namespace = ''; |
| 139 | + if ('FindPHP.cmake' === basename($module)) { |
| 140 | + $subdir = ''; |
| 141 | + } else { |
| 142 | + $subdir = basename(dirname($module, 2)); |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + echo "Processing cmake/$relativeFilename\n"; |
| 147 | + generateModuleDocs( |
| 148 | + $module, |
| 149 | + $namespace, |
| 150 | + $modulesDocsDir . '/' . $subdir, |
| 151 | + 'https://github.com/petk/php-build-system/blob/master/cmake/' . $relativeFilename, |
| 152 | + ); |
| 153 | +} |
0 commit comments