Niklaus Wirth
Niklaus Wirth

Reputation: 870

Run Custom Script after Publish to file system Visual Studio 2017

I created publish profile that publish to File System, and I need to run custom script after publish from context menu of visual studio 2017 is finished.

Upvotes: 4

Views: 3577

Answers (1)

Mr Qian
Mr Qian

Reputation: 23780

Run Custom Script after Publish to file system Visual Studio 2017

I think you can just create a custom target in xxxx.csproj file or xxxxxx.pubxml file.

Just like this:

 <Target Name="RunCustomScript" AfterTargets="GatherAllFilesToPublish">
 <Exec Command="xxxx\xxxx.ps1" />
  </Target>

You can set this target in xxxx.csprojor xxxx.pubxml.

Note that GatherAllFilesToPublish is one of the publish process's system target and when this custom target depends on GatherAllFilesToPublish, it will always run and execute the script adter publish process.

Update 1

Sorry for not understanding your requirements in time. I assume you want to do some operation such as copying when all the imported files are published into the publish folder.

In general, this step does not belong to any system targets and MS has confirmed, that when publishing to file system they don't have any target to launch after that.

"We currently do not support executing custom targets after publish from VS for the file system protocol."

However we can try this way:

1) create a custom target which uses AfterTargets="CopyAllFilesToSingleFolderForPackage" (runs just before the files are copied to the publish location).

2) In custom target, use asynchronous execution AsyncExec of the script to ensure that the publish file to publish folder operation is executed while the script is running.

3) After that, in the script, the execution of the content is delayed by 5 seconds (the time can be changed according to the specific release time of the packaged file) to ensure that the script content is executed after the system publishes all the files.

Step

1) install the nuget package called MSBuild.Extension.Pack into your project.

2) create the custom target and use its AsyncExec to execute your script.

<Target Name="RunCustomScript" AfterTargets="GatherAllFilesToPublish">
    <AsyncExec command="powershell.exe C:\Users\xxxxx\xxxx.ps1" />
</Target>

3) Open your script, please add this on the top of all the content which means that sleep for 5 seconds before executing the contents of the script.

Start-Sleep -s 5  // 5 means 5 seconds

Therefore, the script will executes after all the files are published into the publish folder.

Hope it could help you.

Upvotes: 5

Related Questions