|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Diagnostics; |
| 4 | +using System.IO; |
| 5 | +using System.Linq; |
| 6 | +using System.Runtime.InteropServices; |
| 7 | +using System.Threading.Tasks; |
| 8 | +using Incrementalist.FileSystem; |
| 9 | +using Incrementalist.Git; |
| 10 | +using LibGit2Sharp; |
| 11 | +using Microsoft.Build.Locator; |
| 12 | +using Microsoft.CodeAnalysis.MSBuild; |
| 13 | + |
| 14 | +namespace Incrementalist.Cmd |
| 15 | +{ |
| 16 | + enum FileType |
| 17 | + { |
| 18 | + Code, |
| 19 | + Project, |
| 20 | + Solution, |
| 21 | + Other |
| 22 | + } |
| 23 | + |
| 24 | + class Program |
| 25 | + { |
| 26 | + private static string _originalTitle = null; |
| 27 | + private static bool IsWindows => RuntimeInformation.IsOSPlatform(OSPlatform.Windows); |
| 28 | + |
| 29 | + private static void SetTitle() |
| 30 | + { |
| 31 | + if (IsWindows) // changing console title is not supported on OS X or Linux |
| 32 | + { |
| 33 | + _originalTitle = Console.Title; |
| 34 | + Console.Title = StartupData.ConsoleWindowTitle; |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + private static void ResetTitle() |
| 39 | + { |
| 40 | + if (IsWindows) |
| 41 | + Console.Title = _originalTitle; // reset the console window title back |
| 42 | + } |
| 43 | + |
| 44 | + static async Task<int> Main(string[] args) |
| 45 | + { |
| 46 | + SetTitle(); |
| 47 | + |
| 48 | + if (args.Length >= 1) |
| 49 | + { |
| 50 | + if (args[0].ToLowerInvariant().Equals("help")) |
| 51 | + { |
| 52 | + StartupData.ShowHelp(); |
| 53 | + ResetTitle(); |
| 54 | + return 0; |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + var insideRepo = Repository.IsValid(Directory.GetCurrentDirectory()); |
| 59 | + Console.WriteLine("Are we inside repository? {0}", insideRepo); |
| 60 | + //if (insideRepo) |
| 61 | + //{ |
| 62 | + var repoFolder = Repository.Discover(Directory.GetCurrentDirectory()); |
| 63 | + var repository = new Repository(repoFolder); |
| 64 | + Console.WriteLine("Repo base is located in {0}", repoFolder); |
| 65 | + var workingFolder = Directory.GetParent(repoFolder).Parent; |
| 66 | + |
| 67 | + var affectedFiles = DiffHelper.ChangedFiles(repository, "master").Select(x => Path.GetFullPath(x, workingFolder.FullName)).ToList(); |
| 68 | + foreach (var file in affectedFiles) |
| 69 | + Console.WriteLine("Modified file: {0}", file); |
| 70 | + //} |
| 71 | + |
| 72 | + // Locate and register the default instance of MSBuild installed on this machine. |
| 73 | + MSBuildLocator.RegisterDefaults(); |
| 74 | + |
| 75 | + var msBuild = MSBuildWorkspace.Create(); |
| 76 | + var progress = new Progress<ProjectLoadProgress>(); |
| 77 | + progress.ProgressChanged += ProgressOnProgressChanged; |
| 78 | + |
| 79 | + if (!string.IsNullOrEmpty(repoFolder)) |
| 80 | + { |
| 81 | + |
| 82 | + foreach (var sln in SolutionFinder.GetSolutions(workingFolder.FullName)) |
| 83 | + { |
| 84 | + Console.WriteLine("Found solution file {0}", sln); |
| 85 | + |
| 86 | + var fullSln = await msBuild.OpenSolutionAsync(sln, progress); |
| 87 | + var allDocuments = fullSln.Projects.SelectMany(x => x.Documents) |
| 88 | + .GroupBy(x => x.FilePath, document => FileType.Code) |
| 89 | + .ToDictionary(x => Path.GetFullPath(x.Key, workingFolder.FullName), x => x.First()) |
| 90 | + .Concat(fullSln.Projects.ToDictionary(x => Path.GetFullPath(x.FilePath, workingFolder.FullName), x => FileType.Project)) |
| 91 | + .Concat(new Dictionary<string, FileType>{ { Path.GetFullPath(fullSln.FilePath, workingFolder.FullName), FileType.Solution } }) |
| 92 | + .ToDictionary(x => x.Key, x => x.Value); |
| 93 | + |
| 94 | + Console.WriteLine("Checking to see if affected files are in solution..."); |
| 95 | + foreach (var affectedFile in affectedFiles) |
| 96 | + { |
| 97 | + var fileInSln = allDocuments.ContainsKey(affectedFile); |
| 98 | + Console.WriteLine("{0} in solution? {1}", affectedFile, fileInSln); |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + ResetTitle(); |
| 104 | + return 0; |
| 105 | + } |
| 106 | + |
| 107 | + private static void ProgressOnProgressChanged(object sender, ProjectLoadProgress e) |
| 108 | + { |
| 109 | + Console.WriteLine("{0} {1} {2}", e.ElapsedTime, e.Operation, e.FilePath); |
| 110 | + } |
| 111 | + } |
| 112 | +} |
0 commit comments