Rabin
Rabin

Reputation: 418

Including PSGallery nuget dependency in Visual studio solution

Problem

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

Solution Approach

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 ?

Upvotes: 0

Views: 112

Answers (2)

LoLance
LoLance

Reputation: 28126

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...

Upvotes: 1

Peter Schneider
Peter Schneider

Reputation: 2929

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.

Upvotes: 1

Related Questions