build.php 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /**
  3. * OPcache GUI - build script
  4. *
  5. * @author Andrew Collington, andy@amnuts.com
  6. * @version 3.5.5
  7. * @link https://github.com/amnuts/opcache-gui
  8. * @license MIT, https://acollington.mit-license.org/
  9. */
  10. $remoteJsLocations = [
  11. 'cloudflare' => [
  12. 'cdnjs.cloudflare.com/ajax/libs/react/18.2.0/umd/react.production.min.js',
  13. 'cdnjs.cloudflare.com/ajax/libs/react-dom/18.2.0/umd/react-dom.production.min.js',
  14. 'cdnjs.cloudflare.com/ajax/libs/axios/1.3.6/axios.min.js',
  15. ],
  16. 'jsdelivr' => [
  17. 'cdn.jsdelivr.net/npm/react@18/umd/react.production.min.js',
  18. 'cdn.jsdelivr.net/npm/react-dom@18/umd/react-dom.production.min.js',
  19. 'cdn.jsdelivr.net/npm/axios/dist/axios.min.js',
  20. ],
  21. 'unpkg' => [
  22. 'unpkg.com/react@18/umd/react.production.min.js',
  23. 'unpkg.com/react-dom@18/umd/react-dom.production.min.js',
  24. 'unpkg.com/axios/dist/axios.min.js',
  25. ],
  26. ];
  27. $defaultRemoteJsFrom = array_keys($remoteJsLocations)[0];
  28. $options = getopt('jr:l:', ['local-js', 'remote-js', 'lang:']);
  29. $makeJsLocal = (isset($options['j']) || isset($options['local-js']));
  30. $useRemoteJsFrom = $options['r'] ?? $options['remote-js'] ?? $defaultRemoteJsFrom;
  31. $useLanguage = $options['l'] ?? $options['lang'] ?? null;
  32. $languagePack = 'null';
  33. $parentPath = dirname(__DIR__);
  34. if (!isset($remoteJsLocations[$useRemoteJsFrom])) {
  35. $validRemotes = implode(', ', array_keys($remoteJsLocations));
  36. echo "\nThe '{$useRemoteJsFrom}' remote js location is not valid - must be one of {$validRemotes} - defaulting to '{$defaultRemoteJsFrom}'\n\n";
  37. $useRemoteJsFrom = $defaultRemoteJsFrom;
  38. }
  39. if ($useLanguage !== null) {
  40. $useLanguage = preg_replace('/[^a-z_-]/', '', $useLanguage);
  41. $languageFile = __DIR__ . "/_languages/{$useLanguage}.json";
  42. if (!file_exists($languageFile)) {
  43. echo "\nThe '{$useLanguage}' file does not exist - using default English\n\n";
  44. } else {
  45. $languagePack = "<<< EOJSON\n" . file_get_contents($languageFile) . "\nEOJSON";
  46. }
  47. }
  48. if (!file_exists($parentPath . '/node_modules')) {
  49. echo "🐢 Installing node modules\n";
  50. exec('npm install');
  51. }
  52. echo "🏗️ Building js and css\n";
  53. chdir($parentPath);
  54. exec('npm run compile-jsx');
  55. exec('npm run compile-scss');
  56. echo "🚀 Creating single build file\n";
  57. $template = trim(file_get_contents(__DIR__ . '/template.phps'));
  58. $jsOutput = trim(file_get_contents(__DIR__ . '/interface.js'));
  59. $cssOutput = trim(file_get_contents(__DIR__ . '/interface.css'));
  60. $phpOutput = trim(implode('', array_slice(file($parentPath . '/src/Opcache/Service.php'), 7)));
  61. $output = str_replace(
  62. ['{{JS_OUTPUT}}', '{{CSS_OUTPUT}}', '{{PHP_OUTPUT}}', '{{LANGUAGE_PACK}}'],
  63. [$jsOutput, $cssOutput, $phpOutput, $languagePack],
  64. $template
  65. );
  66. if ($makeJsLocal) {
  67. echo "🔗 Making js locally in-line\n";
  68. $jsContents = [];
  69. foreach ($remoteJsLocations[$useRemoteJsFrom] as $jsUrl) {
  70. $jsContents[] = file_get_contents('https://' . $jsUrl);
  71. }
  72. $output = str_replace('{{JS_LIBRARIES}}',
  73. "<script>\n" . implode(";\n\n", $jsContents) . ";\n</script>",
  74. $output
  75. );
  76. } else {
  77. echo "🔗 Using remote js links from '{$useRemoteJsFrom}'\n";
  78. $output = str_replace('{{JS_LIBRARIES}}',
  79. implode("\n ", array_map(static function ($jsUrl) {
  80. return "<script src=\"//{$jsUrl}\"></script>";
  81. }, $remoteJsLocations[$useRemoteJsFrom])),
  82. $output
  83. );
  84. }
  85. file_put_contents($parentPath . '/index.php', $output);
  86. echo "💯 Done!\n";