/// <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 (JsRuntimeException e) { throw new CoffeeScriptCompilationException(JsErrorHelpers.Format(e)); } return(newContent); }
/// <summary> /// Gets a string containing the compiled CoffeeScript result. /// </summary> /// <param name="input"> /// The input to process. /// </param> /// <param name="options"> /// The AutoPrefixer options. /// </param> /// <returns> /// The <see cref="string"/> containing the compiled CoffeeScript result. /// </returns> public string Process(string input, AutoPrefixerOptions options) { string processedCode; lock (SyncRoot) { this.Initialize(); try { string result = this.javascriptEngine.Evaluate <string>(string.Format(CompilationFunctionCallTemplate, JsonConvert.SerializeObject(input), ConvertAutoPrefixerOptionsToJson(options))); JObject json = JObject.Parse(result); JArray errors = json["errors"] as JArray; if (errors != null && errors.Count > 0) { throw new AutoPrefixerProcessingException(FormatErrorDetails(errors[0])); } processedCode = json.Value <string>("processedCode"); } catch (JsRuntimeException ex) { throw new AutoPrefixerProcessingException(JsErrorHelpers.Format(ex)); } } return(processedCode); }
/// <summary> /// "Optimizes" a CSS code by using Sergey Kryzhanovsky's CSSO /// </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 Optimize(string content, string path) { Initialize(); string newContent; try { var result = _jsEngine.Evaluate <string>( string.Format(OPTIMIZATION_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 CssOptimizationException(FormatErrorDetails(errors[0], content, path)); } newContent = json.Value <string>("minifiedCode"); } catch (JsRuntimeException e) { throw new CssOptimizationException(JsErrorHelpers.Format(e)); } 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 (JsRuntimeException e) { throw new CssAutoprefixingException(JsErrorHelpers.Format(e)); } return(autoprefixingResult); }
/// <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 (JsRuntimeException e) { throw new HandlebarsCompilationException(JsErrorHelpers.Format(e)); } return(newContent); }
/// <summary> /// Gets a string containing the compiled CoffeeScript result. /// </summary> /// <param name="input"> /// The input to compile. /// </param> /// <returns> /// The <see cref="string"/> containing the compiled CoffeeScript result. /// </returns> public string Compile(string input) { string compiledInput; lock (SyncRoot) { this.Initialize(); try { string result = this.javascriptEngine.Evaluate <string>(string.Format(CompilationFunctionCallTemplate, JsonConvert.SerializeObject(input), "{bare: false}")); JObject json = JObject.Parse(result); JArray errors = json["errors"] as JArray; if (errors != null && errors.Count > 0) { throw new CoffeeScriptCompilingException(FormatErrorDetails(errors[0])); } compiledInput = json.Value <string>("compiledCode"); } catch (JsRuntimeException ex) { throw new CoffeeScriptCompilingException(JsErrorHelpers.Format(ex)); } } return(compiledInput); }
/// <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 (JsRuntimeException e) { throw new JsPackingException(JsErrorHelpers.Format(e)); } return(newContent); }
public static string UnmaskFrom(string maskedUrl, string userId) { try { using (var jsEngine = new MsieJsEngine()) { jsEngine.ExecuteResource("VkAudioSync.Vk.audioUnmask.js", Assembly.GetExecutingAssembly()); return(jsEngine.Evaluate <string>($"unmaskUrl(\"{maskedUrl}\", {userId});")); } } catch (JsEngineLoadException e) { throw new Exception($"JS Compile error: {JsErrorHelpers.Format(e)}"); } catch (JsRuntimeException e) { throw new Exception($"JS Runtime error: {JsErrorHelpers.Format(e)}"); } }
/// <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 (JsRuntimeException e) { throw new CssCleaningException(JsErrorHelpers.Format(e)); } 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 (JsRuntimeException e) { throw new TypeScriptCompilationException(JsErrorHelpers.Format(e)); } return(compilationResult); }
/// <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 contentBuilder = new StringBuilder(); 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(); } 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 (JsRuntimeException e) { throw new LessCompilationException(JsErrorHelpers.Format(e)); } return(compilationResult); }
private string ExecuteJs(string exeFilePath, string corlibPath, string entryPoint, out int exitCode, List <string> errors) { string jsOutput = null; exitCode = -1; using (var jsEngine = new MsieJsEngine(engineMode: JsEngineMode.Auto, useEcmaScript5Polyfill: false, useJson2Library: false)) { object exitCodeObj = null; try { jsEngine.Execute(@"var braille_testlib_output = """";"); jsEngine.Execute(@"function braille_test_log(message) { braille_testlib_output += asm0.ToJavaScriptString(message) + ""\r\n""; }"); if (corlibPath != null) { jsEngine.ExecuteFile(corlibPath); } jsEngine.ExecuteFile(exeFilePath + ".js"); exitCodeObj = jsEngine.Evaluate(entryPoint + ".entryPoint()"); } catch (JsEngineLoadException e) { errors.Add("During loading of JavaScript engine an error occurred.\n" + JsErrorHelpers.Format(e)); } catch (JsRuntimeException e) { errors.Add("During execution of JavaScript code an error occurred.\n" + JsErrorHelpers.Format(e)); } if (exitCodeObj == null || exitCodeObj == MsieJavaScriptEngine.Undefined.Value) { exitCode = 0; } else { exitCode = (int)(double)exitCodeObj; } try { jsOutput = (string)jsEngine.Evaluate("braille_testlib_output"); } catch { errors.Add("Exception while evaluating script output"); } } return(jsOutput); }