简体   繁体   English

如何以编程方式更新Visual Studio项目中的项目引用

[英]How to programmatically update project references in a Visual Studio project

I have a file structure like the following: 我的文件结构如下:

- binaries
   - binary1.dll
- dev
   - <developer-name>
      - a.csproj
-trunk
    - a.csproj

Developers who need to work on a.csproj will create a branch in dev and they will work from there. 需要使用a.csproj的开发人员将在dev中创建一个分支,然后从那里开始工作。 Our projects need to have a reference to binary1.dll in the binaries directory. 我们的项目需要在binaries目录中引用binary1.dll。

If the reference is a relative path (the Visual Studio default) then the path will not work both for the project in trunk and the project in the developer's branch. 如果引用是相对路径(Visual Studio默认),则该路径将对主干中的项目以及开发人员分支中的项目均不起作用。

To work around this problem I thought of creating an environment variable and using that in the project file instead: 要变通解决此问题,我想到了创建一个环境变量,并在项目文件中使用它:

<Reference Include="binary1">
   <HintPath>$(MY_ENV_VAR)\binary1.dll</HintPath>
</Reference>

This works perfectly, but I wanted to help developers add references more easily, so I wrote a program that will convert relative paths that point to binary1.dll (for example, from the dev branch a.csproj it would change the path ../../binaries/binary1.dll to $(MY_ENV_VAR)\\binary1.dll ) but I have not figured out how to get that to work. 这完美地工作了,但是我想帮助开发人员更轻松地添加引用,因此我编写了一个程序,该程序将转换指向Binary1.dll的相对路径(例如,从dev分支a.csproj它将更改路径../../binaries/binary1.dll$(MY_ENV_VAR)\\binary1.dll ),但是我还没有弄清楚如何$(MY_ENV_VAR)\\binary1.dll工作。

If I use a pre-build event, the project is already loaded into memory and the event return an error because it cannot write the project file. 如果使用预构建事件,则该项目已经加载到内存中,并且该事件返回错误,因为它无法写入项目文件。

Then I realized that I could override MSBuild targets, and attempted with the targets: BeforeCompiler , AfterCompiler , BeforeBuild , AfterBuild and in all of them the project is already locked. 然后我意识到我可以覆盖MSBuild目标,并尝试使用以下目标: BeforeCompilerAfterCompilerBeforeBuildAfterBuild并且所有这些项目都已锁定。

Then I ran into this answer and I modified my code to call the executable in the GenerateApplicationManifest target, but that one doesn't seem to call the executable at all. 然后,我遇到了这个答案,并修改了代码以在GenerateApplicationManifest目标中调用可执行文件,但似乎根本没有调用可执行文件。

Some other ideas that I have been playing with are creating a new project that does the updating of the second project and have a link between them, but that would duplicate the number of projects. 我一直在考虑的其他一些想法是创建一个新项目,该项目将更新第二个项目并在它们之间建立链接,但是那会重复项目的数量。

I could also just change the depth of trunk, but that only hides the problem and doesn't really solve it. 我也可以改变树干的深度,但这只是隐藏了问题,并没有真正解决问题。 When developers create a branch inside their dev branch to work on different features or bugs then I have the same problem all over again. 当开发人员在他们的dev分支中创建一个分支来处理不同的功能或错误时,我将再次遇到相同的问题。

There might also be another feature which fixes this in a more elegant way, but I haven't been able to find anything. 可能还有另一个功能可以更优雅地解决此问题,但是我什么也找不到。

So my question: How do I get MS Build or pre-build events to modify a CS project? 所以我的问题是:如何获得MS Build或pre-build事件来修改CS项目?

I found a way to do this. 我找到了一种方法。 The issue was that the Visual Studio process itself was locking the project, but Visual Studio could overwrite the file. 问题是Visual Studio进程本身正在锁定项目,但是Visual Studio可能会覆盖文件。 I ended up modifying a.csproj to include: 我最终将a.csproj修改为包括:

<Target Name="BeforeBuild">
    <Exec Command="UpdateReferences.exe" /> <!-- This creates the $(ProjectPath).new file -->
    <Move SourceFiles="$(ProjectPath).new" DestinationFiles="$(ProjectPath)" />
  </Target>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM