-
-
Notifications
You must be signed in to change notification settings - Fork 43
/
practice-exercise-generator.el
199 lines (180 loc) · 7.37 KB
/
practice-exercise-generator.el
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
;;; practice-exercise-generator.el --- Practice Exercise Generator (exercism) -*- lexical-binding: t; -*-
;;; Commentary:
;;; Used to generate solution and test stub files for practice exercises.
;;; Intended to be called by /bin/generate_practice_exercise.
;;; Known issues:
;;; Generates two newlines at the end of the solution and test file.
;;; Code:
(exercism//install-required-packages)
(require 'mustache)
(require 'ht)
(require 'string-inflection)
(require 'json)
(require 'subr-x)
(require 'seq)
(defun exercism/generate-practice-exercise (exercise-slug)
"Generate scaffolding for practice exercise EXERCISE-SLUG from the canonical data found in the problem-specifications."
(with-temp-buffer
(url-insert-file-contents
(concat
"https://raw.githubusercontent.com/exercism/problem-specifications/main/exercises/"
exercise-slug
"/canonical-data.json"))
(goto-char 1)
(let* ((json-object-type 'hash-table)
(json-key-type 'string)
(canonical-data (json-read))
(functions (exercism//retrieve-functions canonical-data)))
(exercism//generate-solution-stubs
exercise-slug canonical-data functions)
(exercism//generate-test-stubs
exercise-slug canonical-data functions))))
(defun exercism//generate-solution-stubs
(exercise-slug canonical-data functions)
"Generate solution file with function stubs.
File location is exercises/practice/EXERCISE-SLUG/EXERCISE-SLUG.el."
(let* ((mustache-partial-paths (list "templates/partials"))
(package-title (exercism//retrieve-title exercise-slug))
(generated-stubs
(mustache-render
(exercism//file-to-string
"templates/exercises/practice/solution.mustache")
(ht
("filename" exercise-slug)
("package-title" package-title)
("functions" functions)))))
(exercism//write-to-file
(concat
"../exercises/practice/" exercise-slug "/" exercise-slug ".el")
generated-stubs)
(exercism//write-to-file
(concat
"../exercises/practice/" exercise-slug "/.meta/example.el")
generated-stubs)))
(defun exercism//retrieve-functions (canonical-data)
"For each function to implement, retrieve function-name and function-paramater from CANONICAL-DATA.
Returns a list of hash-tables.
Each hash-table has the keys:
function-name: The name of the function in kebab-case
function-parameters: The parameters of the function in kebab-case, separated by spaces"
(seq-uniq (flatten-tree
(append
(named-let retrieve ((cases canonical-data))
(if (hash-table-p cases)
(retrieve (gethash "cases" cases))
(mapcar
(lambda (elem)
(if (gethash "cases" elem)
(retrieve (gethash "cases" elem))
(ht
("function-name"
(string-inflection-kebab-case-function
(gethash "property" elem)))
("function-parameters" (string-join
(seq-map
'string-inflection-kebab-case-function
(hash-table-keys (gethash "input" elem)))
" ")))))
cases)))))
(lambda (elem1 elem2)
(equal
(gethash "function-name" elem1)
(gethash "function-name" elem2)))))
(defun exercism//retrieve-title (exercise-slug)
"Retrieve canonical title of exercise EXERCISE-SLUG from metadata.toml."
(with-temp-buffer
(url-insert-file-contents
(concat
"https://raw.githubusercontent.com/exercism/problem-specifications/main/exercises/"
exercise-slug
"/metadata.toml"))
(beginning-of-buffer)
(save-match-data
(re-search-forward "^title *= *\"\\(.*\\)\"$")
(match-string 1))))
(defun exercism//write-to-file (file string)
"Writes STRING into FILE."
(with-temp-file file
(insert string)))
(defun exercism//file-to-string (file)
"Convert FILE to string."
(with-temp-buffer
(insert-file-contents file)
(buffer-string)))
(defun exercism//generate-test-stubs
(exercise-slug canonical-data functions)
"Generate test file with test function stubs.
File location is exercises/practice/EXERCISE-SLUG/EXERCISE-SLUG-test.el.
Includes the expected input and output as a comment, so contributors
don't have to look up that information separately."
(let* ((tests (exercism//retrieve-tests canonical-data))
(package-title (exercism//retrieve-title exercise-slug))
(mustache-partial-paths (list "templates/partials"))
(generated-test-stubs
(mustache-render
(exercism//file-to-string
"templates/exercises/practice/test.mustache")
(ht
("solution-filename" exercise-slug)
("filename" (concat exercise-slug "-test"))
("package-title" package-title)
("functions" functions)
("tests" tests)))))
(exercism//write-to-file
(concat
"../exercises/practice/"
exercise-slug
"/"
exercise-slug
"-test.el")
generated-test-stubs)))
(defun exercism//retrieve-tests (canonical-data)
"Retrieve test data from CANONICAL-DATA.
Deprecated tests (tests that get reimplemented) are discarded.
Returns a list of hash-tables.
Each hash-table has the keys:
test-name: name of test function in kebab-case
uuid: uuid that identifies the test
reimplements: either nil or uuid of test that gets reimplemented by that test
function-under-test: name of the function under test in kebab-case
input: text representation of the input to the function under test
expected: text representation of the expected result of the function under test"
(let* ((tests
(flatten-tree
(append
(named-let retrieve ((cases canonical-data))
(if (hash-table-p cases)
(retrieve (gethash "cases" cases))
(mapcar
(lambda (elem)
(if (gethash "cases" elem)
(retrieve (gethash "cases" elem))
(ht
("test-name"
(string-inflection-kebab-case-function
(replace-regexp-in-string
"[^[:alpha:]-]" ""
(replace-regexp-in-string
"[ |_]" "-" (gethash "description" elem)))))
("uuid" (gethash "uuid" elem))
("reimplements" (gethash "reimplements" elem))
("function-under-test"
(string-inflection-kebab-case-function
(gethash "property" elem)))
("input" (json-encode (gethash "input" elem)))
("expected"
(json-encode (gethash "expected" elem))))))
cases))))))
(reimplemented-uuids
(seq-map
(lambda (hash) (gethash "reimplements" hash)) tests))
(tests-without-reimplemented
(seq-filter
(lambda (elem)
(not
(seq-contains
reimplemented-uuids (gethash "uuid" elem))))
tests)))
tests-without-reimplemented))
(provide 'practice-exercise-generator)
;;; practice-exercise-generator.el ends here