PowerShell: Creating Custom nuget.config
Today, we are going to discuss a PowerShell script which we would be using to create a custom nuget.config file. Before we proceed to create a nuget.config file, it’s important to understand what this config file is and why is it used.
What is nuget.config?
A NuGet.Config file is a simple XML text file containing key/value pairs which are used to control the behaviour of NuGet CLI. The config file contains a top-level <configuration> node, which then contains the section elements mentioned below to control the behaviour.
- config
- bindingRedirects
- packageRestore
- solution
- packageSources
- packageSourceCredentials
- apikeys
- disabledPackageSources
- activePackageSource
- trustedSigners
- fallbackPackageFolders
- packageManagement
You may read more about the same at: https://docs.microsoft.com/en-us/nuget/reference/nuget-config-file
You may have a look at a sample nuget.config at: https://docs.microsoft.com/en-us/nuget/reference/nuget-config-file#example-config-file
Creating Custom Nuget.Config
Now coming back to the topic, we’ll be creating nuget.config using PowerShell. I would creating a nuget.config file that we have discussed before the blog: https://akhilsharma.work/protip-nuget-local-package-installation/
The plan we are going to follow to create our particular config file is:
- Set the path to current directory for creating the nuget.config
- Initialize the XML Writer
- Start the configuration node
- Start the packageSources node
- Provide the local package source
- Close the packageSources node
- Start the disabledPackageSources node
- Disable the package sources which we might impact your process.
- Close the disabledPackageSources node
- Close the configuration node
If the plan is followed properly, we would come up with the following script:
$currentDir = pwd
$Path = $currentDir.Path
$Path = $Path + "\Nuget.config"
# Set The Formatting
$xmlsettings = New-Object System.Xml.XmlWriterSettings
$xmlsettings.Indent = $true
$xmlsettings.IndentChars = " "
# Set the File Name Create The Document
$XmlWriter = [System.XML.XmlWriter]::Create($Path, $xmlsettings)
# Write the XML Decleration and set the XSL
$xmlWriter.WriteStartDocument()
# Start the Root Element
$xmlWriter.WriteStartElement("configuration") # <-- Start <configuration>
$xmlWriter.WriteStartElement("packageSources") # <-- Start <packageSources>
# add a node with attributes and content:
$XmlWriter.WriteStartElement('add')
$XmlWriter.WriteAttributeString('key', 'LocalSource')
$XmlWriter.WriteAttributeString('value', 'D:\this-is-some-optional-directory\some-path')
$xmlWriter.WriteEndElement()
$xmlWriter.WriteEndElement()
$xmlWriter.WriteStartElement("disabledPackageSources") # <-- Start <packageSources>
# add a node with attributes and content:
# add a node with attributes and content:
$XmlWriter.WriteStartElement('add')
$XmlWriter.WriteAttributeString('key', 'nuget.org')
$XmlWriter.WriteAttributeString('value', 'true')
$xmlWriter.WriteEndElement()
# add a node with attributes and content:
$XmlWriter.WriteStartElement('add')
$XmlWriter.WriteAttributeString('key', 'Microsoft Visual Studio Offline Packages')
$XmlWriter.WriteAttributeString('value', 'true')
$xmlWriter.WriteEndElement()
$xmlWriter.WriteEndElement() # <-- End <packageSources>
$xmlWriter.WriteEndElement() # <-- End <configuration>
$xmlWriter.WriteEndDocument()
$xmlWriter.Flush()
$xmlWriter.Close()
You may access the complete script at: https://github.com/akhilvatts/NuGet-Cli-Utility-Scripts/blob/main/Create-Custom-NuGetConfig-PowerShell/custom-nuget-config.ps1
The output of this script would be as below:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="LocalSource" value="D:\this-is-some-optional-directory\some-path" />
</packageSources>
<disabledPackageSources>
<!-- Disable any package sources here -->
<add key="nuget.org" value="true" />
<add key="Microsoft Visual Studio Offline Packages" value="true" />
</disabledPackageSources>
</configuration>
I hope this blog helped!
Cheers!