From b50fbf03fe3d2ddadd3d6ead3587db77d6b6a63a Mon Sep 17 00:00:00 2001 From: Remi Collet Date: Wed, 27 Sep 2017 13:53:04 +0200 Subject: add missing minified JS and CSS files --- glpi-minify.php | 136 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ glpi.spec | 18 +++++++- 2 files changed, 152 insertions(+), 2 deletions(-) create mode 100644 glpi-minify.php diff --git a/glpi-minify.php b/glpi-minify.php new file mode 100644 index 0000000..91aa617 --- /dev/null +++ b/glpi-minify.php @@ -0,0 +1,136 @@ +. + * --------------------------------------------------------------------- + */ + +require '/usr/share/php/natxet/CssMin/autoload.php'; +require '/usr/share/php/Patchwork/autoload-jsqueeze.php'; + +class Task +{ + private function taskMinifyCSS($srce, $dest) { + $l = strlen(dirname(__DIR__))+1; + $in = file_get_contents($srce); + $out = \CssMin::minify($in); + if ($out && strlen($out) < strlen($in)) { + file_put_contents($dest, $out); + printf("+ minify from %-40s to %-40s: %2d%% of %6d\n", substr($srce, $l), substr($dest, $l), round(strlen($out)*100/strlen($in)), strlen($in)); + } else { + printf("- minify from %40s to %40s: skipped", substr($srce, $l), substr($dest, $l)); + } + } + + private function taskMinifyJS($srce, $dest) { + $l = strlen(dirname(__DIR__))+1; + $in = file_get_contents($srce); + $jsqueeze = new \Patchwork\JSqueeze(); + $out = $jsqueeze->squeeze($in); + if ($out && strlen($out) < strlen($in)) { + file_put_contents($dest, $out); + printf("+ minify from %-40s to %-40s: %2d%% of %6d\n", substr($srce, $l), substr($dest, $l), round(strlen($out)*100/strlen($in)), strlen($in)); + } else { + printf("- minify from %40s to %40s: skipped", substr($srce, $l), substr($dest, $l)); + } + } + + /** + * Minify CSS stylesheets + * + * @param string $css_dir path of CSS files + * + * @return this + */ + public function minifyCSS($css_dir) { + + if (is_dir($css_dir)) { + $it = new RegexIterator( + new RecursiveIteratorIterator( + new RecursiveDirectoryIterator($css_dir) + ), + "/\\.css\$/i" + ); + + foreach ($it as $css_file) { + if (!$this->endsWith($css_file->getFilename(), 'min.css')) { + $this->taskMinifyCSS($css_file->getRealpath(), str_replace('.css', '.min.css', $css_file->getRealpath())); + } + } + } + return $this; + } + + /** + * Minify JavaScript files stylesheets + * + * @param string $js_dir path of CSS files + * + * @return this + */ + public function minifyJS($js_dir) { + + if (is_dir($js_dir)) { + $it = new RegexIterator( + new RecursiveIteratorIterator( + new RecursiveDirectoryIterator($js_dir) + ), + "/\\.js\$/i" + ); + + foreach ($it as $js_file) { + if (!$this->endsWith($js_file->getFilename(), 'min.js')) { + $this->taskMinifyJS($js_file->getRealpath(), str_replace('.js', '.min.js', $js_file->getRealpath())); + } + } + } + + return $this; + } + + /** + * Checks if a string ends with another string + * + * @param string $haystack Full string + * @param string $needle Ends string + * + * @return boolean + * @see http://stackoverflow.com/a/834355 + */ + private function endsWith($haystack, $needle) { + $length = strlen($needle); + if ($length == 0) { + return true; + } + + return (substr($haystack, -$length) === $needle); + } +} + +$task = new Task(); +$task->minifyCSS( __DIR__ . '/../css')->minifyJS( __DIR__ . '/../js'); diff --git a/glpi.spec b/glpi.spec index a758bd8..553b37a 100644 --- a/glpi.spec +++ b/glpi.spec @@ -43,7 +43,7 @@ Name: %{gh_project} Version: 9.2 -Release: 2%{?dist} +Release: 3%{?dist} Summary: Free IT asset management software Summary(fr): Gestion Libre de Parc Informatique @@ -59,6 +59,8 @@ Source12: %{name}-9.1-config_path_test.php Source3: %{name}-logrotate Source4: %{name}-nginx.conf Source5: %{name}-fedora-autoloader.php +# Temporary minify script, waiting for consolidation/robo +Source6: %{name}-minify.php BuildArch: noarch BuildRequires: gettext @@ -98,6 +100,11 @@ BuildRequires: php-composer(guzzlehttp/guzzle) >= 6 BuildRequires: php-composer(atoum/atoum) < 4 BuildRequires: php-composer(atoum/atoum) >= 3.2 %endif +BuildRequires: php-cli +BuildRequires: php-composer(natxet/CssMin) < 4 +BuildRequires: php-composer(natxet/CssMin) >= 3.0 +BuildRequires: php-composer(patchwork/jsqueeze) < 3 +BuildRequires: php-composer(patchwork/jsqueeze) >= 2.0 %if %{with_nginx} Requires: nginx-filesystem @@ -227,6 +234,7 @@ ln -s /usr/share/fonts/fontawesome/fontawesome-webfont.ttf lib/font-awesome-4.7. ls lib cp %{SOURCE2} config/config_path.php +cp %{SOURCE6} tools/minify.php mkdir vendor sed -e "s,##DATADIR##,%{_datadir}," \ @@ -258,7 +266,10 @@ EOF %build -# Regenerate the locales +: Minify CSS and JS files +php tools/minify.php + +: Regenerate the locales for po in locales/*.po do msgfmt $po -o $(dirname $po)/$(basename $po .po).mo @@ -486,6 +497,9 @@ fi %changelog +* Wed Sep 27 2017 Remi Collet - 9.2-3 +- add missing minified JS and CSS files + * Tue Sep 26 2017 Remi Collet - 9.2-2 - missing js and sound files -- cgit