Skip to content

Commit a5149b0

Browse files
Tweak scripts (#2371)
1 parent b0eaef3 commit a5149b0

File tree

6 files changed

+65
-48
lines changed

6 files changed

+65
-48
lines changed

bin/add-practice-exercise.ps1

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ Remove-Item -Path "${exerciseDir}/UnitTest1.cs"
4747
(Get-Content -Path ".editorconfig") -Replace "\[\*\.cs\]", "[${exerciseName}.cs]" | Set-Content -Path "${exerciseDir}/.editorconfig"
4848

4949
# Create new generator template and run generator (this will update the tests file)
50-
& dotnet run --project generators new --exercise $Exercise
51-
& dotnet run --project generators generate --exercise $Exercise
50+
bin\update-tests.ps1 -e $Exercise -new
5251

5352
# Output the next steps
5453
$files = Get-Content "exercises/practice/${Exercise}/.meta/config.json" | ConvertFrom-Json | Select-Object -ExpandProperty files

bin/update-exercises.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ $PSNativeCommandUseErrorActionPreference = $true
2525

2626
if ($Exercise) {
2727
& configlet sync --docs --metadata --filepaths --update --yes --exercise $Exercise
28-
& dotnet run --project generators --exercise $Exercise
28+
& dotnet run --project generators update --exercise $Exercise
2929
} else {
3030
& configlet sync --docs --metadata --filepaths --update --yes
31-
& dotnet run --project generators
31+
& dotnet run --project generators update
3232
}

bin/generate-tests.ps1 renamed to bin/update-tests.ps1

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
The tests are generated from canonical data.
77
.PARAMETER Exercise
88
The slug of the exercise to generate the tests for (optional).
9+
.PARAMETER CreateNew
10+
Create a new test generator file before generating the tests (switch).
911
.EXAMPLE
1012
The example below will generate the tests for exercises with a template
1113
PS C:\> ./test.ps1
@@ -17,14 +19,25 @@
1719
[CmdletBinding(SupportsShouldProcess)]
1820
param (
1921
[Parameter(Position = 0, Mandatory = $false)]
20-
[string]$Exercise
22+
[string]$Exercise,
23+
24+
[Parameter()]
25+
[switch]$New
2126
)
2227

2328
$ErrorActionPreference = "Stop"
2429
$PSNativeCommandUseErrorActionPreference = $true
2530

26-
if ($Exercise) {
27-
dotnet run --project generators generate --exercise $Exercise
28-
} else {
29-
dotnet run --project generators generate
31+
function Run-Command($verb) {
32+
if ($Exercise) {
33+
& dotnet run --project generators $verb --exercise $Exercise
34+
} else {
35+
& dotnet run --project generators $verb
36+
}
37+
}
38+
39+
if ($New.IsPresent) {
40+
Run-Command new
3041
}
42+
43+
Run-Command update

generators/CanonicalData.cs

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,12 @@
22
using System.Text.Json;
33
using System.Text.Json.Nodes;
44

5-
using LibGit2Sharp;
6-
75
namespace Generators;
86

97
internal record CanonicalData(Exercise Exercise, JsonNode[] TestCases);
108

119
internal static class CanonicalDataParser
1210
{
13-
static CanonicalDataParser() => ProbSpecs.Sync();
14-
1511
internal static CanonicalData Parse(Exercise exercise) => new(exercise, ParseTestCases(exercise));
1612

1713
private static JsonNode[] ParseTestCases(Exercise exercise) =>
@@ -36,26 +32,4 @@ private static JsonNode ToTestCase(JsonNode testCaseJson, IEnumerable<string> pa
3632
testCaseJson["path"] = JsonSerializer.SerializeToNode(path);
3733
return testCaseJson;
3834
}
39-
40-
private static class ProbSpecs
41-
{
42-
internal static void Sync()
43-
{
44-
Clone();
45-
Pull();
46-
}
47-
48-
private static void Clone()
49-
{
50-
if (!Directory.Exists(Paths.ProbSpecsDir))
51-
Repository.Clone("https://github.com/exercism/problem-specifications.git", Paths.ProbSpecsDir);
52-
}
53-
54-
private static void Pull()
55-
{
56-
using var repo = new Repository(Paths.ProbSpecsDir);
57-
Commands.Pull(repo, new Signature("Exercism", "[email protected]", DateTimeOffset.Now), new PullOptions());
58-
}
59-
}
60-
}
61-
35+
}

generators/ProbSpecs.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using LibGit2Sharp;
2+
3+
namespace Generators;
4+
5+
internal static class ProbSpecs
6+
{
7+
internal static void Sync()
8+
{
9+
Clone();
10+
Pull();
11+
}
12+
13+
private static void Clone()
14+
{
15+
if (!Directory.Exists(Paths.ProbSpecsDir))
16+
Repository.Clone("https://github.com/exercism/problem-specifications.git", Paths.ProbSpecsDir);
17+
}
18+
19+
private static void Pull()
20+
{
21+
using var repo = new Repository(Paths.ProbSpecsDir);
22+
Commands.Pull(repo, new Signature("Exercism", "[email protected]", DateTimeOffset.Now), new PullOptions());
23+
}
24+
}

generators/Program.cs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,38 @@ namespace Generators;
55
public static class Program
66
{
77
static void Main(string[] args) =>
8-
Parser.Default.ParseArguments<NewOptions, GenerateOptions>(args)
9-
.WithParsed<GenerateOptions>(HandleGenerateCommand)
8+
Parser.Default.ParseArguments<NewOptions, UpdateOptions>(args)
109
.WithParsed<NewOptions>(HandleNewCommand)
10+
.WithParsed<UpdateOptions>(HandleUpdateCommand)
11+
.WithParsed<SyncOptions>(HandleSyncCommand)
1112
.WithNotParsed(HandleErrors);
1213

13-
private static void HandleGenerateCommand(GenerateOptions options) =>
14-
Exercises.Templated(options.Exercise).ForEach(TestsGenerator.Generate);
15-
1614
private static void HandleNewCommand(NewOptions options) =>
1715
Exercises.Untemplated(options.Exercise).ForEach(TemplateGenerator.Generate);
1816

17+
private static void HandleUpdateCommand(UpdateOptions options) =>
18+
Exercises.Templated(options.Exercise).ForEach(TestsGenerator.Generate);
19+
20+
private static void HandleSyncCommand(SyncOptions options) =>
21+
ProbSpecs.Sync();
22+
1923
private static void HandleErrors(IEnumerable<Error> errors) =>
2024
errors.ToList().ForEach(Console.Error.WriteLine);
21-
22-
[Verb("generate", isDefault: true, HelpText = "Generate the test file's contents using the exercise's generator template file.")]
23-
private class GenerateOptions
24-
{
25-
[Option('e', "exercise", Required = false, HelpText = "The exercise (slug) which tests file to generate.")]
26-
public string? Exercise { get; set; }
27-
}
2825

2926
[Verb("new", HelpText = "Create a new exercise generator template file.")]
3027
private class NewOptions
3128
{
3229
[Option('e', "exercise", Required = false, HelpText = "The exercise (slug) for which to generate a generator file.")]
3330
public string? Exercise { get; set; }
3431
}
32+
33+
[Verb("update", isDefault: true, HelpText = "Update the test file's contents using the exercise's generator template file.")]
34+
private class UpdateOptions
35+
{
36+
[Option('e', "exercise", Required = false, HelpText = "The exercise (slug) which tests file to generate.")]
37+
public string? Exercise { get; set; }
38+
}
39+
40+
[Verb("sync", HelpText = "Sync the problem specifications repo.")]
41+
private class SyncOptions;
3542
}

0 commit comments

Comments
 (0)