-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSetup.hs
93 lines (78 loc) · 3.18 KB
/
Setup.hs
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
83
84
85
86
87
88
89
90
91
92
93
{-# LANGUAGE CPP #-}
import Data.List ( intersperse, sort )
import Control.Monad (when)
import Distribution.Simple
import Distribution.Simple.Program.Types
import Distribution.Simple.Setup
import Distribution.Simple.UserHooks
import Distribution.Simple.Utils ( rawSystemExit, warn, debug
, findProgramVersion, notice )
import Distribution.Verbosity ( Verbosity )
import System.Directory ( getCurrentDirectory, setCurrentDirectory,
createDirectoryIfMissing, removeFile,
doesFileExist, getDirectoryContents,
getModificationTime )
import System.Cmd ( rawSystem )
main = do
defaultMainWithHooks simpleUserHooks
{ preBuild = \a b -> generatePGF a b >> preBuild simpleUserHooks a b
, preClean = \a b -> preClean simpleUserHooks a b
}
-- {-# OPTIONS -cpp #-}
#ifdef WIN32
pathSeparator = "\\"
#else
pathSeparator = "/"
#endif
-- We can't depend on FilePath because we're in a Setup.hs
(</>) :: FilePath -> FilePath -> FilePath
(</>) a b = a ++ pathSeparator ++ b
mkPath :: [FilePath] -> FilePath
mkPath (f:fs) = foldl (</>) f fs
gfExecutable :: IO FilePath
gfExecutable = return "gf"
generatePGF :: Args -> BuildFlags -> IO ()
generatePGF _ flags = do
orig <- getCurrentDirectory
let verbosity = (fromFlag $ buildVerbosity flags)
autogenDir = mkPath [orig, "dist", "build", "autogen"]
gfSrcDir = mkPath [orig, "src", "gf"]
profileName = "Foods"
profileFlavors = [ "Eng", "Ita" ]
pgfFile = autogenDir </> profileName++".pgf"
notice verbosity "checking for dirty GF files to build"
doBuild <- needsBuild gfSrcDir pgfFile
when doBuild $ do
createDirectoryIfMissing True autogenDir
notice verbosity "Running PGF build"
setCurrentDirectory gfSrcDir
gfExe <- gfExecutable
setCurrentDirectory autogenDir
-- create the PGF files:
rawSystemExit verbosity gfExe $
[ "--make"
-- , "--no-emit-gfo"
, "--gfo-dir="++autogenDir
] ++ [ gfSrcDir </> (profileName ++ f ++ ".gf") |
f <- profileFlavors
]
-- generate the haskell:
rawSystemExit verbosity gfExe [ "--batch"
-- , "--no-emit-gfo"
, "--output-format=haskell"
, "--output-dir=." -- ++autogenDir
, profileName++".pgf" -- hmm
]
setCurrentDirectory orig
needsBuild :: FilePath -> FilePath -> IO Bool
needsBuild dir f = do exists <- doesFileExist f
case exists of
False -> return True
True -> do
files <- getDirectoryContents dir
res <- mapM (\x->compareTimes (dir </> x) f) (filter (".."/=) files)
return $ any (GT==) res
compareTimes :: FilePath -> FilePath -> IO Ordering
compareTimes f1 f2 = do t1 <- getModificationTime f1
t2 <- getModificationTime f2
return $ compare t1 t2