Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute a PowerShell script only before a web deploy Publish task in VS 2012?

Currently I have a post-build event configured in my web project using Visual Studio 2012 like this:

enter image description here

This basically calls a PowerShell script to add a copyright notice to every .cs file.

What I'd like to do is to execute this powershell script only before Publishing the web app to the remote server. Doing so I won't experience a delay every time I need to debug the project. Do you know of any way of accomplishing this?


According to Sayed's answer, I customized a specific publish profile and added this:

<PipelineDependsOn>
  CustomBeforePublish;
  $(PipelineDependsOn);
</PipelineDependsOn>
</PropertyGroup>
<Target Name="CustomBeforePublish">
<Message Text="******* CustomBeforePublish *******" Importance="high" />
<Exec Command="powershell.exe -file &quot;$(ProjectDir)\Copyright.ps1&quot;" />
</Target>
like image 395
Leniel Maccaferri Avatar asked Oct 16 '12 03:10

Leniel Maccaferri


2 Answers

It depends on how you define before but below is one technique.

When you create a publish profile with VS2012 it will create you a .pubxml file in the Properties\PublishProfiles folder (My Project\PublishProfiles for VB). These are MSBuild files and you can edit them to customize the publish process. In your case you can inject a target into the publish process, before the publish actually occurs. You can do that by extending the PipelineDependsOn property as below.

<PropertyGroup>   <PipelineDependsOn>     CustomBeforePublish;     $(PipelineDependsOn);   </PipelineDependsOn> </PropertyGroup>  <Target Name="CustomBeforePublish">   <Message Text="********************************** CustomBeforePublish ***********************************" Importance="high"/> </Target> 

FYI regarding the customization of .wpp.targets, that was the only technique which we had for VS2010. My recommendation here is as follows; customize the .pubxml file for most cases and to only create a .wpp.targets file if you want to customize every publish of the given project.

like image 92
Sayed Ibrahim Hashimi Avatar answered Oct 07 '22 00:10

Sayed Ibrahim Hashimi


Sayed's answer nails the problem. However, I thought about providing a fully working answer (testing in Visual Studio 2017):

 <PropertyGroup>
    <PipelineDependsOn>
       PreBuildScript;
       $(PipelineDependsOn);
    </PipelineDependsOn>
  </PropertyGroup>

  <Target Name="PreBuildScript">
     <Message Text="Executing prebuild script" Importance="high"/>
     <Exec Command="powershell.exe -file &quot;$(ProjectDir)\InnerFolder\script.ps1&quot;" />
   </Target>

Note: This will execute for both Preview and actual Publish action, so one can find out pre-publish errors before the actual Publish.

like image 36
Alexei - check Codidact Avatar answered Oct 06 '22 22:10

Alexei - check Codidact