Have you ever wanted to ship build updates in a NuGet package? If so then you may have noticed that there are issues if you need to import a .targets file which is contained in a NuGet package. To be brief, the issue is that by the time the .targets file is being restored with NuGet it’s already too late because the build has already started. So we need an easy way to invoke NuGet package restore for a given project before the build for the solution/project starts. In this entry you will see how we can use a generated build script to help with this.
A few months back I discovered this issue and blogged about it at SlowCheetah build server support updated. The solution that I implemented was to create a new build script, packageRestore.proj, which is placed in the root of the project when the SlowCheetah NuGet package is installed. If you want to invoke the package restore from a build server then you can just add packageRestore.proj to the list of items to build before the .sln/.csproj file which you are intending to build. I’ve now refactored this into its own NuGet package, PackageRestore.
Here is how you can try it out.
- Create a new project
- Add a few NuGet packages to the project
- Enable NuGet package restore
- Add the PackageRestore package (the command you can use is Install-Package PackageRestore –pre)
- Close solution
- Delete packages folder
- Execute the command msbuild.exe packageRestore.proj
After executing the command you will see that the packages folder along with all its content will be restored. For CI servers when you define your build script all you do is build packageRestore.proj before you build the target .sln/.csproj file.
If you are shipping build updates through NuGet and want to be able to enable package restore in a similar way a SlowCheetah then when you create your NuGet package add PackageRestore as a dependency.
This is an open source project so you can view the source, and help contribute, at https://github.com/sayedihashimi/package-restore.