Skip to content

Commit f85438d

Browse files
committed
Read names of examples from commandline
1 parent 48c7752 commit f85438d

File tree

1 file changed

+22
-11
lines changed

1 file changed

+22
-11
lines changed

message-index/helper-tool.hs

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

3-
import Control.Monad (forM)
3+
import Control.Monad (forM_, forM)
44
import Data.Char (toLower, isSpace)
55
import Text.Read (readMaybe)
66
import System.Directory (createDirectory)
@@ -46,12 +46,13 @@ type ErrorCode = String
4646

4747
readCode :: IO ErrorCode
4848
readCode = do
49-
putStrLn "What is the errorcode that you want to document."
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."
5051
putStr "Input: "
5152
ln <- getLine
5253
case (readMaybe ln) :: Maybe Int of
5354
Nothing -> do
54-
putStrLn "Could not parse the input as an integer. Try again."
55+
putStrLn "Could not parse the input as an integer. Only enter the numeric part of the error."
5556
readCode
5657
Just _ -> pure ln
5758

@@ -97,6 +98,7 @@ type WarningFlag = String
9798
readWarningFlag :: Severity -> IO (Maybe WarningFlag)
9899
readWarningFlag Warning = do
99100
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."
100102
putStr "Input: "
101103
ln <- getLine
102104
pure (Just ln)
@@ -107,13 +109,17 @@ type Version = String
107109

108110
readVersion :: IO Version
109111
readVersion = do
110-
putStrLn "For which version was this error message emitted for the first time?"
112+
putStrLn "Which version of the tool emitted the numeric code (not the message) for the first time?"
111113
putStrLn "Note: For GHC this is most likely 9.6.1."
112114
putStr "Input: "
113115
getLine
114116

115117
-- Examples
116-
type Examples = Int
118+
type Examples = [String]
119+
120+
validateExampleName :: String -> Bool
121+
validateExampleName "" = False
122+
validateExampleName str = not (any isSpace str)
117123

118124
-- | Only ask for examples if the system is GHC.
119125
readExamples :: System -> IO Examples
@@ -122,9 +128,16 @@ readExamples GHC = do
122128
putStr "Input: "
123129
ln <- getLine
124130
case (readMaybe ln) :: Maybe Int of
125-
Nothing -> pure 0
126-
Just n -> pure n
127-
readExamples _ = pure 0
131+
Nothing -> pure []
132+
(Just n) -> forM [1..n] readExample
133+
readExamples _ = pure []
134+
135+
readExample :: Int -> IO String
136+
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
128141

129142
-- Template
130143
data Template =
@@ -189,8 +202,7 @@ createFiles tmpl = do
189202
-- - "messages/XXX-NNNNNN/" and "messages/XXX-NNNNNN/index.md"
190203
-- - "messages/XXX-NNNNNN/before/" and "messages/XXX-NNNNNN/before/Module.hs"
191204
-- - "messages/XXX-NNNNNN/after/" and "messages/XXX-NNNNNN/after/Module.hs"
192-
forM [1 .. (examples tmpl)] $ \n -> do
193-
let example = "example" <> show n
205+
forM_ (examples tmpl) $ \example -> do
194206
let example_dir = message_dir </> example
195207
createDirectory example_dir
196208
createDirectory (example_dir </> "before")
@@ -210,7 +222,6 @@ createFiles tmpl = do
210222
, "-- Insert the fixed example here."
211223
]
212224
writeFile (example_dir </> "after" </> "Example.hs") after_module
213-
pure ()
214225

215226

216227
-------------------------------------------------------------------------------

0 commit comments

Comments
 (0)