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="../typings/node/node.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 fse = require('fs-extra');
import path = require('path');
import ts2dart = require('ts2dart');
import {wrapDiffingPlugin, DiffingBroccoliPlugin, DiffResult} from './diffing-broccoli-plugin';
type Set = {
[s: string]: boolean
};
class TSToDartTranspiler implements DiffingBroccoliPlugin {
static includeExtensions = ['.js', '.ts'];
class TypeScriptToDartTranspiler extends Writer {
constructor(private inputTree, private includePattern = /\.(js|ts)$/) { super(); }
private basePath: string;
private transpiler: ts2dart.Transpiler;
write(readTree, destDir): Promise<void> {
return readTree(this.inputTree).then(dir => this.transpile(dir, destDir));
constructor(public inputPath: string, public cachePath: string, public options) {
options.basePath = inputPath;
this.transpiler = new ts2dart.Transpiler(options);
}
private transpile(inputDir: string, destDir: string) {
var files = this.listRecursive(inputDir);
var toTranspile = [];
for (var f in files) {
// If it's not matching, don't translate.
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);
}
rebuild(treeDiff: DiffResult) {
let toEmit = [];
let getDartFilePath = (path: string) => path.replace(/((\.js)|(\.ts))$/i, '.dart');
treeDiff.changedPaths.forEach((changedPath) => {
let inputFilePath = path.resolve(this.inputPath, changedPath);
private listRecursive(root: string, res: Set = {}): Set {
var paths = fs.readdirSync(root);
paths.forEach((p) => {
p = path.join(root, p);
var stat = fs.statSync(p);
if (stat.isDirectory()) {
this.listRecursive(p, res);
} else {
// Collect *all* files so we can check .dart files that already exist and exclude them.
res[p] = true;
}
// Ignore files which don't need to be transpiled to Dart
let dartInputFilePath = getDartFilePath(inputFilePath);
if (fs.existsSync(dartInputFilePath)) return;
// Prepare to rebuild
toEmit.push(path.resolve(this.inputPath, changedPath));
});
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) {
return new TypeScriptToDartTranspiler(inputTree);
}
export default wrapDiffingPlugin(TSToDartTranspiler);

View File

@ -10,7 +10,7 @@ var path = require('path');
var renderLodashTemplate = require('broccoli-lodash');
var replace = require('broccoli-replace');
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.
@ -44,7 +44,7 @@ function stripModulePrefix(relativePath: string): string {
function getSourceTree() {
// Transpile everything in 'modules' except for rtts_assertions.
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.
var dartSrcs = modulesFunnel(['**/*.dart', '**/*.ng_meta.json', '**/css/**']);
return mergeTrees([transpiled, dartSrcs]);