Repository navigation
Expand file tree
/
Copy pathadd-new-exercise.ps1
More file actions
123 lines (101 loc) · 3.87 KB
/
Copy pathadd-new-exercise.ps1
File metadata and controls
123 lines (101 loc) · 3.87 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
<#
.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"
$csProj = "$exerciseDir/$exerciseName.csproj"
Run-Command "dotnet new xunit -lang ""C#"" -o $exerciseDir -n $exerciseName"
Run-Command "dotnet sln ""$exercisesDir/Exercises.sln"" add $csProj"
Remove-Item -Path "$exerciseDir/UnitTest1.cs"
New-Item -ItemType File -Path "$exerciseDir/$exerciseName.cs"
New-Item -ItemType File -Path "$exerciseDir/Example.cs"
[xml]$proj = Get-Content $csProj
$compilePropertyGroup = $proj.CreateElement("PropertyGroup");
$compileElement = $proj.CreateElement("Compile");
$removeAttribute = $proj.CreateAttribute("Remove");
$removeAttribute.Value = "Example.cs";
$compileElement.Attributes.Append($removeAttribute);
$compilePropertyGroup.AppendChild($compileElement);
$propertyGroup = $proj.Project.PropertyGroup
$propertyGroup.ParentNode.InsertAfter($compilePropertyGroup, $propertyGroup)
$proj.Save($csProj)
}
function Add-Generator {
Write-Output "Adding generator"
$generatorsDir = Resolve-Path "generators"
$generatorsExercisesDir = Join-Path $generatorsDir "Exercises"
$generatorsExerciseGeneratorsDir = Join-Path $generatorsExercisesDir "Generators"
$generator = Join-Path $generatorsExerciseGeneratorsDir "$exerciseName.cs"
$generatorClass = "namespace Exercism.CSharp.Exercises.Generators`n{`n public class $exerciseName : GeneratorExercise`n {`n }`n}"
Set-Content -Path $generator -Value $generatorClass
}
function Copy-Track-Files {
Write-Output "Copying track files"
./copy-track-files.ps1 $Exercise
}
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
Copy-Track-Files
Update-Readme
Update-Tests
Update-Config-Json
exit $LastExitCode