0

I have a NuGet package I'm writting to automatically update the year in a project's copyright. It works, but the problem is, the target seems to run when it's not supposed to. For example: The moment I add the NuGet package it will immediately run, thus causing Visual Studio to complain that the project file I just imported it into was modified.

I've noticed that if I change the copyright year before the build, somehow the target will run before the build and the year will have already been corrected by the time the target runs during the build.

How do I tell MSBuild targets to only run when the user requests a build?

Edit: Currently I'm testing this with a .NET Core project, but it should be able to work with Framework and Standard too.

File: build\TriggersTools.AssemblyCopyrightYear.targets

<!-- Modify the assembly's copyright year -->
<Project>
  <PropertyGroup>
    <TaskFolder Condition=" '$(MSBuildRuntimeType)' == 'Core' ">netstandard1.3</TaskFolder>
    <TaskFolder Condition=" '$(MSBuildRuntimeType)' != 'Core' ">net46</TaskFolder>
    <TaskAssembly>$(MSBuildThisFileDirectory)..\tasks\$(TaskFolder)\TriggersTools.AssemblyCopyrightYear.dll</TaskAssembly>
  </PropertyGroup>

  <UsingTask TaskName="TriggersTools.AssemblyCopyrightYear.CopyrightYear" AssemblyFile="$(TaskAssembly)"/>
  <Target Name="TriggersToolsAssemblyCopyrightYearAssign" BeforeTargets="PrepareForBuild">
    <!-- Run the CopyrightYear task -->
    <CopyrightYear ProjectPath="$(MSBuildProjectFullPath)" />
  </Target>
</Project>

File: buildMultiTargets\TriggersTools.AssemblyCopyrightYear.targets

<!-- Modify the assembly's copyright year -->
<Project>
  <!-- Import the build .targets file since this one is no different -->
  <Import Project="$(MSBuildThisFileDirectory)..\build\TriggersTools.AssemblyCopyrightYear.targets"/>
</Project>
trigger_segfault
  • 554
  • 1
  • 6
  • 23
  • 1
    How about to use BeforeBuild as BeforeTargets for your build? I think MSBuild exepects no changing files when the build has started. See https://stackoverflow.com/questions/5926311/what-is-the-difference-between-a-prebuildevent-beforebuild-target-and-beforecom. – Alois Kraus Jul 15 '18 at 21:07
  • Huh, I was sure I tried BeforeBuild and got the same result but now it works. But unfortunately the other issue you raised still seems to be a problem. When my target modifies the project file's Copyright tag, the change is not honored in the current build, and I'd have to rebuild again to solve that. In general I've been having a lot of trouble with Visual Studio and NuGet packages not using the most up-to-date local feed version so this may be a delay in it working properly. – trigger_segfault Jul 15 '18 at 21:36
  • Nope, it seems that the most up to date version is running, and the MSBuild is still ignoring the .csproj changes. (I should also note, I'm testing this on a .NET Core project, which I've read has certain peculiarities with build targets.) – trigger_segfault Jul 15 '18 at 21:39
  • How about referencing a global AssemblyInfo.cs file which contains the copyright year? That is usually sufficient. What does your patching dll actually do? Patch a .cs file? – Alois Kraus Jul 15 '18 at 21:58
  • It checks `AssemblyInfo.cs` files' `AssemblyCopyrightAttribute` and also checks `PropertyGroup/Copyright` tags in the .csproj file. It will modify any matches it comes into contact with. The goal of the NuGet package is to not require any editing other than including the package, and it doing the rest automatically. – trigger_segfault Jul 15 '18 at 22:01
  • I think you should not edit the csproj files while building a target. Version and Copyright should be located in separate files like nuspec files. Then everything should work. – Alois Kraus Jul 16 '18 at 08:21
  • That's what I was afraid of however, I found a good compromise. Since apparently I can update the Copyright property from my .targets file, I now do that so the current project is up to date, and for the entire file so it doesn't look like the Copyright is out of date when editing the settings. – trigger_segfault Jul 16 '18 at 13:44

0 Answers0