0

I have the following MSBuild script:

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

  <PropertyGroup>
...
    <BuildDependsOn>
      NSwag;
      $(BuildDependsOn)
    </BuildDependsOn>
    <!--<AfterTransform>NSwag</AfterTransform>-->
  </PropertyGroup>

  <ItemGroup>
...
  </ItemGroup>

  <Target Name="NSwag" BeforeTargets="BeforeBuild">
    <Message Text="Generating C# client code via NSwag" Importance="high" />

    <!-- ISSUE HERE -->
    <Copy SourceFiles="..\..\MyClient.cs" DestinationFiles="Gen\MyClient.cs" />
  </Target>

</Project>

The Target "NSwag" above is going to be used for code generation tool. But to simplify things, I use here just a file copy command.

The issue is that the .cs files added within this Target are not visible in the MSBuild compilation:

The type or namespace name 'MyClient' does not exist in the namespace 'MyNamespace'

NOTE: The issue occurs only if the file didn't exist in the destination folder.

NOTE: I was trying to mangle with the following but with no success so far:

  <Target Name="RemoveSourceCodeDuplicates" BeforeTargets="BeforeBuild;BeforeRebuild" DependsOnTargets="UpdateGeneratedFiles">
    <RemoveDuplicates Inputs="@(Compile)">
      <Output TaskParameter="Filtered" ItemName="Compile"/>
    </RemoveDuplicates>
  </Target>

and

  <Target Name="UpdateGeneratedFiles" BeforeTargets="BeforeBuild;BeforeRebuild" DependsOnTargets="NSwag">
    <ItemGroup>
      <Compile Include="Gen\MyClient.cs" Condition="!Exists('Gen\MyClient.cs')" />
    </ItemGroup>
  </Target>

What am I missing here?

Chris W
  • 1,562
  • 20
  • 27

1 Answers1

1

I think I found a workaround for that - check and include the files first (UpdateGeneratedFiles target), then generate them (NSwag target). See the script below:

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

  <PropertyGroup>
...
    <BuildDependsOn>
      NSwag;
      $(BuildDependsOn)
    </BuildDependsOn>
  </PropertyGroup>

  <Target Name="NSwag" BeforeTargets="BeforeBuild;BeforeRebuild"
          DependsOnTargets="UpdateGeneratedFiles">
    <Message Text="Generating C# client code via NSwag" Importance="high" />

    <Copy SourceFiles="..\..\MyClient.cs" DestinationFiles="Gen\MyClient.cs" />
  </Target>

  <Target Name="UpdateGeneratedFiles" BeforeTargets="BeforeBuild;BeforeRebuild" >
    <ItemGroup>
    <Compile Include="Gen\MyClient.cs" Condition="!Exists('Gen\MyClient.cs')" />
    </ItemGroup>
  </Target>

</Project>
Chris W
  • 1,562
  • 20
  • 27