/
Processor.cs
178 lines (148 loc) · 5.87 KB
/
Processor.cs
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
public partial class Processor
{
public string AssemblyFilePath;
public string IntermediateDirectory;
public string KeyFilePath;
public bool SignAssembly;
public string ProjectDirectory;
public string ProjectFilePath;
public string DocumentationFilePath;
public string References;
public string SolutionDirectory;
public List<WeaverEntry> Weavers;
public DebugSymbolsType DebugSymbols;
public List<string> ReferenceCopyLocalPaths;
public List<string> DefineConstants;
public List<WeaverConfigFile> ConfigFiles;
public Dictionary<string, WeaverConfigEntry> ConfigEntries;
public bool GenerateXsd;
IInnerWeaver innerWeaver;
static Dictionary<string, IsolatedAssemblyLoadContext> solutionAssemblyLoadContexts =
new Dictionary<string, IsolatedAssemblyLoadContext>(StringComparer.OrdinalIgnoreCase);
public ILogger Logger;
static readonly object mutex = new object();
static Processor()
{
DomainAssemblyResolver.Connect();
}
public virtual bool Execute()
{
var assembly = typeof(Processor).Assembly;
Logger.LogInfo($"Fody (version {typeof(Processor).Assembly.GetName().Version} @ {assembly.CodeBase}) Executing");
var stopwatch = Stopwatch.StartNew();
try
{
Inner();
return !Logger.ErrorOccurred;
}
catch (Exception exception)
{
Logger.LogException(exception);
return false;
}
finally
{
stopwatch.Stop();
Logger.LogInfo($" Finished Fody {stopwatch.ElapsedMilliseconds}ms.");
}
}
void Inner()
{
ValidateSolutionPath();
ValidateProjectPath();
ValidateAssemblyPath();
ConfigFiles = ConfigFileFinder.FindWeaverConfigFiles(SolutionDirectory, ProjectDirectory, Logger).ToList();
if (!ConfigFiles.Any())
{
ConfigFiles = new List<WeaverConfigFile>
{
ConfigFileFinder.GenerateDefault(ProjectDirectory, Weavers, GenerateXsd)
};
Logger.LogWarning($"Could not find a FodyWeavers.xml file at the project level ({ProjectDirectory}). A default file has been created. Please review the file and add it to your project.");
}
ConfigEntries = ConfigFileFinder.ParseWeaverConfigEntries(ConfigFiles);
var extraEntries = ConfigEntries.Values
.Where(entry => !entry.ConfigFile.IsGlobal && !Weavers.Any(weaver => string.Equals(weaver.ElementName, entry.ElementName)))
.ToArray();
const string missingWeaversHelp = "Add the desired weavers via their nuget package.";
if (extraEntries.Any())
{
throw new WeavingException($"No weavers found for the configuration entries {string.Join(", ", extraEntries.Select(e => e.ElementName))}. " + missingWeaversHelp);
}
if (Weavers.Count == 0)
{
throw new WeavingException("No weavers found. " + missingWeaversHelp);
}
foreach (var weaver in Weavers)
{
if (ConfigEntries.TryGetValue(weaver.ElementName, out var config))
{
weaver.Element = config.Content;
weaver.ExecutionOrder = config.ExecutionOrder;
}
else
{
Logger.LogWarning($"No configuration entry found for the installed weaver {weaver.ElementName}. This weaver will be skipped. You may want to add this weaver to your FodyWeavers.xml");
}
}
ConfigFileFinder.EnsureSchemaIsUpToDate(SolutionDirectory, ProjectDirectory, Weavers, GenerateXsd);
Weavers = Weavers
.Where(weaver => weaver.Element != null)
.OrderBy(weaver => weaver.ExecutionOrder)
.ToList();
lock (mutex)
{
ExecuteInOwnAssemblyLoadContext();
}
}
void ExecuteInOwnAssemblyLoadContext()
{
var loadContext = GetLoadContext();
using (innerWeaver = loadContext.CreateInstanceFromAndUnwrap())
{
innerWeaver.AssemblyFilePath = AssemblyFilePath;
innerWeaver.References = References;
innerWeaver.KeyFilePath = KeyFilePath;
innerWeaver.ReferenceCopyLocalPaths = ReferenceCopyLocalPaths;
innerWeaver.SignAssembly = SignAssembly;
innerWeaver.Logger = Logger;
innerWeaver.SolutionDirectoryPath = SolutionDirectory;
innerWeaver.Weavers = Weavers;
innerWeaver.IntermediateDirectoryPath = IntermediateDirectory;
innerWeaver.DefineConstants = DefineConstants;
innerWeaver.ProjectDirectoryPath = ProjectDirectory;
innerWeaver.ProjectFilePath = ProjectFilePath;
innerWeaver.DocumentationFilePath = DocumentationFilePath;
innerWeaver.DebugSymbols = DebugSymbols;
innerWeaver.Execute();
ReferenceCopyLocalPaths = innerWeaver.ReferenceCopyLocalPaths;
}
innerWeaver = null;
}
IsolatedAssemblyLoadContext GetLoadContext()
{
if (solutionAssemblyLoadContexts.TryGetValue(SolutionDirectory, out var loadContext))
{
if (!WeaversHistory.HasChanged(Weavers.Select(x => x.AssemblyPath)))
{
return loadContext;
}
Logger.LogDebug("A Weaver HasChanged so loading a new AssemblyLoadContext");
loadContext.Unload();
}
return solutionAssemblyLoadContexts[SolutionDirectory] = CreateAssemblyLoadContext();
}
IsolatedAssemblyLoadContext CreateAssemblyLoadContext()
{
Logger.LogDebug("Creating a new AssemblyLoadContext");
return new IsolatedAssemblyLoadContext();
}
public void Cancel()
{
innerWeaver?.Cancel();
}
}