jsindex 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #!/usr/bin/env php
  2. <?php
  3. // Send all errors to stderr
  4. ini_set('display_errors', 'stderr');
  5. // setup composer autoloading
  6. $composerAutoload = [
  7. __DIR__ . '/../vendor/autoload.php', // standalone with "composer install" run
  8. __DIR__ . '/../../../autoload.php', // script is installed as a composer binary
  9. ];
  10. foreach ($composerAutoload as $autoload) {
  11. if (file_exists($autoload)) {
  12. require($autoload);
  13. break;
  14. }
  15. }
  16. if (!class_exists('cebe\jssearch\Indexer')) {
  17. error('Autoloading does not seem to work. Looks like you should run `composer install` first.');
  18. }
  19. // check arguments
  20. $src = [];
  21. foreach($argv as $k => $arg) {
  22. if ($k == 0) {
  23. continue;
  24. }
  25. if ($arg[0] == '-') {
  26. $arg = explode('=', $arg);
  27. switch($arg[0]) {
  28. // TODO allow baseUrl to be set via arg
  29. case '-h':
  30. case '--help':
  31. echo "jssearch index builder\n";
  32. echo "----------------------\n\n";
  33. echo "by Carsten Brandt <mail@cebe.cc>\n\n";
  34. usage();
  35. break;
  36. default:
  37. error("Unknown argument " . $arg[0], "usage");
  38. }
  39. } else {
  40. $src[] = $arg;
  41. }
  42. }
  43. if (empty($src)) {
  44. error("You have to give an input directory.", "usage");
  45. }
  46. $indexer = new \cebe\jssearch\Indexer();
  47. foreach($src as $dir) {
  48. echo "Processing $dir\n";
  49. $files = findFiles($dir);
  50. if (empty($files)) {
  51. echo "No files where found in $dir.\n";
  52. } else {
  53. $indexer->indexFiles($files, $dir);
  54. }
  55. }
  56. $js = $indexer->exportJs();
  57. file_put_contents('jssearch.index.js', $js);
  58. // functions
  59. /**
  60. * Display usage information
  61. */
  62. function usage() {
  63. global $argv;
  64. $cmd = $argv[0];
  65. echo <<<EOF
  66. Usage:
  67. $cmd [src-directory]
  68. --help shows this usage information.
  69. creates and jssearch.index.js file in the current directory.
  70. EOF;
  71. exit(1);
  72. }
  73. /**
  74. * Send custom error message to stderr
  75. * @param $message string
  76. * @param $callback mixed called before script exit
  77. * @return void
  78. */
  79. function error($message, $callback = null) {
  80. $fe = fopen("php://stderr", "w");
  81. fwrite($fe, "Error: " . $message . "\n");
  82. if (is_callable($callback)) {
  83. call_user_func($callback);
  84. }
  85. exit(1);
  86. }
  87. function findFiles($dir, $ext = '.html')
  88. {
  89. if (!is_dir($dir)) {
  90. error("$dir is not a directory.");
  91. }
  92. $dir = rtrim($dir, DIRECTORY_SEPARATOR);
  93. $list = [];
  94. $handle = opendir($dir);
  95. if ($handle === false) {
  96. error('Unable to open directory: ' . $dir);
  97. }
  98. while (($file = readdir($handle)) !== false) {
  99. if ($file === '.' || $file === '..') {
  100. continue;
  101. }
  102. $path = $dir . DIRECTORY_SEPARATOR . $file;
  103. if (is_file($path)) {
  104. if (substr($file, -($l = strlen($ext)), $l) === $ext) {
  105. $list[] = $path;
  106. }
  107. } else {
  108. $list = array_merge($list, findFiles($path, $ext));
  109. }
  110. }
  111. closedir($handle);
  112. return $list;
  113. }