2

I have a .csproj file with multiple targets. It starts like that:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFrameworks>net35;net40;net45;netcoreapp2.0;netstandard2.0</TargetFrameworks>
      ...

It is a C# project that uses Selenium, and need to update javascript file automatically using NPM. Each such javascript file should be marked as Embedded Resource.

I tried several ways to run NPM before build. I tried writing a batch file that runs before the build, I tried installing Nuget extension to update the NPM, and even tried to add a pre-build event that updates the files.

Problem is, it happens per target, while I only need it to happen once, before the build starts.

I searched all the relevant MSBuild docs Microsoft provides looking for a solution but in vain.

Currently I update the scripts like this:

<Target Name="PreBuild" BeforeTargets="PreBuildEvent">
  <Exec Command="cd $(ProjectDir)Properties\NodeResources&#xD;&#xA;call npm update &amp;&amp; copy node_modules\mycompany\firstpackage\dist ..\Resources &amp;&amp; copy node_modules\mycompany\secondpackage\dist ..\Resources" />
</Target>

<ItemGroup>
  <None Remove="Properties\Resources\script1.js" />
  <None Remove="Properties\Resources\script2.js" />
</ItemGroup>

<ItemGroup>
  <EmbeddedResource Include="Properties\Resources\script1.js" />
  <EmbeddedResource Include="Properties\Resources\script2.js" />
</ItemGroup>

Anyone has an idea how to do this?

Itai Bar-Haim
  • 1,686
  • 16
  • 40

1 Answers1

0

You need to understand how the building for multi-frameworks works. It is not taking a one project, and build it to multiple frameworks, but launch some processes depending on the count of frameworks.

So, there is no way to contact between processes.

But, I suggest you to create a stand alone csproj file (it's actually a project file with msbuild configuration), without real project, and inside him, override the build target with your npm command, and command for building the solution.

Another option is to hold a msbuild property or **environment variable` or temp file and so on, and update their status after first execute command, and check the status before the command. There is an unaswered question about this.

baruchiro
  • 5,088
  • 5
  • 44
  • 66