/bin/build-apidocs.php

https://github.com/doctrine/doctrine-website-sphinx · PHP · 127 lines · 93 code · 28 blank · 6 comment · 17 complexity · 332341d1f065564abaee0c8cd77d04f2 MD5 · raw file

  1. <?php
  2. /**
  3. * Use the projects.yml and build apidocs for all projects+versions
  4. *
  5. * Apigen (www.apigen.org) is required.
  6. */
  7. require_once __DIR__ . "/../vendor/autoload.php";
  8. if (!isset($argv[1])) {
  9. echo "missing argument.\n" .
  10. "You need to pass a folder to generate the api in.\n";
  11. exit(1);
  12. }
  13. $output = realpath($argv[1]);
  14. if (!is_dir($output)) {
  15. echo "First argument has to be a directory.\n";
  16. exit(1);
  17. }
  18. if (!file_exists(__DIR__ . '/../github-token.json')) {
  19. echo "Missing github-token.json!";
  20. exit(1);
  21. }
  22. $tokenData = json_decode(file_get_contents(__DIR__ . '/../github-token.json'), true);
  23. $token = $tokenData['token'];
  24. if (!is_string($token)) {
  25. echo "Invalid github-token.json provided";
  26. exit(1);
  27. }
  28. $data = \Symfony\Component\Yaml\Yaml::parse(file_get_contents(__DIR__ . "/../site/projects.yml"));
  29. foreach ($data as $project => $projectDetails) {
  30. if ( ! isset($projectDetails['browse_source_link'])) {
  31. continue;
  32. }
  33. $url = $projectDetails['browse_source_link'];
  34. $ch = curl_init("https://api.github.com/repos/doctrine/" . $projectDetails['repository'] . "/tags");
  35. curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
  36. curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: token $token"));
  37. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  38. $response = curl_exec($ch);
  39. curl_close($ch);
  40. $tagData = json_decode($response, true);
  41. if ( ! $tagData) {
  42. continue;
  43. }
  44. // Fix versions starting with "v", such as "v1.2.5"
  45. foreach ($tagData as $key => $tag) {
  46. $tagData[$key]['name'] = str_replace('v', '', $tag['name']);
  47. }
  48. usort($tagData, function($a, $b) {
  49. return version_compare($a['name'], $b['name']);
  50. });
  51. foreach ($projectDetails['versions'] as $version => $versionData) {
  52. $lastTag = array_reduce(
  53. array_filter($tagData, function($tag) use($version) {
  54. return strpos($tag['name'], $version) === 0;
  55. }),
  56. function ($highestVersion, $testVersion) {
  57. return version_compare($highestVersion['name'], $testVersion['name']) > 0
  58. ? $highestVersion
  59. : $testVersion;
  60. }
  61. );
  62. $checkout = $lastTag['name'];
  63. $directory = $output . "/$project/$version";
  64. $lastCommitFile = $directory . '/.gitcommit';
  65. $path = "source/$project";
  66. if (!file_exists($directory)) {
  67. echo "Creating directory: $directory\n";
  68. mkdir($directory, 0777, true);
  69. }
  70. $lastCommit = (file_exists($lastCommitFile))
  71. ? trim(file_get_contents($lastCommitFile))
  72. : null;
  73. $currentCommitCmd = sprintf("cd %s && git log -1 --oneline", $path);
  74. if (is_dir($path)) {
  75. $updateSourceCmd = sprintf("cd %s && git checkout master && git fetch && git checkout %s", $path, $checkout);
  76. } else {
  77. $updateSourceCmd = sprintf("git clone %s.git %s && cd %s && git checkout %s", $url, $path, $path, $checkout);
  78. }
  79. chdir(__DIR__ . "/../");
  80. echo "Executing $updateSourceCmd\n";
  81. shell_exec($updateSourceCmd);
  82. $currentCommit = trim(current(explode(" ", shell_exec($currentCommitCmd))));
  83. if ($currentCommit === $lastCommit) {
  84. echo "API Docs for $project @ $checkout is already up to date.\n";
  85. continue;
  86. }
  87. chdir(__DIR__ . "/../");
  88. $apiDocs = sprintf(
  89. './vendor/bin/apigen -s %s -d %s/%s --title "%s"',
  90. $path . '/lib/Doctrine',
  91. $output,
  92. "$project/$version",
  93. $projectDetails['title']
  94. );
  95. echo "Generating API Docs: $apiDocs\n";
  96. shell_exec($apiDocs);
  97. file_put_contents($lastCommitFile, $currentCommit);
  98. }
  99. }