Use standard printing/formatting functions
Getty Ritter
7 years ago
| 39 | 39 | where |
| 40 | 40 | |
| 41 | 41 | go q [] (ExposeFile path) = |
| 42 |
return ( |
|
| 42 | return (cOpenFile "file source" (root </> sourceDir q </> path)) | |
| 43 | 43 | go _ [] ExposeSections{} = |
| 44 | 44 | throw ("Splice identifier «" % stext % |
| 45 | 45 | "» matches a file with sections") s |
| 73 | 73 | where |
| 74 | 74 | doReplace file frag = do |
| 75 | 75 | new <- case findFragments root frag e of |
| 76 |
Left err -> cError |
|
| 76 | Left err -> cError ("Fragment error: " % text) err | |
| 77 | 77 | Right x -> x |
| 78 | 78 | return (TL.replace (toIdent frag) new file) |
| 79 | 79 | |
| 80 | 80 | readSection :: FilePath -> T.Text -> IO TL.Text |
| 81 | 81 | readSection path f = do |
| 82 |
contents <- |
|
| 82 | contents <- cOpenFile "section source" path | |
| 83 | 83 | case TL.breakOn (toIdent f) contents of |
| 84 | (_, "") -> cError (format ("Unable to find section " % shown % | |
| 85 | " in file " % string) f path) | |
| 84 | (_, "") -> cError ("Unable to find section " % shown % | |
| 85 | " in file " % string) f path | |
| 86 | 86 | (_, rs) -> |
| 87 | 87 | let (_, skipLine) = TL.breakOn "\n" rs |
| 88 | 88 | (section, _) = TL.breakOn "«end»" (TL.drop 1 skipLine) |
| 92 | 92 | runSplice :: Config -> Options -> IO () |
| 93 | 93 | runSplice conf opts = do |
| 94 | 94 | let root = Sys.takeDirectory (optFile opts) |
| 95 |
f <- |
|
| 95 | f <- cOpenFile "document" (root </> confDocument conf) | |
| 96 | 96 | rs <- doReplacements root (confSources conf) f |
| 97 | 97 | TL.putStrLn rs |
| 14 | 14 | runTest :: Config -> Options -> IO () |
| 15 | 15 | runTest conf opts = do |
| 16 | 16 | let root = Sys.takeDirectory (optFile opts) |
| 17 |
cDebug ( |
|
| 17 | cDebug ("running tests for " % shown) (confDocument conf) | |
| 18 | 18 | forM_ (confSources conf) $ \ samp -> do |
| 19 |
cDebug ( |
|
| 19 | cDebug ("- running test for " % stext) (sourceName samp) | |
| 20 | 20 | runCommand root samp |
| 21 | 21 | |
| 22 | 22 | runCommand :: FilePath -> Source -> IO () |
| 23 | 23 | runCommand root src = do |
| 24 | 24 | forM_ (sourceCommands src) $ \ln -> do |
| 25 | 25 | let dir = root </> sourceDir src |
| 26 |
cDebug ( |
|
| 26 | cDebug (" - $ " % shown % " in " % string) ln dir | |
| 27 | 27 | let process = (Sys.shell ln) { Sys.cwd = Just (root </> sourceDir src) } |
| 28 | 28 | (_, _, _, h) <- Sys.createProcess process |
| 29 | 29 | Sys.waitForProcess h |
| 1 | {-# LANGUAGE OverloadedStrings #-} | |
| 2 | ||
| 3 | module Collage.Utils | |
| 4 | ( cOutput | |
| 5 | , cDebug | |
| 6 | , cWarn | |
| 7 | , cError | |
| 8 | ||
| 9 | , cOpenFile | |
| 10 | ||
| 11 | , throw | |
| 12 | ||
| 13 | , F.format | |
| 14 | , (F.%) | |
| 15 | , F.stext | |
| 16 | , F.text | |
| 17 | , F.string | |
| 18 | , F.shown | |
| 19 | ) where | |
| 20 | ||
| 21 | import qualified Formatting as F | |
| 22 | import qualified Data.Text.Lazy as TL | |
| 23 | import qualified Data.Text.Lazy.Builder as TL | |
| 24 | import qualified Data.Text.Lazy.IO as TL | |
| 25 | import qualified System.Directory as Sys | |
| 26 | import qualified System.Exit as Sys | |
| 27 | import qualified System.IO as Sys | |
| 28 | import qualified System.Posix.IO as Posix | |
| 29 | import qualified System.Posix.Terminal as Posix | |
| 30 | ||
| 31 | import Prelude (FilePath, IO, Either(Left), ($)) | |
| 32 | ||
| 33 | throw :: F.Format (Either TL.Text r) a -> a | |
| 34 | throw f = | |
| 35 | F.runFormat f (\ b -> Left (TL.toLazyText b)) | |
| 36 | ||
| 37 | stderr :: TL.Text -> IO () | |
| 38 | stderr = TL.hPutStr Sys.stderr | |
| 39 | ||
| 40 | -- | Write output to stdout | |
| 41 | cOutput :: TL.Text -> IO () | |
| 42 | cOutput = TL.putStrLn | |
| 43 | ||
| 44 | -- | Write a debug message to stderr. | |
| 45 | cDebug :: F.Format (IO ()) a -> a | |
| 46 | cDebug msg = F.runFormat msg $ \ b -> | |
| 47 | TL.hPutStrLn Sys.stderr (TL.toLazyText b) | |
| 48 | ||
| 49 | -- | Write a warning message to stderr. If we are connected to a TTY, | |
| 50 | -- then this will write in an orange color. | |
| 51 | cWarn :: F.Format (IO ()) a -> a | |
| 52 | cWarn msg = F.runFormat msg $ \b -> do | |
| 53 | isTTY <- Posix.queryTerminal Posix.stdOutput | |
| 54 | if isTTY | |
| 55 | then do stderr "\x1b[93m" | |
| 56 | stderr (TL.toLazyText b) | |
| 57 | stderr "\x1b[39m\n" | |
| 58 | else TL.hPutStrLn Sys.stderr (TL.toLazyText b) | |
| 59 | ||
| 60 | -- | Write an error message to stderr and exit. If we are connected to | |
| 61 | -- a TTY, this message will be in red. | |
| 62 | cError :: F.Format (IO r) a -> a | |
| 63 | cError msg = F.runFormat msg $ \b -> do | |
| 64 | isTTY <- Posix.queryTerminal Posix.stdOutput | |
| 65 | if isTTY | |
| 66 | then do stderr "\x1b[91m" | |
| 67 | stderr (TL.toLazyText b) | |
| 68 | stderr "\x1b[39m\n" | |
| 69 | else TL.hPutStrLn Sys.stderr (TL.toLazyText b) | |
| 70 | Sys.exitFailure | |
| 71 | ||
| 72 | ||
| 73 | cOpenFile :: TL.Text -> FilePath -> IO TL.Text | |
| 74 | cOpenFile purpose path = do | |
| 75 | exists <- Sys.doesFileExist path | |
| 76 | if exists | |
| 77 | then TL.readFile path | |
| 78 | else cError ("Unable to open " F.% F.text F.% | |
| 79 | " file at " F.% F.string) purpose path |