git.iko.soy/typeable/pandoc
on 1.15.0.6 help sign in
typeable/pandoc/Setup.hs
85 lines 3.6 kB history raw
1{-
2Copyright (C) 2006-2015 John MacFarlane <jgm@berkeley.edu>
4This program is free software; you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation; either version 2 of the License, or
7(at your option) any later version.
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
14You should have received a copy of the GNU General Public License
15along with this program; if not, write to the Free Software
16Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17-}
19import Distribution.Simple
20import Distribution.Simple.PreProcess
21import Distribution.Simple.Setup (ConfigFlags(..))
22import Distribution.PackageDescription (PackageDescription(..), FlagName(..))
23import System.Process ( rawSystem )
24import System.FilePath ( (</>) )
25import System.Directory ( findExecutable )
26import Distribution.Verbosity ( Verbosity )
27import Distribution.Simple.Utils (info, notice, installOrdinaryFiles)
28import Distribution.Simple.Setup
29import Distribution.Simple.Program (simpleProgram, Program(..))
30import Distribution.Simple.LocalBuildInfo
31import Data.Version
32import System.Process (readProcess)
33import Text.ParserCombinators.ReadP (readP_to_S, skipSpaces, eof)
34import Control.Monad (when)
35import qualified Control.Exception as E
37main :: IO ()
38main = defaultMainWithHooks $ simpleUserHooks {
39 -- enable hsb2hs preprocessor for .hsb files
40 hookedPreProcessors = [ppBlobSuffixHandler]
41 , hookedPrograms = [(simpleProgram "hsb2hs"){
42 programFindVersion = findHsb2hsVersion }]
43 , postCopy = installManPage
44 }
46findHsb2hsVersion :: Verbosity -> FilePath -> IO (Maybe Version)
47findHsb2hsVersion verb fp = do
48 let handleExitFailure :: IOError -> IO (Maybe Version)
49 handleExitFailure _ = return Nothing
50 E.handle handleExitFailure $ do
51 outp <- readProcess fp ["--version"] ""
52 case readP_to_S (do v <- parseVersion
53 skipSpaces
54 eof
55 return v) outp of
56 ((v,""):_) -> return (Just v)
57 _ -> return Nothing
59ppBlobSuffixHandler :: PPSuffixHandler
60ppBlobSuffixHandler = ("hsb", \_ lbi ->
61 PreProcessor {
62 platformIndependent = True,
63 runPreProcessor = mkSimplePreProcessor $ \infile outfile verbosity ->
64 do let embedData = case lookup (FlagName "embed_data_files")
65 (configConfigurationsFlags (configFlags lbi)) of
66 Just True -> True
67 _ -> False
68 when embedData $
69 do info verbosity $ "Preprocessing " ++ infile ++ " to " ++ outfile
70 hsb2hsPath <- findExecutable "hsb2hs"
71 case hsb2hsPath of
72 Just p -> rawSystem p [infile, infile, outfile]
73 Nothing -> error "hsb2hs is needed to build this program: cabal install hsb2hs"
74 return ()
75 })
77installManPage :: Args -> CopyFlags
78 -> PackageDescription -> LocalBuildInfo -> IO ()
79installManPage _ flags pkg lbi = do
80 let verbosity = fromFlag (copyVerbosity flags)
81 let copydest = fromFlag (copyDest flags)
82 let mandest = mandir (absoluteInstallDirs pkg lbi copydest)
83 </> "man1"
84 notice verbosity $ "Copying man page to " ++ mandest
85 installOrdinaryFiles verbosity mandest [("man", "pandoc.1")]