Skip to content

Commit

Permalink
Script to redeploy testlib.sml to all exercises on changes. (#206)
Browse files Browse the repository at this point in the history
* Script to redeploy testlib.sml to all exercises on changes.

The directory name `sml-bin` makes the most sense to me for these sml
"scripts".  Adding them to the makefile also makes it even easier to
use these than it is when using "#! /usr/bin/env -S poly --script" as
a shebang line.  Consequently this should be portable to any Windows
system that has GNU make installed, so it is probably better than the
shebang.  In the code I used the path library to help make things OS
portable, but I haven't ever used SML on Windows, so it is untested
for that enironment.

* Change file perms to 664 for sml-bin/redeploy-testlib.sml

I had set it to 775 when it had a shebang and lived in `bin`.  Trying
to execute it will fail, so it shouldn't be executable.

* Make redeploy-testlib phone as well.

* Use qualified names.

I shouldn't have combined to actions but I did.  I removed all
unnecessary semicolons while refactoring to use qualified names.
Sorry.

* Format redeploy-testlib.sml like teslib.sml

I also found and removed two more unnecessary semicolons.

* Include testlib sync policy in the README.

I wrote a brief note about keeping testlib.sml in sync for all
exercises in the track, and I mentioned 'make redeploy-testlib' as the
provided tool for handling this requirement.

I did not elaborate on a policy, because we still don't really have
one.  Further work related to #110 should result in a clear policy,
and at that point the README.md might need some more updating.

* Formatting, names, parentheses.

* Update README.md

Co-authored-by: Victor Goff <keeperotphones@gmail.com>

* Add spaces inside tuples.

* Use scripts directory instead of sml-bin.

* Add a documenting comment to redeploy-testlib.sml.

Initially, I thought it must be clear enough from the title/name of
the script itself, but on further reflection, a documenting comment
might help a non-native english speaker avoid the difficulties of
ambiguity.

Co-authored-by: Victor Goff <keeperotphones@gmail.com>
  • Loading branch information
guygastineau and kotp authored Jun 20, 2022
1 parent a76e44e commit c14b2f3
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 2 deletions.
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,7 @@ test-%:
@cd ./exercises/practice/$(exercise) && cat test.sml | sed 's/$(exercise).sml/.meta\/example.sml/' | poly -q
@echo

.PHONY: test
redeploy-testlib:
poly --script scripts/redeploy-testlib.sml

.PHONY: test redeploy-testlib
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ Every exercise must have at least these files:

### `testlib.sml`

This helper has this structures:
The copy of `testlib.sml` for each exercise should be in sync with `lib/testlib.sml`.
`make redeploy-testlib` is provided for synchronizing all exercises with `lib/testlib.sml`
when it is updated.

This helper has these structures:

```sml
structure Expect:
Expand Down
55 changes: 55 additions & 0 deletions scripts/redeploy-testlib.sml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
(* Re-deploy lib/testlib.sml on changes.
*
* When lib.testlib.sml is changed it should be updated for all exercises.
* Use this "script" a la `make redeploy-testlib`.
*)

structure Path = OS.Path
structure File = OS.FileSys

fun printLn x = print (x ^ "\n")

fun fileExists x = File.access (x, [])

fun testLibName base slug =
Path.joinDirFile
{
dir = Path.concat (base, slug),
file = "testlib.sml"
}

fun dirList dirname =
let
fun go d xs =
case File.readDir d of
SOME x => go d (x::xs)
| NONE => (File.closeDir d; xs)
in
go (File.openDir dirname) []
end

fun fileBytes path =
let
val instream = BinIO.openIn path
val bytes = BinIO.inputAll instream
in
BinIO.closeIn instream; bytes
end

fun writeFileBytes bytes path =
let
val out = BinIO.openOut path
in
BinIO.output (out, bytes);
BinIO.closeOut out
end

fun relUnixPathToAbs x = Path.concat (File.getDir (), Path.fromUnixPath x)

val _ =
let
val practice = relUnixPathToAbs "exercises/practice"
val testLib = fileBytes "lib/testlib.sml"
in
List.app (writeFileBytes testLib o testLibName practice) (dirList practice)
end

0 comments on commit c14b2f3

Please sign in to comment.