Skip to content

Commit c1f5b87

Browse files
committed
working on adding commandline parsing (#12)
* working on adding commandline parsing * fixed commandline parsing NuGet package * removed dotnet-nightly build * commandline parsing integration * completed first pass of product
1 parent 57191e0 commit c1f5b87

File tree

5 files changed

+123
-28
lines changed

5 files changed

+123
-28
lines changed

NuGet.config

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,5 @@
66
<packageSources>
77
<clear />
88
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
9-
<add key="dotnet-nightly" value="https://dotnet.myget.org/F/msbuild/api/v3/index.json" />
109
</packageSources>
1110
</configuration>

src/Incrementalist.Cmd/Incrementalist.Cmd.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
</PropertyGroup>
99

1010
<ItemGroup>
11+
<PackageReference Include="CommandLineParser" Version="2.5.0" />
1112
<PackageReference Include="Microsoft.Build.Locator" Version="1.2.2" />
1213
<PackageReference Include="Microsoft.CodeAnalysis.Workspaces.MSBuild" Version="3.0.0" />
1314
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="2.2.0" />

src/Incrementalist.Cmd/Program.cs

Lines changed: 97 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Linq;
66
using System.Runtime.InteropServices;
77
using System.Threading.Tasks;
8+
using CommandLine;
89
using Incrementalist.Cmd.Commands;
910
using Incrementalist.Git;
1011
using Incrementalist.ProjectSystem;
@@ -42,48 +43,119 @@ static async Task<int> Main(string[] args)
4243
{
4344
SetTitle();
4445

45-
if (args.Length >= 1)
46+
SlnOptions options = null;
47+
var result = Parser.Default.ParseArguments<SlnOptions>(args).MapResult(r =>
4648
{
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)
4875
{
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;
5278
}
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;
53103
}
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();
54111

55-
var logger = new ConsoleLogger("Incrementalist", (s, level) => { return level >= LogLevel.Information; }, false);
56-
112+
var affectedFilesStr = string.Join(",", affectedFiles);
57113

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+
}
64116

117+
private static async Task AnaylzeSolutionDIff(SlnOptions options, DirectoryInfo workingFolder, ILogger logger)
118+
{
65119
// Locate and register the default instance of MSBuild installed on this machine.
66120
MSBuildLocator.RegisterDefaults();
67121

68122
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
71128
{
72-
73129
foreach (var sln in SolutionFinder.GetSolutions(workingFolder.FullName))
74130
{
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);
82132
}
83133
}
134+
}
84135

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+
}
87159
}
88160
}
89161
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using CommandLine;
2+
3+
namespace Incrementalist.Cmd
4+
{
5+
6+
public sealed class SlnOptions
7+
{
8+
[Option('s', "sln", HelpText ="The name of the Solution file to be analyzed by Incrementalist.", Required = false)]
9+
public string SolutionFilePath { get; set; }
10+
11+
[Option('f', "file", HelpText = "If specified, writes the output to the named file.", Required = false)]
12+
public string OutputFile { get; set; }
13+
14+
[Option('l', "folders-only", HelpText = "List affected folders instead of .NET projects", Required = false)]
15+
public bool ListFolders { get; set; }
16+
17+
[Option('b', "branch", HelpText = "The git branch to compare against. i.e. the `dev` or the `master` branch.", Required = true, Default = "dev")]
18+
public string GitBranch { get; set; }
19+
20+
[Option(
21+
Default = false,
22+
HelpText = "Prints out extensive debug logs during operation.")]
23+
public bool Verbose { get; set; }
24+
}
25+
}

src/Incrementalist/ProjectSystem/Cmds/FilterAffectedProjectFilesCmd.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Diagnostics.Contracts;
4-
using System.Linq;
53
using System.Threading;
64
using System.Threading.Tasks;
75
using Incrementalist.Git;

0 commit comments

Comments
 (0)