Created
March 7, 2019 07:45
-
-
Save BlythMeister/004d176d95fea248c2443f23ac464e7e to your computer and use it in GitHub Desktop.
Will update, sort, simplify and install paket dependencies in a repo
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
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