|
| 1 | +.SYNOPSIS |
| 2 | + Add a new exercise. |
| 3 | +.DESCRIPTION |
| 4 | + Add the files need to add a new exercise. |
| 5 | +.PARAMETER Exercise |
| 6 | + The slug of the exercise to add. |
| 7 | +.PARAMETER Author |
| 8 | + The author of the exercise. |
| 9 | +.PARAMETER Difficulty |
| 10 | + The difficulty of the exercise on a scale from 1 to 10 (optional, default: 1). |
| 11 | +.EXAMPLE |
| 12 | + The example below will add the "acronym" exercise |
| 13 | + PS C:\> ./bin/add-practice-exercise.ps1 acronym |
| 14 | +#> |
| 15 | + |
| 16 | +[CmdletBinding(SupportsShouldProcess)] |
| 17 | +param ( |
| 18 | + [Parameter(Position = 0, Mandatory = $true)][string]$Exercise, |
| 19 | + [Parameter(Mandatory = $true)][string]$Author, |
| 20 | + [Parameter()][int]$Difficulty = 1 |
| 21 | +) |
| 22 | + |
| 23 | +$ErrorActionPreference = "Stop" |
| 24 | +$PSNativeCommandUseErrorActionPreference = $true |
| 25 | + |
| 26 | +# Use configlet to create the exercise |
| 27 | +& bin/fetch-configlet |
| 28 | +& bin/configlet create --practice-exercise $Exercise --difficulty $Difficulty --author $Author |
| 29 | + |
| 30 | +# Create project |
| 31 | +$exerciseName = (Get-Culture).TextInfo.ToTitleCase($Exercise).Replace("-", "") |
| 32 | +$exerciseDir = "exercises/practice/${Exercise}" |
| 33 | +$project = "${exerciseDir}/${ExerciseName}.csproj" |
| 34 | +& dotnet new xunit --force -lang "C#" --target-framework-override net8.0 -o $exerciseDir -n $ExerciseName |
| 35 | +& dotnet sln exercises/Exercises.sln add $project |
| 36 | + |
| 37 | +# Update project packages |
| 38 | +& dotnet remove $project package coverlet.collector |
| 39 | +& dotnet add $project package Exercism.Tests --version 0.1.0-beta1 |
| 40 | +& dotnet add $project package xunit.runner.visualstudio --version 2.4.3 |
| 41 | +& dotnet add $project package xunit --version 2.4.1 |
| 42 | +& dotnet add $project package Microsoft.NET.Test.Sdk --version 16.8.3 |
| 43 | + |
| 44 | +# Remove and update files |
| 45 | +Remove-Item -Path "${exerciseDir}/UnitTest1.cs" |
| 46 | +Remove-Item -Path "${exerciseDir}/GlobalUsings.cs" |
| 47 | +(Get-Content -Path ".editorconfig") -Replace "\[\*\.cs\]", "[${exerciseName}.cs]" | Set-Content -Path (Join-Path "${exerciseDir}" -ChildPath ".editorconfig") |
| 48 | + |
| 49 | +# Add and run generator (this will update the tests file) |
| 50 | +$generator = "generators/Exercises/Generators/${ExerciseName}.cs" |
| 51 | +Add-Content -Path $generator -Value @" |
| 52 | +using System; |
| 53 | +
|
| 54 | +using Exercism.CSharp.Output; |
| 55 | +
|
| 56 | +namespace Exercism.CSharp.Exercises.Generators; |
| 57 | +
|
| 58 | +internal class ${exerciseName} : ExerciseGenerator |
| 59 | +{ |
| 60 | +} |
| 61 | +"@ |
| 62 | +& dotnet run --project generators --exercise $Exercise |
| 63 | + |
| 64 | +# Output the next steps |
| 65 | +$files = Get-Content "exercises/practice/${Exercise}/.meta/config.json" | ConvertFrom-Json | Select-Object -ExpandProperty files |
| 66 | +Write-Output @" |
| 67 | +Your next steps are: |
| 68 | +- Check the test suite in $($files.test | Join-String -Separator ",") |
| 69 | + - If the tests need changes, update the '${exerciseName}' class in the '${generator}' file |
| 70 | + and then run: 'dotnet run --project generators --exercise ${Exercise}' |
| 71 | +- Any test cases you don't implement, mark them in 'exercises/practice/${slug}/.meta/tests.toml' with "include = false" |
| 72 | +- Create the example solution in $($files.example | Join-String -Separator ",") |
| 73 | +- Verify the example solution passes the tests by running 'bin/verify-exercises ${slug}' |
| 74 | +- Create the stub solution in $($files.solution | Join-String -Separator ",") |
| 75 | +- Update the 'difficulty' value for the exercise's entry in the 'config.json' file in the repo's root |
| 76 | +- Validate CI using 'bin/configlet lint' and 'bin/configlet fmt' |
| 77 | +"@ |
0 commit comments