Are there any .NET Core CLI tasks before installing?

Question

My guess is that when it msbuild

is going to compile the source files, it creates a list of files to build based on the rules Include

and the Exclude

project.

Is there a way to get the task done before the list of files to compile is evaluated?

This means that you can generate a source file and get it in assembly.

Ongoing research and testing

I am creating a .NET Core CLI tool that needs to be run before building a project that uses it because it (CLI tool) generates a file that needs to be included in the assembly.

The project is created using the new system .csproj

, not the old one project.json

.

Along with my .NET .NET CLI project, I created a library project for testing purposes.

If I add this to the .csproj

test library:

<ItemGroup>
    <DotNetCliToolReference Include="MyCliTool" Version="x.x.x" />
    <!-- here x.x.x is just a placeholder -->
</ItemGroup>

<Target Name="MyCliToolTarget" AfterTargets="Restore" BeforeTargets="BeforeBuild">
    <Exec Command="dotnet my-cli-tool" />
</Target>

      

then the file generated by the CLI tool is not counted in compilation if it did not exist before. This means that when a file exists, that's okay, but it also means that the very first build (after cloning, cleaning, or whatever) will always fail.

I've tried several different targets for BeforeTargets

, but I couldn't find a way to get it to work. I tried to set my target in InitialTargets

Project

node but that didn't work either. I tried to set the Outputs

Target

node property to the filename generated by the CLI tool, but the same thing, it fails.

The only solution I have worked with is to manually add the directive Compile

like below:

<ItemGroup>
    <Compile Include="MyGeneratedFile.cs" />
</ItemGroup>

      

This solution is fine at the moment, but the file name might change depending on the CLI tool settings, and this will create two places where the file name needs to be changed when changed, like this:

<ItemGroup>
    <Compile Include="PATH\TO\CUSTOM_FILENAME_HERE.CS" />
    <DotNetCliToolReference Include="MyCliTool" Version="x.x.x" />
</ItemGroup>

<Target Name="MyCliToolTarget" AfterTargets="Restore" BeforeTargets="BeforeBuild">
    <Exec Command="dotnet my-cli-tool --output PATH\TO\CUSTOM_FILENAME_HERE.CS" />
</Target>

      

(see CUSTOM_FILENAME_HERE.CS appears twice)

I know I can use a constant like below:

<PropertyGroup>
    <MyFilename>PATH\TO\CUSTOM_FILENAME_HERE.CS</MyFilename>
</PropertyGroup>

<ItemGroup>
    <Compile Condition="!Exists('$(MyFilename)')" Include="$(MyFilename)" />
    <DotNetCliToolReference Include="MyCliTool" Version="x.x.x" />
</ItemGroup>

<Target Name="MyCliToolTarget" AfterTargets="Restore" BeforeTargets="BeforeBuild">
    <Exec Command="dotnet my-cli-tool --output $(MyFilename)" />
</Target>

      

but I am not satisfied with this approach, so it is difficult for a lambda user to integrate, assuming that this is still a simplified version, because the CLI tool can accept several other options, meaning there are other variables, bla bla bla.

I am using .NET Core SDK 1.0.1.

Sorry for the long question and my noobness with msbuild.

Thanks in advance for your time and help.




Note: Call dotnet my-cli-tool

on a pre-build event, as in:

<PropertyGroup>
    <PreBuildEvent>dotnet my-cli-tool</PreBuildEvent>
</PropertyGroup>

      

doesn't work at all, I get the following error:

Code: MSB3073 Description: The "dotnet my-cli-tool" command came out with code 1. Project: Test library project, not tool one File: C: \ Program Files (x86) \ Microsoft Visual Studio \ 2017 \ Community \ MSBuild \ 15.0 \ Bin \ Microsoft.Common.CurrentVersion.targets Line: 4935

It works fine though:

<PropertyGroup>
    <PreBuildEvent>echo meh</PreBuildEvent>
</PropertyGroup>

      

so this is not a bug with pre-build events.

Anyway, this is another story that I'm a little worried about right now.

+3


source to share


1 answer


The "default items", which he calls the .NET SDK, are part of the static evaluation of the project file - prior to running any target. Thus, you need a target that runs before the items are @(Compile)

needed.

The trick is to include files added to the file system after running the custom tool. This can be done by rescanning all the files and excluding those already part of the project inside the target that is run before the build:



  <Target Name="GenerateSomeFiles" BeforeTargets="BeforeBuild">
    <Exec Command="dotnet my-tool" />
    <ItemGroup>
      <Compile Include="**/*$(DefaultLanguageSourceExtension)"
               Exclude="$(DefaultItemExcludes);$(DefaultExcludesInProjectFolder);$(BaseIntermediateOutputPath)**;$(BaseOutputPath)**;@(Compile)" />
    </ItemGroup>
  </Target>

      

+5


source







All Articles