1
1
module Main where
2
2
3
- import Control.Monad (forM_ , forM )
4
- import Data.Char (toLower , isSpace )
5
- import Text.Read (readMaybe )
3
+ import Control.Monad (forM , forM_ )
4
+ import Data.Char (isSpace , toLower )
6
5
import System.Directory (createDirectory )
7
6
import System.FilePath ((</>) )
7
+ import Text.Read (readMaybe )
8
8
9
9
-------------------------------------------------------------------------------
10
10
-- Querying the user about the diagnostic
@@ -19,100 +19,99 @@ normalize = fmap toLower . strip
19
19
20
20
-- Querying for the system: GHC / GHCup / Stack
21
21
22
- data System = GHC | GHCup | Stack deriving Show
22
+ data System = GHC | GHCup | Stack deriving ( Show )
23
23
24
24
readSystem :: IO System
25
25
readSystem = do
26
- putStrLn " For which system do you want to generate a message?"
27
- putStrLn " 1) GHC"
28
- putStrLn " 2) GHCup"
29
- putStrLn " 3) Stack"
30
- putStr " Input (Default = GHC): "
31
- ln <- getLine
32
- case normalize ln of
33
- " 1" -> pure GHC
34
- " ghc" -> pure GHC
35
- " 2" -> pure GHCup
36
- " ghcup" -> pure GHCup
37
- " 3" -> pure Stack
38
- " stack" -> pure Stack
39
- _ -> pure GHC
26
+ putStrLn " For which system do you want to generate a message?"
27
+ putStrLn " 1) GHC"
28
+ putStrLn " 2) GHCup"
29
+ putStrLn " 3) Stack"
30
+ putStr " Input (Default = GHC): "
31
+ ln <- getLine
32
+ case normalize ln of
33
+ " 1" -> pure GHC
34
+ " ghc" -> pure GHC
35
+ " 2" -> pure GHCup
36
+ " ghcup" -> pure GHCup
37
+ " 3" -> pure Stack
38
+ " stack" -> pure Stack
39
+ _ -> pure GHC
40
40
41
41
-- Querying for the error code
42
42
43
- -- | We need to encode the error code as a string in order
43
+ -- | We need to encode the error code as a string in order
44
44
-- to preserve leading 0's.
45
45
type ErrorCode = String
46
46
47
47
readCode :: IO ErrorCode
48
48
readCode = do
49
- putStrLn " What is the numeric code that you want to document."
50
- putStrLn " For example, enter \" 01234\" if you want to document GHC-01234."
51
- putStr " Input: "
52
- ln <- getLine
53
- case ( readMaybe ln) :: Maybe Int of
54
- Nothing -> do
55
- putStrLn " Could not parse the input as an integer. Only enter the numeric part of the error."
56
- readCode
57
- Just _ -> pure ln
49
+ putStrLn " What is the numeric code that you want to document."
50
+ putStrLn " For example, enter \" 01234\" if you want to document GHC-01234."
51
+ putStr " Input: "
52
+ ln <- getLine
53
+ case readMaybe ln :: Maybe Int of
54
+ Nothing -> do
55
+ putStrLn " Could not parse the input as an integer. Only enter the numeric part of the error."
56
+ readCode
57
+ Just _ -> pure ln
58
58
59
59
-- Title
60
60
type Title = String
61
61
62
62
readTitle :: IO Title
63
63
readTitle = do
64
- putStrLn " What is the title of the error message."
65
- putStr " Input: "
66
- getLine
64
+ putStrLn " What is the title of the error message."
65
+ putStr " Input: "
66
+ getLine
67
67
68
68
-- Summary
69
69
type Summary = String
70
70
71
71
readSummary :: IO Summary
72
72
readSummary = do
73
- putStrLn " Give a short summary of the error message."
74
- putStr " Input: "
75
- getLine
73
+ putStrLn " Give a short summary of the error message."
74
+ putStr " Input: "
75
+ getLine
76
76
77
77
-- Severity
78
- data Severity = Error | Warning deriving Show
78
+ data Severity = Error | Warning deriving ( Show )
79
79
80
80
readSeverity :: IO Severity
81
81
readSeverity = do
82
- putStrLn " What is the severity of the diagnostic."
83
- putStrLn " 1) Error"
84
- putStrLn " 2) Warning"
85
- putStr " Input (Default = Error): "
86
- ln <- getLine
87
- case normalize ln of
88
- " 1" -> pure Error
89
- " error" -> pure Error
90
- " 2" -> pure Warning
91
- " warning" -> pure Warning
92
- _ -> pure Error
82
+ putStrLn " What is the severity of the diagnostic."
83
+ putStrLn " 1) Error"
84
+ putStrLn " 2) Warning"
85
+ putStr " Input (Default = Error): "
86
+ ln <- getLine
87
+ case normalize ln of
88
+ " 1" -> pure Error
89
+ " error" -> pure Error
90
+ " 2" -> pure Warning
91
+ " warning" -> pure Warning
92
+ _ -> pure Error
93
93
94
94
-- Warning flag
95
95
type WarningFlag = String
96
96
97
97
-- | Only ask for a warning flag if Severity = Warning.
98
98
readWarningFlag :: Severity -> IO (Maybe WarningFlag )
99
99
readWarningFlag Warning = do
100
- putStrLn " What is the warning flag which enables this warning."
101
- putStrLn " For example, enter \" -Wtabs\" if you are documenting GHC's warning about tabs in your source file."
102
- putStr " Input: "
103
- ln <- getLine
104
- pure (Just ln)
100
+ putStrLn " What is the warning flag which enables this warning."
101
+ putStrLn " For example, enter \" -Wtabs\" if you are documenting GHC's warning about tabs in your source file."
102
+ putStr " Input: "
103
+ Just <$> getLine
105
104
readWarningFlag _ = pure Nothing
106
105
107
106
-- Version
108
107
type Version = String
109
108
110
109
readVersion :: IO Version
111
110
readVersion = do
112
- putStrLn " Which version of the tool emitted the numeric code (not the message) for the first time?"
113
- putStrLn " Note: For GHC this is most likely 9.6.1."
114
- putStr " Input: "
115
- getLine
111
+ putStrLn " Which version of the tool emitted the numeric code (not the message) for the first time?"
112
+ putStrLn " Note: For GHC this is most likely 9.6.1."
113
+ putStr " Input: "
114
+ getLine
116
115
117
116
-- Examples
118
117
type Examples = [String ]
@@ -124,111 +123,118 @@ validateExampleName str = not (any isSpace str)
124
123
-- | Only ask for examples if the system is GHC.
125
124
readExamples :: System -> IO Examples
126
125
readExamples GHC = do
127
- putStrLn " How many examples should be generated?"
128
- putStr " Input: "
129
- ln <- getLine
130
- case ( readMaybe ln) :: Maybe Int of
131
- Nothing -> pure []
132
- (Just n) -> forM [1 .. n] readExample
126
+ putStrLn " How many examples should be generated?"
127
+ putStr " Input: "
128
+ ln <- getLine
129
+ case readMaybe ln :: Maybe Int of
130
+ Nothing -> pure []
131
+ (Just n) -> forM [1 .. n] readExample
133
132
readExamples _ = pure []
134
133
135
134
readExample :: Int -> IO String
136
135
readExample i = do
137
- putStrLn (" Give a name for example " <> show i)
138
- putStr " Input: "
139
- ln <- getLine
140
- if validateExampleName ln then pure ln else readExample i
136
+ putStrLn (" Give a name for example " <> show i)
137
+ putStr " Input: "
138
+ ln <- getLine
139
+ if validateExampleName ln then pure ln else readExample i
141
140
142
141
-- Template
143
- data Template =
144
- MkTemplate { system :: System
145
- , code :: ErrorCode
146
- , title :: Title
147
- , summary :: Summary
148
- , severity :: Severity
149
- , warningflag :: Maybe WarningFlag
150
- , introduced :: Version
151
- , examples :: Examples
152
- } deriving Show
153
-
142
+ data Template = MkTemplate
143
+ { system :: System ,
144
+ code :: ErrorCode ,
145
+ title :: Title ,
146
+ summary :: Summary ,
147
+ severity :: Severity ,
148
+ warningflag :: Maybe WarningFlag ,
149
+ introduced :: Version ,
150
+ examples :: Examples
151
+ }
152
+ deriving ( Show )
154
153
155
154
readTemplate :: IO Template
156
155
readTemplate = do
157
- putStrLn " This tool helps you create the scaffolding for a new error message on the error-message-index."
158
- putStrLn " You can leave any of the text fields blank and fill them in by hand later."
159
- putStrLn " "
160
- sys <- readSystem
161
- putStrLn " "
162
- code <- readCode
163
- putStrLn " "
164
- title <- readTitle
165
- putStrLn " "
166
- summary <- readSummary
167
- putStrLn " "
168
- severity <- readSeverity
169
- putStrLn " "
170
- warningflag <- readWarningFlag severity
171
- putStrLn " "
172
- version <- readVersion
173
- putStrLn " "
174
- examples <- readExamples sys
175
- pure (MkTemplate sys code title summary severity warningflag version examples)
156
+ putStrLn " This tool helps you create the scaffolding for a new error message on the error-message-index."
157
+ putStrLn " You can leave any of the text fields blank and fill them in by hand later."
158
+ putStrLn " "
159
+ sys <- readSystem
160
+ putStrLn " "
161
+ code <- readCode
162
+ putStrLn " "
163
+ title <- readTitle
164
+ putStrLn " "
165
+ summary <- readSummary
166
+ putStrLn " "
167
+ severity <- readSeverity
168
+ putStrLn " "
169
+ warningflag <- readWarningFlag severity
170
+ putStrLn " "
171
+ version <- readVersion
172
+ putStrLn " "
173
+ examples <- readExamples sys
174
+ pure (MkTemplate sys code title summary severity warningflag version examples)
176
175
177
176
-------------------------------------------------------------------------------
178
177
-- Creating the files and directories from the template
179
178
-------------------------------------------------------------------------------
180
179
181
180
createFiles :: Template -> IO ()
182
181
createFiles tmpl = do
183
- putStrLn " Creating scaffolding for the following configuration:"
184
- print tmpl
185
- putStrLn " "
186
-
187
- -- Create the new directory "messages/XXX-NNNNNN/" and "messages/XXX-NNNNNN/index.md"
188
- let message_dir = " messages" </> case system tmpl of { GHC -> " GHC-" ; GHCup -> " GHCup-" ; Stack -> " S-" } ++ code tmpl
189
- createDirectory message_dir
190
- let toplvl_index = unlines [ " ---"
191
- , " title: " <> title tmpl
192
- , " summary: " <> summary tmpl
193
- , " severity: " <> case severity tmpl of { Warning -> " warning" ; Error -> " error" }
194
- , " introduced: " <> introduced tmpl
195
- , " ---"
196
- , " "
197
- , " Insert your error message here."
198
- ]
199
- writeFile (message_dir </> " index.md" ) toplvl_index
200
-
201
- -- Create the example directories and entries:
202
- -- - "messages/XXX-NNNNNN/" and "messages/XXX-NNNNNN/index.md"
203
- -- - "messages/XXX-NNNNNN/before/" and "messages/XXX-NNNNNN/before/Module.hs"
204
- -- - "messages/XXX-NNNNNN/after/" and "messages/XXX-NNNNNN/after/Module.hs"
205
- forM_ (examples tmpl) $ \ example -> do
206
- let example_dir = message_dir </> example
207
- createDirectory example_dir
208
- createDirectory (example_dir </> " before" )
209
- createDirectory (example_dir </> " after" )
210
- let example_index = unlines [ " ---"
211
- , " title: <insert a title for this example here>"
212
- , " ---"
213
- ]
214
- writeFile (example_dir </> " index.md" ) example_index
215
- let before_module = unlines [ " module Example where"
216
- , " "
217
- , " -- Insert the example containing a bug here."
218
- ]
219
- writeFile (example_dir </> " before" </> " Example.hs" ) before_module
220
- let after_module = unlines [ " module Example where"
221
- , " "
222
- , " -- Insert the fixed example here."
223
- ]
224
- writeFile (example_dir </> " after" </> " Example.hs" ) after_module
225
-
182
+ putStrLn " Creating scaffolding for the following configuration:"
183
+ print tmpl
184
+ putStrLn " "
185
+
186
+ -- Create the new directory "messages/XXX-NNNNNN/" and "messages/XXX-NNNNNN/index.md"
187
+ let message_dir = " messages" </> case system tmpl of { GHC -> " GHC-" ; GHCup -> " GHCup-" ; Stack -> " S-" } ++ code tmpl
188
+ createDirectory message_dir
189
+ let toplvl_index =
190
+ unlines
191
+ [ " ---" ,
192
+ " title: " <> title tmpl,
193
+ " summary: " <> summary tmpl,
194
+ " severity: " <> case severity tmpl of Warning -> " warning" ; Error -> " error" ,
195
+ " introduced: " <> introduced tmpl,
196
+ " ---" ,
197
+ " " ,
198
+ " Insert your error message here."
199
+ ]
200
+ writeFile (message_dir </> " index.md" ) toplvl_index
201
+
202
+ -- Create the example directories and entries:
203
+ -- - "messages/XXX-NNNNNN/" and "messages/XXX-NNNNNN/index.md"
204
+ -- - "messages/XXX-NNNNNN/before/" and "messages/XXX-NNNNNN/before/Module.hs"
205
+ -- - "messages/XXX-NNNNNN/after/" and "messages/XXX-NNNNNN/after/Module.hs"
206
+ forM_ (examples tmpl) $ \ example -> do
207
+ let example_dir = message_dir </> example
208
+ createDirectory example_dir
209
+ createDirectory (example_dir </> " before" )
210
+ createDirectory (example_dir </> " after" )
211
+ let example_index =
212
+ unlines
213
+ [ " ---" ,
214
+ " title: <insert a title for this example here>" ,
215
+ " ---"
216
+ ]
217
+ writeFile (example_dir </> " index.md" ) example_index
218
+ let before_module =
219
+ unlines
220
+ [ " module Example where" ,
221
+ " " ,
222
+ " -- Insert the example containing a bug here."
223
+ ]
224
+ writeFile (example_dir </> " before" </> " Example.hs" ) before_module
225
+ let after_module =
226
+ unlines
227
+ [ " module Example where" ,
228
+ " " ,
229
+ " -- Insert the fixed example here."
230
+ ]
231
+ writeFile (example_dir </> " after" </> " Example.hs" ) after_module
226
232
227
233
-------------------------------------------------------------------------------
228
234
-- Main
229
235
-------------------------------------------------------------------------------
230
236
231
237
main :: IO ()
232
238
main = do
233
- tmpl <- readTemplate
234
- createFiles tmpl
239
+ tmpl <- readTemplate
240
+ createFiles tmpl
0 commit comments