Skip to content

Instantly share code, notes, and snippets.

@shengjie
Created December 23, 2016 10:29
Show Gist options
  • Save shengjie/b4fc409d14f3d7272a0f112ea274c4ed to your computer and use it in GitHub Desktop.
Save shengjie/b4fc409d14f3d7272a0f112ea274c4ed to your computer and use it in GitHub Desktop.
generate gulp config from assetic
use Assetic\Asset\AssetCollectionInterface;
use Assetic\Asset\AssetInterface;
use Assetic\Factory\LazyAssetManager;
use Assetic\Filter\CompassFilter;
use Assetic\Filter\CssRewriteFilter;
use Assetic\Filter\PhpCssEmbedFilter;
use Assetic\Filter\UglifyCssFilter;
use Assetic\Filter\UglifyJs2Filter;
use Assetic\Util\VarUtils;
use Assetic\Filter\TypeScriptFilter;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class AsseticGulpGenerateCommand extends ContainerAwareCommand
{
/** @var LazyAssetManager */
protected $am;
protected $basePath;
protected $rootPath;
protected $gulpConfigs = array();
protected function configure()
{
$this->setName('assetic:gulp-generate');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->am = $this->getContainer()->get('assetic.asset_manager');
$this->rootPath = realpath($this->getContainer()->getParameter('kernel.root_dir') . '/../') . '/';
$this->basePath = $this->correctPath($this->getContainer()->getParameter('assetic.write_to'));
foreach ($this->am->getNames() as $name) {
$this->dumpAsset($name, $output);
}
$code = [
'var gulp = require("gulp");',
'var merge = require("merge2");',
'var ts = require("gulp-typescript");',
'var tsConfig = { "rootDir":"app/Resources/assets/ts" };',
'var compass = require("gulp-compass");',
'var compassConfig = { "config_file":"config.rb", "css":"web/assets/compiled/css", "sass":"app/Resources/assets/scss" };',
'var concat = require("gulp-concat");',
'var cssBase64 = require("gulp-css-base64");',
'var uglifyjs = require("gulp-uglifyjs");',
'var uglifycss = require("gulp-uglifycss");',
'var cssrewrite = require("gulp-rewrite-css");',
'var cssrewriteConfig = { destination:"web/" };',
'',
];
$taskNames = [];
foreach ($this->gulpConfigs as $k => $task) {
$taskNames[] = "asset-install-{$k}";
$code[] = "gulp.task(\"asset-install-{$k}\", function(){";
$code[] = "return " . $task . ";";
$code[] = "});\n\n";
}
$code[] = 'gulp.task("asset-install", ["' . implode('","', $taskNames) . '"], function() {} );';
file_put_contents($this->rootPath . 'gulpfile.js', implode(PHP_EOL, $code));
}
private function correctPath($path)
{
return str_replace($this->rootPath, '', realpath($path));
}
private function dumpAsset($name, OutputInterface $stdout)
{
$asset = $this->am->get($name);
// $formula = $this->am->hasFormula($name) ? $this->am->getFormula($name) : array();
// start by dumping the main asset
$this->doDump($asset, $stdout);
/*
$debug = isset($formula[2]['debug']) ? $formula[2]['debug'] : $this->am->isDebug();
$combine = isset($formula[2]['combine']) ? $formula[2]['combine'] : !$debug;
// dump each leaf if no combine
if (!$combine) {
foreach ($asset as $leaf) {
$this->doDump($leaf, $stdout);
}
}
*/
}
/**
* Performs the asset dump.
*
* @param AssetInterface $asset An asset
* @param OutputInterface $stdout The command output
*
* @throws \RuntimeException If there is a problem writing the asset
*/
private function doDump(AssetInterface $asset, OutputInterface $stdout)
{
$combinations = VarUtils::getCombinations(
$asset->getVars(),
$this->getContainer()->getParameter('assetic.variables')
);
foreach ($combinations as $combination) {
$asset->setValues($combination);
// resolve the target path
$target = $this->basePath . '/' . $asset->getTargetPath();
$target = str_replace('_controller/', '', $target);
$target = VarUtils::resolve($target, $asset->getVars(), $asset->getValues());
$code = [];
if ($asset instanceof AssetCollectionInterface) {
$pipes = [];
foreach ($asset as $leaf) {
$assetToGulp = $this->assetToGulp($leaf);
if ($assetToGulp) {
$pipes[] = $assetToGulp;
}
}
if (count($pipes) > 1) {
$code[] = 'merge(' . implode(",\n", $pipes) . ')';
} else if (count($pipes) == 1) {
$code[] = $pipes[0];
}
} else {
$assetToGulp = $this->assetToGulp($asset);
if ($assetToGulp) {
$code[] = $assetToGulp;
}
}
if (count($code) > 0) {
$code[] = "\n" . '.pipe(concat("' . pathinfo($target, PATHINFO_FILENAME) . '.' . pathinfo($target, PATHINFO_EXTENSION) . '"))';
$code[] = '.pipe(gulp.dest("' . pathinfo($target, PATHINFO_DIRNAME) . '"))';
$this->gulpConfigs[] = implode('', $code);
}
}
}
private function assetToGulp(AssetInterface $leaf)
{
$code = [];
if (empty($leaf->getSourcePath())) {
return false;
}
/** @var AssetInterface $leaf */
$root = $leaf->getSourceRoot();
$path = $leaf->getSourcePath();
$filePath = $this->correctPath($root . '/' . $path);
// $ext = pathinfo($filePath, PATHINFO_EXTENSION);
$code[] = 'gulp.src("' . $filePath . '")';
foreach ($leaf->getFilters() as $filter) {
if ($filter instanceof TypeScriptFilter) {
$code[] = '.pipe(ts(tsConfig))';
} else if ($filter instanceof CompassFilter) {
$code[] = '.pipe(compass(compassConfig))';
}
}
foreach ($leaf->getFilters() as $filter) {
if ($filter instanceof UglifyJs2Filter) {
$code[] = '.pipe(uglifyjs())';
} else if ($filter instanceof UglifyCssFilter) {
$code[] = '.pipe(uglifycss())';
} else if ($filter instanceof PhpCssEmbedFilter) {
$code[] = '.pipe(cssBase64())';
} else if ($filter instanceof CssRewriteFilter) {
$code[] = '.pipe(cssrewrite(cssrewriteConfig))';
}
}
return implode("", $code);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment