简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM