Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from May 14, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
72 changes: 34 additions & 38 deletions tools/broccoli/broccoli-ts2dart.ts
@@ -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);

// 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));
});

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;
}
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));
});
return res;

this.transpiler.transpile(toEmit, this.cachePath);
}
}

export function transpile(inputTree) {
return new TypeScriptToDartTranspiler(inputTree);
}
export default wrapDiffingPlugin(TSToDartTranspiler);
4 changes: 2 additions & 2 deletions tools/broccoli/trees/dart_tree.ts
Expand Up @@ -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.
Expand Down Expand Up @@ -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]);
Expand Down