缓存操作的结果,再次完成时仅扫描更改的操作

本文关键字:操作 扫描 完成时 结果 缓存 | 更新日期: 2023-09-27 18:31:01

我正在使用我的一位同事编写的工具,该工具扫描目录并向解决方案文件和 .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);
                    }
                });
            });

如果您需要更多说明,请告诉我。

非常感谢!

缓存操作的结果,再次完成时仅扫描更改的操作

假设您使用的是 .NET 4.0 及更高版本。

您是否尝试过查看 .NET 4.0 缓存系统?

使用对象缓存的一个很好的例子可以在这里找到