|
| 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"; |
0 commit comments