build(brocolli): convert brocolli-ts2dart to use TreeDiffer

Closes #1720
Closes #1733
This commit is contained in:
Caitlin Potter 2015-05-08 12:46:38 -05:00
parent ecb068019b
commit 3969009fe7
2 changed files with 36 additions and 40 deletions

View File

@ -1,56 +1,52 @@
/// <reference path="./broccoli-writer.d.ts" /> /// <reference path="./broccoli-writer.d.ts" />
/// <reference path="../typings/node/node.d.ts" /> /// <reference path="../typings/node/node.d.ts" />
/// <reference path="../typings/fs-extra/fs-extra.d.ts" /> /// <reference path="../typings/fs-extra/fs-extra.d.ts" />
/// <reference path="./ts2dart.d.ts" />
import Writer = require('broccoli-writer');
import fs = require('fs'); import fs = require('fs');
import fse = require('fs-extra'); import fse = require('fs-extra');
import path = require('path'); import path = require('path');
import ts2dart = require('ts2dart'); import ts2dart = require('ts2dart');
import {wrapDiffingPlugin, DiffingBroccoliPlugin, DiffResult} from './diffing-broccoli-plugin';
type Set = { class TSToDartTranspiler implements DiffingBroccoliPlugin {
[s: string]: boolean static includeExtensions = ['.js', '.ts'];
};
class TypeScriptToDartTranspiler extends Writer { private basePath: string;
constructor(private inputTree, private includePattern = /\.(js|ts)$/) { super(); } private transpiler: ts2dart.Transpiler;
write(readTree, destDir): Promise<void> { constructor(public inputPath: string, public cachePath: string, public options) {
return readTree(this.inputTree).then(dir => this.transpile(dir, destDir)); options.basePath = inputPath;
this.transpiler = new ts2dart.Transpiler(options);
} }
private transpile(inputDir: string, destDir: string) { rebuild(treeDiff: DiffResult) {
var files = this.listRecursive(inputDir); let toEmit = [];
var toTranspile = []; let getDartFilePath = (path: string) => path.replace(/((\.js)|(\.ts))$/i, '.dart');
for (var f in files) { treeDiff.changedPaths.forEach((changedPath) => {
// If it's not matching, don't translate. let inputFilePath = path.resolve(this.inputPath, changedPath);
if (!f.match(this.includePattern)) continue;
var dartVariant = f.replace(this.includePattern, '.dart');
// A .dart file of the same name takes precedence over transpiled code.
if (files.hasOwnProperty(dartVariant)) continue;
toTranspile.push(f);
}
var transpiler = new ts2dart.Transpiler(
{generateLibraryName: true, generateSourceMap: false, basePath: inputDir});
transpiler.transpile(toTranspile, destDir);
}
private listRecursive(root: string, res: Set = {}): Set { // Ignore files which don't need to be transpiled to Dart
var paths = fs.readdirSync(root); let dartInputFilePath = getDartFilePath(inputFilePath);
paths.forEach((p) => { if (fs.existsSync(dartInputFilePath)) return;
p = path.join(root, p);
var stat = fs.statSync(p); // Prepare to rebuild
if (stat.isDirectory()) { toEmit.push(path.resolve(this.inputPath, changedPath));
this.listRecursive(p, res);
} else {
// Collect *all* files so we can check .dart files that already exist and exclude them.
res[p] = true;
}
}); });
return res;
treeDiff.removedPaths.forEach((removedPath) => {
let absolutePath = path.resolve(this.inputPath, removedPath);
// Ignore files which don't need to be transpiled to Dart
let dartInputFilePath = getDartFilePath(absolutePath);
if (fs.existsSync(dartInputFilePath)) return;
let dartOutputFilePath = getDartFilePath(removedPath);
fs.unlinkSync(path.join(this.cachePath, dartOutputFilePath));
});
this.transpiler.transpile(toEmit, this.cachePath);
} }
} }
export function transpile(inputTree) { export default wrapDiffingPlugin(TSToDartTranspiler);
return new TypeScriptToDartTranspiler(inputTree);
}

View File

@ -10,7 +10,7 @@ var path = require('path');
var renderLodashTemplate = require('broccoli-lodash'); var renderLodashTemplate = require('broccoli-lodash');
var replace = require('broccoli-replace'); var replace = require('broccoli-replace');
var stew = require('broccoli-stew'); var stew = require('broccoli-stew');
var ts2dart = require('../broccoli-ts2dart'); import ts2dart from '../broccoli-ts2dart';
/** /**
* A funnel starting at modules, including the given filters, and moving into the root. * A funnel starting at modules, including the given filters, and moving into the root.
@ -44,7 +44,7 @@ function stripModulePrefix(relativePath: string): string {
function getSourceTree() { function getSourceTree() {
// Transpile everything in 'modules' except for rtts_assertions. // Transpile everything in 'modules' except for rtts_assertions.
var tsInputTree = modulesFunnel(['**/*.js', '**/*.ts', '**/*.dart'], ['rtts_assert/**/*']); var tsInputTree = modulesFunnel(['**/*.js', '**/*.ts', '**/*.dart'], ['rtts_assert/**/*']);
var transpiled = ts2dart.transpile(tsInputTree); var transpiled = ts2dart(tsInputTree, {generateLibraryName: true, generateSourceMap: false});
// Native sources, dart only examples, etc. // Native sources, dart only examples, etc.
var dartSrcs = modulesFunnel(['**/*.dart', '**/*.ng_meta.json', '**/css/**']); var dartSrcs = modulesFunnel(['**/*.dart', '**/*.ng_meta.json', '**/css/**']);
return mergeTrees([transpiled, dartSrcs]); return mergeTrees([transpiled, dartSrcs]);