Skip to content

Commit aaa4dec

Browse files
committed
Apply suggested changes
1 parent 9e9b9e9 commit aaa4dec

File tree

1 file changed

+46
-24
lines changed

1 file changed

+46
-24
lines changed

message-index/helper-tool.hs

Lines changed: 46 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
module Main where
22

33
import Control.Monad (forM, forM_)
4-
import Data.Char (isSpace, toLower)
4+
import Data.Char (isLower, isSpace, toLower, toUpper)
55
import System.Directory (createDirectory)
6-
import System.FilePath ((</>))
6+
import System.FilePath ((<.>), (</>))
7+
import System.IO (BufferMode (..), hSetBuffering, stdout)
78
import Text.Read (readMaybe)
89

10+
-------------------------------------------------------------------------------
11+
-- Run this tool with `runghc` on the commandline:
12+
13+
-- $ runghc helper-tool.hs
14+
15+
-------------------------------------------------------------------------------
16+
917
-------------------------------------------------------------------------------
1018
-- Querying the user about the diagnostic
1119
-------------------------------------------------------------------------------
@@ -17,13 +25,13 @@ normalize = fmap toLower . strip
1725
strip = f . f
1826
f = reverse . dropWhile isSpace
1927

20-
-- Querying for the system: GHC / GHCup / Stack
28+
-- Querying for the tool: GHC / GHCup / Stack
2129

22-
data System = GHC | GHCup | Stack deriving (Show)
30+
data Tool = GHC | GHCup | Stack deriving (Show)
2331

24-
readSystem :: IO System
25-
readSystem = do
26-
putStrLn "For which system do you want to generate a message?"
32+
readTool :: IO Tool
33+
readTool = do
34+
putStrLn "Which tool's error code do you want to document?"
2735
putStrLn " 1) GHC"
2836
putStrLn " 2) GHCup"
2937
putStrLn " 3) Stack"
@@ -36,7 +44,10 @@ readSystem = do
3644
"ghcup" -> pure GHCup
3745
"3" -> pure Stack
3846
"stack" -> pure Stack
39-
_ -> pure GHC
47+
"" -> pure GHC
48+
_ -> do
49+
putStrLn "Didn't understand input. Please type a tool name or a number."
50+
readTool
4051

4152
-- Querying for the error code
4253

@@ -61,7 +72,8 @@ type Title = String
6172

6273
readTitle :: IO Title
6374
readTitle = do
64-
putStrLn "What is the title of the error message."
75+
putStrLn "What is the title of the error message?"
76+
putStrLn "This is used as the title of the documentation page as well as in links to the page."
6577
putStr "Input: "
6678
getLine
6779

@@ -71,6 +83,7 @@ type Summary = String
7183
readSummary :: IO Summary
7284
readSummary = do
7385
putStrLn "Give a short summary of the error message."
86+
putStrLn "This appears on the overview page that lists all the documented errors and warnings."
7487
putStr "Input: "
7588
getLine
7689

@@ -89,16 +102,20 @@ readSeverity = do
89102
"error" -> pure Error
90103
"2" -> pure Warning
91104
"warning" -> pure Warning
92-
_ -> pure Error
105+
"" -> pure Error
106+
_ -> do
107+
putStrLn "Please type \"error\" or \"warning\" or a number."
108+
readSeverity
93109

94110
-- Warning flag
95111
type WarningFlag = String
96112

97113
-- | Only ask for a warning flag if Severity = Warning.
98114
readWarningFlag :: Severity -> IO (Maybe WarningFlag)
99115
readWarningFlag Warning = do
100-
putStrLn "What is the warning flag which enables this warning."
116+
putStrLn "What is the warning flag which enables this warning?"
101117
putStrLn "For example, enter \"-Wtabs\" if you are documenting GHC's warning about tabs in your source file."
118+
putStrLn "You can leave this blank if you're not sure."
102119
putStr "Input: "
103120
Just <$> getLine
104121
readWarningFlag _ = pure Nothing
@@ -118,29 +135,30 @@ type Examples = [String]
118135

119136
validateExampleName :: String -> Bool
120137
validateExampleName "" = False
121-
validateExampleName str = not (any isSpace str)
138+
validateExampleName str@(s : _) = not (any isSpace str) && isLower s
122139

123140
-- | Only ask for examples if the system is GHC.
124-
readExamples :: System -> IO Examples
141+
readExamples :: Tool -> IO Examples
125142
readExamples GHC = do
126143
putStrLn "How many examples should be generated?"
127144
putStr "Input: "
128145
ln <- getLine
129146
case readMaybe ln :: Maybe Int of
130147
Nothing -> pure []
131-
(Just n) -> forM [1 .. n] readExample
148+
Just n -> forM [1 .. n] readExample
132149
readExamples _ = pure []
133150

134151
readExample :: Int -> IO String
135152
readExample i = do
136153
putStrLn ("Give a name for example " <> show i)
154+
putStrLn "The name should not contain spaces and begin with a lowercase letter."
137155
putStr "Input: "
138156
ln <- getLine
139157
if validateExampleName ln then pure ln else readExample i
140158

141159
-- Template
142160
data Template = MkTemplate
143-
{ system :: System,
161+
{ tool :: Tool,
144162
code :: ErrorCode,
145163
title :: Title,
146164
summary :: Summary,
@@ -156,7 +174,7 @@ readTemplate = do
156174
putStrLn "This tool helps you create the scaffolding for a new error message on the error-message-index."
157175
putStrLn "You can leave any of the text fields blank and fill them in by hand later."
158176
putStrLn ""
159-
sys <- readSystem
177+
sys <- readTool
160178
putStrLn ""
161179
code <- readCode
162180
putStrLn ""
@@ -184,7 +202,7 @@ createFiles tmpl = do
184202
putStrLn ""
185203

186204
-- 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
205+
let message_dir = "messages" </> case tool tmpl of { GHC -> "GHC-"; GHCup -> "GHCup-"; Stack -> "S-" } ++ code tmpl
188206
createDirectory message_dir
189207
let toplvl_index =
190208
unlines
@@ -200,11 +218,14 @@ createFiles tmpl = do
200218
writeFile (message_dir </> "index.md") toplvl_index
201219

202220
-- 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"
221+
-- - "messages/XXX-NNNNNN/" and "messages/XXX-NNNNNN/example-name/index.md"
222+
-- - "messages/XXX-NNNNNN/example-name/before/" and "messages/XXX-NNNNNN/example-name/before/Module.hs"
223+
-- - "messages/XXX-NNNNNN/example-name/after/" and "messages/XXX-NNNNNN/example-name/after/Module.hs"
206224
forM_ (examples tmpl) $ \example -> do
207225
let example_dir = message_dir </> example
226+
let uppercase "" = ""
227+
uppercase (s : ss) = toUpper s : ss
228+
let example_name = uppercase example
208229
createDirectory example_dir
209230
createDirectory (example_dir </> "before")
210231
createDirectory (example_dir </> "after")
@@ -217,24 +238,25 @@ createFiles tmpl = do
217238
writeFile (example_dir </> "index.md") example_index
218239
let before_module =
219240
unlines
220-
[ "module Example where",
241+
[ "module " <> example_name <> " where",
221242
"",
222243
"-- Insert the example containing a bug here."
223244
]
224-
writeFile (example_dir </> "before" </> "Example.hs") before_module
245+
writeFile (example_dir </> "before" </> example_name <.> "hs") before_module
225246
let after_module =
226247
unlines
227-
[ "module Example where",
248+
[ "module " <> example_name <> " where",
228249
"",
229250
"-- Insert the fixed example here."
230251
]
231-
writeFile (example_dir </> "after" </> "Example.hs") after_module
252+
writeFile (example_dir </> "after" </> example_name <.> "hs") after_module
232253

233254
-------------------------------------------------------------------------------
234255
-- Main
235256
-------------------------------------------------------------------------------
236257

237258
main :: IO ()
238259
main = do
260+
hSetBuffering stdout NoBuffering
239261
tmpl <- readTemplate
240262
createFiles tmpl

0 commit comments

Comments
 (0)