我使用的工具是我的一位同事写的,它扫描目录并在解决方案文件和.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);
}
});
});
如果您需要更多说明,请与我们联系。
非常感谢你!
答案 0 :(得分:0)