Why NuGet Package manager Download/Copy Packages on multiple Locations? - nuget

I have Directory Structure like -
Projects
.nuget
NuGet.exe
NuGet.config
NuGet.targets
**packages (I want to download package for different solution HERE ONLY)**
Sources
Applications
App1
App1.sln (Solution File)
**packages (NuGet downloads packages here first then copies to expected folder, WHY??)**
App1 (Porject Directory)
App1.csproj
App2
App2.sln (Solution File)
**packages (NuGet downloads packages here first then copies to expected folder, WHY??)**
App2 (Porject Directory)
App2.csproj
I have referenced .nuget folder in each solution using following code
Project("{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}") = ".nuget", ".nuget", "{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx}"
ProjectSection(SolutionItems) = preProject
..\..\..\.nuget\NuGet.Config = ..\..\..\.nuget\NuGet.Config
..\..\..\.nuget\NuGet.exe = ..\..\..\.nuget\NuGet.exe
..\..\..\.nuget\NuGet.targets = ..\..\..\.nuget\NuGet.targets
EndProjectSection
EndProject
In every project file (.csproj), I have referenced common NuGet.targets using,
<Import Project="..\..\..\..\.nuget\NuGet.targets" Condition="Exists('..\..\..\..\.nuget\NuGet.targets')" />
In NuGet.config, I have added following line so that it should (must) copy packages in EXPECTED folder ONLY
<add key="repositoryPath" value="..\packages" />
I have mapped Projects folder with TFS and it asks me to add files on both locations because of above problem

NuGet will look for the NuGet.config file based on the current solution's directory.
So taking your App1 solution if the Projects directory is in c:\Projects then your App1.sln file will be in the c:\Projects\Sources\Applications\App1 directory. Now NuGet will look for the NuGet.config directory in:
c:\Projects\Sources\Applications\App1\.nuget\NuGet.Config
c:\Projects\Sources\Applications\App1\NuGet.Config
c:\Projects\Sources\Applications\NuGet.Config
c:\Projects\Sources\NuGet.Config
c:\Projects\NuGet.Config
c:\NuGet.Config
After this it then looks in machine wide locations but I will ignore these for now.
Looking at your directory structure the Projects.nuget directory will not be checked by NuGet. It is not a parent of any of the solution directories.
I would look at putting a NuGet.Config file with the repositoryPath setting in the Sources directory or the Projects directory (not in the .nuget directory). Or having two NuGet.Config files, one in the App1.nuget directory and one in the App2.nuget directory.

Related

.gitignore not ignoring a folder within a directory

It seems a straightforward one, but having researched multiple ways to do it, I can't gitignore a folder within a directory.
I have a root directory which contains all of my code in it. Because it has some back-end NodeJS stuff in it, it has a 'node_modules' folder which contains hundreds of files. As a result, when I try to upload the entire parent folder, GitHub says there's too many files to upload and tells me to reduce the number I'm uploading.
The crucial part is though, the folder has to be uploaded as a whole, as it itself is within a GitHub repository with other files with different folders in.
That means when I go onto my repository, I need this folder's files to display within the folder, and not separately within the repository. I need all of the files to be within this folder, within the parent repository, excluding the node_modules folder.
For example ->
Parent repository -> Child Directory (what I'm uploading) -> Individual files
I've tried to add the node_modules folder to my gitignore through the following methods:
Adding: node_modules/ to my gitignore file
Writing: echo node_modules >> .gitignore through my terminal
Adding a separate gitignore file within my node_modules file with a * in it
None of them have worked and I can't find any other solutions. For reference I'm using a Mac.
Does anyone have any idea what I'm doing wrong, or how it'd be best to do it?
By default, you do not need to include the node_modules folder in your repositories because the package.json file contains all of your project's dependency information. This means that anyone who clones your repository can run npm install and have the entire node_modules folder without problems.
To solve this you can create your own .gitignore file, creating a new file at the root of your project and renaming it to .gitignore (writing exactly that way). Then you can open it with any text editor and add */node_modules to one of the lines.
This will likely solve your problem.

Nuget restore for dependent projects

I have multiple projects in a added in multiple solutions and at different directory levels. I use packages.config to maintain list of dependencies.
On nuget restore for a solution through command line, packages folder is created in the same directory as solution file and gets all dependencies are downloaded. Now there are some projects added as reference which are also present in other solutions. So projects refer different packages folder. On restore only 1 packages folder is created and so some projects referring packages at other location, do not get their packages restored.
Is there any way one can get all dependencies in multiple packages folder in different directories?
You can create a NuGet.Config file with a repositoryPath which overrides the default behaviour.
<configuration>
<config>
<add key="repositoryPath" value="packages" />
</config>
</configuration>
The path is relative to where the directory the NuGet.Config file exists.
Note that the location of the NuGet.Config file should be in a directory that is the same or above the solutions. Whilst you can have multiple NuGet.Config files, it will probably be simpler to have one at the root directory of your solutions. For example:
/Your/Projects/Test/NuGet.Config
/Your/Projects/Test/MySolution.sln
/Your/Projects/Test/Another/MySolution2.sln
Also note that you would need to reinstall the NuGet packages in the projects, or at least fix the reference paths, since they will now be using a different packages directory and will be incorrect.

Is it possible to change where a nuget package adds files to a project?

If I add a nuget package Foo it might add files to the Content folder in my project.
Content
|
- foo.js
- foo.min.js
- foo.map
What if I wanted something like:
Content
|
- Foo
|
- foo.js
- foo.min.js
- foo.map
Most of the other questions I have found are talking about the location of the packages folder - is something like the above possible?
NuGet does not put the files into a Content folder inside your project unless that is the folder you used. You can put the files in any subdirectory inside the project. The jQuery NuGet package puts its files in a Scripts subdirectory, for example, and it has the folder structure inside its NuGet package (.nupkg):
Note that the Content directory here is just tells NuGet that it should put the files in the project directory. It does not add the files into a Content directory but instead adds them inside a Scripts directory inside the project.
By default, the Nuget Package Manager puts all the .js files inside the 'Scripts' folder and .css files inside the 'Content' folder. But if you want to create a subfolder and categorize your packages, you will have to manually do that. Create a sub folder and move the files manually to it.

Nuspec files from nested directory

I have such file structure:
MyProject/MyProject/bin
I am creating Nuget package via nuspec file. So to add bin folder, I have added this command:
<file src="MyProject/MyProject/bin/**" target="bin"/>
But as a result I am receiving this file structure:
bin/MyProject/MyProject/bin
Is there a way to copy files directly into bin folder, without creating nested directories? So my output would be like simple:
bin

NuGet.exe package not including empty folders

I have created a nuspec file for my solution. The solution has a few empty folders.
I have downloaded the latest nuget.exe from https://docs.nuget.org/consume/command-line-reference
However when I run my nuget pack command the empty folders are not being included. Looking at the documentation for the pack command, I am interpreting the fact that empty folders are included by default because of this flag:
ExcludeEmptyDirectories: Prevent inclusion of empty directories when building the package.
Unfortunately I am not able to get the pack command to include my empty folders.
My files structure is similar to
Root
Folder1
Folder2
Folder3
Folder1 and Folder2 have content files within them, but Folder3 is empty
My nuspec file defined as follows:
<?xml version="1.0"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
<id>Package_7.2_140526</id>
<version>7.2.140526</version>
<title>Package 7.2 rev 140526</title>
<authors>My Company</authors>
<owners>My Company</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>A vanilla website</description>
</metadata>
</package>
I have not included individual files because I want the nuget pack to include everything. While my example is only of 3 folders there are significantly more than than.
This is the nuget command I am running from the command line
nuget pack "c:\Projects\Package 7.2 rev 140526\Packagee 7.2 140526.nuspec" -OutputDirectory c:\Projects -NoPackageAnalysis
When that did not work I tried with the -ExcludeEmptyDirectories flag just incase
nuget pack "c:\Projects\Package 7.2 rev 140526\Packagee 7.2 140526.nuspec" -OutputDirectory c:\Projects -NoPackageAnalysis -ExcludeEmptyDirectories
But it still will not include the empty folder.
What needs to be done to get the nuget pack command to include the empty folders?
Update
I used the Nuget Package Explorer to add the empty folders to the solution and then looked in the modified nuspec file. It appears that the only way to include empty folders within nuget is to have a file entry as
<file src="folder3\_._" target="folder3\_._" />
And then have a postdeploy.ps1 file to delete all the files named "_._"
So it appears that will always have to specify the files.
Ps. I tested it with the following structure
\Folder3\Folder4.
If folder4 is empty then folder3 and folder4 are not created unless you include the "_._" file
But if there is a way to include empty folders without having to resort to _._ file then want to know, especially if the blank folder is nested as it is easy to get excluded
Yeah, it seems this is an issue that has a shady status of whether it is solved or not and how. There are some issues in the Nuget Github about this, but I coulnd't get a clear picture of what is the status.
Summary is there is a parameter for the nuget 'pack' command that is called -ExcludeEmptyDirectories that in reality is supposed to do the opposite of what it is called, but it doesn't work either what I could test.
See
https://github.com/NuGet/Home/issues/3009