/// <summary> /// "Packs" a JS code by using Dean Edwards' Packer /// </summary> /// <param name="content">Text content of JS asset</param> /// <returns>Minified text content of JS asset</returns> public string Pack(string content) { Initialize(); string newContent; try { newContent = _jsEngine.CallFunction <string>(PACKING_FUNCTION_NAME, content, _options.Base62Encode, _options.ShrinkVariables); } catch (JsScriptException e) { string errorDetails = JsErrorHelpers.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 JsPackingException(errorMessage); } return(newContent); }
/// <summary> /// "Cleans" CSS code by using Clean-css /// </summary> /// <param name="content">Text content of CSS asset</param> /// <param name="path">Path to CSS file</param> /// <returns>Minified text content of CSS asset</returns> public string Clean(string content, string path) { Initialize(); string newContent; try { var result = _jsEngine.Evaluate <string>( string.Format(CLEANING_FUNCTION_CALL_TEMPLATE, JsonConvert.SerializeObject(content), _optionsString)); var json = JObject.Parse(result); var errors = json["errors"] != null ? json["errors"] as JArray : null; if (errors != null && errors.Count > 0) { throw new CssCleaningException(FormatErrorDetails(errors[0].Value <string>(), true, path)); } if (_options.Severity > 0) { var warnings = json["warnings"] != null ? json["warnings"] as JArray : null; if (warnings != null && warnings.Count > 0) { throw new CssCleaningException(FormatErrorDetails(warnings[0].Value <string>(), false, path)); } } newContent = json.Value <string>("minifiedCode"); } catch (JsScriptException e) { string errorDetails = JsErrorHelpers.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 CssCleaningException(errorMessage); } return(newContent); }
/// <summary> /// "Compiles" a TypeScript code to JS code /// </summary> /// <param name="path">Path to TypeScript file</param> /// <returns>Compilation result</returns> public CompilationResult Compile(string path) { Initialize(); CompilationResult compilationResult; try { var result = _jsEngine.Evaluate <string>(string.Format(COMPILATION_FUNCTION_CALL_TEMPLATE, JsonConvert.SerializeObject(path), _optionsString)); var json = JObject.Parse(result); var errors = json["errors"] != null ? json["errors"] as JArray : null; if (errors != null && errors.Count > 0) { throw new TypeScriptCompilationException(FormatErrorDetails(errors[0], path)); } compilationResult = new CompilationResult { CompiledContent = json.Value <string>("compiledCode"), IncludedFilePaths = json.Value <JArray>("includedFilePaths") .ToObject <IList <string> >() .Distinct(StringComparer.OrdinalIgnoreCase) .Where(p => !p.Equals(path, StringComparison.OrdinalIgnoreCase)) .ToList() }; } catch (JsScriptException e) { string errorDetails = JsErrorHelpers.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 TypeScriptCompilationException(errorMessage); } return(compilationResult); }
/// <summary> /// "Compiles" Handlebars template to JS code /// </summary> /// <param name="content">Text content of Handlebars template</param> /// <param name="path">Path to Handlebars file</param> /// <returns>Translated Handlebars template</returns> public string Compile(string content, string path) { Initialize(); string newContent; try { var result = _jsEngine.Evaluate <string>( string.Format(COMPILATION_FUNCTION_CALL_TEMPLATE, JsonConvert.SerializeObject(content), _optionsString)); var json = JObject.Parse(result); var errors = json["errors"] != null ? json["errors"] as JArray : null; if (errors != null && errors.Count > 0) { throw new HandlebarsCompilationException(FormatErrorDetails(errors[0], content, path)); } var compiledCode = json.Value <string>("compiledCode"); bool isPartial; string templateName = GetTemplateName(path, _options.RootPath, out isPartial); newContent = WrapCompiledTemplateCode(compiledCode, _options.Namespace, templateName, isPartial); } catch (JsScriptException e) { string errorDetails = JsErrorHelpers.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 HandlebarsCompilationException(errorMessage); } return(newContent); }
/// <summary> /// Actualizes a vendor prefixes in CSS code by using Andrey Sitnik's Autoprefixer /// </summary> /// <param name="content">Text content of CSS asset</param> /// <param name="path">Path to CSS asset</param> /// <returns>Autoprefixing result</returns> public AutoprefixingResult Process(string content, string path) { Initialize(); AutoprefixingResult autoprefixingResult; try { var result = _jsEngine.Evaluate <string>( string.Format(AUTOPREFIXING_FUNCTION_CALL_TEMPLATE, JsonConvert.SerializeObject(content), _optionsString)); var json = JObject.Parse(result); var errors = json["errors"] != null ? json["errors"] as JArray : null; if (errors != null && errors.Count > 0) { throw new CssAutoprefixingException(FormatErrorDetails(errors[0], content, path)); } autoprefixingResult = new AutoprefixingResult { ProcessedContent = json.Value <string>("processedCode"), IncludedFilePaths = GetIncludedFilePaths(_options.Stats) }; } catch (JsScriptException e) { string errorDetails = JsErrorHelpers.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 CssAutoprefixingException(errorMessage); } return(autoprefixingResult); }
/// <summary> /// "Compiles" CoffeeScript code to JS code /// </summary> /// <param name="content">Text content written on CoffeeScript</param> /// <param name="path">Path to CoffeeScript file</param> /// <returns>Translated CoffeeScript code</returns> public string Compile(string content, string path) { Initialize(); string newContent; string optionsString = ConvertCompilationOptionsToJson(path, _options).ToString(); try { var result = _jsEngine.Evaluate <string>( string.Format(COMPILATION_FUNCTION_CALL_TEMPLATE, JsonConvert.SerializeObject(content), optionsString)); var json = JObject.Parse(result); var errors = json["errors"] != null ? json["errors"] as JArray : null; if (errors != null && errors.Count > 0) { throw new CoffeeScriptCompilationException(FormatErrorDetails(errors[0], content, path)); } newContent = json.Value <string>("compiledCode"); } catch (JsScriptException e) { string errorDetails = JsErrorHelpers.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 CoffeeScriptCompilationException(errorMessage); } return(newContent); }
/// <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 = JsErrorHelpers.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); }
/// <summary> /// "Compiles" a LESS code to CSS code /// </summary> /// <param name="content">Text content written on LESS</param> /// <param name="path">Path to LESS file</param> /// <returns>Compilation result</returns> public CompilationResult Compile(string content, string path) { Initialize(); CompilationResult compilationResult; string processedContent = content; string globalVariables = _options.GlobalVariables; string modifyVariables = _options.ModifyVariables; if (!string.IsNullOrWhiteSpace(globalVariables) || !string.IsNullOrWhiteSpace(modifyVariables)) { var stringBuilderPool = StringBuilderPool.Shared; StringBuilder contentBuilder = stringBuilderPool.Rent(); if (!string.IsNullOrWhiteSpace(globalVariables)) { contentBuilder.AppendLine(ParseVariables(globalVariables, "GlobalVariables")); } contentBuilder.Append(content); if (!string.IsNullOrWhiteSpace(modifyVariables)) { contentBuilder.AppendLine(); contentBuilder.Append(ParseVariables(modifyVariables, "ModifyVariables")); } processedContent = contentBuilder.ToString(); stringBuilderPool.Return(contentBuilder); } try { var result = _jsEngine.Evaluate <string>(string.Format(COMPILATION_FUNCTION_CALL_TEMPLATE, JsonConvert.SerializeObject(processedContent), JsonConvert.SerializeObject(path), _optionsString)); var json = JObject.Parse(result); var errors = json["errors"] != null ? json["errors"] as JArray : null; if (errors != null && errors.Count > 0) { throw new LessCompilationException(FormatErrorDetails(errors[0], processedContent, path)); } if (_options.Severity > 0) { var warnings = json["warnings"] != null ? json["warnings"] as JArray : null; if (warnings != null && warnings.Count > 0) { throw new LessCompilationException(FormatWarningList(warnings)); } } compilationResult = new CompilationResult { CompiledContent = json.Value <string>("compiledCode"), IncludedFilePaths = json.Value <JArray>("includedFilePaths") .ToObject <IList <string> >() .Distinct(StringComparer.OrdinalIgnoreCase) .ToList() }; } catch (JsScriptException e) { string errorDetails = JsErrorHelpers.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 LessCompilationException(errorMessage); } return(compilationResult); }