1+ /// <reference path="../../typings/node/node.d.ts" />
12'use strict' ;
23
4+ import { MultiCopy } from './multi_copy' ;
35var Funnel = require ( 'broccoli-funnel' ) ;
6+ var glob = require ( 'glob' ) ;
7+ var mergeTrees = require ( 'broccoli-merge-trees' ) ;
48var path = require ( 'path' ) ;
9+ var renderLodashTemplate = require ( 'broccoli-lodash' ) ;
510var replace = require ( 'broccoli-replace' ) ;
611var stew = require ( 'broccoli-stew' ) ;
712var ts2dart = require ( '../broccoli-ts2dart' ) ;
813
9- module . exports = function makeDartTree ( ) {
10- // Transpile everything in 'modules'...
11- var modulesTree = new Funnel ( 'modules' , {
12- include : [ '**/*.js' , '**/*.ts' , '**/*.dart' ] , // .dart file available means don't translate.
13- exclude : [ 'rtts_assert/**/*' ] , // ... except for the rtts_asserts (don't apply to Dart).
14- destDir : '/' // Remove the 'modules' prefix.
15- } ) ;
14+ /**
15+ * A funnel starting at modules, including the given filters, and moving into the root.
16+ * @param include Include glob filters.
17+ */
18+ function modulesFunnel ( include : string [ ] , exclude ?: string [ ] ) {
19+ return new Funnel ( 'modules' , { include, destDir : '/' , exclude} ) ;
20+ }
21+
22+ /**
23+ * Replaces $SCRIPT$ in .html files with actual <script> tags.
24+ */
25+ function replaceScriptTagInHtml ( content : string , relativePath : string ) : string {
26+ var scriptTags = '' ;
27+ if ( relativePath . match ( / ^ b e n c h m a r k s / ) ) {
28+ scriptTags += '<script src="url_params_to_form.js" type="text/javascript"></script>\n' ;
29+ }
30+ var scriptName = relativePath . replace ( / .* \/ ( [ ^ / ] + ) \. h t m l $ / , '$1.dart' ) ;
31+ scriptTags += '<script src="' + scriptName + '" type="application/dart"></script>\n' +
32+ '<script src="packages/browser/dart.js" type="text/javascript"></script>' ;
33+ return content . replace ( '$SCRIPTS$' , scriptTags ) ;
34+ }
35+
36+ function stripModulePrefix ( relativePath : string ) : string {
37+ if ( ! relativePath . match ( / ^ m o d u l e s \/ / ) ) {
38+ throw new Error ( 'Expected path to root at modules/: ' + relativePath ) ;
39+ }
40+ return relativePath . replace ( / ^ m o d u l e s \/ / , '' ) ;
41+ }
1642
17- // Transpile to dart.
18- var dartTree = ts2dart . transpile ( modulesTree ) ;
43+ function getSourceTree ( ) {
44+ // Transpile everything in 'modules' except for rtts_assertions.
45+ var tsInputTree = modulesFunnel ( [ '**/*.js' , '**/*.ts' , '**/*.dart' ] , [ 'rtts_assert/**/*' ] ) ;
46+ var transpiled = ts2dart . transpile ( tsInputTree ) ;
47+ // Native sources, dart only examples, etc.
48+ var dartSrcs = modulesFunnel ( [ '**/*.dart' ] ) ;
49+ return mergeTrees ( [ transpiled , dartSrcs ] ) ;
50+ }
1951
52+ function fixDartFolderLayout ( sourceTree ) {
2053 // Move around files to match Dart's layout expectations.
21- dartTree = stew . rename ( dartTree , function ( relativePath ) {
54+ return stew . rename ( sourceTree , function ( relativePath ) {
2255 // If a file matches the `pattern`, insert the given `insertion` as the second path part.
2356 var replacements = [
2457 { pattern : / ^ b e n c h m a r k s \/ t e s t \/ / , insertion : '' } ,
2558 { pattern : / ^ b e n c h m a r k s \/ / , insertion : 'web' } ,
2659 { pattern : / ^ b e n c h m a r k s _ e x t e r n a l \/ t e s t \/ / , insertion : '' } ,
2760 { pattern : / ^ b e n c h m a r k s _ e x t e r n a l \/ / , insertion : 'web' } ,
28- { pattern : / ^ e x a m p l e . ? \/ / , insertion : 'web/ ' } ,
29- { pattern : / ^ e x a m p l e . ? \/ t e s t \/ / , insertion : '' } ,
61+ { pattern : / ^ e x a m p l e s \/ t e s t \/ / , insertion : '' } ,
62+ { pattern : / ^ e x a m p l e s \/ / , insertion : 'web/ ' } ,
3063 { pattern : / ^ [ ^ \/ ] * \/ t e s t \/ / , insertion : '' } ,
3164 { pattern : / ^ ./ , insertion : 'lib' } , // catch all.
3265 ] ;
@@ -41,7 +74,75 @@ module.exports = function makeDartTree() {
4174 }
4275 throw new Error ( 'Failed to match any path: ' + relativePath ) ;
4376 } ) ;
77+ }
78+
79+ function getHtmlSourcesTree ( ) {
80+ // Replace $SCRIPT$ markers in HTML files.
81+ var htmlSrcsTree = stew . map ( modulesFunnel ( [ '*/src/**/*.html' ] ) , replaceScriptTagInHtml ) ;
82+ // Copy a url_params_to_form.js for each benchmark html file.
83+ var urlParamsToFormTree = new MultiCopy ( '' , {
84+ srcPath : 'tools/build/snippets/url_params_to_form.js' ,
85+ targetPatterns : [ 'modules/benchmarks*/src/*' , 'modules/benchmarks*/src/*/*' ] ,
86+ } ) ;
87+ urlParamsToFormTree = stew . rename ( urlParamsToFormTree , stripModulePrefix ) ;
88+ return mergeTrees ( [ htmlSrcsTree , urlParamsToFormTree ] ) ;
89+ }
90+
91+
92+ function getTemplatedPubspecsTree ( ) {
93+ // The JSON structure for templating pubspec.yaml files.
94+ var BASE_PACKAGE_JSON = require ( '../../../package.json' ) ;
95+ var COMMON_PACKAGE_JSON = {
96+ version : BASE_PACKAGE_JSON . version ,
97+ homepage : BASE_PACKAGE_JSON . homepage ,
98+ bugs : BASE_PACKAGE_JSON . bugs ,
99+ license : BASE_PACKAGE_JSON . license ,
100+ contributors : BASE_PACKAGE_JSON . contributors ,
101+ dependencies : BASE_PACKAGE_JSON . dependencies ,
102+ devDependencies : {
103+ "yargs" : BASE_PACKAGE_JSON . devDependencies [ 'yargs' ] ,
104+ "gulp-sourcemaps" : BASE_PACKAGE_JSON . devDependencies [ 'gulp-sourcemaps' ] ,
105+ "gulp-traceur" : BASE_PACKAGE_JSON . devDependencies [ 'gulp-traceur' ] ,
106+ "gulp" : BASE_PACKAGE_JSON . devDependencies [ 'gulp' ] ,
107+ "gulp-rename" : BASE_PACKAGE_JSON . devDependencies [ 'gulp-rename' ] ,
108+ "through2" : BASE_PACKAGE_JSON . devDependencies [ 'through2' ]
109+ }
110+ } ;
111+ // Generate pubspec.yaml from templates.
112+ // Lodash insists on dropping one level of extension, so first artificially rename the yaml
113+ // files to .yaml.template.
114+ var pubspecs = stew . rename ( modulesFunnel ( [ '**/pubspec.yaml' ] ) , '.yaml' , '.yaml.template' ) ;
115+ // Then render the templates.
116+ return renderLodashTemplate (
117+ pubspecs ,
118+ { files : [ '**/pubspec.yaml.template' ] , context : { 'packageJson' : COMMON_PACKAGE_JSON } } ) ;
119+ }
120+
121+ function getDocsTree ( ) {
122+ // LICENSE files
123+ var licenses = new MultiCopy ( '' , {
124+ srcPath : 'LICENSE' ,
125+ targetPatterns : [ 'modules/*' ] ,
126+ exclude : [ '*/rtts_assert' ] , // Not in dart.
127+ } ) ;
128+ licenses = stew . rename ( licenses , stripModulePrefix ) ;
129+
130+ // Documentation.
131+ // Rename *.dart.md -> *.dart.
132+ var mdTree = stew . rename ( modulesFunnel ( [ '**/*.dart.md' ] ) ,
133+ relativePath => relativePath . replace ( / \. d a r t \. m d $ / , '.md' ) ) ;
134+ // Copy all assets, ignore .js. and .dart. (handled above).
135+ var docs = modulesFunnel ( [ '**/*.md' , '**/*.png' , '**/*.html' , '**/*.css' ] ,
136+ [ '**/*.js.md' , '**/*.dart.md' ] ) ;
137+ return mergeTrees ( [ licenses , mdTree , docs ] ) ;
138+ }
139+
140+ module . exports = function makeDartTree ( ) {
141+ var sourceTree = mergeTrees ( [ getSourceTree ( ) , getHtmlSourcesTree ( ) ] ) ;
142+ sourceTree = fixDartFolderLayout ( sourceTree ) ;
143+
144+ var mergedResult = mergeTrees ( [ sourceTree , getTemplatedPubspecsTree ( ) , getDocsTree ( ) ] ) ;
44145
45146 // Move the tree under the 'dart' folder.
46- return stew . mv ( dartTree , 'dart' ) ;
147+ return stew . mv ( mergedResult , 'dart' ) ;
47148} ;
0 commit comments