Пример #1
0
        public static void CompileFile(SassFileContext fileContext)
        {
            Sass_File_Context file_ctx         = Sass_Api.sass_make_file_context(fileContext.InputPath);
            Sass_Options      file_ctx_options = Sass_Api.sass_file_context_get_options(file_ctx);

            FillUnmanagedContextOptions(ref file_ctx_options, fileContext);

            try
            {
                // Compile Sass file by using context
                int result = Sass_Api.sass_compile_file_context(file_ctx);

                // Copy resulting fields from unmanaged object to managed
                Sass_Context base_ctx = Sass_Api.sass_file_context_get_context(file_ctx);
                if (result == 0)
                {
                    FillManagedContextOutput(fileContext, ref base_ctx);
                }
                else
                {
                    FillManagedContextError(fileContext, ref base_ctx);
                }
            }
            finally
            {
                // Free resources
                Sass_Api.sass_delete_file_context(file_ctx);
            }
        }
Пример #2
0
        public CompileFileResult CompileFile(string inputPath, OutputStyle outputStyle = OutputStyle.Nested, string sourceMapPath = null, bool includeSourceComments = true, int precision = 5, IEnumerable <string> additionalIncludePaths = null)
        {
            string        directoryName = Path.GetDirectoryName(inputPath);
            List <string> includePaths  = new List <string> {
                directoryName
            };

            if (additionalIncludePaths != null)
            {
                includePaths.AddRange(additionalIncludePaths);
            }

            SassFileContext context = new SassFileContext
            {
                // libsass 3.0 expects utf8 path string, but strings in .NET are utf16, so we need to convert it
                InputPath = Utf16ToUtf8(inputPath),
                Options   = new SassOptions
                {
                    OutputStyle           = (int)outputStyle,
                    IncludeSourceComments = includeSourceComments,
                    IncludePaths          = String.Join(";", includePaths),
                    Precision             = precision
                },
                OutputSourceMapFile = sourceMapPath
            };

            _sassInterface.Compile(context);

            if (context.ErrorStatus)
            {
                throw new SassCompileException(context.ErrorMessage);
            }

            return(new CompileFileResult(context.OutputString, context.OutputSourceMap));
        }
Пример #3
0
        /// <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 static CompilationResult CompileFile(string inputPath, string outputPath = null,
                                                    string sourceMapPath = null, CompilationOptions options = null)
        {
            if (inputPath == null)
            {
                throw new ArgumentNullException(
                          nameof(inputPath),
                          string.Format(Strings.Common_ArgumentIsNull, nameof(inputPath))
                          );
            }

            if (string.IsNullOrWhiteSpace(inputPath))
            {
                throw new ArgumentException(
                          string.Format(Strings.Common_ArgumentIsEmpty, nameof(inputPath)),
                          nameof(inputPath)
                          );
            }

            var  fileContext    = new SassFileContext();
            bool indentedSyntax = GetIndentedSyntax(inputPath);

            BeginCompile(fileContext, indentedSyntax, inputPath, outputPath, sourceMapPath, options);

            try
            {
                SassCompilerProxy.CompileFile(fileContext);
            }
            catch (FileNotFoundException e)
            {
                string filePath = e.FileName;
                string text     = string.Format("File to read not found or unreadable: {0}", filePath);
                string message  = string.Format("Internal Error: {0}", text);

                throw new SassСompilationException(message, e)
                      {
                          Status       = 3,
                          Text         = text,
                          File         = null,
                          LineNumber   = -1,
                          ColumnNumber = -1,
                          Source       = null
                      };
            }

            CompilationResult result = EndCompile(fileContext);

            return(result);
        }
Пример #4
0
        public string CompileFile(string inputPath, OutputStyle outputStyle = OutputStyle.Nested, bool sourceComments = true, IEnumerable <string> additionalIncludePaths = null)
        {
            if (outputStyle != OutputStyle.Nested && outputStyle != OutputStyle.Compressed)
            {
                throw new ArgumentException("Only nested and compressed output styles are currently supported by libsass.");
            }

            string        directoryName = Path.GetDirectoryName(inputPath);
            List <string> includePaths  = new List <string> {
                directoryName
            };

            if (additionalIncludePaths != null)
            {
                includePaths.AddRange(additionalIncludePaths);
            }

            SassFileContext context = new SassFileContext
            {
                InputPath = inputPath,
                Options   = new SassOptions
                {
                    OutputStyle         = (int)outputStyle,
                    SourceComments      = sourceComments,
                    SourceMapFile       = String.Empty,
                    OmitSourceMapUrl    = true,
                    SourceMapEmbed      = false,
                    SourceMapContents   = false,
                    SourceMapRoot       = String.Empty,
                    IsIndentedSyntaxSrc = false,
                    IncludePaths        = String.Join(";", includePaths),
                    PluginPaths         = String.Empty,
                    Indent    = String.Empty,
                    Linefeed  = String.Empty,
                    Precision = 0
                }
            };

            _sassInterface.Compile(context);

            if (context.ErrorStatus)
            {
                throw new SassCompileException(context.ErrorMessage);
            }

            return(context.OutputString);
        }
        public static void CompileFile(SassFileContext fileContext)
        {
            Sass_File_Context file_ctx         = Sass_Api.sass_make_file_context(fileContext.InputPath);
            Sass_Options      file_ctx_options = Sass_Api.sass_file_context_get_options(file_ctx);

            FillUnmanagedContextOptions(ref file_ctx_options, fileContext);

            try
            {
                // Compile Sass-file by using context
                int result = Sass_Api.sass_compile_file_context(file_ctx);

                // Copy resulting fields from unmanaged object to managed
                Sass_Context base_ctx = Sass_Api.sass_file_context_get_context(file_ctx);
                if (result == 0)
                {
                    FillManagedContextOutput(fileContext, ref base_ctx);
                }
                else
                {
                    FillManagedContextError(fileContext, ref base_ctx);
                }
            }
            catch (TargetInvocationException e)
            {
                Exception innerException = e.InnerException;
                if (innerException != null)
                {
#if NET45 || NETSTANDARD
                    ExceptionDispatchInfo.Capture(innerException).Throw();
#elif NET40
                    innerException.PreserveStackTrace();
                    throw innerException;
#else
#error No implementation for this target
#endif
                }

                throw;
            }
            finally
            {
                // Free resources
                Sass_Api.sass_delete_file_context(file_ctx);
            }
        }
Пример #6
0
        public CompileFileResult CompileFile(string inputPath, OutputStyle outputStyle = OutputStyle.Nested, string sourceMapPath = null, SourceCommentsMode sourceComments = SourceCommentsMode.Default, int precision = 5, IEnumerable <string> additionalIncludePaths = null)
        {
            if (outputStyle != OutputStyle.Nested && outputStyle != OutputStyle.Compressed)
            {
                throw new ArgumentException("Only nested and compressed output styles are currently supported by libsass.");
            }

            string        directoryName = Path.GetDirectoryName(inputPath);
            List <string> includePaths  = new List <string> {
                directoryName
            };

            if (additionalIncludePaths != null)
            {
                includePaths.AddRange(additionalIncludePaths);
            }

            SassFileContext context = new SassFileContext
            {
                // libsass 3.0 expects utf8 path string, but strings in .NET are utf16, so we need to convert it
                InputPath = Utf16ToUtf8(inputPath),
                Options   = new SassOptions
                {
                    OutputStyle        = (int)outputStyle,
                    SourceCommentsMode = (int)sourceComments,
                    IncludePaths       = String.Join(";", includePaths),
                    ImagePath          = string.Empty,
                    Precision          = precision
                },
                OutputSourceMapFile = sourceMapPath
            };

            _sassInterface.Compile(context);

            if (context.ErrorStatus)
            {
                throw new SassCompileException(context.ErrorMessage);
            }

            return(new CompileFileResult(context.OutputString, context.OutputSourceMap));
        }
Пример #7
0
        public CompileFileResult CompileFile(string inputPath, OutputStyle outputStyle = OutputStyle.Nested, string sourceMapPath = null, bool includeSourceComments = true, int precision = 5, IEnumerable<string> additionalIncludePaths = null)
        {

            string directoryName = Path.GetDirectoryName(inputPath);
            List<string> includePaths = new List<string> { directoryName };
            if (additionalIncludePaths != null)
            {
                includePaths.AddRange(additionalIncludePaths);
            }

            SassFileContext context = new SassFileContext
            {
                // libsass 3.0 expects utf8 path string, but strings in .NET are utf16, so we need to convert it
                InputPath = Utf16ToUtf8(inputPath),
                Options = new SassOptions
                {
                    OutputStyle = (int)outputStyle,
                    IncludeSourceComments = includeSourceComments,
                    IncludePaths = String.Join(";", includePaths),
                    Precision = precision,
                    LineFeed = OutputLineFeed ?? (OutputLineFeed = "\r\n"),
                    OmitSourceMappingUrl = OmitSourceMappingUrl
                },
                OutputSourceMapFile = sourceMapPath
            };

            _sassInterface.Compile(context);

            if (context.ErrorStatus)
            {
                throw new SassCompileException(context.ErrorMessage);
            }

            return new CompileFileResult(context.OutputString, context.OutputSourceMap);
        }
Пример #8
0
        /// <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="SassCompilationException">Sass compilation error.</exception>
        public static CompilationResult CompileFile(string inputPath, string outputPath = null,
                                                    string sourceMapPath = null, CompilationOptions options = null)
        {
            if (inputPath == null)
            {
                throw new ArgumentNullException(
                          nameof(inputPath),
                          string.Format(Strings.Common_ArgumentIsNull, nameof(inputPath))
                          );
            }

            if (string.IsNullOrWhiteSpace(inputPath))
            {
                throw new ArgumentException(
                          string.Format(Strings.Common_ArgumentIsEmpty, nameof(inputPath)),
                          nameof(inputPath)
                          );
            }

            Initialize();

            IFileManager fileManager = _fileManager;

            if (fileManager != null && !fileManager.FileExists(inputPath))
            {
                string description = string.Format("File to read not found or unreadable: {0}", inputPath);
                string message     = string.Format("Internal Error: {0}", description);

                throw new SassCompilationException(message)
                      {
                          ErrorCode      = 3,
                          Description    = description,
                          File           = null,
                          LineNumber     = -1,
                          ColumnNumber   = -1,
                          SourceFragment = null
                      };
            }

            CompilationResult result;
            var  fileContext    = new SassFileContext();
            bool indentedSyntax = GetIndentedSyntax(inputPath);

            BeginCompile(fileContext, indentedSyntax, inputPath, outputPath, sourceMapPath, options);

            try
            {
                _mutex.WaitOne();
                FileManagerMarshaler.SetFileManager(fileManager);
                SassCompilerProxy.CompileFile(fileContext);
            }
            finally
            {
                FileManagerMarshaler.UnsetFileManager();
                _mutex.ReleaseMutex();
            }

            GC.KeepAlive(fileManager);

            result = EndCompile(fileContext);

            return(result);
        }
Пример #9
0
        /// <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="options">Compilation options</param>
        /// <returns>Compilation result</returns>
        public CompilationResult CompileFile(string inputPath, string outputPath = null, CompilationOptions options = null)
        {
            if (string.IsNullOrWhiteSpace(inputPath))
            {
                throw new ArgumentException(string.Format(Strings.Common_ArgumentIsEmpty, "inputPath"), "inputPath");
            }

            CompilationResult result;
            var fileContext = new SassFileContext();

            lock (_compilationSynchronizer)
            {
                if (!_fileManager.FileExists(inputPath))
                {
                    throw new FileNotFoundException(string.Format(Strings.Common_FileNotExist, inputPath), inputPath);
                }

                BeginCompile(fileContext, inputPath, outputPath, options);

                _sassNativeCompiler.CompileFile(fileContext);

                result = EndCompile(fileContext);
            }

            return result;
        }
Пример #10
0
        public string CompileFile(string inputPath, OutputStyle outputStyle = OutputStyle.Nested, bool sourceComments = true, IEnumerable<string> additionalIncludePaths = null)
        {
            if (outputStyle != OutputStyle.Nested && outputStyle != OutputStyle.Compressed)
            {
                throw new ArgumentException("Only nested and compressed output styles are currently supported by libsass.");
            }

            string directoryName = Path.GetDirectoryName(inputPath);
            List<string> includePaths = new List<string> { directoryName };
            if (additionalIncludePaths != null)
            {
                includePaths.AddRange(additionalIncludePaths);
            }

            SassFileContext context = new SassFileContext
            {
                InputPath = inputPath,
                Options = new SassOptions
                {
                    OutputStyle = (int)outputStyle,
                    SourceComments = sourceComments,
                    IncludePaths = String.Join(";", includePaths),
                    ImagePath = String.Empty
                }
            };

            _sassInterface.Compile(context);

            if (context.ErrorStatus)
            {
                throw new SassCompileException(context.ErrorMessage);
            }

            return context.OutputString;
        }