Skip to content

Commit 8b27c9b

Browse files
committed
Merge branch 'master' of git://github.com/CHH/pipe
2 parents c641195 + d9a85b5 commit 8b27c9b

File tree

10 files changed

+167
-80
lines changed

10 files changed

+167
-80
lines changed

bob/pipe_tasks.php

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace Bob\BuildConfig;
44

55
use Pipe\Config,
6-
Pipe\AssetDumper;
6+
Pipe\Manifest;
77

88
function config()
99
{
@@ -21,19 +21,10 @@ function env()
2121
task("assets:dump", function() {
2222
$config = config();
2323
$targetDir = @$_ENV["TARGET_DIR"] ?: $config->precompilePrefix;
24-
$dumper = new AssetDumper($targetDir);
2524

26-
foreach ($config->precompile as $logicalPath) {
27-
$asset = env()->find("$t", array("bundled" => true));
25+
$manifest = new Manifest(env(), "$targetDir/manifest.json", $targetDir);
26+
$manifest->compress = true;
2827

29-
if (!$asset) {
30-
println("Asset '$t' not found!", STDERR);
31-
exit(1);
32-
}
33-
34-
$dumper->add($asset);
35-
}
36-
37-
$dumper->dump();
28+
$manifest->compile($config->precompile);
3829
});
3930

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@
3333
"require-dev": {
3434
"react/http": "*@dev",
3535
"symfony/http-foundation": "*@stable",
36-
"imagine/imagine": "*@dev"
36+
"imagine/imagine": "*@dev",
37+
"mikey179/vfsStream": "1.1.*@stable"
3738
},
3839
"autoload": {
3940
"psr-0": {

lib/Pipe/Asset.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,14 @@ function __toString()
5757
}
5858
}
5959

60+
# Public: Returns the asset's path relative to the environment.
61+
#
62+
# Returns a String.
63+
function getLogicalPath()
64+
{
65+
return $this->logicalPath;
66+
}
67+
6068
# Public: Returns the asset's basename.
6169
#
6270
# includeExtensions - Include extensions in the filename (default: true)

lib/Pipe/AssetDumper.php

Lines changed: 0 additions & 54 deletions
This file was deleted.

lib/Pipe/DirectiveProcessor.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ protected function prepare()
9292

9393
$this->body = $this->getData();
9494

95-
if (preg_match(self::HEADER_PATTERN, $this->getData(), $matches)) {
95+
if (preg_match(static::HEADER_PATTERN, $this->getData(), $matches)) {
9696
$this->header = $matches[0];
9797
$this->body = substr($this->getData(), strlen($matches[0])) ?: '';
9898
}
@@ -140,7 +140,7 @@ protected function getDirectives()
140140
$this->parsedDirectives = array();
141141

142142
foreach (explode("\n", $this->header) as $i => $line) {
143-
if (preg_match(self::DIRECTIVE_PATTERN, $line, $matches)) {
143+
if (preg_match(static::DIRECTIVE_PATTERN, $line, $matches)) {
144144
$argv = Shellwords::split($matches[1]);
145145
$name = array_shift($argv);
146146
$this->parsedDirectives[$i] = array($i, $name, $argv);

lib/Pipe/ImportProcessor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ function render($context = null, $vars = array())
2626
$context->dependOn($resolvedPath);
2727

2828
# Import source code without processing, for LESS files.
29-
return $context->evaluate($resolvedPath, array('processors' => array()));
29+
return $context->evaluate($resolvedPath, array('processors' => array())) . "\n";
3030
}, $this->getData());
3131

3232
return $data;

lib/Pipe/Manifest.php

Lines changed: 100 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,115 @@
44

55
class Manifest
66
{
7+
public $compress = false;
8+
9+
public $files;
10+
public $assets;
11+
12+
protected $environment;
13+
protected $directory;
14+
15+
# Path to manifest file
716
protected $manifest;
817

9-
function __construct()
18+
function __construct(Environment $env, $manifest, $dir = '')
1019
{
11-
$this->manifest = new \StdClass;
20+
$this->files = new \StdClass;
21+
$this->assets = new \StdClass;
22+
23+
$this->environment = $env;
24+
25+
if (empty($dir)) {
26+
$this->directory = dirname($manifest);
27+
} else {
28+
$this->directory = $dir;
29+
}
30+
31+
$this->manifest = $manifest;
1232
}
1333

14-
function add($asset)
34+
function read()
1535
{
16-
$this->manifest->{$asset->logicalPath} = $asset->getDigestName();
17-
return $this;
36+
if (!realpath($this->manifest)) {
37+
throw new \UnexpectedValueException(sprintf(
38+
'Manifest file "%s" not found', $this->manifest
39+
));
40+
}
41+
42+
$m = json_decode(file_get_contents($this->manifest));
43+
44+
if (isset($m->files)) {
45+
foreach ($m->files as $file => $info) {
46+
$this->files->{$file} = $info;
47+
}
48+
}
49+
50+
if (isset($m->assets)) {
51+
foreach ($m->assets as $logicalPath => $digestName) {
52+
$this->assets->{$logicalPath} = $digestName;
53+
}
54+
}
55+
}
56+
57+
# Compiles one or more assets (by logical path) and writes the manifest.
58+
#
59+
# Example
60+
#
61+
# $manifest = new Manifest($env, 'manifest.json', '.');
62+
# $manifest->compile('boo.js');
63+
# $manifest->compile(array('foo.js', 'bar.js'));
64+
#
65+
function compile($assets)
66+
{
67+
$env = $this->environment;
68+
69+
if (!is_dir($this->directory)) {
70+
mkdir($this->directory, 0700, true);
71+
}
72+
73+
$assets = array_filter(array_map(function($path) use ($env) {
74+
return $env->find($path, array('bundled' => true));
75+
}, (array) $assets));
76+
77+
foreach ($assets as $asset) {
78+
$this->files->{$asset->getDigestName()} = array(
79+
'logical_path' => $asset->getLogicalPath(),
80+
'mtime' => date(DATE_ISO8601, $asset->getLastModified()),
81+
'size' => strlen($asset->getBody()),
82+
'digest' => $asset->getDigest()
83+
);
84+
85+
$this->assets->{$asset->getLogicalPath()} = $asset->getDigestName();
86+
87+
$asset->write(array(
88+
'dir' => $this->directory,
89+
'compress' => false,
90+
'include_digest' => true
91+
));
92+
93+
if ($this->compress) {
94+
$asset->write(array(
95+
"dir" => $this->directory,
96+
"compress" => true,
97+
"include_digest" => true
98+
));
99+
}
100+
101+
$this->save();
102+
}
103+
}
104+
105+
function save()
106+
{
107+
return file_put_contents($this->manifest, $this->toJSON());
18108
}
19109

20110
function toJSON()
21111
{
22-
return json_encode($this->manifest);
112+
return json_encode(array(
113+
'assets' => $this->assets,
114+
'files' => $this->files,
115+
));
23116
}
24117
}
118+

tests/Pipe/Test/EnvironmentTest.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,23 @@ function setUp()
1616
$this->environment = $env;
1717
}
1818

19-
function testReadSingleAsset()
19+
function testFindSingleAsset()
2020
{
2121
$asset = $this->environment['asset1.js'];
2222
$this->assertInstanceOf('\\Pipe\\Asset', $asset);
2323
}
2424

25+
function testFindBundledAsset()
26+
{
27+
$asset = $this->environment->find('asset1.js', array('bundled' => true));
28+
$this->assertInstanceOf('\\Pipe\\BundledAsset', $asset);
29+
}
30+
31+
function testFindWithoutExtension()
32+
{
33+
$this->assertNotNull($this->environment->find('asset1'));
34+
}
35+
2536
function testReturnsNullWhenAssetIsNotFound()
2637
{
2738
$this->assertNull($this->environment['foo/bar/baz']);

tests/Pipe/Test/ImportProcessorTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ function dataProvider()
2727
return array(
2828
array(
2929
"screen1.css",
30-
"\nbody { font-size: 0.625em; }\n"
30+
"body { font-size: 0.625em; }\n\n\n"
3131
),
3232
array(
3333
"screen2.css",
34-
"\nbody { font-size: 0.625em; }\n"
34+
"body { font-size: 0.625em; }\n\n\n"
3535
)
3636
);
3737
}

tests/Pipe/Test/ManifestTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace Pipe\Test;
4+
5+
use Pipe\Manifest;
6+
use Pipe\Environment;
7+
use org\bovigo\vfs\vfsStream;
8+
9+
class ManifestTest extends \PHPUnit_Framework_TestCase
10+
{
11+
function testManifestDumpsAssets()
12+
{
13+
$dir = vfsStream::setup('assets');
14+
15+
$env = new Environment;
16+
$env->appendPath(__DIR__ . '/fixtures');
17+
18+
$asset = $env->find('asset1.js', array('bundled' => true));
19+
$digestName = $asset->getDigestName();
20+
21+
$manifest = new Manifest($env, vfsStream::url('assets') . '/manifest.json');
22+
$manifest->compress = true;
23+
$manifest->compile('asset1.js');
24+
25+
$json = json_decode($manifest->toJSON(), true);
26+
27+
$this->assertEquals($digestName, $json["assets"]["asset1.js"]);
28+
29+
$this->assertNotEmpty($json["files"][$digestName]);
30+
31+
$this->assertTrue($dir->hasChild('manifest.json'));
32+
$this->assertTrue($dir->hasChild($digestName));
33+
$this->assertTrue($dir->hasChild($digestName . '.gz'));
34+
}
35+
}
36+

0 commit comments

Comments
 (0)