Skip to content

Instantly share code, notes, and snippets.

@williamdenton
Created July 1, 2019 01:21
Show Gist options
  • Save williamdenton/dcb850f6d21c9d55beacab638314acf4 to your computer and use it in GitHub Desktop.
Save williamdenton/dcb850f6d21c9d55beacab638314acf4 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Newtonsoft.Json;
using ShellProgressBar;
namespace NugetReporter
{
class Program
{
static void Main(string[] args)
{
var packages = new ConcurrentDictionary<string, ConcurrentBag<string>>();
var dir = "../../";
var projectFiles = Directory.GetFiles(dir, "*.csproj", SearchOption.AllDirectories);
var options = new ProgressBarOptions
{
ForegroundColor = ConsoleColor.Yellow,
ForegroundColorDone = ConsoleColor.DarkGreen,
BackgroundColor = ConsoleColor.DarkGray,
BackgroundCharacter = '\u2593',
ProgressBarOnBottom = true,
};
int totalSteps = projectFiles.Length;
using (var progress = new ProgressBar(totalSteps + 1, "Processing Project Files", options))
{
Parallel.ForEach(projectFiles, (projectFile, state, index) =>
{
var relativeProject = projectFile.Substring(dir.Length);
progress.Tick($"Processing {relativeProject}");
ProcessProject(projectFile, relativeProject, packages);
});
progress.Tick("Done");
}
using (var sw = new StreamWriter("package-project-map.json"))
{
var packageToProject = new Dictionary<string, List<string>>();
foreach (var package in packages.Keys.OrderBy(x => x))
{
packageToProject[package] = packages[package].OrderBy(x => x).ToList();
}
sw.Write(JsonConvert.SerializeObject(packageToProject, Formatting.Indented));
}
var packageToService = new Dictionary<string, List<string>>();
var serviceFolder = "/packages/";
var serviceFolderLength = serviceFolder.Length;
foreach (var package in packages.Keys.OrderBy(x => x))
{
var microservices = packages[package]
.Where(p => p.StartsWith(serviceFolder))
.Select(p => p.Substring(serviceFolderLength, p.IndexOf('/', serviceFolderLength) - serviceFolderLength))
.Distinct()
.OrderBy(x => x)
.ToList();
if (microservices.Any())
{
packageToService[package] = microservices;
}
}
using (var sw = new StreamWriter("package-microservice-map.json"))
{
sw.Write(JsonConvert.SerializeObject(packageToService, Formatting.Indented));
}
var serviceToPackage = new Dictionary<string, List<string>>();
foreach (var package in packageToService)
{
foreach (var service in package.Value)
{
serviceToPackage.TryAdd(service, new List<string>());
serviceToPackage[service].Add(package.Key);
}
}
var serviceToPackageOrdered = new Dictionary<string, List<string>>();
foreach (var service in serviceToPackage.Keys.OrderBy(x => x))
{
serviceToPackageOrdered[service] = serviceToPackage[service].OrderBy(x => x).ToList();
}
using (var sw = new StreamWriter("microservice-package-map.json"))
{
sw.Write(JsonConvert.SerializeObject(serviceToPackageOrdered, Formatting.Indented));
}
}
static void ProcessProject(string projectFile, string relativeProjectFile, ConcurrentDictionary<string, ConcurrentBag<string>> packages)
{
var dotnetRestore = new ProcessStartInfo()
{
FileName = "dotnet",
Arguments = $"restore {projectFile}",
RedirectStandardOutput = true,
};
using (var process = Process.Start(dotnetRestore))
{
process.WaitForExit();
}
var dotnet = new ProcessStartInfo()
{
FileName = "dotnet",
Arguments = $"list {projectFile} package",
RedirectStandardOutput = true,
};
using (var process = Process.Start(dotnet))
using (var sr = process.StandardOutput)
{
var project = sr.ReadLine();
if (project.StartsWith("error:"))
{
Console.WriteLine($"'{project}' in {relativeProjectFile}");
return;
}
var target = sr.ReadLine();
var columnheader = sr.ReadLine();
while (!sr.EndOfStream)
{
var package = sr.ReadLine();
if (string.IsNullOrWhiteSpace(package))
{
break;
}
if (!package.StartsWith(" > "))
{
throw new Exception("bugger");
}
var endOfPackageName = package.IndexOf(" ", 5);
var packageName = package.Substring(5, endOfPackageName - 5);
if (!packages.ContainsKey(packageName))
{
packages.GetOrAdd(packageName, new ConcurrentBag<string>());
}
packages[packageName].Add(relativeProjectFile);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment