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

Multiple file managers #13

Closed
jws305 opened this issue Mar 6, 2017 · 2 comments
Closed

Multiple file managers #13

jws305 opened this issue Mar 6, 2017 · 2 comments

Comments

@jws305
Copy link

jws305 commented Mar 6, 2017

I have two separate subsystems in which I use this library to compile Sass. The subsystems are completely disjoint and require different file managers. Since the SassCompiler is now static, how can I support this use case?

@Taritsyn
Copy link
Owner

Taritsyn commented Mar 7, 2017

Hello, John!

In fact, and earlier the static classes was used for these purposes. You just didn't see it due to the tricky wrapper.

This approach has been criticized, and therefore I had to convert the compiler to a static class and refuse locks.

You can use a wrapper with locks:

using System;

using LibSassHost;

namespace LibSassHost.Wrapper
{
	/// <summary>
	/// Sass-compiler wrapper
	/// </summary>
	public sealed class SassCompilerWrapper
	{
		/// <summary>
		/// Instance of file manager
		/// </summary>
		private readonly IFileManager _fileManager;

		/// <summary>
		/// Synchronizer of compilation
		/// </summary>
		private static readonly object _compilationSynchronizer = new object();


		/// <summary>
		/// Constructs an instance of Sass-compiler wrapper
		/// </summary>
		public SassCompilerWrapper()
			: this(null)
		{ }

		/// <summary>
		/// Constructs an instance of Sass-compiler wrapper
		/// </summary>
		/// <param name="fileManager">File manager</param>
		public SassCompilerWrapper(IFileManager fileManager)
		{
			_fileManager = fileManager;
		}


		/// <summary>
		/// "Compiles" a Sass-code to CSS-code
		/// </summary>
		/// <param name="content">Text content written on Sass</param>
		/// <param name="options">Compilation options</param>
		/// <returns>Compilation result</returns>
		/// <exception cref="ArgumentException"/>
		/// <exception cref="ArgumentNullException" />
		/// <exception cref="SassСompilationException">Sass compilation error.</exception>
		public CompilationResult Compile(string content, CompilationOptions options = null)
		{
			CompilationResult result;

			lock (_compilationSynchronizer)
			{
				SassCompiler.FileManager = _fileManager;

				try
				{
					result = SassCompiler.Compile(content, options);
				}
				finally
				{
					SassCompiler.FileManager = null;
				}
			}

			return result;
		}

		/// <summary>
		/// "Compiles" a Sass-code to CSS-code
		/// </summary>
		/// <param name="content">Text content written on Sass</param>
		/// <param name="indentedSyntax">Flag for whether to enable Sass Indented Syntax
		/// for parsing the data string</param>
		/// <param name="options">Compilation options</param>
		/// <returns>Compilation result</returns>
		/// <exception cref="ArgumentException"/>
		/// <exception cref="ArgumentNullException" />
		/// <exception cref="SassСompilationException">Sass compilation error.</exception>
		public CompilationResult Compile(string content, bool indentedSyntax, CompilationOptions options = null)
		{
			CompilationResult result;

			lock (_compilationSynchronizer)
			{
				SassCompiler.FileManager = _fileManager;

				try
				{
					result = SassCompiler.Compile(content, indentedSyntax, options);
				}
				finally
				{
					SassCompiler.FileManager = null;
				}
			}

			return result;
		}

		/// <summary>
		/// "Compiles" a Sass-code to CSS-code
		/// </summary>
		/// <param name="content">Text content written on Sass</param>
		/// <param name="inputPath">Path to input file</param>
		/// <param name="outputPath">Path to output file</param>
		/// <param name="sourceMapPath">Path to source map file</param>
		/// <param name="options">Compilation options</param>
		/// <returns>Compilation result</returns>
		/// <exception cref="ArgumentException"/>
		/// <exception cref="ArgumentNullException" />
		/// <exception cref="SassСompilationException">Sass compilation error.</exception>
		public CompilationResult Compile(string content, string inputPath, string outputPath = null,
			string sourceMapPath = null, CompilationOptions options = null)
		{
			CompilationResult result;

			lock (_compilationSynchronizer)
			{
				SassCompiler.FileManager = _fileManager;

				try
				{
					result = SassCompiler.Compile(content, inputPath, outputPath, sourceMapPath, options);
				}
				finally
				{
					SassCompiler.FileManager = null;
				}
			}

			return result;
		}

		/// <summary>
		/// "Compiles" a Sass-file to CSS-code
		/// </summary>
		/// <param name="inputPath">Path to input file</param>
		/// <param name="outputPath">Path to output file</param>
		/// <param name="sourceMapPath">Path to source map file</param>
		/// <param name="options">Compilation options</param>
		/// <returns>Compilation result</returns>
		/// <exception cref="ArgumentException"/>
		/// <exception cref="ArgumentNullException" />
		/// <exception cref="SassСompilationException">Sass compilation error.</exception>
		public CompilationResult CompileFile(string inputPath, string outputPath = null,
			string sourceMapPath = null, CompilationOptions options = null)
		{
			CompilationResult result;

			lock (_compilationSynchronizer)
			{
				SassCompiler.FileManager = _fileManager;

				try
				{
					result = SassCompiler.CompileFile(inputPath, outputPath, sourceMapPath, options);
				}
				finally
				{
					SassCompiler.FileManager = null;
				}
			}

			return result;
		}
	}
}

@jws305
Copy link
Author

jws305 commented Mar 7, 2017

Thanks for the quick response! This worked great.

@jws305 jws305 closed this as completed Mar 7, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants