packages feed

casa-reoc-0.1.0.0: examples/write-examples.hs

{-# OPTIONS_GHC -Wall #-}

{- | Writes the documents of each example into its own directory of
@examples@, with the placeholders of the document build:

> cabal run reoc-examples -- --output examples
-}
module Main (main) where

import qualified Data.Text.IO as Text
import ReOC (renderReOCErrors, writeDocuments)
import ReOC.Examples (examples)
import System.Environment (getArgs)
import System.Exit (die)
import System.FilePath ((</>))
import System.IO (stderr)

usage :: String
usage =
  unlines
    [ "usage: reoc-examples [--output DIRECTORY]"
    , ""
    , "Writes the documents of each example into its own directory of DIRECTORY (default examples)."
    ]

main :: IO ()
main = do
  args <- getArgs
  output <- case args of
    [] -> pure "examples"
    ["--output", d] -> pure d
    _ -> die usage
  mapM_
    ( \(name, filled) ->
        case filled of
          Left errors -> do
            Text.hPutStr stderr (renderReOCErrors errors)
            die ("the example " <> name <> " is not valid")
          Right r -> do
            writeDocuments (output </> name) r
            putStrLn ("wrote " <> output </> name)
    )
    examples