What's the current preferred method for setting up NuGet to have a common package directory across all solutions in a project? Also is there a way I can include PCL packages in a common folder across different projects (so that packages can be shared across projects that target different platforms). I've seen similar questions asked before but I cannot find a clear answer that pertains to a recent version of NuGet.
If you include a nuget.config file at a directory which is a common ancestor of all of your solutions, then when you open any those solutions withing Visual Studio it will use this configuration file to configure NuGet.
One of the things nuget.config configures is the location for the nuget packages folder for the solution.
Reference: https://docs.nuget.org/consume/nuget-config-file
Sample nuget.config: note that the repositoryPath
is with respect to the location of the nuget.config file.
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageRestore>
<add key="enabled" value="True" />
</packageRestore>
<config>
<add key="repositoryPath" value="packages" />
</config>
</configuration>
Update-package <package name> -reinstall
in these cases. That's worked for me. –
Emanate In case anyone else comes across this, here is how I solved the problem: John Carpenter steered me in the right direction. There is also some information I found on Nuget's wesite: https://docs.nuget.org/consume/package-restore/migrating-to-automatic-package-restore
In order to accomplish this I edited Owen Johnson's Powershell migration script here's a link to his script: https://github.com/owen2/AutomaticPackageRestoreMigrationScript
For me the script was not quite exhaustive enough and it didn't correct the hintpaths in the project files (which is a problem). *Note make sure you DO NOT RUN THIS if you don't have a copy of your code saved somewhere or in source control. No guarantees if this screws up your projects. Here's the script I used:
########################################
# Regex Patterns for Really Bad Things!
$listOfBadStuff = @(
#sln regex
"\s*(\.nuget\\NuGet\.(exe|targets)) = \1",
#*proj regexes
"\s*<Import Project=""(\$\(SolutionDir\))?(\d|\w|\s|\.|\\)*(\.nuget|package).*?/>",
"\s*<Target Name=""(EnsureNuGetPackageBuildImports|EnsureBclBuildImported)""(.|\n)*?>(.|\n)*?</Target>",
"\s*<RestorePackages>\w*</RestorePackages>",
#HintPaths
"\s*<HintPath>(\$\(SolutionDir\))?(\d|\w|\s|\.|\\)*package.*?</HintPath>"
)
#######################
# Delete NuGet.targets and package folders
ls -Recurse -include 'NuGet.exe','NuGet.targets','NuGet.config' |
foreach {
remove-item $_ -recurse -force
write-host deleted $_
}
#########################################################################################
# Fix Project and Solution Files to reverse damage done by "Enable NuGet Package Restore
ls -Recurse -include *.csproj, *.sln, *.fsproj, *.vbproj, *.wixproj, *.vcxproj |
foreach {
sp $_ IsReadOnly $false
$content = cat $_.FullName | Out-String
$origContent = $content
foreach($badStuff in $listOfBadStuff){
$content = $content -replace $badStuff, ""
}
if ($origContent -ne $content)
{
$content | out-file -encoding "UTF8" $_.FullName
write-host messed with $_.Name
}
}
I added a command to remove the hintpaths. After running the script you may get some Nuget errors because some packages need a hintpath. In order to fix these you need to reinstall those packages. The following link on Nuget Website shows you how to do this: http://blog.nuget.org/20121231/a-quick-tutorial-on-update-package-command.html
So the steps I took to perform the full migration: 1) Run the Powershell script at the root level of your code base. 2) Add your NuGet.config file to the root of your code base (use the one John Carpenter provided as a template) 3) Reinstall any packages that need to have a hintpath (should fix any Nuget errors you have). 4) Now when you build any solution, Nuget should place your packages in your central package folder.
My solution is different from the traditional ones here which add/edit the Nuget.config file. For my case, configuration to Nuget.config files to find the necessary packages didn't help any projects I had and the problem still persisted with all the namespace libraries not being found even with the common System library.
So, for some projects that can't locate the packages folder which was put directly under the solution folder, I did some tweaking and found out that the code script for the project file "{project-name}.csproj" mentions where the package is located.
However, the path was set relatively to the project file I assumed and hence, it kept looking for packages folder under this project directory instead of the root solution container.
An example from my case (You can open the script for these files through VSCode or other text editors than Microsoft VS):
before fix, project file script -> package path locater
after fix, project file script -> package path locater
From above example, I've added "..\" prior to most "packages\{package-name}" to locate the path from the root of the solution instead (assumption here but it worked).
Important Note: add the prefix "..\" to "packages\{package-name}" to all occurrence of packages in the file EXCEPT FOR: package.config file here not a path
For me, it helped all the projects with issues in that solution folder to find and recognize packages in package folder after it was moved. Hopefully, this answer will help someone that has a similar struggle with me. Have fun coding guys!
© 2022 - 2024 — McMap. All rights reserved.