Skip to content

Commit

Permalink
<bugfix> Fix issue with missing fonts copied. Issue #25
Browse files Browse the repository at this point in the history
  • Loading branch information
shakaran committed Oct 5, 2017
1 parent aa58a10 commit 6d8ade8
Show file tree
Hide file tree
Showing 2 changed files with 163 additions and 63 deletions.
211 changes: 148 additions & 63 deletions Command/BuildAssetsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class BuildAssetsCommand extends ContainerAwareCommand
* @var Kernel
*/
protected $kernel;

/**
* @var string
*/
Expand All @@ -36,62 +36,88 @@ class BuildAssetsCommand extends ContainerAwareCommand
* @var
*/
protected $webdir;

protected $builddir;

protected function configure()
{
$this->setName('avanzu:admin:build-assets')
->setDescription('Concatenate and Uglify asset groups to static files')
->addOption('compress', 'c', InputOption::VALUE_NONE, 'compress javascripts')
->addOption('mangle', 'm', InputOption::VALUE_NONE, 'mangle javascripts')
->addOption('uglifyjs-bin', false, InputOption::VALUE_OPTIONAL, 'uglifyjs binary', '/usr/bin/env uglifyjs')
->addOption('uglifycss-bin', false, InputOption::VALUE_OPTIONAL, 'uglifycss binary', '/usr/bin/env uglifycss')
->setDescription('Concatenate and Uglify asset groups to static files')
->addOption('compress', 'c', InputOption::VALUE_NONE, 'compress javascripts')
->addOption('mangle', 'm', InputOption::VALUE_NONE, 'mangle javascripts')
->addOption('uglifyjs-bin', false, InputOption::VALUE_OPTIONAL, 'uglifyjs binary', '/usr/bin/env uglifyjs')
->addOption('uglifycss-bin', false, InputOption::VALUE_OPTIONAL, 'uglifycss binary', '/usr/bin/env uglifycss')
;

$this->resdir = realpath(dirname(__FILE__) . '/../Resources');
$this->pubdir = $this->resdir . '/public';
}

protected function execute(InputInterface $input, OutputInterface $output)
{
/** @var $kernel Kernel */
$this->kernel = $this->getContainer()->get('kernel');
$this->webdir = realpath($this->getContainer()->getParameter('kernel.root_dir') . '/../web');
$this->builddir = $this->pubdir . '/static/' . $input->getOption('env');
$assets = $this->partition($this->resolveAll(include($this->resdir . '/config/assets.php')));

$assetsFiles = $this->resdir . '/config/assets.php';
$output->writeln('Loading assets files config from ' . $assetsFiles);

$assets = $this->partition($this->resolveAll(include($assetsFiles)));
$fs = new Filesystem();

foreach($assets['scripts'] as $group => $files) {

$output->writeln('Processing scripts');
foreach($assets['scripts'] as $group => $files)
{
$this->processScript($group, $files, $fs, $input, $output);
}


$output->writeln('Processing styles');
foreach($assets['styles'] as $group => $files) {
$this->processStyle($group, $files, $fs, $input, $output);
}


$output->writeln('Processing styles');
foreach($assets['fonts'] as $group => $files) {
$this->processFonts($group, $files, $fs, $input, $output);
}

$fontsdir = $this->builddir . '/fonts';
$output->writeln('Searching for fonts under ' . $fontsdir);

$fs->exists($fontsdir) or $fs->mkdir($fontsdir);

foreach($this->findFonts() as $name => $path) {
$fs->copy($path, "$fontsdir/$name");

$fontsFound = $this->findFonts();

if(!empty($fontsFound))
{
foreach($fontsFound as $name => $path)
{
$output->writeln('Font found: ' . $name . ' in ' . $path);
$fs->copy($path, "$fontsdir/$name");
}
}
else
{
$output->writeln('No fonts found');
}
}

protected function findFonts()
{
$finder = new Finder();
$finder->files()->in("$this->pubdir")->path('/fonts');
$fonts = [];
/** @var SplFileInfo $file */
foreach($finder as $file) {
foreach($finder as $file)
{
if(isset($fonts[$file->getFilename()])) continue;
$fonts[$file->getFilename()] = $file->getRealPath();
}

return $fonts;
}

/**
* @param $name
* @param $files
Expand All @@ -103,44 +129,44 @@ protected function processScript($name, $files, $fs, $in, $out)
{
$dir = $this->builddir . '/scripts/';
$file = $dir . $this->group2file($name, '.js');

$fs->exists($dir) or $fs->mkdir($dir);

$command = [$in->getOption('uglifyjs-bin')];
if($in->getOption('compress'))
$command[] = "-c 'dead_code,drop_debugger,drop_console,keep_fargs,unused=false,properties=false'";
if($in->getOption('mangle'))
$command[] = '-m';

$command[] = "-o $file";
$command[] = implode(' ', $files);

$proc = new Process(implode(' ', $command));

$out->writeln($proc->getCommandLine());
$proc->run(function ($type, $buffer) use ($in, $out) {
if (Process::ERR === $type) {
$out->write("<comment>$buffer</comment>");
} else {
$out->writeln($buffer);
}
});
if($in->getOption('mangle'))
$command[] = '-m';
$command[] = "-o $file";
$command[] = implode(' ', $files);
$proc = new Process(implode(' ', $command));
$out->writeln($proc->getCommandLine() . PHP_EOL);
$proc->run(function ($type, $buffer) use ($in, $out) {
if (Process::ERR === $type) {
$out->write("<comment>$buffer</comment>");
} else {
$out->writeln($buffer);
}
});
}

protected function processStyle($name, $files, $fs, $in, $out)
{
$dir = $this->builddir . '/styles/';
$file = $dir . $this->group2file($name, '.css');

$fs->exists($dir) or $fs->mkdir($dir);

$command = [$in->getOption('uglifycss-bin')];
$command[] = implode(' ', $files);
$command[] = "> $file";

$proc = new Process(implode(' ', $command));

$out->writeln($proc->getCommandLine());
$out->writeln($proc->getCommandLine() . PHP_EOL);
$proc->run(function ($type, $buffer) use ($in, $out) {
if (Process::ERR === $type) {
$out->writeln("<error>$buffer</error>");
Expand All @@ -149,51 +175,84 @@ protected function processStyle($name, $files, $fs, $in, $out)
}
});
}


/**
*
* @param unknown $name
* @param unknown $files
* @param unknown $fs
* @param unknown $in
* @param unknown $out
*/
protected function processFonts($name, $files, $fs, $in, $out)
{
$dir = $this->builddir . '/fonts/';

$fs->exists($dir) or $fs->mkdir($dir);

if(!empty($files))
{
foreach($files as $file)
{
$fs->copy($file, $dir . basename($file));
}
}
}

protected function group2file($name, $extension)
{
return str_replace('_', '-', preg_replace('!(_js|_css)$!', '', $name)) . $extension;
}

protected function endsWith($extension, $file)
{
return strrpos($file, $extension) === (strlen($file) - strlen($extension));
}

protected function isImage($file)
{
return strpos(mime_content_type($file), 'image/') === 0;
}

protected function resolveAll($assets)
{
$resolved = [];

foreach ($assets as $name => $inputs) {
if (!isset($resolved[$name])) {
$resolved[$name] = [];
}

foreach ($inputs['inputs'] as $input) {
$resolved[$name] = array_unique(array_merge($resolved[$name], $this->resolve($assets, $input)));
}
}

return $resolved;
}


/**
*
* @param array $resolved
* @return array[]|unknown
*/
protected function partition($resolved)
{
echo 'Partitioning assets in groups ' . PHP_EOL;

$grouped = [
'scripts' => [],
'styles' => [],
'images' => [],
'files' => [],
'fonts' => [],
];

foreach ($resolved as $group => $files) {
foreach ($files as $file) {
echo 'Group: ' . $group . PHP_EOL;
foreach ($files as $file)
{
echo ' File: ' . $file . PHP_EOL;
switch (true) {
case $this->endsWith('.js', $file):
$grouped['scripts'][$group][] = $file;
Expand All @@ -204,32 +263,58 @@ protected function partition($resolved)
case $this->isImage($file):
$grouped['images'][$group][] = $file;
break;

// Fonts
case $this->endsWith('.otf', $file):
$grouped['fonts'][$group][] = $file;
break;
case $this->endsWith('.eot', $file):
$grouped['fonts'][$group][] = $file;
break;
case $this->endsWith('.otf', $file):
$grouped['fonts'][$group][] = $file;
break;
case $this->endsWith('.svg', $file):
$grouped['fonts'][$group][] = $file;
break;
case $this->endsWith('.ttf', $file):
$grouped['fonts'][$group][] = $file;
break;
case $this->endsWith('.woff', $file):
$grouped['fonts'][$group][] = $file;
break;
case $this->endsWith('.woff2', $file):
$grouped['fonts'][$group][] = $file;
break;

default:
$grouped['files'][$group][] = $file;
break;
}
}

echo PHP_EOL;
}

return $grouped;
}

protected function resolve($groups, $input)
{
$resolved = [];
if(strpos($input, '@') === false) {
return [$this->webdir . '/' . $input];
}

$cleaned = str_replace('@', '', $input);
if(isset($groups[$cleaned])) {
foreach($groups[$cleaned]['inputs'] as $candidate) {
$resolved = array_merge($resolved, $this->resolve($groups, $candidate));
}

return $resolved;
}

if(($star = strpos($input, '*')) === false) {
try
{
Expand All @@ -248,7 +333,7 @@ protected function resolve($groups, $input)
}
}
}

return $resolved;
}
}
}
15 changes: 15 additions & 0 deletions Resources/config/demo/demo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ parameters:
avanzu_admin_theme.navbar_msg_listener.class: Avanzu\AdminThemeBundle\EventListener\NavbarMessageListDemoListener
avanzu_admin_theme.setup_menu_listener.class: Avanzu\AdminThemeBundle\EventListener\SidebarSetupMenuDemoListener
avanzu_admin_theme.setup_knp_menu_listener.class: Avanzu\AdminThemeBundle\EventListener\DemoKnpMenuListener
avanzu_admin_theme.menu_builder.class: Avanzu\AdminThemeBundle\Menu\MenuBuilder

services:
avanzu_admin_theme.navbar_user_listener:
Expand Down Expand Up @@ -35,4 +36,18 @@ services:
class: "%avanzu_admin_theme.setup_knp_menu_listener.class%"
tags:
- { name: kernel.event_listener, event: theme.sidebar_setup_knp_menu, method: onSetupKnpMenu }

# See http://symfony.com/doc/current/bundles/KnpMenuBundle/menu_service.html
avanzu_admin_theme.menu_builder:
class: "%avanzu_admin_theme.menu_builder.class%"
arguments: ["@knp_menu.factory"]

avanzu_admin_theme.main_menu:
class: Knp\Menu\MenuItem # the service definition requires setting the class
factory: ["@app.menu_builder", createMainMenu]
arguments: ~
tags:
- { name: knp_menu.menu, alias: avanzu_admin_theme_main_menu } # The alias is what is used to retrieve the menu



0 comments on commit 6d8ade8

Please sign in to comment.