Commit e132ed8d by Qiang Xue

initial implementation of asset command.

parent f21499dd
......@@ -129,7 +129,7 @@ class Application extends \yii\base\Application
'migrate' => 'yii\console\controllers\MigrateController',
'app' => 'yii\console\controllers\AppController',
'cache' => 'yii\console\controllers\CacheController',
'script' => 'yii\console\controllers\ScriptController',
'asset' => 'yii\console\controllers\AssetController',
);
}
......
......@@ -15,7 +15,7 @@ use yii\console\Controller;
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class ScriptController extends Controller
class AssetController extends Controller
{
public $defaultAction = 'compress';
......@@ -33,13 +33,15 @@ class ScriptController extends Controller
*/
public $targets = array();
public $assetManager = array();
public $jsCompressor = 'java -jar compiler.jar --js {from} --js_output_file {to}';
public $cssCompressor = 'java -jar yuicompressor.jar {from} -o {to}';
public function actionCompress($configFile, $bundleFile)
{
$this->loadConfiguration($configFile);
$bundles = $this->loadBundles($this->bundles, $this->extensions);
$targets = $this->loadTargets($this->targets, $bundles);
// $this->publishBundles($bundles, $this->publishOptions);
$this->publishBundles($bundles, $this->publishOptions);
$timestamp = time();
foreach ($targets as $target) {
if (!empty($target->js)) {
......@@ -96,11 +98,38 @@ class ScriptController extends Controller
protected function loadTargets($targets, $bundles)
{
// build the dependency order of bundles
$registered = array();
foreach ($bundles as $name => $bundle) {
$this->registerBundle($bundles, $name, $registered);
}
$bundleOrders = array_combine(array_keys($registered), range(0, count($bundles) - 1));
// fill up the target which has empty 'depends'.
$referenced = array();
foreach ($targets as $name => $target) {
if (empty($target['depends'])) {
if (!isset($all)) {
$all = $name;
} else {
throw new Exception("Only one target can have empty 'depends' option. Found two now: $all, $name");
}
} else {
foreach ($target['depends'] as $bundle) {
if (!isset($referenced[$bundle])) {
$referenced[$bundle] = $name;
} else {
throw new Exception("Target '{$referenced[$bundle]}' and '$name' cannot contain the bundle '$bundle' at the same time.");
}
}
}
}
if (isset($all)) {
$targets[$all]['depends'] = array_diff(array_keys($registered), array_keys($referenced));
}
// adjust the 'depends' order for each target according to the dependency order of bundles
// create an AssetBundle object for each target
foreach ($targets as $name => $target) {
if (!isset($target['basePath'])) {
throw new Exception("Please specify 'basePath' for the '$name' target.");
......@@ -172,11 +201,7 @@ class ScriptController extends Controller
$map = array();
foreach ($targets as $name => $target) {
foreach ($target->depends as $bundle) {
if (!isset($map[$bundle])) {
$map[$bundle] = $name;
} else {
throw new Exception("Bundle '$bundle' is found in both target '{$map[$bundle]}' and '$name'.");
}
$map[$bundle] = $name;
}
}
......@@ -236,7 +261,8 @@ class ScriptController extends Controller
file_put_contents($bundleFile, <<<EOD
<?php
/**
* Do not modify this file manually as it is automatically generated by the "yiic script" command.
* This file is generated by the "yiic script" command.
* DO NOT MODIFY THIS FILE DIRECTLY.
* @version $version
*/
return $array;
......@@ -246,11 +272,82 @@ EOD
protected function compressJsFiles($inputFiles, $outputFile)
{
if (is_string($this->jsCompressor)) {
$tmpFile = $outputFile . '.tmp';
$this->combineJsFiles($inputFiles, $tmpFile);
$log = shell_exec(strtr($this->jsCompressor, array(
'{from}' => $tmpFile,
'{to}' => $outputFile,
)));
@unlink($tmpFile);
} else {
$log = call_user_func($this->jsCompressor, $this, $inputFiles, $outputFile);
}
}
protected function compressCssFiles($inputFiles, $outputFile)
{
if (is_string($this->cssCompressor)) {
$tmpFile = $outputFile . '.tmp';
$this->combineCssFiles($inputFiles, $tmpFile);
$log = shell_exec(strtr($this->cssCompressor, array(
'{from}' => $inputFiles,
'{to}' => $outputFile,
)));
} else {
$log = call_user_func($this->cssCompressor, $this, $inputFiles, $outputFile);
}
}
public function combineJsFiles($files, $tmpFile)
{
$content = '';
foreach ($files as $file) {
$content .= "/*** BEGIN FILE: $file ***/\n"
. file_get_contents($file)
. "/*** END FILE: $file ***/\n";
}
file_put_contents($tmpFile, $content);
}
public function combineCssFiles($files, $tmpFile)
{
// todo: adjust url() references in CSS files
$content = '';
foreach ($files as $file) {
$content .= "/*** BEGIN FILE: $file ***/\n"
. file_get_contents($file)
. "/*** END FILE: $file ***/\n";
}
file_put_contents($tmpFile, $content);
}
public function actionTemplate($configFile)
{
$template = <<<EOD
<?php
return array(
//
'bundles' => require('path/to/bundles.php'),
//
'extensions' => require('path/to/namespaces.php'),
//
'targets' => array(
'all' => array(
'basePath' => __DIR__,
'baseUrl' => '/test',
'js' => 'all-{ts}.js',
'css' => 'all-{ts}.css',
),
),
'assetManager' => array(
'basePath' => __DIR__,
'baseUrl' => '/test',
),
);
EOD;
file_put_contents($configFile, $template);
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment