cymake.hs 2.04 KB
Newer Older
Björn Peemöller 's avatar
Björn Peemöller committed
1 2 3
{- |
    Module      :  $Header$
    Description :  Main module
Björn Peemöller 's avatar
Björn Peemöller committed
4
    Copyright   :  (c) 2005        Martin Engelke
5
                       2011 - 2014 Björn Peemöller
Björn Peemöller 's avatar
Björn Peemöller committed
6 7 8 9 10
    License     :  OtherLicense

    Maintainer  :  bjp@informatik.uni-kiel.de
    Stability   :  experimental
    Portability :  portable
Bjoern Peemoeller's avatar
Bjoern Peemoeller committed
11 12 13 14 15 16

    Command line tool for generating Curry representations (e.g. FlatCurry,
    AbstractCurry) for a Curry source file including all imported modules.
-}
module Main (main) where

17 18
import Curry.Base.Monad (runCYIO)

Björn Peemöller 's avatar
Björn Peemöller committed
19
import Base.Messages
Björn Peemöller 's avatar
Björn Peemöller committed
20
import Files.CymakePath (cymakeGreeting, cymakeVersion)
Björn Peemöller 's avatar
Björn Peemöller committed
21
import Html.CurryHtml   (source2html)
Björn Peemöller 's avatar
Björn Peemöller committed
22 23

import CurryBuilder (buildCurry)
24
import CompilerOpts (Options (..), CymakeMode (..), getCompilerOpts, usage)
Bjoern Peemoeller's avatar
Bjoern Peemoeller committed
25 26 27

-- |The command line tool cymake
main :: IO ()
28
main = getCompilerOpts >>= cymake
Bjoern Peemoeller's avatar
Bjoern Peemoeller committed
29

Björn Peemöller 's avatar
Björn Peemöller committed
30
-- |Invoke the curry builder w.r.t the command line arguments
31 32
cymake :: (String, Options, [String], [String]) -> IO ()
cymake (prog, opts, files, errs)
Björn Peemöller 's avatar
Björn Peemöller committed
33 34 35 36
  | mode == ModeHelp           = printUsage prog
  | mode == ModeVersion        = printVersion
  | mode == ModeNumericVersion = printNumericVersion
  | not $ null errs            = badUsage prog errs
37
  | null files                 = badUsage prog ["no input files"]
38 39 40 41
  | mode == ModeHtml           =
    runCYIO (mapM_ (source2html opts) files) >>= okOrAbort
  | otherwise                  =
    runCYIO (mapM_ (buildCurry  opts) files) >>= okOrAbort
Björn Peemöller 's avatar
Björn Peemöller committed
42
  where mode = optMode opts
43
        okOrAbort = either abortWithMessages return
Bjoern Peemoeller's avatar
Bjoern Peemoeller committed
44

Björn Peemöller 's avatar
Björn Peemöller committed
45
-- |Print the usage information of the command line tool
46 47 48
printUsage :: String -> IO ()
printUsage prog = putStrLn $ usage prog

Björn Peemöller 's avatar
Björn Peemöller committed
49 50 51 52 53 54 55 56
-- |Print the program version
printVersion :: IO ()
printVersion = putStrLn cymakeGreeting

-- |Print the numeric program version
printNumericVersion :: IO ()
printNumericVersion = putStrLn cymakeVersion

Björn Peemöller 's avatar
Björn Peemöller committed
57
-- |Print errors and abort execution on bad parameters
Bjoern Peemoeller's avatar
Bjoern Peemoeller committed
58 59 60 61
badUsage :: String -> [String] -> IO ()
badUsage prog errs = do
  putErrsLn $ map (\ err -> prog ++ ": " ++ err) errs
  abortWith ["Try '" ++ prog ++ " --help' for more information"]