forked from madskristensen/WebCompiler
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompilerBuildTask.cs
More file actions
94 lines (79 loc) · 3.77 KB
/
CompilerBuildTask.cs
File metadata and controls
94 lines (79 loc) · 3.77 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
using System;
using System.IO;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
namespace WebCompiler
{
/// <summary>
/// An MSBuild task for running web compilers on a given config file.
/// </summary>
public class CompilerBuildTask : Task
{
/// <summary>
/// The file path of the compilerconfig.json file
/// </summary>
public string FileName { get; set; }
/// <summary>
/// Execute the Task
/// </summary>
public override bool Execute()
{
FileInfo configFile = new FileInfo(FileName);
if (!configFile.Exists)
{
Log.LogWarning(configFile.FullName + " does not exist");
return true;
}
Log.LogMessage(MessageImportance.High, Environment.NewLine + "WebCompiler: Begin compiling " + configFile.Name);
ConfigFileProcessor processor = new ConfigFileProcessor();
processor.BeforeProcess += (s, e) => { if (e.ContainsChanges) FileHelpers.RemoveReadonlyFlagFromFile(e.Config.GetAbsoluteOutputFile()); };
processor.AfterProcess += Processor_AfterProcess;
processor.BeforeWritingSourceMap += (s, e) => { FileHelpers.RemoveReadonlyFlagFromFile(e.ResultFile); };
processor.AfterWritingSourceMap += Processor_AfterWritingSourceMap;
FileMinifier.BeforeWritingMinFile += (s, e) => { if (e.ContainsChanges) FileHelpers.RemoveReadonlyFlagFromFile(e.ResultFile); };
FileMinifier.AfterWritingMinFile += FileMinifier_AfterWritingMinFile;
FileMinifier.BeforeWritingGzipFile += (s, e) => { if (e.ContainsChanges) FileHelpers.RemoveReadonlyFlagFromFile(e.ResultFile); };
FileMinifier.AfterWritingGzipFile += FileMinifier_AfterWritingGzipFile;
CompilerService.Initializing += (s, e) => { Log.LogMessage(MessageImportance.High, "WebCompiler installing updated versions of the compilers..."); };
try
{
var results = processor.Process(configFile.FullName);
bool isSuccessful = true;
foreach (CompilerResult result in results)
{
if (result.HasErrors)
{
isSuccessful = false;
foreach (var error in result.Errors)
{
Log.LogError("WebCompiler", "0", "", error.FileName, error.LineNumber, error.ColumnNumber, error.LineNumber, error.ColumnNumber, error.Message, null);
}
}
}
Log.LogMessage(MessageImportance.High, "WebCompiler: Done compiling " + configFile.Name);
return isSuccessful;
}
catch (Exception ex)
{
Log.LogError(ex.Message);
return false;
}
}
private void FileMinifier_AfterWritingGzipFile(object sender, MinifyFileEventArgs e)
{
Log.LogMessage(MessageImportance.High, "\tGzipped " + FileHelpers.MakeRelative(FileName, e.ResultFile));
}
private void Processor_AfterProcess(object sender, CompileFileEventArgs e)
{
Log.LogMessage(MessageImportance.High, "\tCompiled " + e.Config.OutputFile);
}
private void Processor_AfterWritingSourceMap(object sender, SourceMapEventArgs e)
{
Log.LogMessage(MessageImportance.High, "\tSourceMap " + FileHelpers.MakeRelative(FileName, e.ResultFile));
}
private void FileMinifier_AfterWritingMinFile(object sender, MinifyFileEventArgs e)
{
Log.LogMessage(MessageImportance.High, "\tMinified " + FileHelpers.MakeRelative(FileName, e.ResultFile));
}
}
}