Repository navigation
Expand file tree
/
Copy pathMain.hs
More file actions
80 lines (65 loc) · 2.3 KB
/
Copy pathMain.hs
File metadata and controls
80 lines (65 loc) · 2.3 KB
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
module Main(main) where
import Control.Exception
import Control.Monad
import Data.Char
import System.Cmd
import System.Directory
import System.Exit
import System.FilePath
main = do
let obj = "obj"
eps <- files "graphics" "eps"
tex <- files "." "tex"
bib <- files "." "bib"
for eps $
\e -> replaceDirectory e obj <== [e] $ copyFile
for bib $
\b -> replaceDirectory b obj <== [b] $ \from to -> do
copyFile from to
system_ "obj" $ "bibtex -quiet " ++ takeBaseName (takeFileName b)
for tex $
\t -> replaceDirectory t obj <== [t,"thesis.fmt"] $ \from to -> do
system_ "." $ "lhs2tex " ++ from ++ " -o " ++ to
system_ "obj" "texify --quiet thesis.tex"
copyFile "obj/thesis.dvi" "thesis.dvi"
for = flip mapM
files dir ext = do
s <- getDirectoryContents dir
s <- return $ filter ((==) ('.':ext) . takeExtension) s
return $ map (dir </>) s
system_ dir cmd = do
orig <- getCurrentDirectory
bracket_ (setCurrentDirectory dir) (setCurrentDirectory orig) $ do
putStrLn cmd
res <- system $ cmd ++ " > stdout.txt 2> stderr.txt"
out <- readFile "stdout.txt"
err <- readFile "stderr.txt"
putStr $ out ++ err
when (res /= ExitSuccess) $ do
reportError out
putStrLn "System command failed! Press enter to continue"
getChar
exitWith (ExitFailure 1)
reportError :: String -> IO ()
reportError s2 = do
b <- doesFileExist file
when (b && not (null pos) && all isDigit pos) $ do
src <- readFile file
putStr $ unlines $ map f $ take 7 $ drop (read pos - 3) $ zip [1..] $ lines src
where
s = last $ "" : lines s2
(pre,post) = splitAt 3 s
(front,rest) = break (== ':') post
file = pre ++ front
pos = takeWhile (/= ':') $ drop 1 rest
f (p,s) = show p ++ " : " ++ s
(<==) :: FilePath -> [FilePath] -> (FilePath -> FilePath -> IO ()) -> IO ()
(<==) to froms@(from:_) action = do
b <- doesFileExist to
rebuild <- if not b then return True else do
from2 <- liftM maximum $ mapM getModificationTime froms
to2 <- getModificationTime to
return $ to2 < from2
when rebuild $ do
putStrLn $ "Building: " ++ to
action from to