Skip to content

Instantly share code, notes, and snippets.

@neremin
Created April 29, 2018 13:30
Show Gist options
  • Save neremin/6373bc180b7f284572a47f20e1d0ea97 to your computer and use it in GitHub Desktop.
Save neremin/6373bc180b7f284572a47f20e1d0ea97 to your computer and use it in GitHub Desktop.
Offline NuGet Repository installation.
In case you work offline, copy Install.ps1 and Uninstall.ps1 to your directory %_LOCAL_REPO_% (which is just random name for reference) with nuget packages.
Using PowerShell:
1. Close all running VS (or them after installation).
3. Run Install.ps1 script from %_LOCAL_REPO_% folder (to remove: run Uninstall.ps1).
Using VS user interface (does not require VS reloading):
1. Copy/move packages from %_LOCAL_REPO_% to some local directory of your choice. Let's call it %_LOCAL_REPO_% for further reference.
2. Run VS
3. Tools => Options => NuGet Package Manager => Package Sources:
4. In the "Available package sources:" Add "Local Repo" pointing to your %_LOCAL_REPO_% directory, and move it up in the list to be the first Repo for queries.
5. OK Options dialog [and (re-)build your solution(s) if needed].
NOTE: You can copy manually downloaded nuget packages to that %_LOCAL_REPO_% directory to use them normally.
function NuGetAddLocalRepo ([string] $localRepoPath)
{
## NuGet Package Manager config file path
$config = "$Env:APPDATA\NuGet\NuGet.Config"
If ( -Not (Test-Path($config)) ) {
Write-Host "NuGet Package Manager config not found!"
return
}
## Open an parse config XML
$xml = [xml](Get-Content $config)
## Value: Current directory absolute path
$repoDir = (get-item -path $localRepoPath)
$repoPath = $repoDir.FullName
$sources = $xml.configuration.packageSources
## Find source package for current dir
$repoNode = $sources.add | where {$_.value -eq $repoPath}
if ($repoNode) {
$disabledNode = $xml.configuration.disabledPackageSources.add |
where {$_.key -eq $repoNode.key -and $_.value -eq "true" }
if ($disabledNode) {
[void] $disabledNode.ParentNode.RemoveChild($disabledNode)
$xml.Save([string]$config)
Write-Host "Re-Enabled '$($repoNode.key)'."
Write-Host "Please reload Visual Studio for changes to take effect!"
return
}
Write-Host "Local repo is already installed and enabled."
return
}
$repoName = $repoDir.Name
$repoNode = $sources.add | where {$_.key -eq $repoName}
if ($repoNode) {
$repoName = $repoDir.FullName.Replace("\", "_").Replace("/", "_")
}
$repoNode = $sources.add | where {$_.key -eq $repoName}
if ($repoNode) {
Write-Host "Repo name conflict."
return
}
$repoNode = $xml.CreateElement("add")
$repoNode.SetAttribute("key", $repoName)
$repoNode.SetAttribute("value", $repoPath)
[void] $sources.PrependChild($repoNode)
$xml.Save([string]$config)
Write-Host "Successfully added local repo!"
Write-Host "Please reload Visual Studio for changes to take effect!"
}
Write-Host "Installing current directory as NuGet packages source..."
NuGetAddLocalRepo (get-item -path ".\")
function NuGetRemoveLocalRepo ([string] $localRepoPath)
{
## NuGet Package Manager config file path
$config = "$Env:APPDATA\NuGet\NuGet.Config"
If ( -Not (Test-Path($config)) ) {
Write-Host "NuGet Package Manager config not found!"
return
}
## Open an parse config XML
$xml = [xml](Get-Content $config)
## Value: Current directory absolute path
$repoDir = (get-item -path $localRepoPath)
$repoPath = $repoDir.FullName
$sources = $xml.configuration.packageSources
## Find source package for current dir
$repoNode = $sources.add | where {$_.value -eq $repoPath}
if (!$repoNode) {
Write-Host "Repo for the given path was not installed."
return
}
$disabledNode = $xml.configuration.disabledPackageSources.add |
where {$_.key -eq $repoNode.key }
if ($disabledNode) {
[void] $disabledNode.ParentNode.RemoveChild($disabledNode)
}
[void] $repoNode.ParentNode.RemoveChild($repoNode)
$xml.Save([string]$config)
Write-Host "Local repo has been uninstalled successfully!"
Write-Host "Please reload Visual Studio for changes to take effect!"
}
Write-Host "Uninstalling current directory as NuGet packages source..."
NuGetRemoveLocalRepo (get-item -path ".\")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment