4

I have setup a python project in Visual Studio 2017 Enterprise (Version 15.4) as part of a mixed-project solution also containing various C#(.Net 4.6.2, .Net Core 2.0 and Xamarin) projects. During the build I want to generate several .py source code files using some self-written C# tool which is also part of the build. Additionally all .py files listed as "Compile" items in the .pyproj file should be compressed into a .zip file and written to some output directory.

In my .pyprof MSBuild file I craeated two "custom build step" targets like so:

<Target Name="MyPreBuildStep" BeforeTargets="Build">
    Call self-written py-Code generator
</Target>

<Target Name="MyPostBuildStep" AfterTargets="Build" 
    Inputs="@(Compile)" 
    Outputs="$(OutputPath)\$(PythonProjectName).zip">
   <Zip ZipFileName="$(OutputPath)\$(PythonProjectName).zip" 
      WorkingDirectory="$(MSBuildProjectDirectory)\..\" Files="@(Compile)" />
</Target>

The Zip Task being imported from the MSBuildCommunityTasks.

If I right-click on the project in Visual Studio and hit "Rebuild" everything works fine. The py-code is generated in advance and all @(Compile) items are put into the .zip file.

Unfortunately the normal "Build" NEVER invokes the above targets. So with my current setup, I can build my 40-projects containing solution just by hitting "Build" BUT the python projects needs an extra-invitation by hitting "Rebuild". Very annoying.

I tried to investigate the contents of "Microsoft.Python.Tools.Web.targets" and "Microsoft.Python.Tools.targets". Somewhere inside these files a comment says: "The Build target is invoked as part of the Publish phase." hmmm.

Can somebody tell me how to specify some simple custom build steps in a .pyproj that just work during a normal "Build"?

Thank you.

Chris M
  • 73
  • 7

1 Answers1

1

Build is only invoked when required, and this is determined based on comparing the last modified times of input and output files.

Mostly these are automatically collected, but in your case the output file is not recognized and so does not influence whether it needs to be built. (If you inspect Microsoft.PythonTools.targets and find the BuiltProjectOutputGroup and SourceFilesProjectOutputGroup targets, these are the ones that provide the files.)

In this case, you should only need to add the ZIP file as an output file so that it is checked. You can do this with:

<ItemGroup>
    <OutputFiles Include="$(OutputPath)\$(PythonProjectName).zip">
        <Visible>false</Visible>
    </OutputFiles>
</ItemGroup>

Note that it needs to be available without running the build target, so you have to be able to compute the path statically, but now the project should rebuild if this file does not exist or any of the source files are newer than it.

Zooba
  • 11,221
  • 3
  • 37
  • 40