diff --git a/bin/add-practice-exercise.ps1 b/bin/add-practice-exercise.ps1 index 0ea300ec9..25668ce6a 100644 --- a/bin/add-practice-exercise.ps1 +++ b/bin/add-practice-exercise.ps1 @@ -47,8 +47,7 @@ Remove-Item -Path "${exerciseDir}/UnitTest1.cs" (Get-Content -Path ".editorconfig") -Replace "\[\*\.cs\]", "[${exerciseName}.cs]" | Set-Content -Path "${exerciseDir}/.editorconfig" # Create new generator template and run generator (this will update the tests file) -& dotnet run --project generators new --exercise $Exercise -& dotnet run --project generators generate --exercise $Exercise +bin\update-tests.ps1 -e $Exercise -new # Output the next steps $files = Get-Content "exercises/practice/${Exercise}/.meta/config.json" | ConvertFrom-Json | Select-Object -ExpandProperty files diff --git a/bin/update-exercises.ps1 b/bin/update-exercises.ps1 index e46bd0f98..78009459d 100644 --- a/bin/update-exercises.ps1 +++ b/bin/update-exercises.ps1 @@ -25,8 +25,8 @@ $PSNativeCommandUseErrorActionPreference = $true if ($Exercise) { & configlet sync --docs --metadata --filepaths --update --yes --exercise $Exercise - & dotnet run --project generators --exercise $Exercise + & dotnet run --project generators update --exercise $Exercise } else { & configlet sync --docs --metadata --filepaths --update --yes - & dotnet run --project generators + & dotnet run --project generators update } diff --git a/bin/generate-tests.ps1 b/bin/update-tests.ps1 similarity index 62% rename from bin/generate-tests.ps1 rename to bin/update-tests.ps1 index 453c5e36a..b4462f248 100644 --- a/bin/generate-tests.ps1 +++ b/bin/update-tests.ps1 @@ -6,6 +6,8 @@ The tests are generated from canonical data. .PARAMETER Exercise The slug of the exercise to generate the tests for (optional). +.PARAMETER CreateNew + Create a new test generator file before generating the tests (switch). .EXAMPLE The example below will generate the tests for exercises with a template PS C:\> ./test.ps1 @@ -17,14 +19,25 @@ [CmdletBinding(SupportsShouldProcess)] param ( [Parameter(Position = 0, Mandatory = $false)] - [string]$Exercise + [string]$Exercise, + + [Parameter()] + [switch]$New ) $ErrorActionPreference = "Stop" $PSNativeCommandUseErrorActionPreference = $true -if ($Exercise) { - dotnet run --project generators generate --exercise $Exercise -} else { - dotnet run --project generators generate +function Run-Command($verb) { + if ($Exercise) { + & dotnet run --project generators $verb --exercise $Exercise + } else { + & dotnet run --project generators $verb + } +} + +if ($New.IsPresent) { + Run-Command new } + +Run-Command update diff --git a/generators/CanonicalData.cs b/generators/CanonicalData.cs index 82dc68571..9d8d6912e 100644 --- a/generators/CanonicalData.cs +++ b/generators/CanonicalData.cs @@ -2,16 +2,12 @@ using System.Text.Json; using System.Text.Json.Nodes; -using LibGit2Sharp; - namespace Generators; internal record CanonicalData(Exercise Exercise, JsonNode[] TestCases); internal static class CanonicalDataParser { - static CanonicalDataParser() => ProbSpecs.Sync(); - internal static CanonicalData Parse(Exercise exercise) => new(exercise, ParseTestCases(exercise)); private static JsonNode[] ParseTestCases(Exercise exercise) => @@ -36,26 +32,4 @@ private static JsonNode ToTestCase(JsonNode testCaseJson, IEnumerable pa testCaseJson["path"] = JsonSerializer.SerializeToNode(path); return testCaseJson; } - - private static class ProbSpecs - { - internal static void Sync() - { - Clone(); - Pull(); - } - - private static void Clone() - { - if (!Directory.Exists(Paths.ProbSpecsDir)) - Repository.Clone("https://github.com/exercism/problem-specifications.git", Paths.ProbSpecsDir); - } - - private static void Pull() - { - using var repo = new Repository(Paths.ProbSpecsDir); - Commands.Pull(repo, new Signature("Exercism", "info@exercism.org", DateTimeOffset.Now), new PullOptions()); - } - } -} - +} \ No newline at end of file diff --git a/generators/ProbSpecs.cs b/generators/ProbSpecs.cs new file mode 100644 index 000000000..91e6312b2 --- /dev/null +++ b/generators/ProbSpecs.cs @@ -0,0 +1,24 @@ +using LibGit2Sharp; + +namespace Generators; + +internal static class ProbSpecs +{ + internal static void Sync() + { + Clone(); + Pull(); + } + + private static void Clone() + { + if (!Directory.Exists(Paths.ProbSpecsDir)) + Repository.Clone("https://github.com/exercism/problem-specifications.git", Paths.ProbSpecsDir); + } + + private static void Pull() + { + using var repo = new Repository(Paths.ProbSpecsDir); + Commands.Pull(repo, new Signature("Exercism", "info@exercism.org", DateTimeOffset.Now), new PullOptions()); + } +} \ No newline at end of file diff --git a/generators/Program.cs b/generators/Program.cs index 44f4634a4..08d287436 100644 --- a/generators/Program.cs +++ b/generators/Program.cs @@ -5,26 +5,23 @@ namespace Generators; public static class Program { static void Main(string[] args) => - Parser.Default.ParseArguments(args) - .WithParsed(HandleGenerateCommand) + Parser.Default.ParseArguments(args) .WithParsed(HandleNewCommand) + .WithParsed(HandleUpdateCommand) + .WithParsed(HandleSyncCommand) .WithNotParsed(HandleErrors); - private static void HandleGenerateCommand(GenerateOptions options) => - Exercises.Templated(options.Exercise).ForEach(TestsGenerator.Generate); - private static void HandleNewCommand(NewOptions options) => Exercises.Untemplated(options.Exercise).ForEach(TemplateGenerator.Generate); + private static void HandleUpdateCommand(UpdateOptions options) => + Exercises.Templated(options.Exercise).ForEach(TestsGenerator.Generate); + + private static void HandleSyncCommand(SyncOptions options) => + ProbSpecs.Sync(); + private static void HandleErrors(IEnumerable errors) => errors.ToList().ForEach(Console.Error.WriteLine); - - [Verb("generate", isDefault: true, HelpText = "Generate the test file's contents using the exercise's generator template file.")] - private class GenerateOptions - { - [Option('e', "exercise", Required = false, HelpText = "The exercise (slug) which tests file to generate.")] - public string? Exercise { get; set; } - } [Verb("new", HelpText = "Create a new exercise generator template file.")] private class NewOptions @@ -32,4 +29,14 @@ private class NewOptions [Option('e', "exercise", Required = false, HelpText = "The exercise (slug) for which to generate a generator file.")] public string? Exercise { get; set; } } + + [Verb("update", isDefault: true, HelpText = "Update the test file's contents using the exercise's generator template file.")] + private class UpdateOptions + { + [Option('e', "exercise", Required = false, HelpText = "The exercise (slug) which tests file to generate.")] + public string? Exercise { get; set; } + } + + [Verb("sync", HelpText = "Sync the problem specifications repo.")] + private class SyncOptions; } \ No newline at end of file