-
Notifications
You must be signed in to change notification settings - Fork 0
/
Haskeleton.hs
167 lines (128 loc) · 3.95 KB
/
Haskeleton.hs
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
-- Usage: runhaskell Haskeleton.hs New.Module
module Haskeleton (main) where
import Data.List (groupBy, isInfixOf)
import Data.Monoid ((<>))
import System.Directory (createDirectoryIfMissing)
import System.Environment (getArgs)
import System.FilePath (joinPath, takeDirectory, (<.>))
main :: IO ()
main = do
ms <- getArgs
mapM_ createDirectories ms
mapM_ writeFiles ms
mapM_ updateFiles ms
where
createDirectories m = do
createBenchmarkDirectory m
createLibraryDirectory m
createTestSuiteDirectory m
writeFiles m = do
writeBenchmarkFile m
writeLibraryFile m
writeTestSuiteFile m
updateFiles m = do
updateCabal m
updateCriterion m
updateLibrary m
--
createDirectory :: FilePath -> IO ()
createDirectory = createDirectoryIfMissing True . takeDirectory
path :: FilePath -> String -> String -> FilePath
path d s m = joinPath (d : parts (m <> s)) <.> "hs"
parts :: String -> [String]
parts = split '.'
placeholder :: String
placeholder = "New.Module"
pragma :: String
pragma = "-- HASKELETON: "
replace :: (Eq a) => [a] -> [a] -> [a] -> [a]
replace [] _ _ = []
replace xs@(h : t) old new =
if a == old
then new <> replace b old new
else h : replace t old new
where
(a, b) = splitAt (length old) xs
split :: (Eq a) => a -> [a] -> [[a]]
split x xs = fmap (dropWhile (== x)) (groupBy (const (x /=)) xs)
updateFile :: FilePath -> String -> IO ()
updateFile p m = do
contents <- readFile p
seq (length contents) (return ())
writeFile p (unlines (go =<< lines contents))
where
go line = if pragma `isInfixOf` line
then [replace (replace line pragma "") placeholder m, line]
else [line]
-- Benchmark
createBenchmarkDirectory :: String -> IO ()
createBenchmarkDirectory = createDirectory . benchmarkPath
benchmarkDirectory :: String
benchmarkDirectory = "benchmark"
benchmarkPath :: String -> FilePath
benchmarkPath = path benchmarkDirectory benchmarkSuffix
benchmarkTemplate :: String -> String
benchmarkTemplate m = unlines
[ "module " <> m <> "Bench (benchmarks) where"
, ""
, "import " <> m <> " ()"
, ""
, "import Criterion"
, ""
, "benchmarks :: [Benchmark]"
, "benchmarks = []"
]
benchmarkSuffix :: String
benchmarkSuffix = "Bench"
writeBenchmarkFile :: String -> IO ()
writeBenchmarkFile m = writeFile (benchmarkPath m) (benchmarkTemplate m)
-- Cabal
cabalPath :: FilePath
cabalPath = "CTG1371.cabal"
updateCabal :: String -> IO ()
updateCabal = updateFile cabalPath
-- Criterion
criterionPath :: FilePath
criterionPath = benchmarkPath ""
updateCriterion :: String -> IO ()
updateCriterion = updateFile criterionPath
-- Library
createLibraryDirectory :: String -> IO ()
createLibraryDirectory = createDirectory . libraryPath
libraryDirectory :: String
libraryDirectory = "library"
libraryPath :: String -> FilePath
libraryPath = path libraryDirectory librarySuffix
libraryTemplate :: String -> String
libraryTemplate m = unlines
[ "-- | TODO"
, "module " <> m <> " () where"
]
librarySuffix :: String
librarySuffix = ""
updateLibrary :: String -> IO ()
updateLibrary = updateFile (libraryPath "CTG1371")
writeLibraryFile :: String -> IO ()
writeLibraryFile m = writeFile (libraryPath m) (libraryTemplate m)
-- Test Suite
createTestSuiteDirectory :: String -> IO ()
createTestSuiteDirectory = createDirectory . testSuitePath
testSuiteDirectory :: String
testSuiteDirectory = "test-suite"
testSuitePath :: String -> FilePath
testSuitePath = path testSuiteDirectory testSuiteSuffix
testSuiteTemplate :: String -> String
testSuiteTemplate m = unlines
[ "module " <> m <> "Spec (spec) where"
, ""
, "import " <> m <> " ()"
, ""
, "import Test.Hspec"
, ""
, "spec :: Spec"
, "spec = it \"is\" pending"
]
testSuiteSuffix :: String
testSuiteSuffix = "Spec"
writeTestSuiteFile :: String -> IO ()
writeTestSuiteFile m = writeFile (testSuitePath m) (testSuiteTemplate m)