Created
October 4, 2012 13:56
-
-
Save atifaziz/3833679 to your computer and use it in GitHub Desktop.
TypeScript compilation from C# using MS Script Control
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
using System.Globalization; | |
using System.IO; | |
using System.Linq; | |
using System.Text; | |
static class Program | |
{ | |
[STAThread] | |
static int Main(string[] args) | |
{ | |
try | |
{ | |
return Run(args); | |
} | |
catch (Exception e) | |
{ | |
Console.Error.WriteLine(e.ToString()); | |
return 0xbad; | |
} | |
} | |
static int Run(IEnumerable<string> args) | |
{ | |
var appDataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); | |
var npmModulesPath = Path.Combine(appDataPath, "npm", "node_modules"); | |
var tsBinPath = Path.Combine(npmModulesPath, "typescript", "bin"); | |
var sourceFilePath = args.FirstOrDefault(a => !string.IsNullOrWhiteSpace(a)); | |
var ts = sourceFilePath == null || sourceFilePath == "-" | |
? Console.In.ReadToEnd() | |
: File.ReadAllText(sourceFilePath); | |
var scType = Type.GetTypeFromProgID("MSScriptControl.ScriptControl.1"); | |
dynamic sc = Activator.CreateInstance(scType); | |
sc.Language = "JavaScript"; | |
var tsPath = Path.Combine(tsBinPath, "typescript.js"); | |
if (!File.Exists(tsPath)) | |
throw new FileNotFoundException(@"TypeScript does not appear to be installed as an NPM global package. See http://www.typescriptlang.org/ for more information.", tsBinPath); | |
var compiler = File.ReadAllText(tsPath); | |
sc.AddCode(compiler); | |
var resultObject = sc.Eval(@" | |
// credit: http://typescript.codeplex.com/discussions/397810 | |
(function(source) { | |
var nop = function() {}; | |
var outfile = { | |
source: [], | |
Write: function (s) { this.source.push(s); }, | |
WriteLine: function (s) { this.Write(s + '\n'); }, | |
Close: nop | |
}; | |
var outerr = { Write: nop, WriteLine: nop, Close: nop }; | |
var errors = []; | |
var compiler = new TypeScript.TypeScriptCompiler(outfile, outerr); | |
compiler.setErrorCallback(function (start, len, msg) { | |
errors.push({ start: start, len: len, message: msg }); }); | |
compiler.parser.errorRecovery = true; | |
compiler.addUnit(source, ''); | |
compiler.typeCheck(); | |
compiler.emit(false, function() { return outfile; }); | |
return { | |
output: outfile.source.join(''), | |
errors: errors | |
}; | |
})(" + ts.ToJson() + @");"); | |
var errors = | |
from dynamic error in (IEnumerable) resultObject.errors | |
select new | |
{ | |
Start = (int) error.start, | |
Length = (int) error.len, | |
Message = (string) error.message, | |
} | |
into error | |
where error.Start >= 0 | |
select string.Format(@"({0},{1}): {2}", | |
error.Start, error.Length, error.Message); | |
errors = errors.ToArray(); | |
Array.ForEach((string[]) errors, Console.Error.WriteLine); | |
Console.WriteLine((string) resultObject.output); | |
return !errors.Any() ? 0 : 1; | |
} | |
static string ToJson(this string s) // adapted from http://goo.gl/4NGTn | |
{ | |
var length = (s = s ?? string.Empty).Length; | |
var sb = new StringBuilder().Append('"'); | |
for (var index = 0; index < length; index++) | |
{ | |
var ch = s[index]; | |
switch (ch) | |
{ | |
case '\\': | |
case '"': | |
{ | |
sb.Append('\\'); | |
sb.Append(ch); | |
break; | |
} | |
case '\b': sb.Append("\\b"); break; | |
case '\t': sb.Append("\\t"); break; | |
case '\n': sb.Append("\\n"); break; | |
case '\f': sb.Append("\\f"); break; | |
case '\r': sb.Append("\\r"); break; | |
default: | |
{ | |
if (ch < ' ') | |
{ | |
sb.Append("\\u"); | |
sb.Append(((int)ch).ToString("x4", NumberFormatInfo.InvariantInfo)); | |
} | |
else | |
{ | |
sb.Append(ch); | |
} | |
break; | |
} | |
} | |
} | |
return sb.Append('"').ToString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment