Skip to content

Instantly share code, notes, and snippets.

@BlythMeister
Created March 7, 2019 07:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BlythMeister/004d176d95fea248c2443f23ac464e7e to your computer and use it in GitHub Desktop.
Save BlythMeister/004d176d95fea248c2443f23ac464e7e to your computer and use it in GitHub Desktop.
Will update, sort, simplify and install paket dependencies in a repo
void Main()
{
Run(@"C:\My\Repo\Root");
}
void Run(string rootDir)
{
Console.WriteLine($"Starting at {DateTime.UtcNow.ToString("u")}");
Console.WriteLine($"Running against: {rootDir}");
RunPaketCommand(rootDir, "update");
SortReferences(rootDir);
SortDependencies(rootDir);
RunPaketCommand(rootDir, "simplify");
RunPaketCommand(rootDir, "install");
Console.WriteLine($"Done at {DateTime.UtcNow.ToString("u")}");
}
void RunPaketCommand(string rootDir, string command)
{
Console.WriteLine($"Running paket command: {command}");
var paketProcess = new ProcessStartInfo(Path.Combine(rootDir, ".paket", "paket.exe"), command)
{
WorkingDirectory = rootDir,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true
};
var process = Process.Start(paketProcess);
while (!process.StandardOutput.EndOfStream)
{
Console.WriteLine(process.StandardOutput.ReadLine());
}
}
void SortReferences(string rootDir)
{
foreach (var file in Directory.GetFiles(rootDir, "paket.references", SearchOption.AllDirectories))
{
Console.WriteLine($"Sorting reference file: {file}");
var content = File.ReadAllLines(file);
var newContent = new List<string>();
var nugetBlock = new List<string>();
var onNuget = false;
var previousLineBreak = false;
foreach (var line in content)
{
if (line.Trim().StartsWith("group"))
{
onNuget = false;
}
else if (!string.IsNullOrWhiteSpace(line))
{
onNuget = true;
previousLineBreak = false;
}
else
{
previousLineBreak = true;
}
if (onNuget && !string.IsNullOrWhiteSpace(line))
{
nugetBlock.Add(line);
}
if (!onNuget)
{
if (nugetBlock.Any())
{
newContent.AddRange(nugetBlock.OrderBy(x => x));
nugetBlock.Clear();
}
if (previousLineBreak)
{
newContent.Add("");
previousLineBreak = false;
}
if (!string.IsNullOrWhiteSpace(line))
{
newContent.Add(line);
}
}
}
if (nugetBlock.Any())
{
newContent.AddRange(nugetBlock.OrderBy(x => x));
}
File.WriteAllLines(file, newContent);
}
}
void SortDependencies(string rootDir)
{
foreach (var file in Directory.GetFiles(rootDir, "paket.dependencies", SearchOption.AllDirectories))
{
Console.WriteLine($"Sorting dependencies file: {file}");
var content = File.ReadAllLines(file);
var newContent = new List<string>();
var nugetBlock = new List<string>();
var onNuget = false;
var previousLineBreak = false;
foreach (var line in content)
{
if (line.Trim().StartsWith("nuget"))
{
onNuget = true;
previousLineBreak = false;
}
else if (!string.IsNullOrWhiteSpace(line))
{
onNuget = false;
}
else
{
previousLineBreak = true;
}
if (onNuget && !string.IsNullOrWhiteSpace(line))
{
nugetBlock.Add(line);
}
if (!onNuget)
{
if (nugetBlock.Any())
{
newContent.AddRange(nugetBlock.OrderBy(x => x));
nugetBlock.Clear();
}
if (previousLineBreak)
{
newContent.Add("");
previousLineBreak = false;
}
if (!string.IsNullOrWhiteSpace(line))
{
newContent.Add(line);
}
}
}
if (nugetBlock.Any())
{
newContent.AddRange(nugetBlock.OrderBy(x => x));
}
File.WriteAllLines(file, newContent);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment