/// <summary> /// Generates a detailed error message /// </summary> /// <param name="errorDetails">Error details</param> /// <param name="sourceCode">Source code</param> /// <param name="currentFilePath">Path to current LESS-file</param> /// <param name="dependencies">List of dependencies</param> /// <returns>Detailed error message</returns> private static string FormatErrorDetails(JToken errorDetails, string sourceCode, string currentFilePath, DependencyCollection dependencies) { var type = errorDetails.Value<string>("type"); var message = errorDetails.Value<string>("message"); var filePath = errorDetails.Value<string>("fileName"); if (string.IsNullOrWhiteSpace(filePath)) { filePath = currentFilePath; } var lineNumber = errorDetails.Value<int>("lineNumber"); var columnNumber = errorDetails.Value<int>("columnNumber"); string newSourceCode = string.Empty; if (string.Equals(filePath, currentFilePath, StringComparison.OrdinalIgnoreCase)) { newSourceCode = sourceCode; } else { var dependency = dependencies.GetByUrl(filePath); if (dependency != null) { newSourceCode = dependency.Content; } } string sourceFragment = SourceCodeNavigator.GetSourceFragment(newSourceCode, new SourceCodeNodeCoordinates(lineNumber, columnNumber)); var errorMessage = new StringBuilder(); if (!string.IsNullOrWhiteSpace(type)) { errorMessage.AppendFormatLine("{0}: {1}", CoreStrings.ErrorDetails_ErrorType, type); } errorMessage.AppendFormatLine("{0}: {1}", CoreStrings.ErrorDetails_Message, message); if (!string.IsNullOrWhiteSpace(filePath)) { errorMessage.AppendFormatLine("{0}: {1}", CoreStrings.ErrorDetails_File, filePath); } if (lineNumber > 0) { errorMessage.AppendFormatLine("{0}: {1}", CoreStrings.ErrorDetails_LineNumber, lineNumber.ToString(CultureInfo.InvariantCulture)); } if (columnNumber > 0) { errorMessage.AppendFormatLine("{0}: {1}", CoreStrings.ErrorDetails_ColumnNumber, columnNumber.ToString(CultureInfo.InvariantCulture)); } if (!string.IsNullOrWhiteSpace(sourceFragment)) { errorMessage.AppendFormatLine("{1}:{0}{0}{2}", Environment.NewLine, CoreStrings.ErrorDetails_SourceError, sourceFragment); } return errorMessage.ToString(); }
/// <summary> /// Fills the list of LESS-files, that were added to a LESS-asset /// by using the LESS <code>@import</code> rules /// </summary> /// <param name="rootAssetUrl">URL of root LESS-asset file</param> /// <param name="parentStylesheet">Parent LESS-stylesheet</param> /// <param name="dependencies">List of LESS-files, that were added to a /// LESS-asset by using the LESS <code>@import</code> rules</param> public void FillDependencies(string rootAssetUrl, LessStylesheet parentStylesheet, DependencyCollection dependencies) { foreach (string dataUriFunctionImageUrl in parentStylesheet.DataUriFunctionAssetUrls) { var dependency = new Dependency(dataUriFunctionImageUrl, string.Empty); dependencies.Add(dependency); } foreach (LessImport import in parentStylesheet.Imports) { string dependencyUrl = import.Url; LessImportOptions dependencyOptions = import.ImportOptions; if (UrlHelpers.StartsWithProtocol(dependencyUrl)) { if (!dependencies.ContainsUrl(dependencyUrl)) { var dependency = new Dependency(dependencyUrl, string.Empty, false); dependencies.Add(dependency); } continue; } if (string.Equals(dependencyUrl, rootAssetUrl, StringComparison.OrdinalIgnoreCase)) { continue; } var duplicateDependency = dependencies.GetByUrl(dependencyUrl); bool isDuplicateDependency = (duplicateDependency != null); bool isEmptyDependency = isDuplicateDependency && (duplicateDependency.Content.Length == 0); bool isNonExistentOptionalDependency = false; if (!isDuplicateDependency || isEmptyDependency) { if (!LessStylesheetExists(dependencyUrl)) { if (dependencyOptions.Optional) { isNonExistentOptionalDependency = true; } else { throw new FileNotFoundException( string.Format(CoreStrings.Common_FileNotExist, dependencyUrl)); } } var stylesheet = new LessStylesheet(dependencyUrl, string.Empty); string dependencyContent = null; if (dependencyOptions.Less || dependencyOptions.Inline) { if (!isNonExistentOptionalDependency) { stylesheet = GetLessStylesheet(dependencyUrl, dependencyOptions); dependencyContent = stylesheet.Content; } if (isEmptyDependency && stylesheet.Content.Length > 0) { duplicateDependency.Content = dependencyContent; duplicateDependency.IsObservable = true; } else { var dependency = new Dependency(dependencyUrl, dependencyContent); dependencies.Add(dependency); } } else { if (!isNonExistentOptionalDependency) { dependencyContent = string.Empty; } if (!isDuplicateDependency) { var dependency = new Dependency(dependencyUrl, dependencyContent, false); dependencies.Add(dependency); } } FillDependencies(rootAssetUrl, stylesheet, dependencies); } } }