Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow compilation of sass via string parameters instead of solely file-based sass compilation. #56

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*.log
*.lib
*.ncb
*.ncrunchsolution
*.obj
*.opensdf
*.orig
Expand All @@ -31,6 +32,7 @@
[Dd]ebug/
[Rr]elease/
_ReSharper
_ReSharper.*
TestResults/
packages/

Expand Down
38 changes: 29 additions & 9 deletions SassAndCoffee.Core.Tests/SassFileCompilerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
using Xunit;

public class SassFileCompilerTest {
[Fact]
public void ScssSmokeTest() {
var input = @"
private readonly ISassCompiler _fixture;
private const string GOOD_SCSS_INPUT = @"
// SCSS

.error {
Expand All @@ -24,23 +23,44 @@ public void ScssSmokeTest() {
border-width: 3px;
}
";
var actual = compileInput("test.scss", input);

private const string BAD_SASS_INPUT = ".foo bar[val=\"//\"] { baz: bang; }";

public SassFileCompilerTest() {
_fixture = new SassCompiler();
}

[Fact]
public void ScssSmokeTest() {
var actual = compileInput("test.scss", GOOD_SCSS_INPUT);
Assert.False(string.IsNullOrWhiteSpace(actual));
}

[Fact]
public void SassNegativeSmokeTest() {
var input = ".foo bar[val=\"//\"] { baz: bang; }";
try {
compileInput("test.sass", input);
compileInput("test.sass", BAD_SASS_INPUT);
} catch (Exception e) {
Assert.True(e.ToString().Contains("Syntax"));
}
}

string compileInput(string filename, string input) {
var fixture = new SassCompiler();
[Fact]
public void ScssCompileAsStringTest() {
var actual = _fixture.CompileScss(GOOD_SCSS_INPUT, true);
Assert.False(string.IsNullOrWhiteSpace(actual));
}

[Fact]
public void SassNegativeCompileAsStringTest() {
try {
_fixture.CompileSass(BAD_SASS_INPUT, true);
} catch (Exception e) {
Assert.True(e.ToString().Contains("Syntax"));
}
}

string compileInput(string filename, string input) {
using (var of = File.CreateText(filename)) {
of.WriteLine(input);
}
Expand All @@ -49,7 +69,7 @@ string compileInput(string filename, string input) {

// TODO: Fix this
// fixture.Init(TODO);
string result = fixture.Compile(filename, true, null);
string result = _fixture.Compile(filename, true, null);
Console.WriteLine(result);
return result;
} finally {
Expand Down
18 changes: 10 additions & 8 deletions SassAndCoffee.Ruby/Sass/ISassCompiler.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
namespace SassAndCoffee.Ruby.Sass {
using System;
using System.Collections.Generic;

public interface ISassCompiler : IDisposable {
string Compile(string path, bool compressed, IList<string> dependentFileList);
}
}
namespace SassAndCoffee.Ruby.Sass {
using System;
using System.Collections.Generic;

public interface ISassCompiler : IDisposable {
string Compile(string path, bool compressed, IList<string> dependentFileList);
string CompileScss(string input, bool compressed);
string CompileSass(string input, bool compressed);
}
}
72 changes: 60 additions & 12 deletions SassAndCoffee.Ruby/Sass/SassCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,26 +57,74 @@ public string Compile(string path, bool compressed, IList<string> dependentFileL
result = (string)_sassCompiler.compile(File.ReadAllText(pathInfo.FullName), compilerOptions);
} catch (Exception e) {
// Provide more information for SassSyntaxErrors
if (e.Message == "Sass::SyntaxError") {
dynamic error = e;
StringBuilder sb = new StringBuilder();
sb.AppendFormat("{0}\n\n", error.to_s());
sb.AppendFormat("Backtrace:\n{0}\n\n", error.sass_backtrace_str(pathInfo.FullName) ?? "");
sb.AppendFormat("FileName: {0}\n\n", error.sass_filename() ?? pathInfo.FullName);
sb.AppendFormat("MixIn: {0}\n\n", error.sass_mixin() ?? "");
sb.AppendFormat("Line Number: {0}\n\n", error.sass_line() ?? "");
sb.AppendFormat("Sass Template:\n{0}\n\n", error.sass_template ?? "");
throw new Exception(sb.ToString(), e);
} else {
if (e.Message == "Sass::SyntaxError") {
var messageFromException = buildErrorMessageFromException(pathInfo, e);
throw new Exception(messageFromException, e);
}
else {
throw;
}
} finally {
_pal.OnOpenInputFileStream = null;
}
return result;
}
}
}

public string CompileScss(string input, bool compressed) {
lock (_lock) {
Initialize();

string result;
try {
result = (string) _sassCompiler.compile(input, compressed ? _scssOptionCompressed : _scssOption);
}
catch (Exception e) {
// Provide more information for SassSyntaxErrors
if (e.Message == "Sass::SyntaxError") {
var messageFromException = buildErrorMessageFromException(null, e);
throw new Exception(messageFromException, e);
}

throw;
}
return result;
}
}

public string CompileSass(string input, bool compressed) {
lock (_lock) {
Initialize();

string result;
try {
result = (string) _sassCompiler.compile(input, compressed ? _sassOptionCompressed : _sassOption);
}
catch (Exception e) {
// Provide more information for SassSyntaxErrors
if (e.Message == "Sass::SyntaxError") {
var messageFromException = buildErrorMessageFromException(null, e);
throw new Exception(messageFromException, e);
}

throw;
}
return result;
}
}

private static string buildErrorMessageFromException(FileSystemInfo pathInfo, dynamic error) {
var fullName = pathInfo != null ? pathInfo.FullName : "No file";
var sb = new StringBuilder();
sb.AppendFormat("{0}\n\n", error.to_s());
sb.AppendFormat("Backtrace:\n{0}\n\n", error.sass_backtrace_str(fullName) ?? "");
sb.AppendFormat("FileName: {0}\n\n", error.sass_filename() ?? fullName);
sb.AppendFormat("MixIn: {0}\n\n", error.sass_mixin() ?? "");
sb.AppendFormat("Line Number: {0}\n\n", error.sass_line() ?? "");
sb.AppendFormat("Sass Template:\n{0}\n\n", error.sass_template ?? "");
return sb.ToString();
}

private void Initialize() {
if (!_initialized) {
_pal = new ResourceRedirectionPlatformAdaptationLayer();
Expand Down
40 changes: 24 additions & 16 deletions SassAndCoffee.Ruby/Sass/SassCompilerProxy.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
namespace SassAndCoffee.Ruby.Sass {
using System.Collections.Generic;
using SassAndCoffee.Core;

public class SassCompilerProxy : ProxyBase<ISassCompiler>, ISassCompiler {

public SassCompilerProxy() { }

public SassCompilerProxy(ISassCompiler compiler)
: base(compiler) { }

public string Compile(string path, bool compressed, IList<string> dependentFileList) {
return WrappedItem.Compile(path, compressed, dependentFileList);
}
}
}
namespace SassAndCoffee.Ruby.Sass {
using System.Collections.Generic;
using SassAndCoffee.Core;

public class SassCompilerProxy : ProxyBase<ISassCompiler>, ISassCompiler {

public SassCompilerProxy() { }

public SassCompilerProxy(ISassCompiler compiler)
: base(compiler) { }

public string Compile(string path, bool compressed, IList<string> dependentFileList) {
return WrappedItem.Compile(path, compressed, dependentFileList);
}

public string CompileScss(string input, bool compressed) {
return WrappedItem.CompileScss(input, compressed);
}

public string CompileSass(string input, bool compressed) {
return WrappedItem.CompileSass(input, compressed);
}
}
}