Example #1
0
        private void InnerPostProcess(IAsset asset, CssAutoprefixer cssAutoprefixer)
        {
            string         content = asset.Content;
            string         newContent;
            string         assetUrl = asset.Url;
            IList <string> dependencies;

            try
            {
                newContent = !string.IsNullOrWhiteSpace(content) ?
                             cssAutoprefixer.Process(content, assetUrl).ProcessedContent
                                        :
                             content ?? string.Empty
                ;
                dependencies = GetIncludedFilePaths(Stats);
            }
            catch (AutoprefixerProcessingException e)
            {
                string errorDetails = AutoprefixerErrorHelpers.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 AssetPostProcessingException(
                          string.Format(CoreStrings.PostProcessors_PostprocessingSyntaxError,
                                        CODE_TYPE, assetUrl, POSTPROCESSOR_NAME, errorMessage));
            }
            catch (Exception e)
            {
                throw new AssetPostProcessingException(
                          string.Format(CoreStrings.PostProcessors_PostprocessingFailed,
                                        CODE_TYPE, assetUrl, POSTPROCESSOR_NAME, e.Message), e);
            }

            asset.Content = newContent;
            asset.VirtualPathDependencies = asset.VirtualPathDependencies.Union(dependencies).ToList();
        }
Example #2
0
        /// <summary>
        /// Initializes a Autoprefixer
        /// </summary>
        private void Initialize()
        {
            if (_initialized)
            {
                return;
            }

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

                try
                {
                    _jsEngine.EmbedHostObject(COUNTRY_STATISTICS_SERVICE_VARIABLE_NAME,
                                              CountryStatisticsService.Instance);

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

                    _jsEngine.ExecuteResource(ResourceHelpers.GetResourceName(AUTOPREFIXER_LIBRARY_FILE_NAME),
                                              assembly);
                    _jsEngine.ExecuteResource(ResourceHelpers.GetResourceName(AUTOPREFIXER_HELPER_FILE_NAME),
                                              assembly);
                    _jsEngine.Execute($"var autoprefixerHelper = new AutoprefixerHelper({_serializedOptions});");
                }
                catch (JsException e)
                {
                    throw AutoprefixerErrorHelpers.WrapAutoprefixerLoadException(e, true);
                }

                _initialized = true;
            }
        }
Example #3
0
        /// <summary>
        /// Constructs an instance of the Autoprefixer
        /// </summary>
        /// <param name="jsEngineFactory">JS engine factory</param>
        /// <param name="options">Processing options</param>
        public Autoprefixer(IJsEngineFactory jsEngineFactory, ProcessingOptions options)
        {
            if (jsEngineFactory == null)
            {
                throw new ArgumentNullException(nameof(jsEngineFactory));
            }

            _options           = options ?? _defaultOptions;
            _serializedOptions = SerializeProcessingOptions(_options);

            try
            {
                _jsEngine = jsEngineFactory.CreateEngine();
            }
            catch (JsEngineLoadException e)
            {
                throw AutoprefixerErrorHelpers.WrapAutoprefixerLoadException(e);
            }
            catch (Exception e)
            {
                throw AutoprefixerErrorHelpers.WrapAutoprefixerLoadException(e, true);
            }
        }
Example #4
0
        /// <summary>
        /// Constructs an instance of the Autoprefixer
        /// </summary>
        /// <param name="createJsEngineInstance">Delegate that creates an instance of JS engine</param>
        /// <param name="options">Processing options</param>
        public Autoprefixer(Func <IJsEngine> createJsEngineInstance, ProcessingOptions options)
        {
            if (createJsEngineInstance == null)
            {
                throw new ArgumentNullException(nameof(createJsEngineInstance));
            }

            _options           = options ?? _defaultOptions;
            _serializedOptions = SerializeProcessingOptions(_options);

            try
            {
                _jsEngine = createJsEngineInstance();
            }
            catch (JsEngineLoadException e)
            {
                throw AutoprefixerErrorHelpers.WrapAutoprefixerLoadException(e);
            }
            catch (Exception e)
            {
                throw AutoprefixerErrorHelpers.WrapAutoprefixerLoadException(e, true);
            }
        }
Example #5
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 = AutoprefixerErrorHelpers.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);
        }