Skip to content

Commit ba5ec8a

Browse files
Write used references file only if different (#90)
Since the analyzer runs on every keystroke in the IDE it's important to reduce I/O and first-chance exceptions
1 parent 12754d8 commit ba5ec8a

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

src/Analyzer/ReferenceTrimmerAnalyzer.cs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ private static void DumpUsedReferences(CompilationAnalysisContext context)
9090
}
9191
}
9292

93-
File.WriteAllLines(Path.Combine(Path.GetDirectoryName(declaredReferencesPath), UsedReferencesFileName), usedReferences);
93+
WriteUsedReferencesToFile(usedReferences, declaredReferencesPath);
9494

9595
Dictionary<string, List<string>> packageAssembliesDict = new(StringComparer.OrdinalIgnoreCase);
9696
foreach (DeclaredReference declaredReference in declaredReferences.References)
@@ -141,6 +141,30 @@ private static void DumpUsedReferences(CompilationAnalysisContext context)
141141
}
142142
}
143143

144+
private static void WriteUsedReferencesToFile(HashSet<string> usedReferences, string declaredReferencesPath)
145+
{
146+
string filePath = Path.Combine(Path.GetDirectoryName(declaredReferencesPath), UsedReferencesFileName);
147+
148+
string text = string.Join(Environment.NewLine, usedReferences.OrderBy(s => s));
149+
150+
try
151+
{
152+
if (File.Exists(filePath))
153+
{
154+
string oldText = File.ReadAllText(filePath);
155+
if (string.Equals(text, oldText, StringComparison.OrdinalIgnoreCase))
156+
{
157+
return;
158+
}
159+
}
160+
161+
File.WriteAllText(filePath, text);
162+
}
163+
catch
164+
{
165+
}
166+
}
167+
144168
private static string? GetDeclaredReferencesPath(CompilationAnalysisContext context)
145169
{
146170
foreach (AdditionalText additionalText in context.Options.AdditionalFiles)

0 commit comments

Comments
 (0)