-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
lint.nim
79 lines (68 loc) · 2.37 KB
/
lint.nim
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
import std/[json, os, terminal]
import ".."/[cli, helpers]
template writeError(description: string, details: string) =
stdout.styledWriteLine(fgRed, description & ":")
stdout.writeLine(details)
stdout.write "\n"
result = false
proc isValidTrackConfig(trackDir: string): bool =
result = true
let configJsonPath = trackDir / "config.json"
if fileExists(configJsonPath):
try:
let j = parseFile(configJsonPath)
except:
writeError("JSON parsing error", getCurrentExceptionMsg())
else:
writeError("Missing file", configJsonPath)
proc subdirsContain(dir: string, files: openArray[string]): bool =
## Returns `true` if every file in `files` exists in every subdirectory of
## `dir`.
##
## Returns `true` if `dir` does not exist or has no subdirectories.
result = true
if dirExists(dir):
for subdir in getSortedSubdirs(dir):
for file in files:
let path = subdir / file
if not fileExists(path):
writeError("Missing file", path)
proc conceptExerciseFilesExist(trackDir: string): bool =
## Returns true if every subdirectory in `trackDir/exercises/concept` has the
## required files.
const
conceptExerciseFiles = [
".docs" / "hints.md",
".docs" / "instructions.md",
".docs" / "introduction.md",
".meta" / "config.json",
]
let conceptDir = trackDir / "exercises" / "concept"
result = subdirsContain(conceptDir, conceptExerciseFiles)
proc conceptFilesExist(trackDir: string): bool =
## Returns true if every subdirectory in `trackDir/concepts` has the required
## files.
const
conceptFiles = [
"about.md",
"introduction.md",
"links.json",
]
let conceptsDir = trackDir / "concepts"
result = subdirsContain(conceptsDir, conceptFiles)
proc lint*(conf: Conf) =
echo "The lint command is under development.\n" &
"Please re-run this command regularly to see if your track passes " &
"the latest linting rules.\n"
let trackDir = conf.trackDir
let b1 = isValidTrackConfig(trackDir)
let b2 = conceptExerciseFilesExist(trackDir)
let b3 = conceptFilesExist(trackDir)
if b1 and b2 and b3:
echo """
Basic linting finished successfully:
- config.json exists and is valid JSON
- Every concept exercise has the required .md files and a .meta/config.json file
- Every concept has the required .md files and links.json file"""
else:
quit(1)