Skip to content

Commit 586abae

Browse files
Using the official CoffeeScript compiler. Fixed #68
1 parent 6647022 commit 586abae

File tree

11 files changed

+119
-52
lines changed

11 files changed

+119
-52
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
- [x] Shortcut to easily setup compilation of single files
44
- [x] LESS: Added StrictUnits and RootPath compiler support
55
- [x] LESS: Added relative URL compiler support (#63)
6+
- [x] Use the official CoffeeScript compiler (#68)
67
- [ ] Generate gulpfile.js from compilerconfig.json (#34)
78
- [ ] Preview window (#6)
89
- [ ] File globbing pattern support (#49)

build/build.cmd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ if not exist node.7z (
2020
echo Installing packages...
2121
call npm install flatten-packages -g --quiet > nul
2222
call npm install less --quiet > nul
23+
call npm install coffee-script --quiet > nul
2324
call npm install iced-coffee-script --quiet > nul
2425

2526

Lines changed: 96 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,23 @@
11
using System;
2+
using System.Diagnostics;
23
using System.IO;
4+
using System.Text;
35
using System.Text.RegularExpressions;
4-
using CoffeeSharp;
56

67
namespace WebCompiler
78
{
89
internal class CoffeeScriptCompiler : ICompiler
910
{
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+
}
1221

1322
public CompilerResult Compile(Config config)
1423
{
@@ -24,40 +33,105 @@ public CompilerResult Compile(Config config)
2433
OriginalContent = content,
2534
};
2635

27-
CoffeeScriptOptions options = new CoffeeScriptOptions(config);
28-
2936
try
3037
{
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+
}
3373
}
3474
catch (Exception ex)
3575
{
3676
CompilerError error = new CompilerError
3777
{
3878
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,
4082
};
4183

42-
Match match = _error.Match(ex.Message);
84+
result.Errors.Add(error);
85+
}
4386

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+
}
4989

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);
5393

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+
};
56107

57-
result.Errors.Add(error);
58-
}
108+
start.EnvironmentVariables["PATH"] = _path + ";" + start.EnvironmentVariables["PATH"];
59109

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;
61135
}
62136
}
63137
}

src/WebCompiler/Compile/CoffeeScriptOptions.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,8 @@ class CoffeeScriptOptions : BaseOptions
77
public CoffeeScriptOptions(Config config)
88
{
99
Bare = GetValue(config, "bare").ToLowerInvariant() == trueStr;
10-
Globals = GetValue(config, "globals").ToLowerInvariant() == trueStr;
1110
}
1211

1312
public bool Bare { get; set; }
14-
15-
public bool Globals { get; set; }
1613
}
1714
}

src/WebCompiler/Compile/CompilerService.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ internal static ICompiler GetCompiler(Config config)
4343
break;
4444

4545
case ".COFFEE":
46-
compiler = new CoffeeScriptCompiler();
46+
Initialize();
47+
compiler = new CoffeeScriptCompiler(_path);
4748
break;
4849

4950
case ".ICED":

src/WebCompiler/WebCompiler.csproj

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,6 @@
3636
<HintPath>..\..\packages\AjaxMin.5.14.5506.26202\lib\net40\AjaxMin.dll</HintPath>
3737
<Private>True</Private>
3838
</Reference>
39-
<Reference Include="CoffeeScriptHttpHandler, Version=0.6.0.0, Culture=neutral, processorArchitecture=MSIL">
40-
<HintPath>..\..\packages\CoffeeSharp.0.6\lib\CoffeeScriptHttpHandler.dll</HintPath>
41-
<Private>True</Private>
42-
</Reference>
43-
<Reference Include="CoffeeSharp, Version=0.6.0.0, Culture=neutral, processorArchitecture=MSIL">
44-
<HintPath>..\..\packages\CoffeeSharp.0.6\lib\CoffeeSharp.dll</HintPath>
45-
<Private>True</Private>
46-
</Reference>
47-
<Reference Include="Jurassic, Version=2.1.0.0, Culture=neutral, processorArchitecture=MSIL">
48-
<HintPath>..\..\packages\CoffeeSharp.0.6\lib\Jurassic.dll</HintPath>
49-
<Private>True</Private>
50-
</Reference>
5139
<Reference Include="LibSass.x86, Version=0.0.0.0, Culture=neutral, processorArchitecture=x86">
5240
<HintPath>..\..\packages\libsassnet.3.2.5\lib\net40\LibSass.x86.dll</HintPath>
5341
<Private>True</Private>

src/WebCompiler/packages.config

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
33
<package id="AjaxMin" version="5.14.5506.26202" targetFramework="net45" />
4-
<package id="CoffeeSharp" version="0.6" targetFramework="net45" />
54
<package id="libsassnet" version="3.2.5" targetFramework="net45" />
65
<package id="Newtonsoft.Json" version="7.0.1" targetFramework="net45" />
76
<package id="NuGet.CommandLine" version="2.8.5" targetFramework="net45" />

src/WebCompilerTest/Compile/CoffeeScriptTest.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,19 @@ public void Cleanup()
2121
{
2222
File.Delete("../../artifacts/coffee/test.js");
2323
File.Delete("../../artifacts/coffee/test.min.js");
24+
File.Delete("../../artifacts/coffee/test.js.map");
2425
}
2526

2627
[TestMethod, TestCategory("CoffeeScript")]
2728
public void CompileCoffeeScript()
2829
{
2930
var result = _processor.Process("../../artifacts/coffeeconfig.json");
30-
FileInfo info = new FileInfo("../../artifacts/coffee/test.js");
31-
Assert.IsTrue(info.Exists);
32-
Assert.IsTrue(info.Length > 5);
31+
FileInfo js = new FileInfo("../../artifacts/coffee/test.js");
32+
FileInfo map = new FileInfo("../../artifacts/coffee/test.js.map");
33+
Assert.IsTrue(js.Exists);
34+
Assert.IsTrue(map.Exists);
35+
Assert.IsTrue(js.Length > 5);
36+
Assert.IsTrue(map.Length > 5);
3337
}
3438

3539
[TestMethod, TestCategory("CoffeeScript")]
@@ -38,7 +42,7 @@ public void CompileCoffeeScriptWithError()
3842
var result = _processor.Process("../../artifacts/coffeeconfigerror.json");
3943
var error = result.First().Errors[0];
4044
Assert.AreEqual(1, error.LineNumber);
41-
Assert.AreEqual("error: unexpected ==", error.Message);
45+
Assert.AreEqual("unexpected ==", error.Message);
4246
}
4347
}
4448
}

src/WebCompilerTest/artifacts/coffeeconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"outputFile": "coffee/test.js",
44
"inputFile": "coffee/test.coffee",
55
"minify": { },
6-
"includeInProject": true
6+
"includeInProject": true,
7+
"sourceMap": true
78
}
89
]

src/WebCompilerVsix/JSON/compilerconfig-schema.json

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,6 @@
103103
},
104104
"minify": {
105105
"$ref": "#/definitions/cssOptions"
106-
},
107-
"sourceMap": {
108-
"description": "Set to true to automatically create inline source map files.",
109-
"type": "boolean"
110106
}
111107
}
112108
},
@@ -121,9 +117,6 @@
121117
"bare": {
122118
"description": "CoffeeScript only. Compile the JavaScript without the top-level function safety wrapper.",
123119
"type": "boolean"
124-
},
125-
"globals": {
126-
"type": "boolean"
127120
}
128121
}
129122
},
@@ -203,6 +196,10 @@
203196
"description": "The relative path to the desired output file name.",
204197
"type": "string",
205198
"minLength": 1
199+
},
200+
"sourceMap": {
201+
"description": "Set to true to automatically create inline source map files.",
202+
"type": "boolean"
206203
}
207204
},
208205

0 commit comments

Comments
 (0)