Skip to content

Commit 7bb0db1

Browse files
author
epriestley
committedJan 25, 2011
Celerity, a Haste-style static resource management system.
1 parent fed4c58 commit 7bb0db1

23 files changed

+531
-9
lines changed
 

‎scripts/__init_script__.php

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
/*
4+
* Copyright 2011 Facebook, Inc.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
$include_path = ini_get('include_path');
20+
ini_set('include_path', $include_path.':'.dirname(__FILE__).'/../../');
21+
@require_once 'libphutil/src/__phutil_library_init__.php';
22+
if (!@constant('__LIBPHUTIL__')) {
23+
echo "ERROR: Unable to load libphutil. Update your PHP 'include_path' to ".
24+
"include the parent directory of libphutil/.\n";
25+
exit(1);
26+
}
27+
28+
if (!ini_get('date.timezone')) {
29+
date_default_timezone_set('America/Los_Angeles');
30+
}
31+
32+
phutil_load_library(dirname(__FILE__).'/../src/');

‎scripts/celerity_mapper.php

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
#!/usr/bin/env php
2+
<?php
3+
4+
require_once dirname(__FILE__).'/__init_script__.php';
5+
6+
if ($argc != 2) {
7+
$self = basename($argv[0]);
8+
echo "usage: {$self} <webroot>\n";
9+
exit(1);
10+
}
11+
12+
phutil_require_module('phutil', 'filesystem');
13+
phutil_require_module('phutil', 'filesystem/filefinder');
14+
phutil_require_module('phutil', 'future/exec');
15+
phutil_require_module('phutil', 'parser/docblock');
16+
17+
$root = Filesystem::resolvePath($argv[1]);
18+
19+
echo "Finding static resources...\n";
20+
$files = id(new FileFinder($root))
21+
->withType('f')
22+
->withSuffix('js')
23+
->withSuffix('css')
24+
->setGenerateChecksums(true)
25+
->find();
26+
27+
echo "Processing ".count($files)." files";
28+
29+
$file_map = array();
30+
foreach ($files as $path => $hash) {
31+
echo ".";
32+
$name = '/'.Filesystem::readablePath($path, $root);
33+
$file_map[$name] = array(
34+
'hash' => $hash,
35+
'disk' => $path,
36+
);
37+
}
38+
echo "\n";
39+
40+
$runtime_map = array();
41+
42+
$parser = new PhutilDocblockParser();
43+
foreach ($file_map as $path => $info) {
44+
$data = Filesystem::readFile($info['disk']);
45+
$matches = array();
46+
$ok = preg_match('@/[*][*].*?[*]/@s', $data, $matches);
47+
if (!$ok) {
48+
throw new Exception(
49+
"File {$path} does not have a header doc comment. Encode dependency ".
50+
"data in a header docblock.");
51+
}
52+
53+
list($description, $metadata) = $parser->parse($matches[0]);
54+
55+
$provides = preg_split('/\s+/', trim(idx($metadata, 'provides')));
56+
$requires = preg_split('/\s+/', trim(idx($metadata, 'requires')));
57+
$provides = array_filter($provides);
58+
$requires = array_filter($requires);
59+
60+
if (count($provides) !== 1) {
61+
throw new Exception(
62+
"File {$path} must @provide exactly one Celerity target.");
63+
}
64+
65+
$provides = reset($provides);
66+
67+
$type = 'js';
68+
if (preg_match('/\.css$/', $path)) {
69+
$type = 'css';
70+
}
71+
72+
$path = '/res/'.substr($info['hash'], 0, 8).$path;
73+
74+
$runtime_map[$provides] = array(
75+
'path' => $path,
76+
'type' => $type,
77+
'requires' => $requires,
78+
);
79+
}
80+
81+
$runtime_map = var_export($runtime_map, true);
82+
$runtime_map = preg_replace('/\s+$/m', '', $runtime_map);
83+
$runtime_map = preg_replace('/array \(/', 'array(', $runtime_map);
84+
85+
$resource_map = <<<EOFILE
86+
<?php
87+
88+
/**
89+
* This file is automatically generated. Use 'celerity_mapper.php' to rebuild
90+
* it.
91+
* @generated
92+
*/
93+
94+
celerity_register_resource_map({$runtime_map});
95+
96+
EOFILE;
97+
98+
echo "Writing map...\n";
99+
Filesystem::writeFile(
100+
$root.'/../src/__celerity_resource_map__.php',
101+
$resource_map);
102+
echo "Done.\n";

‎src/__celerity_resource_map__.php

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
/**
4+
* This file is automatically generated. Use 'celerity_mapper.php' to rebuild
5+
* it.
6+
* @generated
7+
*/
8+
9+
celerity_register_resource_map(array(
10+
'phabricator-core-css' =>
11+
array(
12+
'path' => '/res/ffa0140c/rsrc/css/base.css',
13+
'type' => 'css',
14+
'requires' =>
15+
array(
16+
),
17+
),
18+
'phabricator-syntax-css' =>
19+
array(
20+
'path' => '/res/bf911307/rsrc/css/syntax.css',
21+
'type' => 'css',
22+
'requires' =>
23+
array(
24+
),
25+
),
26+
'javelin-init-dev' =>
27+
array(
28+
'path' => '/res/c57a9e89/rsrc/js/javelin/init.dev.js',
29+
'type' => 'js',
30+
'requires' =>
31+
array(
32+
),
33+
),
34+
'javelin-init-prod' =>
35+
array(
36+
'path' => '/res/f0172c54/rsrc/js/javelin/init.min.js',
37+
'type' => 'js',
38+
'requires' =>
39+
array(
40+
),
41+
),
42+
'javelin-lib-dev' =>
43+
array(
44+
'path' => '/res/3e747182/rsrc/js/javelin/javelin.dev.js',
45+
'type' => 'js',
46+
'requires' =>
47+
array(
48+
),
49+
),
50+
'javelin-lib-prod' =>
51+
array(
52+
'path' => '/res/9438670e/rsrc/js/javelin/javelin.min.js',
53+
'type' => 'js',
54+
'requires' =>
55+
array(
56+
),
57+
),
58+
));

‎src/__phutil_library_map__.php

+7
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@
4646
'AphrontURIMapper' => 'aphront/mapper',
4747
'AphrontView' => 'view/base',
4848
'AphrontWebpageResponse' => 'aphront/response/webpage',
49+
'CelerityAPI' => 'infratructure/celerity/api',
50+
'CelerityResourceController' => 'infratructure/celerity/controller',
51+
'CelerityResourceMap' => 'infratructure/celerity/map',
52+
'CelerityStaticResourceResponse' => 'infratructure/celerity/response',
4953
'ConduitAPIMethod' => 'applications/conduit/method/base',
5054
'ConduitAPIRequest' => 'applications/conduit/protocol/request',
5155
'ConduitAPI_conduit_connect_Method' => 'applications/conduit/method/conduit/connect',
@@ -121,10 +125,12 @@
121125
array(
122126
'_qsprintf_check_scalar_type' => 'storage/qsprintf',
123127
'_qsprintf_check_type' => 'storage/qsprintf',
128+
'celerity_register_resource_map' => 'infratructure/celerity/map',
124129
'qsprintf' => 'storage/qsprintf',
125130
'queryfx' => 'storage/queryfx',
126131
'queryfx_all' => 'storage/queryfx',
127132
'queryfx_one' => 'storage/queryfx',
133+
'require_celerity_resource' => 'infratructure/celerity/api',
128134
'vqsprintf' => 'storage/qsprintf',
129135
'vqueryfx' => 'storage/queryfx',
130136
'xsprintf_query' => 'storage/qsprintf',
@@ -161,6 +167,7 @@
161167
'AphrontSideNavView' => 'AphrontView',
162168
'AphrontTableView' => 'AphrontView',
163169
'AphrontWebpageResponse' => 'AphrontResponse',
170+
'CelerityResourceController' => 'AphrontController',
164171
'ConduitAPI_conduit_connect_Method' => 'ConduitAPIMethod',
165172
'ConduitAPI_differential_creatediff_Method' => 'ConduitAPIMethod',
166173
'ConduitAPI_differential_setdiffproperty_Method' => 'ConduitAPIMethod',

‎src/aphront/default/configuration/AphrontDefaultApplicationConfiguration.php

+5
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,11 @@ public function getURIMap() {
8181
'changeset/(?<id>\d+)/$' => 'DifferentialChangesetViewController',
8282
),
8383

84+
'/res/' => array(
85+
'(?<hash>[a-f0-9]{8})/(?<path>[^.]+\.(?:css|js))$'
86+
=> 'CelerityResourceController',
87+
),
88+
8489
'.*' => 'AphrontDefaultApplicationController',
8590
);
8691
}

‎src/applications/differential/view/difftableofcontents/DifferentialDiffTableOfContentsView.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public function render() {
9696

9797
$rows[] =
9898
'<tr>'.
99-
'<td class="differential-toc-char" title={$chartitle}>'.$char.'</td>'.
99+
'<td class="differential-toc-char" title='.$chartitle.'>'.$char.'</td>'.
100100
'<td class="differential-toc-prop">'.$pchar.'</td>'.
101101
'<td class="differential-toc-ftype">'.$desc.'</td>'.
102102
'<td class="differential-toc-file">'.$link.$lines.'</td>'.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
/*
4+
* Copyright 2011 Facebook, Inc.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
final class CelerityAPI {
20+
21+
private static $response;
22+
23+
public static function getStaticResourceResponse() {
24+
if (empty(self::$response)) {
25+
self::$response = new CelerityStaticResourceResponse();
26+
}
27+
return self::$response;
28+
}
29+
30+
}
31+
32+
function require_celerity_resource($symbol) {
33+
$response = CelerityAPI::getStaticResourceResponse();
34+
$response->requireResource($symbol);
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
/**
3+
* This file is automatically generated. Lint this module to rebuild it.
4+
* @generated
5+
*/
6+
7+
8+
9+
phutil_require_module('phabricator', 'infratructure/celerity/response');
10+
11+
12+
phutil_require_source('CelerityAPI.php');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
/*
4+
* Copyright 2011 Facebook, Inc.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
class CelerityResourceController extends AphrontController {
20+
21+
private $path;
22+
private $hash;
23+
24+
public function willProcessRequest(array $data) {
25+
$this->path = $data['path'];
26+
$this->hash = $data['hash'];
27+
}
28+
29+
public function processRequest() {
30+
$path = $this->path;
31+
32+
// Sanity checking to keep this from exposing anything sensitive.
33+
$path = preg_replace('@(//|\\.\\.)@', '', $path);
34+
$matches = null;
35+
if (!preg_match('/\.(css|js)$/', $path, $matches)) {
36+
throw new Exception("Only CSS and JS resources may be served.");
37+
}
38+
39+
$type = $matches[1];
40+
41+
42+
$root = dirname(phutil_get_library_root('phabricator'));
43+
44+
try {
45+
$data = Filesystem::readFile($root.'/webroot/'.$path);
46+
} catch (Exception $ex) {
47+
return new Aphront404Response();
48+
}
49+
50+
$response = new AphrontFileResponse();
51+
$response->setContent($data);
52+
switch ($type) {
53+
case 'css':
54+
$response->setMimeType("text/css; charset=utf-8");
55+
break;
56+
case 'js':
57+
$response->setMimeType("text/javascript; charset=utf-8");
58+
break;
59+
}
60+
61+
return $response;
62+
}
63+
64+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
/**
3+
* This file is automatically generated. Lint this module to rebuild it.
4+
* @generated
5+
*/
6+
7+
8+
9+
phutil_require_module('phabricator', 'aphront/controller');
10+
phutil_require_module('phabricator', 'aphront/response/file');
11+
12+
phutil_require_module('phutil', 'filesystem');
13+
phutil_require_module('phutil', 'moduleutils');
14+
15+
16+
phutil_require_source('CelerityResourceController.php');

0 commit comments

Comments
 (0)
Failed to load comments.