Skip to content

Commit 18de560

Browse files
committed
Add jake task for making instrumented tsc
1 parent 655039c commit 18de560

File tree

3 files changed

+98
-4
lines changed

3 files changed

+98
-4
lines changed

Jakefile

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ function concatenateFiles(destinationFile, sourceFiles) {
123123
}
124124

125125
var useDebugMode = false;
126+
var host = (process.env.host || process.env.TYPESCRIPT_HOST || "node");
127+
var compilerFilename = "tsc.js";
126128
/* Compiles a file from a list of sources
127129
* @param outFile: the target file name
128130
* @param sources: an array of the names of the source files
@@ -134,10 +136,9 @@ var useDebugMode = false;
134136
function compileFile(outFile, sources, prereqs, prefixes, useBuiltCompiler, noOutFile) {
135137
file(outFile, prereqs, function() {
136138
var dir = useBuiltCompiler ? builtLocalDirectory : LKGDirectory;
137-
var compilerFilename = "tsc.js";
138139
var options = "-removeComments --module commonjs -noImplicitAny "; //" -propagateEnumConstants "
139140

140-
var cmd = (process.env.host || process.env.TYPESCRIPT_HOST || "node") + " " + dir + compilerFilename + " " + options + " ";
141+
var cmd = host + " " + dir + compilerFilename + " " + options + " ";
141142
if (useDebugMode) {
142143
cmd = cmd + " " + path.join(harnessDirectory, "external/es5compat.ts") + " " + path.join(harnessDirectory, "external/json2.ts") + " ";
143144
}
@@ -230,7 +231,7 @@ task("generate-diagnostics", [diagnosticInfoMapTs])
230231

231232

232233
// Local target to build the compiler and services
233-
var tscFile = path.join(builtLocalDirectory, "tsc.js");
234+
var tscFile = path.join(builtLocalDirectory, compilerFilename);
234235
compileFile(tscFile, compilerSources, [builtLocalDirectory, copyright].concat(compilerSources), [copyright], /*useBuiltCompiler:*/ false);
235236

236237
var servicesFile = path.join(builtLocalDirectory, "typescriptServices.js");
@@ -473,3 +474,36 @@ var perftscJsPath = "built/local/perftsc.js";
473474
compileFile(perftscJsPath, [perftscPath], [tscFile, perftscPath, "tests/perfsys.ts"].concat(libraryTargets), [], true);
474475
desc("Builds augmented version of the compiler for perf tests");
475476
task("perftsc", [perftscJsPath]);
477+
478+
// Instrumented compiler
479+
var loggedIOpath = harnessDirectory + 'loggedIO.ts';
480+
var loggedIOJsPath = builtLocalDirectory + 'loggedIO.js';
481+
file(loggedIOJsPath, [builtLocalDirectory], function() {
482+
var temp = builtLocalDirectory + 'temp';
483+
jake.mkdirP(temp);
484+
var options = "--outdir " + temp + ' ' + loggedIOpath;
485+
var cmd = host + " " + LKGDirectory + compilerFilename + " " + options + " ";
486+
console.log(cmd + "\n");
487+
var ex = jake.createExec([cmd]);
488+
ex.addListener("cmdEnd", function() {
489+
fs.renameSync(temp + '/harness/loggedIO.js', loggedIOJsPath);
490+
jake.rmRf(temp);
491+
});
492+
ex.run();
493+
}, {async: true});
494+
495+
var instrumenterPath = harnessDirectory + 'instrumenter.ts';
496+
var instrumenterJsPath = builtLocalDirectory + 'instrumenter.js';
497+
compileFile(instrumenterJsPath, [instrumenterPath], [tscFile, instrumenterPath], [], true);
498+
499+
desc("Builds an instrumented tsc.js");
500+
task('tsc-instrumented', [loggedIOJsPath, instrumenterJsPath, tscFile], function() {
501+
var cmd = host + ' ' + instrumenterJsPath + ' record iocapture ' + builtLocalDirectory + compilerFilename;
502+
console.log(cmd);
503+
var ex = jake.createExec([cmd]);
504+
ex.addListener("error", function(e) {
505+
console.log('error running instrumenter');
506+
console.log(e);
507+
});
508+
ex.run();
509+
}, { async: true });

src/harness/instrumenter.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
declare var require: any, process: any;
2+
var fs: any = require('fs');
3+
var path: any = require('path');
4+
5+
function instrumentForRecording(fn: string, tscPath: string) {
6+
instrument(tscPath, 'sys = Playback.wrapSystem(sys); sys.startRecord("' + fn + '");', 'sys.endRecord();');
7+
}
8+
9+
function instrumentForReplay(logFilename: string, tscPath: string) {
10+
instrument(tscPath, 'sys = Playback.wrapSystem(sys); sys.startReplay("' + logFilename + '");');
11+
}
12+
13+
function instrument(tscPath: string, prepareCode: string, cleanupCode: string = '') {
14+
var bak = tscPath + '.bak';
15+
fs.exists(bak, (backupExists: boolean) => {
16+
var filename = tscPath;
17+
if (backupExists) {
18+
filename = bak;
19+
}
20+
21+
fs.readFile(filename, 'utf-8', (err: any, tscContent: string) => {
22+
if (err) throw err;
23+
24+
fs.writeFile(bak, tscContent, (err: any) => {
25+
if (err) throw err;
26+
27+
fs.readFile(path.resolve(path.dirname(tscPath) + '/loggedIO.js'), 'utf-8', (err: any, loggerContent: string) => {
28+
if (err) throw err;
29+
30+
var invocationLine = 'ts.executeCommandLine(sys.args);';
31+
var index1 = tscContent.indexOf(invocationLine);
32+
var index2 = index1 + invocationLine.length;
33+
var newContent = tscContent.substr(0, index1) + loggerContent + prepareCode + invocationLine + cleanupCode + tscContent.substr(index2) + '\r\n';
34+
fs.writeFile(tscPath, newContent);
35+
});
36+
});
37+
});
38+
});
39+
}
40+
41+
var isJson = (arg: string) => arg.indexOf(".json") > 0;
42+
43+
var record = process.argv.indexOf('record');
44+
var tscPath = process.argv[process.argv.length - 1];
45+
if (record >= 0) {
46+
console.log('Instrumenting ' + tscPath + ' for recording');
47+
instrumentForRecording(process.argv[record + 1], tscPath);
48+
} else if (process.argv.some(isJson)) {
49+
var filename = process.argv.filter(isJson)[0];
50+
instrumentForReplay(filename, tscPath);
51+
}
52+
53+

src/harness/loggedIO.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
/// <reference path="..\..\src\compiler\sys.ts" />
2+
/// <reference path="..\..\src\harness\harness.ts" />
3+
/// <reference path="..\..\src\harness\runnerbase.ts" />
24

35
interface FileInformation {
46
contents: string;
@@ -258,7 +260,12 @@ module Playback {
258260
memoize((path) => findResultByFields(replayLog.pathsResolved, { path: path }, !ts.isRootedDiskPath(ts.normalizeSlashes(path)) && replayLog.currentDirectory ? replayLog.currentDirectory + '/' + path : ts.normalizeSlashes(path))));
259261

260262
wrapper.readFile = recordReplay(wrapper.readFile, underlying)(
261-
(path) => callAndRecord(underlying.readFile(path), recordLog.filesRead, { path: path, codepage: 0 }),
263+
(path) => {
264+
var result = underlying.readFile(path);
265+
var logEntry = { path: path, codepage: 0, result: { contents: result, codepage: 0 } };
266+
recordLog.filesRead.push(logEntry);
267+
return result;
268+
},
262269
memoize((path) => findResultByPath(wrapper, replayLog.filesRead, path).contents));
263270

264271
wrapper.writeFile = recordReplay(wrapper.writeFile, underlying)(

0 commit comments

Comments
 (0)