Skip to content

Commit

Permalink
Tweak scripts (#2371)
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikSchierboom authored Feb 5, 2025
1 parent b0eaef3 commit a5149b0
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 48 deletions.
3 changes: 1 addition & 2 deletions bin/add-practice-exercise.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions bin/update-exercises.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
23 changes: 18 additions & 5 deletions bin/generate-tests.ps1 → bin/update-tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
28 changes: 1 addition & 27 deletions generators/CanonicalData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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) =>
Expand All @@ -36,26 +32,4 @@ private static JsonNode ToTestCase(JsonNode testCaseJson, IEnumerable<string> 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", "[email protected]", DateTimeOffset.Now), new PullOptions());
}
}
}

}
24 changes: 24 additions & 0 deletions generators/ProbSpecs.cs
Original file line number Diff line number Diff line change
@@ -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", "[email protected]", DateTimeOffset.Now), new PullOptions());
}
}
31 changes: 19 additions & 12 deletions generators/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,38 @@ namespace Generators;
public static class Program
{
static void Main(string[] args) =>
Parser.Default.ParseArguments<NewOptions, GenerateOptions>(args)
.WithParsed<GenerateOptions>(HandleGenerateCommand)
Parser.Default.ParseArguments<NewOptions, UpdateOptions>(args)
.WithParsed<NewOptions>(HandleNewCommand)
.WithParsed<UpdateOptions>(HandleUpdateCommand)
.WithParsed<SyncOptions>(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<Error> 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
{
[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;
}

0 comments on commit a5149b0

Please sign in to comment.