forked from madskristensen/WebCompiler
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
71 lines (58 loc) · 2.97 KB
/
Program.cs
File metadata and controls
71 lines (58 loc) · 2.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace WebCompiler
{
class Program
{
static int Main(params string[] args)
{
string configPath = args[0];
string file = args.Length > 1 ? args[1] : null;
var configs = GetConfigs(configPath, file);
if (configs == null)
{
Console.WriteLine("\x1B[33mNo configurations matched");
return 0;
}
ConfigFileProcessor processor = new ConfigFileProcessor();
EventHookups(processor, configPath);
var results = processor.Process(configPath, configs);
var errorResults = results.Where(r => r.HasErrors);
foreach (var result in errorResults)
foreach (var error in result.Errors)
{
Console.Write("\x1B[31m" + error.Message);
}
return errorResults.Any() ? 1 : 0;
}
private static void EventHookups(ConfigFileProcessor processor, string configPath)
{
// For console colors, see http://stackoverflow.com/questions/23975735/what-is-this-u001b9-syntax-of-choosing-what-color-text-appears-on-console
processor.BeforeProcess += (s, e) => { Console.WriteLine($"Processing \x1B[36m{e.Config.InputFile}"); if (e.ContainsChanges) FileHelpers.RemoveReadonlyFlagFromFile(e.Config.GetAbsoluteOutputFile()); };
processor.AfterProcess += (s, e) => { Console.WriteLine($" \x1B[32mCompiled"); };
processor.BeforeWritingSourceMap += (s, e) => { if (e.ContainsChanges) FileHelpers.RemoveReadonlyFlagFromFile(e.ResultFile); };
processor.AfterWritingSourceMap += (s, e) => { Console.WriteLine($" \x1B[32mSourcemap"); };
processor.ConfigProcessed += (s, e) => { Console.WriteLine("\t"); };
FileMinifier.BeforeWritingMinFile += (s, e) => { FileHelpers.RemoveReadonlyFlagFromFile(e.ResultFile); };
FileMinifier.AfterWritingMinFile += (s, e) => { Console.WriteLine($" \x1B[32mMinified"); };
FileMinifier.BeforeWritingGzipFile += (s, e) => { FileHelpers.RemoveReadonlyFlagFromFile(e.ResultFile); };
FileMinifier.AfterWritingGzipFile += (s, e) => { Console.WriteLine($" \x1B[32mGZipped"); };
}
private static IEnumerable<Config> GetConfigs(string configPath, string file)
{
var configs = ConfigHandler.GetConfigs(configPath);
if (configs == null || !configs.Any())
return null;
if (file != null)
{
if (file.StartsWith("*"))
configs = configs.Where(c => Path.GetExtension(c.InputFile).Equals(file.Substring(1), StringComparison.OrdinalIgnoreCase));
else
configs = configs.Where(c => c.InputFile.Equals(file, StringComparison.OrdinalIgnoreCase));
}
return configs;
}
}
}