Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run a PowerShell script before my .NET project is transfered into memory and built?

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

like image 812
miran80 Avatar asked Sep 16 '25 14:09

miran80


1 Answers

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

like image 132
jalsh Avatar answered Sep 19 '25 02:09

jalsh