0

Problem

How to share postbuild Powershell Script in multiple solution so that each solution can invoke the script in post build event?

Solution Approach

  • Created a local PSGallary

  • Pushed my script into the gallery

  • In the VS, added the new PSGallary as a nuget Source

  • Pulled the postbuild script as the nuget package in the solution

Now i am stuck. The script is saved outside the solution in a nuget folder like this. enter image description here

I don't want to hardcode the path of the script since, the versions changes and so does the path. How can i get the path of the actual nuget package content in post build event without hard coding it? is there other better approaches ?

Community
  • 1
  • 1
Rabin
  • 418
  • 3
  • 13

2 Answers2

1

You might use the Nuget CLI with the OutputDirectory parameter to install the package in the project directory. Afterwards you can use the $(ProjectDir) macro in your post build event.

Peter Schneider
  • 2,879
  • 1
  • 14
  • 17
  • Its an option. I tried it. It looks like I cannot manage/update package through VS > Manage Nuget Packages> update or delete. Also if i update the package using the command the , it will change the path of the script .. – Rabin Oct 15 '19 at 20:20
1

Not sure if it's better approach for you, here're two directions which may help:

1.Nuget way: Include your powershell script as content files when creating the nuget package, then when you build the project, the script will be copied to output path automatically. So you can specify the post-build event with macro $(OutputPath). Anyone interested in it can check blog here.

For your .net core project which uses PackageReference, use the ContentFiles element. There's many topics about how to create the package with content files using ContentFiles element online, so I won't talk details here, but if you want to choose this way and meet some issues, let me know that :)

2.MSBuild way: Using something like Directory.Build.props to customize your build. Since your original problem is to share postbuild Powershell Script in multiple solution, let's place the script test.ps1 in path C:\PostBuildScript\, let's create a .txt file with content below, and then rename it to Directory.Build.props:

<Project>
  <PropertyGroup>
    <ScriptPath>C:\xxx\xxx\</ScriptPath>
  </PropertyGroup>
</Project>

Now if we place this file in Solution-A's directory, all projects in A solution can recognize $(ScriptPath), so we can use parameter like $(ScriptPath)test.ps1 in build event. If you meet code9009 when calling PS script in build event, see here.

And if those solutions are under folder TestFolder, place the Directory.Build.props file in that folder, it will work for all solutions in that folder. Also, there's many directions to customize your post-build events, it depends on your needs...

LoLance
  • 25,666
  • 1
  • 39
  • 73