4

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

0

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

Comments

0

for me worked

  <Target Name="GenerateABI" BeforeTargets="BeforeCompile">
    <Exec Command="powershell.exe -ExecutionPolicy Bypass -File &quot;$(MSBuildProjectDirectory)\Scripts\GenerateABI.ps1&quot;" />
  </Target>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.