/// <summary> /// Compile JS. /// </summary> private static void CompileJS() { if (config.Automations == null) { return; } foreach (var automation in config.Automations.Where(a => a.Type.ToLower() == "js")) { if (automation.WaitBeforeParsing > 0) { Thread.Sleep(automation.WaitBeforeParsing); } if (automation.fswEntries == null) { continue; } var js = string.Empty; var af = new List <string>(); foreach (var entry in automation.fswEntries) { var files = GetFiles( entry.Path, entry.Pattern); foreach (var file in files) { if (af.Contains(file)) { continue; } af.Add(file); } } js += GetFileContents(af); if (automation.ParseTags) { js = ReplaceTags(js); } try { if (automation.Minify) { js = new JavaScriptMinifier().Minify(js); } } catch { Console.WriteLine("Could not minify JS"); } var cc = compiledContent .SingleOrDefault(c => c.TagName == automation.TagName); if (cc == null) { cc = new CompiledContent { TagName = automation.TagName, Type = automation.Type }; compiledContent.Add(cc); } cc.Content = js; cc.LastCompile = DateTime.Now; var path = automation.DestFile; if (string.IsNullOrWhiteSpace(path)) { continue; } var pathIsAbs = path.Substring(1, 1) == ":" || path.StartsWith("\\"); if (!pathIsAbs) { path = Path.Combine( configPath, path); } try { File.WriteAllText( path, js); } catch { Console.WriteLine("Could not write JS to dest file {0}", automation.DestFile); } } }
/// <summary> /// Compile LESS. /// </summary> private static void CompileLESS() { if (config.Automations == null) { return; } foreach (var automation in config.Automations.Where(a => a.Type.ToLower() == "less")) { if (automation.WaitBeforeParsing > 0) { Thread.Sleep(automation.WaitBeforeParsing); } if (automation.fswEntries == null) { continue; } var less = string.Empty; var css = string.Empty; var af = new List <string>(); foreach (var entry in automation.fswEntries) { var files = GetFiles( entry.Path, entry.Pattern); foreach (var file in files) { if (af.Contains(file)) { continue; } af.Add(file); } } less += GetFileContents(af); if (automation.ParseTags) { less = ReplaceTags(less); } var lessConfig = new DotlessConfiguration { MinifyOutput = automation.Minify }; try { css = Less.Parse(less, lessConfig); } catch { Console.WriteLine("Error while parsing LESS to CSS"); } if (string.IsNullOrWhiteSpace(css)) { continue; } var cc = compiledContent .SingleOrDefault(c => c.TagName == automation.TagName); if (cc == null) { cc = new CompiledContent { TagName = automation.TagName, Type = automation.Type }; compiledContent.Add(cc); } cc.Content = css; cc.LastCompile = DateTime.Now; var path = automation.DestFile; if (string.IsNullOrWhiteSpace(path)) { continue; } var pathIsAbs = path.Substring(1, 1) == ":" || path.StartsWith("\\"); if (!pathIsAbs) { path = Path.Combine( configPath, path); } try { File.WriteAllText( path, css); } catch { Console.WriteLine("Could not write CSS to dest file {0}", automation.DestFile); } } }
/// <summary> /// Compile HTML. /// </summary> private static void CompileHTML() { if (config.Automations == null) { return; } foreach (var automation in config.Automations.Where(a => a.Type.ToLower() == "html")) { if (automation.WaitBeforeParsing > 0) { Thread.Sleep(automation.WaitBeforeParsing); } if (automation.fswEntries == null) { continue; } var html = string.Empty; var af = new List <string>(); foreach (var entry in automation.fswEntries) { var files = GetFiles( entry.Path, entry.Pattern); foreach (var file in files) { if (af.Contains(file)) { continue; } af.Add(file); } } html += GetFileContents(af); if (automation.ParseTags) { html = ReplaceTags(html); } if (automation.Minify) { var lines = html.Split('\r'); if (lines.Length == 1) { lines = html.Split('\n'); } html = lines.Aggregate(string.Empty, (current, line) => current + line.Trim()); } var cc = compiledContent .SingleOrDefault(c => c.TagName == automation.TagName); if (cc == null) { cc = new CompiledContent { TagName = automation.TagName, Type = automation.Type }; compiledContent.Add(cc); } cc.Content = html; cc.LastCompile = DateTime.Now; var path = automation.DestFile; if (string.IsNullOrWhiteSpace(path)) { Console.WriteLine("No destFile specified for HTML type"); continue; } var pathIsAbs = path.Substring(1, 1) == ":" || path.StartsWith("\\"); if (!pathIsAbs) { path = Path.Combine( configPath, path); } try { File.WriteAllText( path, html); } catch { Console.WriteLine("Could not write JS to dest file {0}", automation.DestFile); } } }