Giter VIP home page Giter VIP logo

Comments (2)

Taritsyn avatar Taritsyn commented on June 19, 2024

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

from libsasshost.

jws305 avatar jws305 commented on June 19, 2024

Thanks for the quick response! This worked great.

from libsasshost.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.