2011-10-04 28 views
4

我有一个VS 2010解决方案,其中包含许多项目。 项目参考解决方案中的其他项目。 我注意到,当我在的csproj文件中像这样有一个错误的项目引用路径:自动修复ProjectReference在构建csproj文件之前包含路径

<ProjectReference Include="..\..\..\..\WrongFolder\OtherProject.csproj"> 
    <Project>{CD795AA6-9DC4-4451-A8BA-29BACF847AAC}</Project> 
    <Name>OtherProject</Name> 
</ProjectReference> 

的Visual Studio可以解决这个问题上打开了解决方案:

<ProjectReference Include="..\..\..\..\RightFolder\OtherProject.csproj"> 
    <Project>{CD795AA6-9DC4-4451-A8BA-29BACF847AAC}</Project> 
    <Name>OtherProject</Name> 
</ProjectReference> 

我想它使用GUID从Project元素中唯一标识解决方案中的项目,以便修复路径。

MSBuild另一方面似乎并没有解决这一问题,并且构建解决方案失败。

有没有办法让MSBuild修复路径,或者使用其他工具或命令将其作为预构建步骤,以便解决方案可以正确构建?

谢谢!

+0

您是否找出解决方案?我会认为会有一些你可以使用的环境变量/宏东西(类似于'Include =“$(ProjectPath:NameOfProject)\ NameOfProject.csproj”'),但还没有找到任何东西。 – CuppM

+0

我没有:(只是手动修复文件,据我记忆。谢谢你的建议,我可能会尝试一些日子 – axk

回答

1

这是VisualStudio功能的一部分。但是你可以在构建之前调用一个工具来解决引用。以下是您可以详细说明的草稿代码:

using System; 
using System.IO; 
using System.Collections.Generic; 
using System.Text.RegularExpressions; 
using System.Xml; 

namespace FixProjectReferences 
{ 

class Program 
{ 

// License: This work is licensed under a Creative Commons 
// Attribution-ShareAlike 3.0 Unported License. 
// Author: Marlos Fabris 
// Summary: Updates the project references in csproj. 
// Param: 
// args[0] = Main project (c:\mainProject.csproj) 
// args[1] = Folder to scan other projects (c:\other) 
static void Main(string[] args) 
{ 
    string mainProject = args[0]; 
    string folder = args[1]; 
    FileInfo mainPrjInfo = new FileInfo(mainProject); 
    string currentDir = Directory.GetCurrentDirectory(); 

    // Lists all project files in the directory specified 
    // and scans the GUID's. 
    DirectoryInfo info = new DirectoryInfo(folder); 
    FileInfo[] projects = info.GetFiles("*.csproj", 
     SearchOption.AllDirectories); 

    Dictionary<Guid, string> prjGuids = new Dictionary<Guid, string>(); 

    foreach (var project in projects) 
    { 
     if (project.FullName == mainPrjInfo.FullName) 
      continue; 

     Regex regex = new Regex("<ProjectGuid>(\\{.*?\\})</ProjectGuid>"); 
     Match match = regex.Match(File.ReadAllText(project.FullName)); 
     string guid = match.Groups[1].Value; 

     prjGuids.Add(new Guid(guid), project.FullName); 
    } 

    // Loads the main project and verifies if the references are valid. 
    // If not, updates with the correct ones from the list 
    // previously generated. 
    XmlDocument doc = new XmlDocument(); 
    doc.Load(mainPrjInfo.FullName); 
    XmlNamespaceManager ns = new XmlNamespaceManager(doc.NameTable); 
    ns.AddNamespace("ns", 
     "http://schemas.microsoft.com/developer/msbuild/2003"); 
    var nodes = doc.SelectNodes("//ns:ProjectReference", ns); 
    foreach (XmlNode node in nodes) 
    { 
     string referencePath = node.Attributes["Include"].Value; 
     string path = Path.Combine(mainPrjInfo.Directory.FullName, 
      referencePath); 
     if (File.Exists(path)) 
      continue; 

     string projectGuid = node.SelectSingleNode("./ns:Project", 
      ns).InnerText; 
     Guid tempGuid = new Guid(projectGuid); 
     if (prjGuids.ContainsKey(tempGuid)) 
     { 
      node.Attributes["Include"].Value = prjGuids[tempGuid]; 
     } 
    } 

    doc.Save(mainPrjInfo.FullName); 
} 

} 

} 
相关问题