Skip to content

Commit

Permalink
Merge pull request #1690 from iamkrillin/master
Browse files Browse the repository at this point in the history
Fix some bugs with less
  • Loading branch information
madskristensen committed Dec 30, 2014
2 parents cc290e6 + e100d71 commit 2cf37f3
Show file tree
Hide file tree
Showing 8 changed files with 210 additions and 97 deletions.
86 changes: 81 additions & 5 deletions EditorExtensions/PreBuildTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,39 @@ namespace MadsKristensen.EditorExtensions
/// </summary>
public class PreBuildTask : Microsoft.Build.Utilities.Task
{
private List<string> toRemove = new List<string>()
{
"*.md",
"*.markdown",
"*.html",
"*.txt",
"LICENSE",
"README",
"CHANGELOG",
"CNAME",
"*.old",
"*.patch",
"*.ico",
"Makefile.*",
"Rakefile",
"*.yml",
"test.*",
"generate-*",
"media",
"images",
"man",
"benchmark",
"build",
"scripts",
"test",
"tst",
"tests",
"testing",
"examples",
"*.tscache",
"example",
};

Type _path;
Type _directory;
Type _directoryInfo;
Expand All @@ -42,6 +75,7 @@ public PreBuildTask()

public override bool Execute()
{
ClearPath(@"resources\nodejs");
Directory.CreateDirectory(@"resources\nodejs\tools");
// Force npm to install modules to the subdirectory
// https://npmjs.org/doc/files/npm-folders.html#More-Information
Expand Down Expand Up @@ -87,16 +121,57 @@ public override bool Execute()

Log.LogMessage(MessageImportance.High, "Installed " + moduleResults.Count() + " modules. Flattening...");

// Delete .tscache
if (Directory.Exists(@"resources\nodejs\tools\node_modules\tslint\.tscache"))
Directory.Delete(@"resources\nodejs\tools\node_modules\tslint\.tscache", true);

if (!FlattenModulesAsync().Result)
return false;

CleanPath(@"resources\nodejs\tools\node_modules");
return true;
}

private void ClearPath(string path)
{
string[] dirs = (string[])_directory.GetMethod("GetDirectories", new Type[] { typeof(string) }).Invoke(null, new object[] { path });
foreach (string dir in dirs)
{
Log.LogMessage(MessageImportance.High, "Removing " + dir + "...");
_directory.GetMethod("Delete", new Type[] { typeof(string), typeof(bool) }).Invoke(null, new object[] { dir, true });
}

string[] files = (string[])_directory.GetMethod("GetFiles", new Type[] { typeof(string) }).Invoke(null, new object[] { path });
foreach (string file in files)
{
Log.LogMessage(MessageImportance.High, "Removing " + file + "...");
_file.GetMethod("Delete", new Type[] { typeof(string) }).Invoke(null, new object[] { file });
}
}

private void CleanPath(string path)
{
Log.LogMessage(MessageImportance.High, "Working on " + path + "...");
foreach (string pattern in toRemove)
{
string[] dirs = (string[])_directory.GetMethod("GetDirectories", new Type[] { typeof(string), typeof(string) }).Invoke(null, new object[] { path, pattern });
foreach (string dir in dirs)
{
Log.LogMessage(MessageImportance.High, "Removing " + dir + "...");
_directory.GetMethod("Delete", new Type[] { typeof(string), typeof(bool) }).Invoke(null, new object[] { dir, true });
}

string[] files = (string[])_directory.GetMethod("GetFiles", new Type[] { typeof(string), typeof(string) }).Invoke(null, new object[] { path, pattern });
foreach (string file in files)
{
Log.LogMessage(MessageImportance.High, "Removing " + file + "...");
_file.GetMethod("Delete", new Type[] { typeof(string) }).Invoke(null, new object[] { file });
}
}

string[] tocheck = (string[])_directory.GetMethod("GetDirectories", new Type[] { typeof(string) }).Invoke(null, new object[] { path });
foreach (string s in tocheck)
{
CleanPath(s);
}
}

Task DownloadNodeAsync()
{
var file = new FileInfo(@"resources\nodejs\node.exe");
Expand Down Expand Up @@ -267,7 +342,8 @@ where dir.Name.Equals("node_modules", StringComparison.OrdinalIgnoreCase)
}

string targetDir = Path.Combine(baseDir.FullName, "node_modules", module.Name);
if (!Directory.Exists(targetDir))
bool dircheck = (bool)_directory.GetMethod("Exists", new Type[] { typeof(string) }).Invoke(null, new object[] { targetDir });
if (!dircheck)
{
enumerateDirectories = _directoryInfo.GetMethod("MoveTo", new Type[] { typeof(string) });
//module.MoveTo(targetDir);
Expand Down
144 changes: 73 additions & 71 deletions EditorExtensions/Resources/server/services/srv-less.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ var less = require("less"),
//#endregion

//#region Handler
var handleLess = function(writer, params) {
fs.readFile(params.sourceFileName, 'utf8', function(err, data) {
var handleLess = function (writer, params) {
fs.readFile(params.sourceFileName, { encoding: 'utf8' }, function (err, data) {
if (err) {
writer.write(JSON.stringify({
Success: false,
Expand All @@ -24,6 +24,7 @@ var handleLess = function(writer, params) {
return;
}

//data = data.replace('\uFEFF', ''); //strip BOM
var css, map;
var mapFileName = params.targetFileName + ".map";
var sourceDir = path.dirname(params.sourceFileName);
Expand All @@ -41,96 +42,97 @@ var handleLess = function(writer, params) {
strictMath: params.strictMath !== undefined,
};

less.render(data, options, function(error, output) {
if (error) {
writer.write(JSON.stringify({
Success: false,
SourceFileName: params.sourceFileName,
TargetFileName: params.targetFileName,
MapFileName: params.mapFileName,
Remarks: "LESS: Error parsing input file.",
Details: error.message,
Errors: [{
Line: error.line,
Column: error.column,
Message: "LESS: " + error.message,
FileName: error.filename
}]
}));
writer.end();
return;
}
less.render(data, options)
.then(function (output) {
css = output.css;

css = output.css;
map = JSON.parse(output.map);
if (output.map)
map = JSON.parse(output.map);

if (params.autoprefixer !== undefined) {
var autoprefixedOutput = require("./srv-autoprefixer")
.processAutoprefixer(css, map, params.autoprefixerBrowsers,
params.targetFileName, params.targetFileName);
if (params.autoprefixer !== undefined) {
var autoprefixedOutput = require("./srv-autoprefixer")
.processAutoprefixer(css, map, params.autoprefixerBrowsers,
params.targetFileName, params.targetFileName);

if (!autoprefixedOutput.Success) {
writer.write(JSON.stringify({
Success: false,
SourceFileName: params.sourceFileName,
TargetFileName: params.targetFileName,
MapFileName: params.mapFileName,
Remarks: "LESS: " + autoprefixedOutput.Remarks,
Details: autoprefixedOutput.Remarks,
Errors: [{
Message: "LESS: " + autoprefixedOutput.Remarks,
FileName: params.sourceFileName
}]
}));
writer.end();
return;
if (!autoprefixedOutput.Success) {
writer.write(JSON.stringify({
Success: false,
SourceFileName: params.sourceFileName,
TargetFileName: params.targetFileName,
MapFileName: params.mapFileName,
Remarks: "LESS: " + autoprefixedOutput.Remarks,
Details: autoprefixedOutput.Remarks,
Errors: [{
Message: "LESS: " + autoprefixedOutput.Remarks,
FileName: params.sourceFileName
}]
}));
writer.end();
return;
}

css = autoprefixedOutput.css;
map = autoprefixedOutput.map;
}

css = autoprefixedOutput.css;
map = autoprefixedOutput.map;
}
if (params.rtlcss !== undefined) {
var rtlTargetWithoutExtension = params.targetFileName.substr(0, params.targetFileName.lastIndexOf("."));
var rtlTargetFileName = rtlTargetWithoutExtension + ".rtl.css";
var rtlMapFileName = rtlTargetFileName + ".map";
var rtlResult = require("./srv-rtlcss").processRtlCSS(css, map, params.targetFileName, rtlTargetFileName);

if (params.rtlcss !== undefined) {
var rtlTargetWithoutExtension = params.targetFileName.substr(0, params.targetFileName.lastIndexOf("."));
var rtlTargetFileName = rtlTargetWithoutExtension + ".rtl.css";
var rtlMapFileName = rtlTargetFileName + ".map";
var rtlResult = require("./srv-rtlcss")
.processRtlCSS(css, map, params.targetFileName, rtlTargetFileName);
if (rtlResult.Success) {
writer.write(JSON.stringify({
Success: true,
SourceFileName: params.sourceFileName,
TargetFileName: params.targetFileName,
MapFileName: params.mapFileName,
RtlSourceFileName: params.targetFileName,
RtlTargetFileName: rtlTargetFileName,
RtlMapFileName: rtlMapFileName,
Remarks: "Successful!",
Content: css,
Map: JSON.stringify(map),
RtlContent: rtlResult.css,
RtlMap: JSON.stringify(rtlResult.map)
}));

if (rtlResult.Success) {
} else {
throw new Error("Error while processing RTLCSS");
}
} else {
writer.write(JSON.stringify({
Success: true,
SourceFileName: params.sourceFileName,
TargetFileName: params.targetFileName,
MapFileName: params.mapFileName,
RtlSourceFileName: params.targetFileName,
RtlTargetFileName: rtlTargetFileName,
RtlMapFileName: rtlMapFileName,
Remarks: "Successful!",
Content: css,
Map: JSON.stringify(map),
RtlContent: rtlResult.css,
RtlMap: JSON.stringify(rtlResult.map)
Map: JSON.stringify(map)
}));

writer.end();
} else {
throw new Error("Error while processing RTLCSS");
}
} else {

writer.end();
return;
},
function (error) {
writer.write(JSON.stringify({
Success: true,
Success: false,
SourceFileName: params.sourceFileName,
TargetFileName: params.targetFileName,
MapFileName: params.mapFileName,
Remarks: "Successful!",
Content: css,
Map: JSON.stringify(map)
Remarks: "LESS: Error parsing input file.",
Details: error.message,
Errors: [{
Line: error.line,
Column: error.column,
Message: "LESS: " + error.message,
FileName: error.filename
}]
}));
}

writer.end();
});
writer.end();
return;
});
});
};
//#endregion
Expand Down
24 changes: 12 additions & 12 deletions EditorExtensions/Resources/server/services/srv-scss.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
//#region Imports
var sass = require("node-sass"),
path = require("path"),
xRegex = require("xregexp").XRegExp;
path = require("path")
//#endregion

//#region Handler
var handleSass = function(writer, params) {
var handleSass = function (writer, params) {
sass.render({
file: params.sourceFileName,
outFile: params.targetFileName,
Expand All @@ -14,8 +13,9 @@ var handleSass = function(writer, params) {
outputStyle: params.outputStyle,
sourceMap: params.mapFileName,
omitSourceMapUrl: params.sourceMapURL === undefined,
success: function(css, map) {
map = JSON.parse(map);
success: function (result) {
var css = result.css;
var map = result.map;

if (params.autoprefixer !== undefined) {
var autoprefixedOutput = require("./srv-autoprefixer")
Expand Down Expand Up @@ -84,20 +84,20 @@ var handleSass = function(writer, params) {

writer.end();
},
error: function(error) {
var regex = xRegex.exec(error, xRegex("(?<fileName>.+):(?<line>.\\d+): error: (?<fullMessage>(?<message>.*))", 'gi'));
error: function (error) {
writer.write(JSON.stringify({
Success: false,
SourceFileName: params.sourceFileName,
TargetFileName: params.targetFileName,
MapFileName: params.mapFileName,
Remarks: "SASS: An error has occured while processing your request.",
Details: regex.message,
Details: error.message,
Errors: [{
Line: regex.line,
Message: "SASS: " + regex.message,
FileName: regex.fileName,
FullMessage: "SASS" + regex.fullMessage
Line: error.line,
Column: error.column,
Message: "Message: " + error.message,
FileName: error.file,
FullMessage: "Code: " + error.code + " Message: " + error.message
}]
}));
writer.end();
Expand Down
1 change: 1 addition & 0 deletions EditorExtensions/Resources/server/we-nodejs-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ var start = function (port) {

service(response, params);
} catch (e) {
console.logLine(e.message);
response.write(JSON.stringify({ Success: false, Remarks: e.stack }));
response.end();
}
Expand Down
3 changes: 3 additions & 0 deletions EditorExtensions/Shared/Compilers/CompilationChainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ public void VsTextViewCreated(IVsTextView textViewAdapter)
var graph = GetGraph(contentType);
notifier.CompilationReady += async (s, e) =>
{
if (!e.CompilerResult.IsSuccess)
return;
if (!settings.CompileOnSave || !settings.EnableChainCompilation || (bool)s)
return;
Expand Down
5 changes: 4 additions & 1 deletion EditorExtensions/Shared/Compilers/NodeServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ public static void Down()
public static async Task<CompilerResult> CallServiceAsync(string path, bool reattempt = false)
{
await Up();
return await _server.CallService(path, reattempt);
if (_server != null)
return await _server.CallService(path, reattempt);
else
return CompilerResult.GenerateResult(path, "", "", false, "Unable to start node", "", null, false);
}

public NodeServer()
Expand Down
Loading

0 comments on commit 2cf37f3

Please sign in to comment.