3

I already know how to run arbitrary PowerShell script before building starts.

<Target Name="BeforeBuild">
  <Exec Command="powershell.exe -executionpolicy bypass -command &quot;&amp; {.\script.ps1 '$(SolutionDir)'}&quot;" />
  <Sleep Delay="3000"/>
</Target>

My problem is that the script I want to run modifies part of a .cs file. But this script above runs after the code was already copied into memory and when it builds, it uses the content of .cs file before it was modified.

Is there a way to start buidling process but run PowerShell script earlier, before my code is copied into memory? You could say I want to run a preprocessor that will call an external executable before any compilation process even begins.

If not, is there a way to build the project twice in a row without having to click Build in my IDE twice? (dirty hack that would fix my issue)

Quick note about <Sleep Delay="5000"/>. Because my PowerShell script is starting a process that is asynchronous and very short I also added a Delay task I found posted here: https://stackoverflow.com/a/41213107

miran80
  • 945
  • 7
  • 22

1 Answers1

2

BeforeBuild target happens after compilation happens, so your code will already be compiled by then... to achieve the desired functionality, you should try using BeforeCompile target:

<Target Name="BeforeCompile">
  <Exec Command="powershell.exe -executionpolicy bypass -command &quot;&amp; {.\script.ps1 '$(SolutionDir)'}&quot;" />
  <Sleep Delay="3000"/>
</Target>

See this Microsoft Docs link for more info

jalsh
  • 801
  • 6
  • 18