Base.hs 4.31 KB
Newer Older
1
module Base (
Andrey Mokhov's avatar
Andrey Mokhov committed
2 3 4 5 6 7 8
    module Control.Applicative,
    module Control.Monad.Extra,
    module Data.Char,
    module Data.Function,
    module Data.List,
    module Data.Maybe,
    module Data.Monoid,
9 10
    module Development.Shake,
    module Development.Shake.Classes,
Andrey Mokhov's avatar
Andrey Mokhov committed
11 12 13
    module Development.Shake.Config,
    module Development.Shake.FilePath,
    module Development.Shake.Util,
Andrey Mokhov's avatar
Andrey Mokhov committed
14 15 16 17 18 19 20
    module System.Console.ANSI,
    shakeFilesPath, configPath, bootPackageConstraints, packageDependencies,
    replaceEq, replaceSeparators, decodeModule,
    unifyPath, (-/-), chunksOfSize,
    putColoured, putOracle, putBuild, putSuccess, putError,
    bimap, minusOrd, intersectOrd,
    removeFileIfExists
21 22
    ) where

Andrey Mokhov's avatar
Andrey Mokhov committed
23 24 25 26 27 28 29 30
import Control.Applicative
import Control.Monad.Extra
import Data.Char
import Data.Function
import Data.List
import Data.Maybe
import Data.Monoid
import Development.Shake hiding (unit, (*>))
31
import Development.Shake.Classes
Andrey Mokhov's avatar
Andrey Mokhov committed
32
import Development.Shake.Config
33
import Development.Shake.FilePath
Andrey Mokhov's avatar
Andrey Mokhov committed
34
import Development.Shake.Util
Andrey Mokhov's avatar
Andrey Mokhov committed
35 36 37
import System.Console.ANSI
import qualified System.Directory as IO
import System.IO
38

Andrey Mokhov's avatar
Andrey Mokhov committed
39
-- Build system files and paths
40 41 42 43 44
shakeFilesPath :: FilePath
shakeFilesPath = "_build/"

configPath :: FilePath
configPath = "shake/cfg/"
45 46 47

bootPackageConstraints :: FilePath
bootPackageConstraints = shakeFilesPath ++ "boot-package-constraints"
48 49 50

packageDependencies :: FilePath
packageDependencies = shakeFilesPath ++ "package-dependencies"
Andrey Mokhov's avatar
Andrey Mokhov committed
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141

-- Utility functions
replaceIf :: (a -> Bool) -> a -> [a] -> [a]
replaceIf p to = map (\from -> if p from then to else from)

replaceEq :: Eq a => a -> a -> [a] -> [a]
replaceEq from = replaceIf (== from)

replaceSeparators :: Char -> String -> String
replaceSeparators = replaceIf isPathSeparator

-- Given a module name extract the directory and file names, e.g.:
-- decodeModule "Data.Functor.Identity" = ("Data/Functor/", "Identity")
decodeModule :: String -> (FilePath, String)
decodeModule = splitFileName . replaceEq '.' '/'

-- Normalise a path and convert all path separators to /, even on Windows.
unifyPath :: FilePath -> FilePath
unifyPath = toStandard . normaliseEx

-- Combine paths using </> and apply unifyPath to the result
(-/-) :: FilePath -> FilePath -> FilePath
a -/- b = unifyPath $ a </> b

infixr 6 -/-

-- (chunksOfSize size strings) splits a given list of strings into chunks not
-- exceeding the given 'size'.
chunksOfSize :: Int -> [String] -> [[String]]
chunksOfSize _    [] = []
chunksOfSize size strings = reverse chunk : chunksOfSize size rest
  where
    (chunk, rest) = go [] 0 strings
    go res _         []     = (res, [])
    go res chunkSize (s:ss) =
        if newSize > size then (res, s:ss) else go (s:res) newSize ss
      where
        newSize = chunkSize + length s

-- A more colourful version of Shake's putNormal
putColoured :: Color -> String -> Action ()
putColoured colour msg = do
    liftIO $ setSGR [SetColor Foreground Vivid colour]
    putNormal msg
    liftIO $ setSGR []
    liftIO $ hFlush stdout

-- Make oracle output more distinguishable
putOracle :: String -> Action ()
putOracle = putColoured Blue

-- Make build output more distinguishable
putBuild :: String -> Action ()
putBuild = putColoured White

-- A more colourful version of success message
putSuccess :: String -> Action ()
putSuccess = putColoured Green

-- A more colourful version of error message
putError :: String -> Action a
putError msg = do
    putColoured Red msg
    error $ "GHC build system error: " ++ msg

-- Depending on Data.Bifunctor only for this function seems an overkill
bimap :: (a -> b) -> (c -> d) -> (a, c) -> (b, d)
bimap f g (x, y) = (f x, g y)

-- Depending on Data.List.Ordered only for these two functions seems an overkill
minusOrd :: Ord a => [a] -> [a] -> [a]
minusOrd [] _  = []
minusOrd xs [] = xs
minusOrd (x:xs) (y:ys) = case compare x y of
    LT -> x : minusOrd xs (y:ys)
    EQ ->     minusOrd xs ys
    GT ->     minusOrd (x:xs) ys

intersectOrd :: (a -> b -> Ordering) -> [a] -> [b] -> [a]
intersectOrd cmp = loop
  where
    loop [] _ = []
    loop _ [] = []
    loop (x:xs) (y:ys) = case cmp x y of
         LT ->     loop xs (y:ys)
         EQ -> x : loop xs ys
         GT ->     loop (x:xs) ys

-- Convenient helper function for removing a file that doesn't necessarily exist
removeFileIfExists :: FilePath -> Action ()
removeFileIfExists f = liftIO . whenM (IO.doesFileExist f) $ IO.removeFile f