Developments in Digital
Developments in Digital

Share Static Content with NuGet Packages

One option for sharing static content such as scripts across projects until Universal Packages are more mature is to use NuGet packages.

To create the NuGet package, we specify the folders which we wish to include within the package as content files within a .nuspec file:

<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
  <metadata minClientVersion="3.3.0">
    <id>MyPackage</id>
    <version>0.1.0</version>
    <title>MyPackage</title>
    <authors></authors>
    <owners></owners>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>My package.</description>
    <contentFiles>
      <files include="scripts/**/*.ps*1" buildAction="None" />
    </contentFiles>
  </metadata>
  <files>
    <file src="**/*.ps*1" target="content" />
    <file src="**/*.ps*1" target="contentFiles/any/any" />
  </files>
</package>

Here, we are creating a package of any PowerShell scripts under the local scripts folder.

Once the package is built and published, we can reference the package as normal within a Visual Studio project.

For projects that don't use Visual Studio project files, you can also manually create a packages.config file to reference your NuGet package:

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="MyPackage" version="0.1.0" />  
</packages>

You can use the NuGet cli to do a restore outside the context of Visual Studio:

nuget restore packages.config -PackagesDirectory ./packages

For packages.config based restores, the contents of the NuGet package will be available in:

packages\MyPackage.<version>\content