forked from exercism/fsharp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
add-new-exercise.ps1
110 lines (90 loc) · 3.42 KB
/
add-new-exercise.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<#
.SYNOPSIS
Add a new exercise.
.DESCRIPTION
Add the files need to add a new exercise.
.PARAMETER Exercise
The slug of the exercise to add.
.PARAMETER Topics
The topics of the exercise (optional).
.PARAMETER Core
Indicates if the exercise is a core exercise (optional).
.PARAMETER Difficulty
The difficulty of the exercise on a scale from 1 to 10 (optional).
.PARAMETER UnlockedBy
The slug of the exercise that unlocks exercise (optional).
.EXAMPLE
The example below will add the "acronym" exercise
PS C:\> ./add-new-exercise.ps1 acronym
.EXAMPLE
The example below will add the "acronym" exercise as a core exercise
PS C:\> ./add-new-exercise.ps1 acronym -Core
.EXAMPLE
The example below will add the "acronym" exercise as a core exercise, with
two topics, a specified difficulty and being unlocked by "two-fer"
PS C:\> ./add-new-exercise.ps1 acronym -Core -Topics strings,optional -Difficulty 3 -UnlockedBy two-fer
#>
param (
[Parameter(Position = 0, Mandatory = $true)][string]$Exercise,
[Parameter()][string[]]$Topics = @(),
[Parameter()][switch]$Core,
[Parameter()][int]$Difficulty = 1,
[Parameter()]$UnlockedBy
)
# Import shared functionality
. ./shared.ps1
$exerciseName = (Get-Culture).TextInfo.ToTitleCase($Exercise).Replace("-", "")
$exercisesDir = Resolve-Path "exercises"
$exerciseDir = Join-Path $exercisesDir $Exercise
function Add-Project {
Write-Output "Adding project"
$fsProj = "$exerciseDir/$exerciseName.fsproj"
Run-Command "dotnet new xunit -lang ""F#"" -o $exerciseDir -n $exerciseName"
Run-Command "dotnet new tool-manifest -o $exerciseDir"
Run-Command "dotnet tool install --tool-manifest $exerciseDir/.config/dotnet-tools.json fantomas-tool"
Run-Command "dotnet sln ""$exercisesDir/Exercises.sln"" add $fsProj"
Remove-Item -Path "$exerciseDir/Program.fs"
Remove-Item -Path "$exerciseDir/Tests.fs"
New-Item -ItemType File -Path "$exerciseDir/$exerciseName.fs" -Value "module $exerciseName"
New-Item -ItemType File -Path "$exerciseDir/Example.fs" -Value "module $exerciseName"
[xml]$proj = Get-Content $fsProj
$proj.Project.ItemGroup[0].Compile[0].Include = "$exerciseName.fs"
$proj.Project.ItemGroup[0].Compile[1].Include = "${exerciseName}Tests.fs"
$proj.Save($fsProj)
}
function Add-Generator {
Write-Output "Adding generator"
$generatorsDir = Resolve-Path "generators"
$generator = "type $exerciseName() =`n inherit GeneratorExercise()`n"
Add-Content -Path "$generatorsDir/Generators.fs" -Value $generator
}
function Update-Readme {
Write-Output "Updating README"
./update-docs.ps1 $Exercise
}
function Update-Tests {
Write-Output "Updating test suite"
./generate-tests.ps1 $Exercise
}
function Update-Config-Json {
Write-Output "Updating config.json"
$configJson = Resolve-Path "config.json"
$config = Get-Content $configJson | ConvertFrom-JSON
$config.exercises += [pscustomobject]@{
slug = $Exercise;
uuid = [Guid]::NewGuid();
core = $Core.IsPresent;
unlocked_by = $UnlockedBy;
difficulty = $Difficulty;
topics = $Topics;
}
ConvertTo-Json -InputObject $config -Depth 10 | Set-Content -Path $configJson
Run-Command "./bin/fetch-configlet"
Run-Command "./bin/configlet fmt ."
}
Add-Project
Add-Generator
Update-Readme
Update-Tests
Update-Config-Json
exit $LastExitCode