Skip to content

Commit

Permalink
Add support for dart imports
Browse files Browse the repository at this point in the history
  • Loading branch information
pierredup committed Dec 13, 2013
1 parent 4ff3e53 commit 051255b
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 2 deletions.
74 changes: 72 additions & 2 deletions src/Assetic/Filter/DartFilter.php
Expand Up @@ -13,27 +13,63 @@

use Assetic\Asset\AssetInterface;
use Assetic\Exception\FilterException;
use Assetic\Factory\AssetFactory;
use Assetic\Util\DartUtils;

/**
* Compiles Dart into Javascript.
*
* @link http://dartlang.org/
*/
class DartFilter extends BaseProcessFilter
class DartFilter extends BaseProcessFilter implements DependencyExtractorInterface
{
/**
* @var string
*/
private $dartBin;

/**
* @param string $dartBin
*/
public function __construct($dartBin = '/usr/bin/dart2js')
{
$this->dartBin = $dartBin;
}

/**
* @param AssetInterface $asset
* @throws \RuntimeException
*/
public function filterLoad(AssetInterface $asset)
{
$input = tempnam(sys_get_temp_dir(), 'assetic_dart');
$output = tempnam(sys_get_temp_dir(), 'assetic_dart');

file_put_contents($input, $asset->getContent());
$content = $asset->getContent();

$sourcePath = $asset->getSourceDirectory();

$callback = function ($matches) use ($sourcePath, &$content) {
if (!$matches['url'] || null === $sourcePath || 'dart:' === substr($matches['url'], 0, 5)) {
return;
}

$currentDir = getcwd();

chdir($sourcePath);
$importPath = str_replace('\\', '/', realpath($matches['url']));
chdir($currentDir);

if ('/' !== substr($importPath, 0, 1)) {
$importPath = '/' . $importPath;
}

$content = str_replace($matches['url'], $importPath, $content);
};

DartUtils::filterImports($content, $callback);

file_put_contents($input, $content);

$pb = $this->createProcessBuilder()
->add($this->dartBin)
Expand Down Expand Up @@ -61,6 +97,40 @@ public function filterLoad(AssetInterface $asset)
unlink($output);
}

/**
* @param AssetFactory $factory
* @param string $content
* @param null $loadPath
* @return array|\Assetic\Asset\AssetInterface[]
*/
public function getChildren(AssetFactory $factory, $content, $loadPath = null)
{
if (null === $loadPath) {
return array();
}

$children = array();

foreach (DartUtils::extractImports($content) as $reference) {
if ('dart:' === substr($reference, 0, 5)) {
// skip imports that is not files
continue;
}

if (file_exists($file = $loadPath.'/'.$reference)) {
$coll = $factory->createAsset($file, array(), array('root' => $loadPath));

foreach ($coll as $leaf) {
$leaf->ensureFilter($this);
$children[] = $leaf;
break;
}
}
}

return $children;
}

public function filterDump(AssetInterface $asset)
{
}
Expand Down
23 changes: 23 additions & 0 deletions src/Assetic/Util/DartUtils.php
@@ -0,0 +1,23 @@
<?php

/*
* This file is part of the Assetic package, an OpenSky project.
*
* (c) 2010-2013 OpenSky Project Inc
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Assetic\Util;

/**
* Dart Utils.
*
* @author Pierre du Plessis <pdples@gmail.com>
*/
class DartUtils extends CssUtils
{
const REGEX_IMPORTS = '/import? (\'|"|)(?<url>[^\'"\)\n\r]*)\1\)?;?/';
const REGEX_COMMENTS = '/((?:\/\*[^*]*\*+(?:[^\/][^*]*\*+)*\/)|\/\/[^\n]+)/';
}
14 changes: 14 additions & 0 deletions tests/Assetic/Test/Filter/DartFilterTest.php
Expand Up @@ -12,6 +12,7 @@
namespace Assetic\Test\Filter;

use Assetic\Asset\StringAsset;
use Assetic\Asset\FileAsset;
use Assetic\Filter\DartFilter;

/**
Expand All @@ -35,6 +36,19 @@ protected function tearDown()
$this->filter = null;
}

public function testImport()
{
$asset = new FileAsset(__DIR__.'/fixtures/dart/main.dart');
$asset->load();

$this->filter->filterLoad($asset);

$this->assertContains(
'Generated by dart2js, the Dart to JavaScript compiler.',
$asset->getContent()
);
}

public function testFilterLoad()
{
$input = <<<EOM
Expand Down
12 changes: 12 additions & 0 deletions tests/Assetic/Test/Filter/fixtures/dart/library/library.dart
@@ -0,0 +1,12 @@
import 'dart:html';

librayFunction() {
var msg = query('#msg');
var btn = new ButtonElement();
var someThing = new Element.a();
someThing.href = 'New Cool Link';

btn.text = 'Click me!';
btn.on.click.add((e) => msg.text = 'Dart!');
document.body.nodes.add(btn);
}
5 changes: 5 additions & 0 deletions tests/Assetic/Test/Filter/fixtures/dart/main.dart
@@ -0,0 +1,5 @@
import "library/library.dart";

main() {
librayFunction();
}

0 comments on commit 051255b

Please sign in to comment.