1
1
using System ;
2
+ using System . Diagnostics ;
2
3
using System . IO ;
4
+ using System . Text ;
3
5
using System . Text . RegularExpressions ;
4
- using CoffeeSharp ;
5
6
6
7
namespace WebCompiler
7
8
{
8
9
internal class CoffeeScriptCompiler : ICompiler
9
10
{
10
- private static CoffeeScriptEngine _engine = new CoffeeScriptEngine ( ) ;
11
- private static Regex _error = new Regex ( ":(?<line>[0-9]+):(?<column>[0-9]+):(?<message>.+)" , RegexOptions . Compiled ) ;
11
+ private static Regex _errorRx = new Regex ( ":(?<line>[0-9]+):(?<column>[0-9]+).*error: (?<message>.+)" , RegexOptions . Compiled ) ;
12
+ private string _path ;
13
+ private string _output = string . Empty ;
14
+ private string _error = string . Empty ;
15
+ private string _temp = Path . Combine ( Path . GetTempPath ( ) , ".coffee-script" ) ;
16
+
17
+ public CoffeeScriptCompiler ( string path )
18
+ {
19
+ _path = path ;
20
+ }
12
21
13
22
public CompilerResult Compile ( Config config )
14
23
{
@@ -24,40 +33,105 @@ public CompilerResult Compile(Config config)
24
33
OriginalContent = content ,
25
34
} ;
26
35
27
- CoffeeScriptOptions options = new CoffeeScriptOptions ( config ) ;
28
-
29
36
try
30
37
{
31
- string compilerResult = _engine . Compile ( content , filename : info . FullName , bare : options . Bare , globals : options . Globals ) ;
32
- result . CompiledContent = compilerResult ;
38
+ RunCompilerProcess ( config , info ) ;
39
+
40
+ string tempFile = Path . ChangeExtension ( Path . Combine ( _temp , info . Name ) , ".js" ) ;
41
+
42
+ if ( File . Exists ( tempFile ) )
43
+ {
44
+ result . CompiledContent = File . ReadAllText ( tempFile ) ;
45
+
46
+ if ( config . SourceMap )
47
+ {
48
+ string mapFile = tempFile + ".map" ;
49
+ if ( File . Exists ( mapFile ) )
50
+ result . SourceMap = File . ReadAllText ( mapFile ) ;
51
+ }
52
+ }
53
+
54
+ if ( _error . Length > 0 )
55
+ {
56
+ CompilerError ce = new CompilerError
57
+ {
58
+ FileName = info . FullName ,
59
+ Message = _error . Replace ( baseFolder , string . Empty ) ,
60
+ } ;
61
+
62
+ var match = _errorRx . Match ( _error ) ;
63
+
64
+ if ( match . Success )
65
+ {
66
+ ce . Message = match . Groups [ "message" ] . Value . Replace ( baseFolder , string . Empty ) ;
67
+ ce . LineNumber = int . Parse ( match . Groups [ "line" ] . Value ) ;
68
+ ce . ColumnNumber = int . Parse ( match . Groups [ "column" ] . Value ) ;
69
+ }
70
+
71
+ result . Errors . Add ( ce ) ;
72
+ }
33
73
}
34
74
catch ( Exception ex )
35
75
{
36
76
CompilerError error = new CompilerError
37
77
{
38
78
FileName = info . FullName ,
39
- Message = ex . Message . Replace ( info . FullName , string . Empty ) . Trim ( )
79
+ Message = string . IsNullOrEmpty ( _error ) ? ex . Message : _error ,
80
+ LineNumber = 0 ,
81
+ ColumnNumber = 0 ,
40
82
} ;
41
83
42
- Match match = _error . Match ( ex . Message ) ;
84
+ result . Errors . Add ( error ) ;
85
+ }
43
86
44
- if ( match . Success )
45
- {
46
- int line ;
47
- if ( int . TryParse ( match . Groups [ "line" ] . Value , out line ) )
48
- error . LineNumber = line ;
87
+ return result ;
88
+ }
49
89
50
- int column ;
51
- if ( int . TryParse ( match . Groups [ "column" ] . Value , out column ) )
52
- error . ColumnNumber = column ;
90
+ private void RunCompilerProcess ( Config config , FileInfo info )
91
+ {
92
+ string arguments = ConstructArguments ( config ) ;
53
93
54
- error . Message = match . Groups [ "message" ] . Value . Trim ( ) ;
55
- }
94
+ ProcessStartInfo start = new ProcessStartInfo
95
+ {
96
+ WorkingDirectory = info . Directory . FullName ,
97
+ UseShellExecute = false ,
98
+ WindowStyle = ProcessWindowStyle . Hidden ,
99
+ CreateNoWindow = true ,
100
+ FileName = "cmd.exe" ,
101
+ Arguments = $ "/c \" \" { Path . Combine ( _path , "node_modules\\ .bin\\ coffee.cmd" ) } \" { arguments } \" { info . FullName } \" \" ",
102
+ StandardOutputEncoding = Encoding . UTF8 ,
103
+ StandardErrorEncoding = Encoding . UTF8 ,
104
+ RedirectStandardOutput = true ,
105
+ RedirectStandardError = true ,
106
+ } ;
56
107
57
- result . Errors . Add ( error ) ;
58
- }
108
+ start . EnvironmentVariables [ "PATH" ] = _path + ";" + start . EnvironmentVariables [ "PATH" ] ;
59
109
60
- return result ;
110
+ Process p = Process . Start ( start ) ;
111
+ var stdout = p . StandardOutput . ReadToEndAsync ( ) ;
112
+ var stderr = p . StandardError . ReadToEndAsync ( ) ;
113
+ p . WaitForExit ( ) ;
114
+
115
+ _output = stdout . Result ;
116
+ _error = stderr . Result ;
117
+ }
118
+
119
+ private string ConstructArguments ( Config config )
120
+ {
121
+ string arguments = $ " --compile --output \" { _temp } \" ";
122
+
123
+ if ( config . SourceMap )
124
+ arguments += " --map" ;
125
+
126
+ CoffeeScriptOptions options = new CoffeeScriptOptions ( config ) ;
127
+
128
+ if ( options . Bare )
129
+ arguments += " --bare" ;
130
+
131
+ //if (options.Globals)
132
+ // arguments += " --globals";
133
+
134
+ return arguments ;
61
135
}
62
136
}
63
137
}
0 commit comments