我正在使用我的一位同事编写的工具,它扫描目录并将名字对象添加到解决方案文件和 .csproj 文件中。
我正在从文本文件中扫描具有 =“某些类别”的文件,以在 .sln 文件和 .csproj 文件中设置名字对象。
问题是项目列表有大约 700 个项目。我正在相应地更改解决方案和项目的位置,但是解决方案所依赖的某些项目与解决方案文件的名字不同,因此 Visual Studio 在加载项目文件时会引发一些错误。
要解决此问题,我必须修复我的文本文件中的名字对象并再次运行该工具,但该工具会扫描所有内容,而不仅仅是更改的文件。
有没有办法可以缓存结果并仅扫描已更改的内容?我认为当我重新运行该工具来修复错误项目的名称时,它会为我节省很多时间。这是标记 .csproj 文件的方法。
internal static void MarkAllProjects()
{
const string assemblyOutputTypeForLibrary = "library";
Dictionary<string, string> projectEntries = new Dictionary<string, string>();
using (var stream = new System.IO.StreamReader(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("ProjectMarker.ProjectList.txt")))
{
while (stream.EndOfStream == false)
{
string[] projectEntry = stream.ReadLine().Split(new char[] { '=' }, StringSplitOptions.RemoveEmptyEntries);
if (projectEntry.Count() == 2 && projectEntries.ContainsKey(projectEntry[0]) == false)
{
projectEntries.Add(projectEntry[0], projectEntry[1]);
}
}
}
Parallel.ForEach<KeyValuePair<string, string>>(projectEntries, projectEntry =>
{
var files = Directory.GetFiles(sourceDirectoryRoot, projectEntry.Key, SearchOption.AllDirectories);
Parallel.ForEach<string>(files, file =>
{
XDocument projectFileAsXml = XDocument.Parse(File.ReadAllText(file, Encoding.UTF8));
string markerElementIdentifier = string.Empty;
string postBuildEventIdentifier = string.Empty;
string assemblyNameIdentifier = string.Empty;
string propertyGroupIdentifier = string.Empty;
string assemblyOutputType = string.Empty;
string projectOutputType = string.Empty;
if (projectFileAsXml.Root.GetDefaultNamespace() != null && string.IsNullOrWhiteSpace(projectFileAsXml.Root.GetDefaultNamespace().NamespaceName) == false)
{
markerElementIdentifier = string.Concat("{", projectFileAsXml.Root.GetDefaultNamespace(), "}", "ItemDefinitionGroup");
postBuildEventIdentifier = string.Concat("{", projectFileAsXml.Root.GetDefaultNamespace(), "}", "PostBuildEvent");
assemblyNameIdentifier = string.Concat("{", projectFileAsXml.Root.GetDefaultNamespace(), "}", "AssemblyName");
propertyGroupIdentifier = string.Concat("{", projectFileAsXml.Root.GetDefaultNamespace(), "}", "PropertyGroup");
assemblyOutputType = string.Concat("{", projectFileAsXml.Root.GetDefaultNamespace(), "}", "OutputType");
projectOutputType = string.Concat("{", projectFileAsXml.Root.GetDefaultNamespace(), "}", "ProjectTypeGuids");
}
else
{
markerElementIdentifier = "ItemDefinitionGroup";
postBuildEventIdentifier = "PostBuildEvent";
assemblyNameIdentifier = "AssemblyName";
propertyGroupIdentifier = "PropertyGroup";
assemblyOutputType = "OutputType";
projectOutputType = "ProjectTypeGuids";
}
if (projectFileAsXml.Root.Element(markerElementIdentifier) == null
|| (projectFileAsXml.Root.Element(markerElementIdentifier) != null
&& projectFileAsXml.Root.Element(markerElementIdentifier).Attribute("Label") == null))
{
projectFileAsXml.Root.Add(new XElement(markerElementIdentifier, new XAttribute("Label", projectEntry.Value)));
if (projectFileAsXml.Root.Descendants(assemblyOutputType).FirstOrDefault() != null
&& projectFileAsXml.Root.Descendants(assemblyOutputType).First().Value.ToLower() == assemblyOutputTypeForLibrary
&& projectFileAsXml.Root.Descendants(assemblyNameIdentifier).FirstOrDefault() != null
&& projectFileAsXml.Root.Descendants(assemblyNameIdentifier).First().Value.Contains("Test") == false
&& (projectFileAsXml.Root.Descendants(projectOutputType).FirstOrDefault() == null
|| projectTypeGuidsToBeExcluded.Exists(item => projectFileAsXml.Root.Descendants(projectOutputType).First().Value.ToUpper().Contains(item)) == false))
{
string nugetPublishDirectory = string.Concat(targetDirectoryRoot, "\\NuGetPublishings\\", projectEntry.Value, "\\", projectFileAsXml.Root.Descendants(assemblyNameIdentifier).First().Value, "\\lib");
Directory.CreateDirectory(nugetPublishDirectory);
if (projectFileAsXml.Root.Descendants(postBuildEventIdentifier).FirstOrDefault() == null)
{
projectFileAsXml.Root.Add(new XElement(propertyGroupIdentifier,
new XElement(postBuildEventIdentifier,
string.Concat("copy \"$(TargetDir)$(TargetFileName)\" ", "\"$(SolutionDir)..\\NuGetPublishings\\", projectEntry.Value, "\\$(ProjectName)\\lib\\$(TargetFileName)\""))));
}
else
{
projectFileAsXml.Root.Descendants(postBuildEventIdentifier).First().Value
+= Environment.NewLine + string.Concat("copy \"$(TargetDir)$(TargetFileName)\" ", "\"$(SolutionDir)..\\NuGetPublishings\\", projectEntry.Value, "\\$(ProjectName)\\lib\\$(TargetFileName)\"");
}
}
projectFileAsXml.Save(file);
}
});
});
如果您需要更多说明,请告诉我。
太感谢了!