Get all nuget packages in solution - c#

I'm trying to write a unit test to enforce consolidation of Nuget packages (we have a build requirement that all unit tests pass so this would keep PRs that aren't consolidating from passing) and I was attempting to use Nuget.Core to do that. However, I cannot seem to find my way through their libraries and no one has asked this question yet. So, how can I get all the Nuget packages a given solution references programmatically?

This is the final solution (along with unit test). The key is to use the Directory library to iterate over all the projects in the solution and then use NuGet.Core to analyze the NuGet packages in each project.
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NuGet;
using Shouldly;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace UnitTests
{
[TestClass]
public class NugetConsolidationTest
{
private List<string> _ignoredPackages = new List<string>();
[TestMethod]
public void AllNugetPackagesAreConsolidated()
{
var packageVersionMapping = new Dictionary<string, List<string>>();
var parentDir = (Directory.GetParent(Directory.GetCurrentDirectory()).Parent).Parent.FullName;
var files = Directory.GetFiles(parentDir, "packages.config", SearchOption.AllDirectories);
foreach (var packageFile in files)
{
var file = new PackageReferenceFile(packageFile);
var refs = file.GetPackageReferences(true);
foreach (var packageRef in refs)
{
if (_ignoredPackages.Contains(packageRef.Id))
continue;
if (!packageVersionMapping.ContainsKey(packageRef.Id))
packageVersionMapping[packageRef.Id] = new List<string>() { packageRef.Version.ToFullString() };
else
{
if (packageVersionMapping[packageRef.Id].All(x => !x.Equals(packageRef.Version.ToFullString(),
StringComparison.InvariantCultureIgnoreCase)))
packageVersionMapping[packageRef.Id].Add(packageRef.Version.ToFullString());
}
}
}
var errors = packageVersionMapping.Where(x => x.Value.Count > 1)?.
Select(x => $"Package {x.Key} has {x.Value.Count} separate versions installed! Current versions are {string.Join(", ", x.Value)}");
errors.ShouldBeEmpty();
}
}
}

You can always read the package.config files and parse them.
The one that's inside the solution directory with reference other packages.config file is one for each project contained in the solution.

Related

NuGet dependency tree [duplicate]

Is there a way, either textual or graphical, to view the hierarchy of dependencies between NuGet packages?
If you're using the new .csproj, you could get all dependencies with reference in here (after project built):
{ProjectDir}\obj\project.assets.json
Like #neil-barnwell solution, but works with NuGet.Core 2.7+
Install-Package NuGet.Core
Here is the code
using System;
using System.Linq;
using System.Runtime.Versioning;
using System.IO;
using NuGet;
public class Program
{
public static void Main(string[] args)
{
var frameworkName = new FrameworkName(".NETFramework, Version=4.0");
// var packageSource = "https://www.nuget.org/api/v2/";
var packageSource = Path.Combine(Environment.GetEnvironmentVariable("LocalAppData"), "NuGet", "Cache");
var repository = PackageRepositoryFactory.Default.CreateRepository(packageSource);
const bool prerelease = false;
var packages = repository.GetPackages()
.Where(p => prerelease ? p.IsAbsoluteLatestVersion : p.IsLatestVersion)
.Where(p => VersionUtility.IsCompatible(frameworkName, p.GetSupportedFrameworks()));
foreach (IPackage package in packages)
{
GetValue(repository, frameworkName, package, prerelease, 0);
}
Console.WriteLine();
Console.WriteLine("Press Enter...");
Console.ReadLine();
}
private static void GetValue(IPackageRepository repository, FrameworkName frameworkName, IPackage package, bool prerelease, int level)
{
Console.WriteLine("{0}{1}", new string(' ', level * 3), package);
foreach (PackageDependency dependency in package.GetCompatiblePackageDependencies(frameworkName))
{
IPackage subPackage = repository.ResolveDependency(dependency, prerelease, true);
GetValue(repository, frameworkName, subPackage, prerelease, level + 1);
}
}
}
It is also possible to write code against the API in NuGet.Core. Install it via NuGet:
install-package nuget.core
Then you can get a repository object and walk the graph. Here's a sample app I just built:
using System;
using System.Collections.Generic;
using System.Linq;
using NuGet;
namespace ConsoleApplication2
{
class Program
{
static void Main()
{
var repo = new LocalPackageRepository(#"C:\Code\Common\Group\Business-Logic\packages");
IQueryable<IPackage> packages = repo.GetPackages();
OutputGraph(repo, packages, 0);
}
static void OutputGraph(LocalPackageRepository repository, IEnumerable<IPackage> packages, int depth)
{
foreach (IPackage package in packages)
{
Console.WriteLine("{0}{1} v{2}", new string(' ', depth), package.Id, package.Version);
IList<IPackage> dependentPackages = new List<IPackage>();
foreach (var dependency in package.Dependencies)
{
dependentPackages.Add(repository.FindPackage(dependency.Id, dependency.VersionSpec.ToString()));
}
OutputGraph(repository, dependentPackages, depth += 3);
}
}
}
}
In my case, this app outputs something like this:
MyCompany.Castle v1.1.0.3
Castle.Windsor v2.5.3
Castle.Core v2.5.2
MyCompany.Common v1.1.0.6
CommonServiceLocator v1.0
MyCompany.Enum v1.1.0.7
MyCompany.Common v1.1.0.6
CommonServiceLocator v1.0
MyCompany.Enum v1.1.0.7
MyCompany.Enum v1.1.0.7
MyCompany.Versioning v1.3
Castle.Core v2.5.2
Castle.Windsor v2.5.3
Castle.Core v2.5.2
CommonServiceLocator v1.0
NUnit v2.5.10.11092
RhinoMocks v3.6
I've found a nice NPM package to print the dependency tree into console. Of course if you don't mind using/installing NPM/Node.JS.
Considering other solutions, this is the most simple one, you don't need to write your own code or register something, and you get just such dependency tree as you expect. But it works only with packages.config format.
I can't believe this functionality is absent in free Visual Studio editions or nuget.exe too.
I Can Has .NET Core (GitHub repository) produces nice graphs of NuGet dependencies along with a Graphviz representation. And as its name implies, you also get .NET Core compatibility information for free.
If you prefer to run it locally on your computer, I Can Has .NET Core also offers a console mode.
I add a compatible solution with the latest version of nuget-core
install-package nuget.core
This is the console App to get the dependencies graph
class Program
{
static void Main()
{
Console.Write("Enter the local repo folder: ");
var repoFolder = Console.ReadLine();
var repo = new LocalPackageRepository(repoFolder);
IQueryable<IPackage> packages = repo.GetPackages();
OutputGraph(repo, packages, 0);
}
static void OutputGraph(LocalPackageRepository repository, IEnumerable<IPackage> packages, int depth)
{
foreach (IPackage package in packages)
{
Console.WriteLine("{0}{1} v{2}", new string(' ', depth), package.Id, package.Version);
IList<IPackage> dependentPackages = new List<IPackage>();
foreach (var dependencySet in package.DependencySets)
{
foreach (var dependency in dependencySet.Dependencies)
{
var dependentPackage = repository.FindPackage(dependency.Id, dependency.VersionSpec, true, true);
if (dependentPackage != null)
{
dependentPackages.Add(dependentPackage);
}
}
}
OutputGraph(repository, dependentPackages, depth + 3);
}
}
}
Package Visualized from NuGet 1.4 should work. See http://docs.nuget.org/docs/release-notes/nuget-1.4
Since this is an old question, it is important to note the following:
This is a built-in feature in the new csproj format. In Visual Studio 2017 and up, open the Solution Explorer and you can find you packages like:
{Your project}->Dependencies->Packages
You can open each NuGet dependency tree and run with it recursively, effectively seeing not only the dependency tree for specific packages, but also which NuGet packages your project actually installs.
Another option you have is to use the nuget-deps-tree npm package.
It supports both the packages.config format and the newer assets format used by .NET projects.
FYI, MyGet.org has this kind of visualization built-in. You can view dependency graphs on the Feed Details page.
https://github.com/mikehadlow/AsmSpy using this to identify assembly version across a project

C# TestProject - How do I get CodeFilePath & LineNumber for each test?

Given:
I have a project/solution containing tests. It is written in C# and is using nUnit.
What I want:
Somehow to retrieve all the tests in the Project/Solution without executing them and also giving me the CodeFilePath and LineNumber for each test.
What I tried / investigated:
dotnet test --list-tests : is giving me nothing (just the displayname)
NUnit3TestAdapter : is at somepoint exposing those values (see NUnitTestAdapter).
But how can I retrieve them?
Thanks #Charlie for pointing out a possible solution. I did basically exactly as you stated.
IMPORTANT NOTICE:
This seems to not work with dot.net CORE tests currently because of this issue: https://github.com/nunit/nunit-console/issues/710
Also DiaSession runs into an exception when used like this in dot.net CORE :-(
using System;
using System.Xml;
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
using NUnit.Engine;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
var testAssemblyPath = #"C:\src\qata\src\tests\external\SomethingProject.Tests\bin\Debug\net461\SomethingProject.Tests.dll";
var package = new TestPackage(testAssemblyPath);
var testEngine = new TestEngine();
var runner = testEngine.GetRunner(package);
var nUnitXml = runner.Explore(TestFilter.Empty);
var session = new DiaSession(testAssemblyPath);
foreach (XmlNode testNode in nUnitXml.SelectNodes("//test-case"))
{
var testName = testNode.Attributes["fullname"]?.Value;
var className = testNode.Attributes["classname"]?.Value;
var methodName = testNode.Attributes["methodname"]?.Value;
var navigationData = session.GetNavigationData(className, methodName);
Console.WriteLine($"{testName} - {navigationData.FileName} - {navigationData.MinLineNumber}.");
}
}
}
}

Getting a SemanticModel of a cshtml file?

I'd like to use Roslyn to analyze semantic information within the context of a block of C# code inside a Razor View.
Is there any way (within Visual Studio 2015, or even in a unit test) to get the SemanticModel that represents this code?
Razor files contain a C# projection buffer with the generated C# code (including the parts that you don't write yourself). This buffer has full Roslyn services and is exactly what you're looking for.
You need to walk through the TextView's BufferGraph and find the CSharp buffer; you can then get its Document and semantic model.
If you're starting from the cursor location, you need simply need to map that location to a CSharp buffer.
Note that it is perfectly legal for a TextView to contain multiple CSharp buffers. (although the Razor editor will never do that)
If you aren't working in a TextView, you need to do all of this yourself; you need to run the Razor source through the Razor compiler to get the generated C# source, then compile that with Roslyn to get a semantic model.
Extract the code representing the view from the Razor view file using RazorTemplateEngine.GenerateCode and CSharpCodeProvider.GenerateCodeFromCompileUnit (or the VBCodeProvider if you want the intermediate source as VB.NET). You can then use Roslyn to parse the code.
There's an example of using Roslyn with Razor view files here.
Take note that GenerateCode carries a caveat:
This type/member supports the .NET Framework infrastructure and is not intended to be used directly from your code.
Just in case anyone else gets stuck on this, I have mini sample app which may help.
I had a CMS class like this:
public partial class CMS
{
public static string SomeKey
{
get { return (string) ResourceProvider.GetResource("some_key"); }
}
// ... and many more ...
}
... and I wanted to find out which of these were used throughout my solution for a report ... Enter Roslyn!
The following app will print out the count for the used and unused references:
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.FindSymbols;
using Microsoft.CodeAnalysis.MSBuild;
using Microsoft.CSharp;
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Razor;
namespace TranslationSniffer
{
class Program
{
static void Main(string[] args)
{
new Program().Go().Wait();
}
public async Task Go()
{
// Roslyn!
var ws = MSBuildWorkspace.Create();
// Store the translation keys...
List<string> used = new List<string>();
List<string> delete = new List<string>();
string solutionRoot = #"C:\_Code\PathToProject\";
string sln = solutionRoot + "MySolution.sln";
// Load the solution, and find all the cshtml Razor views...
var solution = await ws.OpenSolutionAsync(sln);
var mainProj = solution.Projects.Where(x => x.Name == "ConsumerWeb").Single();
FileInfo[] cshtmls = new DirectoryInfo(solutionRoot).GetFiles("*.cshtml", SearchOption.AllDirectories);
// Go through each Razor View - generate the equivalent CS and add to the project for compilation.
var host = new RazorEngineHost(RazorCodeLanguage.Languages["cshtml"]);
var razor = new RazorTemplateEngine(host);
var cs = new CSharpCodeProvider();
var csOptions = new CodeGeneratorOptions();
foreach (var cshtml in cshtmls)
{
using (StreamReader re = new StreamReader(cshtml.FullName))
{
try
{
// Let Razor do it's thang...
var compileUnit = razor.GenerateCode(re).GeneratedCode;
// Pull the code into a stringbuilder, and append to the main project:
StringBuilder sb = new StringBuilder();
using (StringWriter rw = new StringWriter(sb))
{
cs.GenerateCodeFromCompileUnit(compileUnit, rw, csOptions);
}
// Get the new immutable project
var doc = mainProj.AddDocument(cshtml.Name + ".cs", sb.ToString());
mainProj = doc.Project;
}
catch(Exception ex)
{
Console.WriteLine("Compile fail for: {0}", cshtml.Name);
// throw;
}
continue;
}
}
// We now have a new immutable solution, as we have changed the project instance...
solution = mainProj.Solution;
// Pull out our application translation list (its in a static class called 'CMS'):
var mainCompile = await mainProj.GetCompilationAsync();
var mainModel = mainCompile.GetTypeByMetadataName("Resources.CMS");
var translations = mainModel.GetMembers().Where(x => x.Kind == SymbolKind.Property).ToList();
foreach (var translation in translations)
{
var references = await SymbolFinder.FindReferencesAsync(translation, solution) ;
if (!references.First().Locations.Any())
{
Console.WriteLine("{0} translation is not used!", translation.Name);
delete.Add(translation.Name);
}
else
{
Console.WriteLine("{0} :in: {1}", translation.Name, references.First().Locations.First().Document.Name);
used.Add(translation.Name);
}
}
Console.WriteLine();
Console.WriteLine("Used references {0}. Unused references: {1}", used.Count, delete.Count);
return;
}
}
}
Roslyn only models cshtml files while they are open, but during that time they are similar to every other source file in the Workspace model.
Is there something specific you have tried that isn't working?

How to get all branches of repository with SharpSvn?

I'm trying to get all branches of repository by using SharpSvn but I can not find any method can do it.
Is it possible to get all branches by using SharpSvn?
I think Matt Z was on the right track, but that code doesn't compile. Here is an adjusted version that should work with the latest version of SharpSVN (as of Dec 2015).
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using SharpSvn;
....
private List<string> GetSVNPaths()
{
List<string> files = new List<string>();
using (SvnClient svnClient = new SvnClient())
{
Collection<SvnListEventArgs> contents;
//you can get the url from the TortoiseSVN repo-browser if you aren't sure
if (svnClient.GetList(new Uri(#"https://your-repository-url/"), out contents))
{
files.AddRange(contents.Select(item => item.Path));
}
}
return files;
}
I don't know anything about SharpSVN, but branches in Subversion are just directory trees -- there's nothing special about them.
If your repository follows they typical layout with three top-level directories (trunk/ branches/ tags/), you can just check out the /branches directory to get all the branches side-by-side.
The SharpSvn.SvnClient class has a GetList() function that works really well:
using (SvnClient svnClient = new SvnClient())
{
Collection contents;
List files = new List();
if (svnClient.GetList(new Uri(svnUrl), out contents))
{
foreach(SvnListEventArgs item in contents)
{
files.Add(item.Path);
}
}
}
Once you have the collection, you can get the path of each item at the location. You can also use the Entry object to get information concerning each item, including whether it is a directory or a file, when it was last modified, etc.

c# check files on server using web client

C# 2008
I have using the WebClient DownloadFile method.
I can download the files I want. However, the client has insisted on creating different folders which will contain the version number. So the name of the folders would be something like this: 1.0.1, 1.0.2, 1.0.3, etc.
So the files will be contained in the latest version in this case folder 1.0.3. However, how can my web client detect which is the latest one?
The client will check this when it starts up. Unless I actually download all the folders and then compare. I am not sure how else I can do this.
Many thanks for any advice,
Create a page which gives you the current version number.
string versionNumber = WebClient.DownloadString();
Allow directory browsing in IIS and download the root folder. Then you could find the latest version number and construct the actual url to download. Here's a sample (assuming your directories will be of the form Major.Minor.Revision):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text.RegularExpressions;
class Program
{
static void Main(string[] args)
{
using (var client = new WebClient())
{
var directories = client.DownloadString("http://example.com/root");
var latestVersion = GetVersions(directories).Max();
if (latestVersion != null)
{
// construct url here for latest version
client.DownloadFile(...);
}
}
}
static IEnumerable<Version> GetVersions(string directories)
{
var regex = new Regex(#"<a href=""[^""]*/([0-9]+\.[0-9]+\.[0-9])+/"">",
RegexOptions.IgnoreCase);
foreach (Match match in regex.Matches(directories))
{
var href = match.Groups[1].Value;
yield return new Version(href);
}
yield break;
}
}
This question might have some useful information for you. Please read my answer which deals with enumerating files on a remote server.

Categories

Resources