Skip to content

Commit a1b4944

Browse files
committed
Update System.CommandLine
1 parent 84dd2dc commit a1b4944

File tree

3 files changed

+89
-92
lines changed

3 files changed

+89
-92
lines changed

Bootstrap/Program.cs

Lines changed: 86 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,119 +1,116 @@
11
using System.CommandLine;
2-
using System.CommandLine.Invocation;
32
using System.Text.Json;
43
using Microsoft.Build.Locator;
54
using TestMyCode.CSharp.Core.Compiler;
65
using TestMyCode.CSharp.Core.Data;
76
using TestMyCode.CSharp.Core.Test;
87

9-
namespace TestMyCode.CSharp.Bootstrap;
8+
MSBuildLocator.RegisterDefaults();
109

11-
public static class Program
10+
Option<bool> generatePointsFile = new(aliases: new[]
1211
{
13-
public static async Task Main(string[] args)
14-
{
15-
MSBuildLocator.RegisterDefaults();
16-
17-
RootCommand rootCommand = Program.GenerateCommands();
18-
rootCommand.Handler = CommandHandler.Create(async (bool generatePointsFile, bool runTests, DirectoryInfo? projectDir, FileInfo? outputFile) =>
19-
{
20-
if (!generatePointsFile && !runTests)
21-
{
22-
Console.WriteLine("You didn't give me a task! Use --help for... help! Leave a like if you found out this to be useful!");
23-
24-
return;
25-
}
12+
"--generate-points-file",
13+
"-p",
14+
}, description: "Generates JSON file of all the points that can be achieved from the project.");
2615

27-
string projectDirectory = projectDir?.FullName ?? Environment.CurrentDirectory;
16+
Option<bool> runTests = new(aliases: new[]
17+
{
18+
"--run-tests",
19+
"-t"
20+
}, description: "Runs tests of the project.");
2821

29-
ProjectCompiler compiler = new();
22+
Option<DirectoryInfo> projectDir = new Option<DirectoryInfo>(
23+
aliases: new[]
24+
{
25+
"--project-dir",
26+
"-d"
27+
},
28+
description: "Sets the directory where the project is located at. By default using the working directory.",
29+
getDefaultValue: () => new DirectoryInfo(Environment.CurrentDirectory)
30+
).ExistingOnly();
31+
32+
Option<FileInfo?> outputFile = new Option<FileInfo?>(
33+
aliases: new[]
34+
{
35+
"--output-file",
36+
"-o"
37+
},
38+
description: "The output file used to write results."
39+
).LegalFileNamesOnly();
3040

31-
List<string> assemblyPaths;
41+
RootCommand rootCommand = new(description: "TMC helper for running C# projects.")
42+
{
43+
generatePointsFile,
44+
runTests,
45+
projectDir,
46+
outputFile
47+
};
3248

33-
try
34-
{
35-
assemblyPaths = compiler.CompileTestProjects(projectDirectory);
36-
}
37-
catch (CompilationFaultedException exception)
38-
{
39-
Console.WriteLine(exception.Message);
49+
rootCommand.SetHandler(CommandHandler, generatePointsFile, runTests, projectDir, outputFile);
4050

41-
Environment.Exit(1);
51+
return await rootCommand.InvokeAsync(args);
4252

43-
return;
44-
}
53+
static async Task<int> CommandHandler(bool generatePointsFile, bool runTests, DirectoryInfo projectDir, FileInfo? outputFile)
54+
{
55+
if (!generatePointsFile && !runTests)
56+
{
57+
Console.WriteLine("You didn't give me a task! Use --help for... help! Leave a like if you found out this to be useful!");
4558

46-
TestProjectData projectData = new();
47-
foreach (string assemblyPath in assemblyPaths)
48-
{
49-
projectData.LoadProject(assemblyPath);
50-
}
59+
return 1;
60+
}
5161

52-
if (generatePointsFile)
53-
{
54-
FileInfo resultsFile = outputFile ?? new FileInfo(Path.Combine(projectDirectory, ".tmc_available_points.json"));
62+
ProjectCompiler compiler = new();
5563

56-
await WriteToFile(resultsFile, projectData.Points);
57-
}
64+
List<string> assemblyPaths;
5865

59-
if (runTests)
60-
{
61-
ProjectTestRunner testRunner = new(projectData);
62-
testRunner.RunAssemblies(projectData.Assemblies);
66+
try
67+
{
68+
assemblyPaths = compiler.CompileTestProjects(projectDir);
69+
}
70+
catch (CompilationFaultedException exception)
71+
{
72+
Console.WriteLine(exception.Message);
73+
74+
return 1;
75+
}
6376

64-
FileInfo resultsFile = outputFile ?? new FileInfo(Path.Combine(projectDirectory, ".tmc_test_results.json"));
77+
TestProjectData projectData = new();
78+
foreach (string assemblyPath in assemblyPaths)
79+
{
80+
projectData.LoadProject(assemblyPath);
81+
}
6582

66-
await WriteToFile(resultsFile, testRunner.TestResults);
67-
}
83+
if (generatePointsFile)
84+
{
85+
FileInfo resultsFile = outputFile ?? new FileInfo(Path.Combine(projectDir.FullName, ".tmc_available_points.json"));
6886

69-
static async Task WriteToFile<T>(FileInfo resultsFile, T data)
70-
{
71-
if (resultsFile.Exists)
72-
{
73-
resultsFile.Delete();
74-
}
87+
await WriteToFile(resultsFile, projectData.Points);
88+
}
7589

76-
await using FileStream stream = resultsFile.Open(FileMode.OpenOrCreate, FileAccess.Write);
90+
if (runTests)
91+
{
92+
ProjectTestRunner testRunner = new(projectData);
93+
testRunner.RunAssemblies(projectData.Assemblies);
7794

78-
await JsonSerializer.SerializeAsync(stream, data, options: new JsonSerializerOptions
79-
{
80-
WriteIndented = true
81-
});
82-
}
83-
});
95+
FileInfo resultsFile = outputFile ?? new FileInfo(Path.Combine(projectDir.FullName, ".tmc_test_results.json"));
8496

85-
await rootCommand.InvokeAsync(args);
97+
await WriteToFile(resultsFile, testRunner.TestResults);
8698
}
8799

88-
private static RootCommand GenerateCommands()
100+
static async Task WriteToFile<T>(FileInfo resultsFile, T data)
89101
{
90-
return new RootCommand(description: "TMC helper for running C# projects.")
102+
if (resultsFile.Exists)
103+
{
104+
resultsFile.Delete();
105+
}
106+
107+
await using FileStream stream = resultsFile.Open(FileMode.OpenOrCreate, FileAccess.Write);
108+
109+
await JsonSerializer.SerializeAsync(stream, data, options: new JsonSerializerOptions
91110
{
92-
new Option<bool>(aliases: new[]
93-
{
94-
"--generate-points-file",
95-
"-p",
96-
}, description: "Generates JSON file containing all the points that can be achieved from given project."),
97-
98-
new Option<bool>(aliases: new[]
99-
{
100-
"--run-tests",
101-
"-t"
102-
}, description: "Runs tests for the project, by default using the working directory."),
103-
104-
new Option<DirectoryInfo>(aliases: new[]
105-
{
106-
"--project-dir"
107-
}, description: "Sets the directory where the project that should be used is located at.")
108-
{
109-
Argument = new Argument<DirectoryInfo>().ExistingOnly()
110-
},
111-
112-
new Option<FileInfo>(aliases: new[]
113-
{
114-
"--output-file",
115-
"-o"
116-
}, description: "The output file used to write results.")
117-
};
111+
WriteIndented = true
112+
});
118113
}
119-
}
114+
115+
return 0;
116+
}

Bootstrap/TestMyCode.CSharp.Bootstrap.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
<ItemGroup>
2020
<PackageReference Include="Microsoft.Build.Locator" Version="1.4.10-g38d95f4814" />
21-
<PackageReference Include="System.CommandLine" Version="2.0.0-beta1.20574.7" />
21+
<PackageReference Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
2222
</ItemGroup>
2323

2424
<ItemGroup>

Core/Compiler/ProjectCompiler.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ public ProjectCompiler()
1717
this.projectCollection.SetGlobalProperty("PublishDir", Path.Combine(ProjectCompiler.BinPath, ProjectCompiler.OutputPath));
1818
}
1919

20-
public List<string> CompileTestProjects(string projectPath)
20+
public List<string> CompileTestProjects(DirectoryInfo projectDir)
2121
{
2222
List<string> files = new();
2323

24-
foreach (string projectFile in Directory.EnumerateFiles(projectPath, "*Tests.csproj", SearchOption.AllDirectories))
24+
foreach (string projectFile in Directory.EnumerateFiles(projectDir.FullName, "*Tests.csproj", SearchOption.AllDirectories))
2525
{
2626
string projectRoot = Path.GetDirectoryName(projectFile) ?? string.Empty;
2727

0 commit comments

Comments
 (0)