private string BuildSassFile(string inputFilePath, string outputFilePath, string sourceMapFilePath, bool buildSourceMap = false)
        {
            try
            {
                var options = new CompilationOptions
                {
                    SourceMap = buildSourceMap
                };

                var result = SassCompiler.CompileFile(inputFilePath, outputFilePath, sourceMapFilePath, options);
                _fileService.SaveFile(outputFilePath, result.CompiledContent);
                if (buildSourceMap && sourceMapFilePath.HasValue())
                {
                    _fileService.SaveFile(sourceMapFilePath, result.SourceMap);
                }
            }
            catch (SassCompilationException ex)
            {
                var errorDetails = SassErrorHelpers.GenerateErrorDetails(ex);
                _logger.Error <SassService>(ex, errorDetails);
                return(ex.Message);
            }

            return(string.Empty);
        }
 private static void WriteError(string title, SassСompilationException exception)
 {
     Console.WriteLine("{0} See details:", title);
     Console.WriteLine();
     Console.WriteLine(SassErrorHelpers.Format(exception));
     Console.WriteLine();
 }
Beispiel #3
0
        private void InnerTranslate(IAsset asset, CompilationOptions sassOptions, bool enableNativeMinification)
        {
            string         assetTypeName = asset.AssetTypeCode == Constants.AssetTypeCode.Sass ? "Sass" : "SCSS";
            string         newContent;
            string         assetUrl = asset.Url;
            IList <string> dependencies;

            try
            {
                CompilationResult result = SassCompiler.Compile(asset.Content, assetUrl, options: sassOptions);
                newContent   = result.CompiledContent;
                dependencies = result.IncludedFilePaths;
            }
            catch (SassСompilationException e)
            {
                throw new AssetTranslationException(
                          string.Format(CoreStrings.Translators_TranslationSyntaxError,
                                        assetTypeName, OUTPUT_CODE_TYPE, assetUrl, SassErrorHelpers.Format(e)));
            }
            catch (Exception e)
            {
                throw new AssetTranslationException(
                          string.Format(CoreStrings.Translators_TranslationFailed,
                                        assetTypeName, OUTPUT_CODE_TYPE, assetUrl, e.Message), e);
            }

            asset.Content  = newContent;
            asset.Minified = enableNativeMinification;
            asset.RelativePathsResolved   = false;
            asset.VirtualPathDependencies = dependencies;
        }
        private static Exception CreateUnexpectedException(JsonTextReader reader, string expected)
        {
            string description = string.Format(Strings.JsonReader_UnexpectedCharacterEncountered, expected);
            string message     = SassErrorHelpers.GenerateCompilationErrorMessage("JsonReaderError", description,
                                                                                  string.Empty, reader.LineNumber, reader.LinePosition, reader.Path);

            return(new FormatException(message));
        }
Beispiel #5
0
 /// <summary>
 /// Static constructor
 /// </summary>
 /// <exception cref="SassCompilerLoadException">Failed to load a Sass-compiler.</exception>
 static SassCompiler()
 {
     if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
     {
         try
         {
             AssemblyResolver.Initialize();
         }
         catch (InvalidOperationException e)
         {
             throw SassErrorHelpers.WrapCompilerLoadException(e);
         }
     }
 }
Beispiel #6
0
        /// <summary>
        /// Initializes a Sass compiler
        /// </summary>
        private static void Initialize()
        {
            if (_initialized)
            {
                return;
            }

            lock (_initializationSynchronizer)
            {
                if (_initialized)
                {
                    return;
                }

                try
                {
                    _version         = SassCompilerProxy.GetVersion();
                    _languageVersion = SassCompilerProxy.GetLanguageVersion();
                }
                catch (DllNotFoundException e)
                {
                    throw WrapDllNotFoundException(e);
                }
#if NETSTANDARD1_3
                catch (TypeLoadException e)
#else
                catch (EntryPointNotFoundException e)
#endif
                {
                    string message = e.Message;
                    if (message.ContainsQuotedValue(DllName.Universal) &&
                        (message.ContainsQuotedValue("libsass_version") || message.ContainsQuotedValue("libsass_language_version")))
                    {
                        _version         = "0.0.0";
                        _languageVersion = "0.0";
                    }
                    else
                    {
                        throw SassErrorHelpers.WrapCompilerLoadException(e, true);
                    }
                }
                catch (Exception e)
                {
                    throw SassErrorHelpers.WrapCompilerLoadException(e, true);
                }

                _initialized = true;
            }
        }
Beispiel #7
0
        /// <summary>
        /// Initializes a Sass compiler
        /// </summary>
        private void InitializeCompiler()
        {
            if (_initialized)
            {
                return;
            }

            lock (_initializationSynchronizer)
            {
                if (_initialized)
                {
                    return;
                }

                string serializedOptions = _jsonSerializer.SerializeObject(_options);

                try
                {
                    _jsEngine = _createJsEngineInstance();
                    _jsEngine.EmbedHostObject(FILE_MANAGER_VARIABLE_NAME, _fileManager);
                    _jsEngine.SetVariableValue(CURRENT_OS_PLATFORM_NAME, GetCurrentOSPlatformName());

                    Assembly assembly = this.GetType()
#if !NET40
                                        .GetTypeInfo()
#endif
                                        .Assembly
                    ;

                    _jsEngine.ExecuteResource(ResourceHelpers.GetResourceName(ES6_POLYFILLS_FILE_NAME), assembly, true);
                    _jsEngine.ExecuteResource(ResourceHelpers.GetResourceName(SASS_LIBRARY_FILE_NAME), assembly, true);
                    _jsEngine.ExecuteResource(ResourceHelpers.GetResourceName(SASS_HELPER_FILE_NAME), assembly, true);
                    _jsEngine.Execute($"var sassHelper = new SassHelper({serializedOptions});");

                    _version = _jsEngine.Evaluate <string>("SassHelper.getVersion();");
                }
                catch (JsEngineLoadException e)
                {
                    throw SassErrorHelpers.WrapCompilerLoadException(e);
                }
                catch (JsException e)
                {
                    throw SassErrorHelpers.WrapCompilerLoadException(e, true);
                }

                _initialized = true;
            }
        }
        private static void WriteError(string title, SassException exception)
        {
            Console.WriteLine("{0} See details:", title);
            Console.WriteLine();
            Console.Write(exception.Message);

            string errorDetails = SassErrorHelpers.GenerateErrorDetails(exception, true);

            if (errorDetails.Length > 0)
            {
                Console.WriteLine();
                Console.WriteLine();
                Console.Write(errorDetails);
            }

            Console.WriteLine();
            Console.WriteLine();
        }
 protected static void CompileFile()
 {
     try
     {
         var options = new CompilationOptions {
             SourceMap = true, OutputStyle = OutputStyle.Compressed, SourceMapFileUrls = true
         };
         CompilationResult compilationResult = SassCompiler.Compile(_importingContent, OutputhPath, OutputhmapPath, options: options);
         Console.WriteLine("Compiled content:{1}{1}{0}{1}", compilationResult.CompiledContent,
                           Environment.NewLine);
         Console.WriteLine("Source map:{1}{1}{0}{1}", compilationResult.SourceMap, Environment.NewLine);
         Console.WriteLine("Included file paths: {0}",
                           string.Join(", ", compilationResult.IncludedFilePaths));
     }
     catch (SassСompilationException ex)
     {
         Console.WriteLine(SassErrorHelpers.Format(ex));
     }
 }
        private void InnerTranslate(IAsset asset, CompilationOptions sassOptions, bool enableNativeMinification)
        {
            string         assetTypeName = asset.AssetTypeCode == Constants.AssetTypeCode.Sass ? "Sass" : "SCSS";
            string         newContent;
            string         assetUrl = asset.Url;
            IList <string> dependencies;

            try
            {
                CompilationResult result = SassCompiler.Compile(asset.Content, assetUrl, options: sassOptions);
                newContent   = result.CompiledContent;
                dependencies = result.IncludedFilePaths;
            }
            catch (SassCompilationException e)
            {
                string errorDetails = SassErrorHelpers.GenerateErrorDetails(e, true);

                var           stringBuilderPool   = StringBuilderPool.Shared;
                StringBuilder errorMessageBuilder = stringBuilderPool.Rent();
                errorMessageBuilder.AppendLine(e.Message);
                errorMessageBuilder.AppendLine();
                errorMessageBuilder.Append(errorDetails);

                string errorMessage = errorMessageBuilder.ToString();
                stringBuilderPool.Return(errorMessageBuilder);

                throw new AssetTranslationException(
                          string.Format(CoreStrings.Translators_TranslationSyntaxError,
                                        assetTypeName, OUTPUT_CODE_TYPE, assetUrl, errorMessage));
            }
            catch (Exception e)
            {
                throw new AssetTranslationException(
                          string.Format(CoreStrings.Translators_TranslationFailed,
                                        assetTypeName, OUTPUT_CODE_TYPE, assetUrl, e.Message), e);
            }

            asset.Content  = newContent;
            asset.Minified = enableNativeMinification;
            asset.RelativePathsResolved   = false;
            asset.VirtualPathDependencies = dependencies;
        }
Beispiel #11
0
        /// <summary>
        /// Returns a string that represents the current exception
        /// </summary>
        /// <returns>A string that represents the current exception</returns>
        public override string ToString()
        {
            string errorDetails = SassErrorHelpers.GenerateErrorDetails(this, true);

            var           stringBuilderPool = StringBuilderPool.Shared;
            StringBuilder resultBuilder     = stringBuilderPool.Rent();

            resultBuilder.Append(this.GetType().FullName);
            resultBuilder.Append(": ");
            resultBuilder.Append(this.Message);

            if (errorDetails.Length > 0)
            {
                resultBuilder.AppendLine();
                resultBuilder.AppendLine();
                resultBuilder.Append(errorDetails);
            }

            if (this.InnerException != null)
            {
                resultBuilder.Append(" ---> ");
                resultBuilder.Append(this.InnerException.ToString());
            }

            if (this.StackTrace != null)
            {
                resultBuilder.AppendLine();
                resultBuilder.AppendLine(this.StackTrace);
            }

            string result = resultBuilder.ToString();

            stringBuilderPool.Return(resultBuilder);

            return(result);
        }
Beispiel #12
0
        private static SassCompilerLoadException WrapDllNotFoundException(
            DllNotFoundException originalDllNotFoundException)
        {
            string originalMessage = originalDllNotFoundException.Message;
            string description;
            string message;
            bool   isMonoRuntime = Utils.IsMonoRuntime();

            if ((isMonoRuntime && originalMessage == DllName.Universal) ||
                originalMessage.ContainsQuotedValue(DllName.Universal))
            {
                const string buildInstructionsUrl = "https://github.com/Taritsyn/LibSassHost#{0}";
                const string manualInstallationInstructionsUrl = "https://github.com/Taritsyn/LibSassHost#{0}";
                Architecture osArchitecture = RuntimeInformation.OSArchitecture;

                var           stringBuilderPool  = StringBuilderPool.Shared;
                StringBuilder descriptionBuilder = stringBuilderPool.Rent();
                if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                {
                    descriptionBuilder.AppendFormat(Strings.Compiler_AssemblyNotFound, DllName.ForWindows);
                    descriptionBuilder.Append(" ");
                    if (osArchitecture == Architecture.X64 || osArchitecture == Architecture.X86)
                    {
                        descriptionBuilder.AppendFormat(Strings.Compiler_NuGetPackageInstallationRequired,
                                                        Utils.Is64BitProcess() ?
                                                        "LibSassHost.Native.win-x64"
                                                                :
                                                        "LibSassHost.Native.win-x86"
                                                        );
                    }
                    else
                    {
                        descriptionBuilder.AppendFormat(Strings.Compiler_NoNuGetPackageForProcessorArchitecture,
                                                        "LibSassHost.Native.win-*",
                                                        osArchitecture.ToString().ToLowerInvariant()
                                                        );
                        descriptionBuilder.Append(" ");
                        descriptionBuilder.AppendFormat(Strings.Compiler_BuildNativeAssemblyForCurrentProcessorArchitecture,
                                                        DllName.ForWindows,
                                                        string.Format(buildInstructionsUrl, "windows")
                                                        );
                    }
                }
                else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
                {
                    descriptionBuilder.AppendFormat(Strings.Compiler_AssemblyNotFound, DllName.ForLinux);
                    descriptionBuilder.Append(" ");
                    if (isMonoRuntime)
                    {
                        descriptionBuilder.AppendFormat(Strings.Compiler_ManualInstallationUnderMonoRequired,
                                                        "LibSassHost.Native.linux-*",
                                                        string.Format(manualInstallationInstructionsUrl, "linux")
                                                        );
                    }
                    else
                    {
                        if (osArchitecture == Architecture.X64)
                        {
                            descriptionBuilder.AppendFormat(Strings.Compiler_NuGetPackageInstallationRequired,
                                                            "LibSassHost.Native.linux-x64");
                        }
                        else
                        {
                            descriptionBuilder.AppendFormat(Strings.Compiler_NoNuGetPackageForProcessorArchitecture,
                                                            "LibSassHost.Native.linux-*",
                                                            osArchitecture.ToString().ToLowerInvariant()
                                                            );
                            descriptionBuilder.Append(" ");
                            descriptionBuilder.AppendFormat(Strings.Compiler_BuildNativeAssemblyForCurrentProcessorArchitecture,
                                                            DllName.ForLinux,
                                                            string.Format(buildInstructionsUrl, "linux-1")
                                                            );
                        }
                    }
                }
                else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
                {
                    descriptionBuilder.AppendFormat(Strings.Compiler_AssemblyNotFound, DllName.ForOsx);
                    descriptionBuilder.Append(" ");
                    if (isMonoRuntime)
                    {
                        descriptionBuilder.AppendFormat(Strings.Compiler_ManualInstallationUnderMonoRequired,
                                                        "LibSassHost.Native.osx-*",
                                                        string.Format(manualInstallationInstructionsUrl, "os-x")
                                                        );
                    }
                    else
                    {
                        if (osArchitecture == Architecture.X64)
                        {
                            descriptionBuilder.AppendFormat(Strings.Compiler_NuGetPackageInstallationRequired,
                                                            "LibSassHost.Native.osx-x64");
                        }
                        else
                        {
                            descriptionBuilder.AppendFormat(Strings.Compiler_NoNuGetPackageForProcessorArchitecture,
                                                            "LibSassHost.Native.osx-*",
                                                            osArchitecture.ToString().ToLowerInvariant()
                                                            );
                            descriptionBuilder.Append(" ");
                            descriptionBuilder.AppendFormat(Strings.Compiler_BuildNativeAssemblyForCurrentProcessorArchitecture,
                                                            DllName.ForOsx,
                                                            string.Format(buildInstructionsUrl, "os-x-1")
                                                            );
                        }
                    }
                }
                else
                {
                    descriptionBuilder.Append(Strings.Compiler_OperatingSystemNotSupported);
                }

                description = descriptionBuilder.ToString();
                stringBuilderPool.Return(descriptionBuilder);

                message = SassErrorHelpers.GenerateCompilerLoadErrorMessage(description);
            }
            else
            {
                description = originalMessage;
                message     = SassErrorHelpers.GenerateCompilerLoadErrorMessage(description, true);
            }

            var wrapperEngineLoadException = new SassCompilerLoadException(message, originalDllNotFoundException)
            {
                Description = description
            };

            return(wrapperEngineLoadException);
        }