|
5 | 5 | using System.Linq; |
6 | 6 | using System.Runtime.InteropServices; |
7 | 7 | using System.Threading.Tasks; |
| 8 | +using CommandLine; |
8 | 9 | using Incrementalist.Cmd.Commands; |
9 | 10 | using Incrementalist.Git; |
10 | 11 | using Incrementalist.ProjectSystem; |
@@ -42,48 +43,119 @@ static async Task<int> Main(string[] args) |
42 | 43 | { |
43 | 44 | SetTitle(); |
44 | 45 |
|
45 | | - if (args.Length >= 1) |
| 46 | + SlnOptions options = null; |
| 47 | + var result = Parser.Default.ParseArguments<SlnOptions>(args).MapResult(r => |
46 | 48 | { |
47 | | - if (args[0].ToLowerInvariant().Equals("help")) |
| 49 | + options = r; |
| 50 | + return 0; |
| 51 | + }, _ => 1); |
| 52 | + |
| 53 | + if (result != 0) |
| 54 | + { |
| 55 | + ResetTitle(); |
| 56 | + return result; |
| 57 | + } |
| 58 | + |
| 59 | + var exitCode = await RunIncrementalist(options); |
| 60 | + |
| 61 | + ResetTitle(); |
| 62 | + return exitCode; |
| 63 | + } |
| 64 | + |
| 65 | + private static async Task<int> RunIncrementalist(SlnOptions options) |
| 66 | + { |
| 67 | + var logger = new ConsoleLogger("Incrementalist", |
| 68 | + (s, level) => level >= (options.Verbose ? LogLevel.Debug : LogLevel.Information), false); |
| 69 | + |
| 70 | + try |
| 71 | + { |
| 72 | + var pwd = Directory.GetCurrentDirectory(); |
| 73 | + var insideRepo = Repository.IsValid(pwd); |
| 74 | + if (!insideRepo) |
48 | 75 | { |
49 | | - StartupData.ShowHelp(); |
50 | | - ResetTitle(); |
51 | | - return 0; |
| 76 | + logger.LogError("Current path {0} is not located inside any known Git repository.", pwd); |
| 77 | + return -2; |
52 | 78 | } |
| 79 | + |
| 80 | + var repoFolder = Repository.Discover(pwd); |
| 81 | + var workingFolder = Directory.GetParent(repoFolder).Parent; |
| 82 | + |
| 83 | + |
| 84 | + |
| 85 | + if (!string.IsNullOrEmpty(repoFolder)) |
| 86 | + { |
| 87 | + if (options.ListFolders) |
| 88 | + { |
| 89 | + await AnalyzeFolderDiff(options, workingFolder, logger); |
| 90 | + } |
| 91 | + else |
| 92 | + { |
| 93 | + await AnaylzeSolutionDIff(options, workingFolder, logger); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + return 0; |
| 98 | + } |
| 99 | + catch (Exception ex) |
| 100 | + { |
| 101 | + logger.LogError(ex, "Error encountered during execution of Incrementalist."); |
| 102 | + return -1; |
53 | 103 | } |
| 104 | + } |
| 105 | + |
| 106 | + private static async Task AnalyzeFolderDiff(SlnOptions options, DirectoryInfo workingFolder, ILogger logger) |
| 107 | + { |
| 108 | + var settings = new BuildSettings(options.GitBranch, options.SolutionFilePath, workingFolder.FullName); |
| 109 | + var emitTask = new EmitAffectedFoldersTask(settings, logger); |
| 110 | + var affectedFiles = await emitTask.Run(); |
54 | 111 |
|
55 | | - var logger = new ConsoleLogger("Incrementalist", (s, level) => { return level >= LogLevel.Information; }, false); |
56 | | - |
| 112 | + var affectedFilesStr = string.Join(",", affectedFiles); |
57 | 113 |
|
58 | | - var insideRepo = Repository.IsValid(Directory.GetCurrentDirectory()); |
59 | | - Console.WriteLine("Are we inside repository? {0}", insideRepo); |
60 | | - |
61 | | - var repoFolder = Repository.Discover(Directory.GetCurrentDirectory()); |
62 | | - Console.WriteLine("Repo base is located in {0}", repoFolder); |
63 | | - var workingFolder = Directory.GetParent(repoFolder).Parent; |
| 114 | + HandleAffectedFiles(options, affectedFilesStr); |
| 115 | + } |
64 | 116 |
|
| 117 | + private static async Task AnaylzeSolutionDIff(SlnOptions options, DirectoryInfo workingFolder, ILogger logger) |
| 118 | + { |
65 | 119 | // Locate and register the default instance of MSBuild installed on this machine. |
66 | 120 | MSBuildLocator.RegisterDefaults(); |
67 | 121 |
|
68 | 122 | var msBuild = MSBuildWorkspace.Create(); |
69 | | - |
70 | | - if (!string.IsNullOrEmpty(repoFolder)) |
| 123 | + if (!string.IsNullOrEmpty(options.SolutionFilePath)) |
| 124 | + { |
| 125 | + await ProcessSln(options, options.SolutionFilePath, workingFolder, msBuild, logger); |
| 126 | + } |
| 127 | + else |
71 | 128 | { |
72 | | - |
73 | 129 | foreach (var sln in SolutionFinder.GetSolutions(workingFolder.FullName)) |
74 | 130 | { |
75 | | - var settings = new BuildSettings("dev", sln, workingFolder.FullName); |
76 | | - var emitTask = new EmitDependencyGraphTask(settings, msBuild, logger); |
77 | | - var affectedFiles = await emitTask.Run(); |
78 | | - foreach (var file in affectedFiles) |
79 | | - { |
80 | | - logger.LogInformation("{0}", file); |
81 | | - } |
| 131 | + await ProcessSln(options, sln, workingFolder, msBuild, logger); |
82 | 132 | } |
83 | 133 | } |
| 134 | + } |
84 | 135 |
|
85 | | - ResetTitle(); |
86 | | - return 0; |
| 136 | + private static async Task ProcessSln(SlnOptions options, string sln, DirectoryInfo workingFolder, |
| 137 | + MSBuildWorkspace msBuild, ILogger logger) |
| 138 | + { |
| 139 | + var settings = new BuildSettings(options.GitBranch, sln, workingFolder.FullName); |
| 140 | + var emitTask = new EmitDependencyGraphTask(settings, msBuild, logger); |
| 141 | + var affectedFiles = await emitTask.Run(); |
| 142 | + |
| 143 | + var affectedFilesStr = string.Join(",", affectedFiles); |
| 144 | + |
| 145 | + HandleAffectedFiles(options, affectedFilesStr); |
| 146 | + } |
| 147 | + |
| 148 | + private static void HandleAffectedFiles(SlnOptions options, string affectedFilesStr) |
| 149 | + { |
| 150 | +// Check to see if we're planning on writing out to the file system or not. |
| 151 | + if (!string.IsNullOrEmpty(options.OutputFile)) |
| 152 | + { |
| 153 | + File.WriteAllText(options.OutputFile, affectedFilesStr); |
| 154 | + } |
| 155 | + else |
| 156 | + { |
| 157 | + Console.WriteLine(affectedFilesStr); |
| 158 | + } |
87 | 159 | } |
88 | 160 | } |
89 | 161 | } |
0 commit comments