Changed build functionality

* Using local js now embeds the js - no extra files to contend with
* Can now select between different remote locations
This commit is contained in:
Andrew Collington 2023-08-15 17:56:23 +01:00
parent 0f3c71770a
commit bfb12e7e7c
3 changed files with 49 additions and 18 deletions

View file

@ -4,22 +4,48 @@
* OPcache GUI - build script * OPcache GUI - build script
* *
* @author Andrew Collington, andy@amnuts.com * @author Andrew Collington, andy@amnuts.com
* @version 3.4.0 * @version 3.5.0
* @link https://github.com/amnuts/opcache-gui * @link https://github.com/amnuts/opcache-gui
* @license MIT, https://acollington.mit-license.org/ * @license MIT, https://acollington.mit-license.org/
*/ */
$options = getopt('jl:', ['local-js', 'lang:']); $remoteJsLocations = [
'cloudflare' => [
'cdnjs.cloudflare.com/ajax/libs/react/18.2.0/umd/react.production.min.js',
'cdnjs.cloudflare.com/ajax/libs/react-dom/18.2.0/umd/react-dom.production.min.js',
'cdnjs.cloudflare.com/ajax/libs/axios/1.3.6/axios.min.js',
],
'jsdelivr' => [
'cdn.jsdelivr.net/npm/react@18/umd/react.production.min.js',
'cdn.jsdelivr.net/npm/react-dom@18/umd/react-dom.production.min.js',
'cdn.jsdelivr.net/npm/axios/dist/axios.min.js',
],
'unpkg' => [
'unpkg.com/react@18/umd/react.production.min.js',
'unpkg.com/react-dom@18/umd/react-dom.production.min.js',
'unpkg.com/axios/dist/axios.min.js',
],
];
$defaultRemoteJsFrom = array_keys($remoteJsLocations)[0];
$options = getopt('jr:l:', ['local-js', 'remote-js', 'lang:']);
$makeJsLocal = (isset($options['j']) || isset($options['local-js'])); $makeJsLocal = (isset($options['j']) || isset($options['local-js']));
$useRemoteJsFrom = $options['r'] ?? $options['remote-js'] ?? $defaultRemoteJsFrom;
$useLanguage = $options['l'] ?? $options['lang'] ?? null; $useLanguage = $options['l'] ?? $options['lang'] ?? null;
$languagePack = 'null'; $languagePack = 'null';
$parentPath = dirname(__DIR__); $parentPath = dirname(__DIR__);
if (!isset($remoteJsLocations[$useRemoteJsFrom])) {
$validRemotes = implode(', ', array_keys($remoteJsLocations));
echo "\nThe '{$useRemoteJsFrom}' remote js location is not valid - must be one of {$validRemotes} - defaulting to '{$defaultRemoteJsFrom}'\n\n";
$useRemoteJsFrom = $defaultRemoteJsFrom;
}
if ($useLanguage !== null) { if ($useLanguage !== null) {
$useLanguage = preg_replace('/[^a-z_-]/', '', $useLanguage); $useLanguage = preg_replace('/[^a-z_-]/', '', $useLanguage);
$languageFile = __DIR__ . "/_languages/{$useLanguage}.json"; $languageFile = __DIR__ . "/_languages/{$useLanguage}.json";
if (!file_exists($languageFile)) { if (!file_exists($languageFile)) {
echo "The '{$useLanguage}' file does not exist - using default English\n\n"; echo "\nThe '{$useLanguage}' file does not exist - using default English\n\n";
} else { } else {
$languagePack = "<<< EOJSON\n" . file_get_contents($languageFile) . "\nEOJSON"; $languagePack = "<<< EOJSON\n" . file_get_contents($languageFile) . "\nEOJSON";
} }
@ -46,18 +72,25 @@ $output = str_replace(
[$jsOutput, $cssOutput, $phpOutput, $languagePack], [$jsOutput, $cssOutput, $phpOutput, $languagePack],
$template $template
); );
if ($makeJsLocal) { if ($makeJsLocal) {
echo "🔗 Making js links local\n"; echo "🔗 Making js locally in-line\n";
$jsTags = []; $jsContents = [];
$matched = preg_match_all('!<script src="([^"]*)"></script>!', $output, $jsTags); foreach ($remoteJsLocations[$useRemoteJsFrom] as $jsUrl) {
if ($matched) { $jsContents[] = file_get_contents('https://' . $jsUrl);
foreach ($jsTags[1] as $jsUrl) {
$jsFile = basename($jsUrl);
$jsFilePath = $parentPath . '/' . $jsFile;
file_put_contents($jsFilePath, file_get_contents('https:' . $jsUrl));
$output = str_replace($jsUrl, $jsFile, $output);
}
} }
$output = str_replace('{{JS_LIBRARIES}}',
"<script>\n" . implode(";\n\n", $jsContents) . ";\n</script>",
$output
);
} else {
echo "🔗 Using remote js links from '{$useRemoteJsFrom}'\n";
$output = str_replace('{{JS_LIBRARIES}}',
implode("\n ", array_map(static function ($jsUrl) {
return "<script src=\"//{$jsUrl}\"></script>";
}, $remoteJsLocations[$useRemoteJsFrom])),
$output
);
} }
file_put_contents($parentPath . '/index.php', $output); file_put_contents($parentPath . '/index.php', $output);

View file

@ -8,7 +8,7 @@ namespace Amnuts\Opcache;
* A simple but effective single-file GUI for the OPcache PHP extension. * A simple but effective single-file GUI for the OPcache PHP extension.
* *
* @author Andrew Collington, andy@amnuts.com * @author Andrew Collington, andy@amnuts.com
* @version 3.4.0 * @version 3.5.0
* @link https://github.com/amnuts/opcache-gui * @link https://github.com/amnuts/opcache-gui
* @license MIT, https://acollington.mit-license.org/ * @license MIT, https://acollington.mit-license.org/
*/ */
@ -71,9 +71,7 @@ $opcache = (new Service($options))->handle();
<meta name="viewport" content="width=device-width,initial-scale=1.0"> <meta name="viewport" content="width=device-width,initial-scale=1.0">
<meta name="robots" content="noindex, nofollow" /> <meta name="robots" content="noindex, nofollow" />
<title>OPcache statistics on <?= $opcache->getData('version', 'host'); ?></title> <title>OPcache statistics on <?= $opcache->getData('version', 'host'); ?></title>
<script src="//cdnjs.cloudflare.com/ajax/libs/react/18.2.0/umd/react.production.min.js"></script> {{JS_LIBRARIES}}
<script src="//cdnjs.cloudflare.com/ajax/libs/react-dom/18.2.0/umd/react-dom.production.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/axios/1.3.6/axios.min.js"></script>
<style> <style>
{{CSS_OUTPUT}} {{CSS_OUTPUT}}
</style> </style>

View file

@ -8,7 +8,7 @@ use Exception;
class Service class Service
{ {
public const VERSION = '3.4.0'; public const VERSION = '3.5.0';
protected $tz; protected $tz;
protected $data; protected $data;