Created
August 10, 2014 17:00
-
-
Save chucker/54cc5f059d63716496b0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
using System.Linq; | |
using System.Text.RegularExpressions; | |
ILookup<string, string> localPackages, | |
remotePackages; | |
using (var process = new Process()) | |
{ | |
process.StartInfo = | |
new ProcessStartInfo | |
( | |
"clist", | |
"-localonly" | |
); | |
process.StartInfo.UseShellExecute = false; | |
process.StartInfo.RedirectStandardOutput = true; | |
process.Start(); | |
localPackages = GetPackagesFromCList(process) | |
.ToLookup(x => x[0], x => x[1]); | |
} | |
using (var process = new Process()) | |
{ | |
process.StartInfo = | |
new ProcessStartInfo | |
( | |
"clist", | |
string.Join(" ", localPackages.Select(x => x.Key) | |
.ToArray()) | |
); | |
process.StartInfo.UseShellExecute = false; | |
process.StartInfo.RedirectStandardOutput = true; | |
process.Start(); | |
remotePackages = GetPackagesFromCList(process) | |
.Where(x => localPackages.Contains(x[0])) | |
.ToLookup(x => x[0], x => x[1]); | |
} | |
foreach (var item in remotePackages) | |
{ | |
string localVersionStr, remoteVersionStr; | |
// no matching package | |
if (!localPackages.Contains(item.Key)) | |
continue; | |
var localItem = localPackages[item.Key]; | |
// no non-matching version | |
if (item.All(v => localItem.Contains(v))) | |
continue; | |
{ | |
var localVersions = localItem.Distinct() | |
.ToArray(); | |
localVersionStr = | |
localVersions.Count() > 1 ? | |
"s: " + string.Join(", ", localVersions) : | |
": " + localVersions[0]; | |
} | |
{ | |
var remoteVersions = item.Distinct() | |
.ToArray(); | |
remoteVersionStr = | |
remoteVersions.Count() > 1 ? | |
"s: " + string.Join(", ", remoteVersions) : | |
": " + remoteVersions[0]; | |
} | |
Console.WriteLine | |
( | |
@" | |
Package {0}: | |
local version{1} | |
remote version{2}", | |
item.Key, | |
localVersionStr, | |
remoteVersionStr | |
); | |
} | |
private static IEnumerable<string[]> GetPackagesFromCList | |
(Process process) | |
{ | |
string[] output = | |
process.StandardOutput | |
.ReadToEnd() | |
.Split | |
( | |
new[] { Environment.NewLine }, | |
StringSplitOptions.RemoveEmptyEntries | |
); | |
return | |
output.Select(l => Regex.Split(l, @"\s+")); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment