Skip to content

Commit

Permalink
More updates (#1285)
Browse files Browse the repository at this point in the history
* Simplify check code snippets

* Simplify test.ps1

* More updates

* Update version

* MOre tweak
  • Loading branch information
ErikSchierboom authored Oct 20, 2024
1 parent 1459e07 commit b51af7a
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 150 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/check-code-snippets.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
- name: Setup .NET
uses: actions/setup-dotnet@6bd8b7f7774af54e05809fcc5431931b3eb1ddee
with:
dotnet-version: "7.0.100"
dotnet-version: "8.0.403"

- name: Validate Code Snippets
run: pwsh ./check-code-snippets.ps1
run: pwsh ./bin/check-code-snippets.ps1
12 changes: 8 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@ on:

jobs:
test:
runs-on: ubuntu-20.04
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@d632683dd7b4114ad314bca15554477dd762a938
- name: Checkout code
uses: actions/checkout@d632683dd7b4114ad314bca15554477dd762a938

- uses: actions/setup-dotnet@6bd8b7f7774af54e05809fcc5431931b3eb1ddee
with:
dotnet-version: "8.0.200"
- run: pwsh ./test.ps1
dotnet-version: "8.0.403"

- name: Run tests
run: pwsh ./bin/test.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
The difficulty of the exercise on a scale from 1 to 10 (optional, default: 1).
.EXAMPLE
The example below will add the "acronym" exercise
PS C:\> ./add-practice-exercise.ps1 acronym
PS C:\> bin/add-practice-exercise.ps1 acronym
#>

[CmdletBinding(SupportsShouldProcess)]
Expand Down
18 changes: 18 additions & 0 deletions bin/check-code-snippets.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<#
.SYNOPSIS
Validate F# code snippets in concept exercise documentation.
.DESCRIPTION
Run a CLI utility that will:
- recursively search a list of directories for Markdown documentation
- extract any F# code snippets
- evaluate them to verify correct syntax (no type checking is performed)
.EXAMPLE
PS C:\> bin/check-code-snippets.ps1
Verifies all *.md files under the `concepts` and `exercises` directories
#>

$ErrorActionPreference = "Stop"
$PSNativeCommandUseErrorActionPreference = $true

Write-Output "Searching documentation for code snippets"
& dotnet run -c Release --project tools/CodeFenceChecker
55 changes: 26 additions & 29 deletions test.ps1 → bin/test.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
The slug of the exercise to verify (optional).
.EXAMPLE
The example below will verify the full solution
PS C:\> ./test.ps1
PS C:\> bin/test.ps1
.EXAMPLE
The example below will verify the "acronym" exercise
PS C:\> ./test.ps1 acronym
PS C:\> bin/test.ps1 acronym
#>

[CmdletBinding(SupportsShouldProcess)]
Expand All @@ -23,12 +23,12 @@ param (
[string]$Exercise
)

# Import shared functionality
. ./shared.ps1
$ErrorActionPreference = "Stop"
$PSNativeCommandUseErrorActionPreference = $true

function Invoke-Build-Generators {
Write-Output "Building generators"
Run-Command "dotnet build ./generators"
& dotnet build ./generators
}

function Clean($BuildDir) {
Expand All @@ -48,18 +48,22 @@ function Enable-All-UnitTests($BuildDir) {
}
}

function Add-Packages-ExerciseImplementation($PracticeExercisesDir) {
& dotnet add "${PracticeExercisesDir}/sgf-parsing" package FParsec
}

function Test-Refactoring-Projects($PracticeExercisesDir) {
Write-Output "Testing refactoring projects"
@("tree-building", "ledger", "markdown") | ForEach-Object {
Run-Command "dotnet test $practiceExercisesDir/$_"
dotnet test "${PracticeExercisesDir}/${_}"
}
}

function Set-ExerciseImplementation {
function Set-ExampleImplementation {
[CmdletBinding(SupportsShouldProcess)]
param($ExercisesDir, $ReplaceFileName)

if ($PSCmdlet.ShouldProcess("Exercise $ReplaceFileName", "replace solution with example")) {
if ($PSCmdlet.ShouldProcess("Exercise ${ReplaceFileName}", "replace solution with example")) {
Get-ChildItem -Path $ExercisesDir -Include "*.fsproj" -Recurse | ForEach-Object {
$stub = Join-Path -Path $_.Directory ($_.BaseName + ".fs")
$example = Join-Path -Path $_.Directory ".meta" $ReplaceFileName
Expand All @@ -69,44 +73,39 @@ function Set-ExerciseImplementation {
}
}

function Use-ExerciseImplementation {
function Use-ExampleImplementation {
[CmdletBinding(SupportsShouldProcess)]
param($ConceptExercisesDir, $PracticeExercisesDir)

if ($PSCmdlet.ShouldProcess("Exercises directory", "replace all solutions with corresponding examples")) {
Write-Output "Replacing concept exercise stubs with exemplar"
Set-ExerciseImplementation $ConceptExercisesDir "Exemplar.fs"
Set-ExampleImplementation $ConceptExercisesDir "Exemplar.fs"

Write-Output "Replacing practice exercise stubs with example"
Set-ExerciseImplementation $PracticeExercisesDir "Example.fs"
Set-ExampleImplementation $PracticeExercisesDir "Example.fs"
}
}

function Add-Packages-ExerciseImplementation($PracticeExercisesDir) {
Run-Command "dotnet add $PracticeExercisesDir/sgf-parsing package FParsec"
}

function Test-ExerciseImplementation($Exercise, $BuildDir, $ConceptExercisesDir, $PracticeExercisesDir) {
Write-Output "Running tests"

if (-Not $Exercise) {
Run-Command "dotnet test $buildDir/Exercises.sln"
dotnet test $BuildDir
}
elseif (Test-Path "$ConceptExercisesDir/$Exercise") {
Run-Command "dotnet test $conceptExercisesDir/$exercise"
elseif (Test-Path "${ConceptExercisesDir}/${Exercise}") {
dotnet test "${ConceptExercisesDir}/${Exercise}"
}
elseif (Test-Path "$PracticeExercisesDir/$Exercise") {
Run-Command "dotnet test $practiceExercisesDir/$exercise"
elseif (Test-Path "${PracticeExercisesDir}/${Exercise}") {
dotnet test "${PracticeExercisesDir}/${Exercise}"
}
else {
throw "Could not find exercise '$Exercise'"
throw "Could not find exercise '${Exercise}'"
}
}


$buildDir = Join-Path $PSScriptRoot "build"
$conceptExercisesDir = Join-Path $buildDir "concept"
$practiceExercisesDir = Join-Path $buildDir "practice"
$buildDir = "${PSScriptRoot}/build"
$practiceExercisesDir = "${buildDir}/practice"
$conceptExercisesDir = "${buildDir}/concept"
$sourceDir = Resolve-Path "exercises"

Clean $buildDir
Expand All @@ -115,11 +114,9 @@ Enable-All-UnitTests $buildDir

if (!$Exercise) {
Invoke-Build-Generators
Test-Refactoring-Projects -PracticeExercisesDir $practiceExercisesDir
Test-Refactoring-Projects $practiceExercisesDir
}

Use-ExerciseImplementation -ConceptExercisesDir $conceptExercisesDir -PracticeExercisesDir $practiceExercisesDir
Use-ExampleImplementation $conceptExercisesDir $practiceExercisesDir
Add-Packages-ExerciseImplementation -PracticeExercisesDir $practiceExercisesDir
Test-ExerciseImplementation -Exercise $Exercise -BuildDir $buildDir -ConceptExercisesDir $conceptExercisesDir -PracticeExercisesDir $practiceExercisesDir

exit $LastExitCode
15 changes: 15 additions & 0 deletions bin/update-exercise-tools.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<#
.SYNOPSIS
Update exercise tools.
.DESCRIPTION
Update the exercise tools for each exercise.
.EXAMPLE
PS C:\> bin/update-exercise-tools.ps1
#>

$ErrorActionPreference = "Stop"
$PSNativeCommandUseErrorActionPreference = $true

Get-ChildItem -Path exercises -Filter dotnet-tools.json -Force -Recurse | ForEach-Object {
dotnet tool update fantomas-tool --tool-manifest $_.FullName
}
32 changes: 32 additions & 0 deletions bin/update-exercises.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<#
.SYNOPSIS
Update the exercises.
.DESCRIPTION
Update the exercises' docs, metadata, and tests.
.PARAMETER Exercise
The slug of the exercise to update (optional).
.EXAMPLE
The example below will update all exercises
PS C:\> bin/update-exercises.ps1
.EXAMPLE
The example below will update the "acronym" exercise
PS C:\> bin/update-exercises.ps1 acronym
#>

[CmdletBinding(SupportsShouldProcess)]
param (
[Parameter(Position = 0, Mandatory = $false)][string]$Exercise
)

$ErrorActionPreference = "Stop"
$PSNativeCommandUseErrorActionPreference = $true

& bin/fetch-configlet

if ($Exercise) {
& configlet sync --docs --metadata --filepaths --update --yes --exercise $Exercise
& dotnet run --project generators --exercise $Exercise
} else {
& configlet sync --docs --metadata --filepaths --update --yes
& dotnet run --project generators
}
46 changes: 0 additions & 46 deletions check-code-snippets.ps1

This file was deleted.

68 changes: 0 additions & 68 deletions update-exercise-tools.ps1

This file was deleted.

0 comments on commit b51af7a

Please sign in to comment.