Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Add support for dart imports #545

Open
wants to merge 1 commit into from

2 participants

@pierredup

Add support for dart imports.

This solution might be a bit hacky, as it changes the path of the imported file to an absolute path in the source content before running the dart2js filter, because dart does not support specifying paths for library imports, so currently this is the only way to handle imports (that I can think of).

Fixes #430

src/Assetic/Filter/DartFilter.php
((23 lines not shown))
public function __construct($dartBin = '/usr/bin/dart2js')
{
$this->dartBin = $dartBin;
}
+ /**
+ * @param AssetInterface $asset
+ * @throws \RuntimeException
+ * @throws $this
@stof Collaborator
stof added a note

really ?

I'm not sure how that got there (autocomplete in phpstorm)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
src/Assetic/Filter/DartFilter.php
((33 lines not shown))
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 = __DIR__;
@stof Collaborator
stof added a note

this looks really wrong

Yeah I agree, but I'm not sure about an elegant way to revert back to the current directory. Any suggestions?

@stof Collaborator
stof added a note

well, if you need to change the working dir, you need to get the current working dir before that (with getcwd()), not an arbitrary directory. you are not reverting it back, you are changing it to something else

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
src/Assetic/Filter/DartFilter.php
((30 lines not shown))
+ break;
+ }
+ }
+ }
+
+ return $children;
+ }
+
+ /**
+ * @param string $content
+ * @param callable $callback
+ * @param int $limit
+ * @param int $count
+ * @return string
+ */
+ protected function filterImports($content, $callback, $limit = -1, &$count = 0)
@stof Collaborator
stof added a note

I don't think you need this method. Call DartUtils directly (the protected method filterImports in the base class forwarding to CssUtils is just here for backward compatibility)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 13, 2013
  1. @pierredup

    Add support for dart imports

    pierredup authored pierredup committed
This page is out of date. Refresh to see the latest.
View
74 src/Assetic/Filter/DartFilter.php
@@ -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)
@@ -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)
{
}
View
23 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]+)/';
+}
View
14 tests/Assetic/Test/Filter/DartFilterTest.php
@@ -12,6 +12,7 @@
namespace Assetic\Test\Filter;
use Assetic\Asset\StringAsset;
+use Assetic\Asset\FileAsset;
use Assetic\Filter\DartFilter;
/**
@@ -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
View
12 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);
+}
View
5 tests/Assetic/Test/Filter/fixtures/dart/main.dart
@@ -0,0 +1,5 @@
+import "library/library.dart";
+
+main() {
+ librayFunction();
+}
Something went wrong with that request. Please try again.