summaryrefslogtreecommitdiff
path: root/build.hs
blob: a27535bf1c4eae26a60b537253181a34ad2baad8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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
#!/usr/bin/env cabal
{- cabal:
build-depends: base
             , directory
             , filepath
             , shake
             , unix
-}

import Control.Exception (throw)
import Control.Monad (forM_, unless)
import Data.Functor
import Data.Maybe
import Development.Shake
import System.Directory (getCurrentDirectory, setCurrentDirectory)
import System.Environment (getArgs, lookupEnv, withArgs)
import System.FilePath
import System.Posix.User (getRealUserID)

-- | Id of current user
type UID = String

options =
    shakeOptions
        { -- Generate a report that can be opened in a browser to analyze build
          shakeReport = ["report.html"]
        , -- Use all CPUs provided by the platform
          shakeThreads = 0
        , -- Put Shake's database in directory '_build'
          shakeFiles = "_build"
        , shakeVerbosity = Diagnostic
        }

install :: IO ()
install = do
    setCurrentDirectory "lambda-calcul/haskell"
    pwd <- getCurrentDirectory
    uid <- show . toInteger <$> getRealUserID
    putStrLn $ "Building mli in directory " <> pwd <> " as user " <> uid
    args <- getArgs
    withArgs args $ runShake pwd uid

runShake :: FilePath -> UID -> IO ()
runShake pwd uid = shakeArgs options $ do
    let needHaskellSources = do
            needDirectoryFiles
                "."
                [ "src//*.hs"
                , "test//*.hs"
                , "data//*.*"
                , "app//*.hs"
                ]
            need ["minilang.cabal"]

    want ["bin/mli"]

    "bin/mli" %> \bin -> do
        needHaskellSources
        cmd_ "cabal" ["build", "all"]
        cmd_ "cabal" ["test", "all"]
        Stdout exePath <- cmd "cabal" ["list-bin", "mli"]
        dirExists <- doesDirectoryExist "bin"
        unless dirExists $ cmd_ "mkdir" ["bin"]
        cmd_ "cp" [head (lines exePath), bin]

    -- "ui/dist/index.html" %> \_ -> do
    --     needDirectoryFiles
    --         "ui"
    --         [ "src//*.js"
    --         , "src//*.html"
    --         , "src//*.css"
    --         , "package.json"
    --         , "webpack.config.js"
    --         , ".babelrc"
    --         ]
    --     cmd_ (Cwd "ui") "npm" ["i"]
    --     cmd (Cwd "ui") "npm" ["run", "all"]

needDirectoryFiles dir patterns =
    need =<< getDirectoryFiles "" ((dir </>) <$> patterns)

main = install