-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeGen.cs
More file actions
160 lines (134 loc) · 5.42 KB
/
CodeGen.cs
File metadata and controls
160 lines (134 loc) · 5.42 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
using System;
using System.IO;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using System.Collections.Generic;
public class CodeGen : ToolTask
{
public bool Clean { get; set; }
[Required]
public ITaskItem[] Inputs { get; set; }
[Required]
public string TargetName { get; set; }
[Required]
public string TLogLocation { get; set; }
public override bool Execute()
{
try
{
bool append = false;
foreach (var input in Inputs)
{
string inputPath = input.ItemSpec;
string fullInputPath = input.GetMetadata("FullPath");
string command = input.GetMetadata("Command");
string outputsStr = input.GetMetadata("Outputs");
string additionalInputsStr = input.GetMetadata("AdditionalInputs");
string dependencyFile = input.GetMetadata("DependencyFile");
var outputs = outputsStr.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
var additionalInputs = additionalInputsStr.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
var dependencies = new List<string>();
try
{
var lines = File.ReadAllLines(dependencyFile);
foreach (var line in lines)
{
var parts = line.Split(new[] { ':' }, 2);
if (parts.Length != 2)
continue;
var files = parts[1].Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
foreach (var file in files)
dependencies.Add(file);
}
}
catch (FileNotFoundException)
{
}
catch (DirectoryNotFoundException)
{
}
var allInputs = new List<string>();
allInputs.Add(inputPath);
allInputs.AddRange(dependencies);
allInputs.AddRange(additionalInputs);
if (Clean)
{
File.Delete(dependencyFile);
foreach (var output in outputs)
File.Delete(output);
}
else
{
if (OutOfDate(allInputs, outputs))
{
inputCommand = command;
base.Execute();
}
string commandTLogPath = Path.Combine(TLogLocation, $"{TargetName}.command.1.tlog");
string readTLogPath = Path.Combine(TLogLocation, $"{TargetName}.read.1.tlog");
string writeTLogPath = Path.Combine(TLogLocation, $"{TargetName}.write.1.tlog");
using (StreamWriter writer = new StreamWriter(commandTLogPath, append: append))
{
writer.WriteLine($"^{fullInputPath}");
writer.WriteLine(command);
}
using (StreamWriter writer = new StreamWriter(readTLogPath, append: append))
{
writer.WriteLine($"^{fullInputPath}");
foreach (var dependency in dependencies)
writer.WriteLine(Path.GetFullPath(dependency));
foreach (var additionalInput in additionalInputs)
writer.WriteLine(Path.GetFullPath(additionalInput));
}
using (StreamWriter writer = new StreamWriter(writeTLogPath, append: append))
{
writer.WriteLine($"^{fullInputPath}");
foreach (var output in outputs)
writer.WriteLine(Path.GetFullPath(output));
}
append = true;
}
}
}
catch (Exception ex)
{
Log.LogErrorFromException(ex);
return false;
}
return true;
}
private bool OutOfDate(List<string> inputFiles, string[] outputFiles)
{
if (outputFiles.Length == 0)
return false;
var newestInput = File.GetLastWriteTimeUtc(inputFiles[0]);
foreach (var inputFile in inputFiles)
{
var lastWriteTime = File.GetLastWriteTimeUtc(inputFile);
if (lastWriteTime > newestInput)
newestInput = lastWriteTime;
}
foreach (var outputFile in outputFiles)
{
if (!File.Exists(outputFile))
return true;
var lastWriteTime = File.GetLastWriteTimeUtc(outputFile);
if (lastWriteTime < newestInput)
return true;
}
return false;
}
private string inputCommand;
protected override string ToolName => "cmd.exe";
protected override string GenerateFullPathToTool() => ToolName;
protected override string GenerateCommandLineCommands()
{
var command = new CommandLineBuilder();
command.AppendSwitch("/c " + inputCommand);
return command.ToString();
}
protected override void LogEventsFromTextOutput(string singleLine, MessageImportance messageImportance)
{
base.LogEventsFromTextOutput(singleLine, MessageImportance.High);
}
}