|
| 1 | +{-# LANGUAGE OverloadedStrings #-} |
| 2 | + |
| 3 | +import qualified Control.Monad |
| 4 | +import Data.Makefile (Makefile) |
| 5 | +import Data.Makefile.Parse (parseAsMakefile) |
| 6 | +import Options.Applicative |
| 7 | +import PyEncode ( |
| 8 | + asString, |
| 9 | + toPyEntries, |
| 10 | + ) |
| 11 | +import System.Directory (doesFileExist) |
| 12 | +import System.Exit (die) |
| 13 | +import Text.Pretty.Simple |
| 14 | + |
| 15 | +data Args = Args |
| 16 | + { file :: String |
| 17 | + , debug :: Bool |
| 18 | + , parseOnly :: Bool |
| 19 | + , force :: Bool |
| 20 | + , output :: FilePath |
| 21 | + } |
| 22 | + |
| 23 | +input :: Parser Args |
| 24 | +input = |
| 25 | + Args |
| 26 | + <$> strArgument |
| 27 | + ( showDefault |
| 28 | + <> value "Makefile" |
| 29 | + <> metavar "MAKEFILE" |
| 30 | + <> showDefault |
| 31 | + <> help "Makefile to read" |
| 32 | + ) |
| 33 | + <*> switch (long "debug" <> short 'd' <> help "Produce debug output") |
| 34 | + <*> switch |
| 35 | + ( long "parse-only" |
| 36 | + <> short 'p' |
| 37 | + <> help |
| 38 | + "Implies --debug, produces only parsed structure" |
| 39 | + ) |
| 40 | + <*> switch |
| 41 | + (long "force" <> short 'f' <> help "Overwrite tasks.py if it exists") |
| 42 | + <*> strOption |
| 43 | + ( long "output" |
| 44 | + <> short 'o' |
| 45 | + <> showDefault |
| 46 | + <> value "tasks.py" |
| 47 | + <> help |
| 48 | + "tasks file to write" |
| 49 | + ) |
| 50 | + |
| 51 | +parseFile :: FilePath -> IO Makefile |
| 52 | +parseFile fpath = do |
| 53 | + parsed <- parseAsMakefile fpath |
| 54 | + case parsed of |
| 55 | + Left _ -> error $ "unable to parse " ++ fpath ++ ", aborting" |
| 56 | + Right mf -> return mf |
| 57 | + |
| 58 | +run :: Args -> IO () |
| 59 | +run Args{file = f, debug = False, output = optOutfile, force = optForce, parseOnly = False} = |
| 60 | + abortIfExists optOutfile optForce |
| 61 | + >> parseFile f |
| 62 | + >>= writer optOutfile |
| 63 | + . asString |
| 64 | + . toPyEntries |
| 65 | +run Args{file = f, debug = True, output = optOutfile, force = optForce, parseOnly = False} = |
| 66 | + do |
| 67 | + abortIfExists optOutfile optForce |
| 68 | + mkFl <- parseFile f |
| 69 | + debugParsed mkFl |
| 70 | + (writer optOutfile . asString . toPyEntries) mkFl |
| 71 | +run Args{file = f, parseOnly = True} = parseFile f >>= debugParsed |
| 72 | + |
| 73 | +debugParsed :: Makefile -> IO () |
| 74 | +debugParsed mkFl = do |
| 75 | + pPrint mkFl |
| 76 | + putStrLn "=>" |
| 77 | + pPrint $ toPyEntries mkFl |
| 78 | + |
| 79 | +abortIfExists :: FilePath -> Bool -> IO () |
| 80 | +abortIfExists optOutfile optForce = do |
| 81 | + v <- doesFileExist optOutfile |
| 82 | + Control.Monad.when (v && not optForce) $ |
| 83 | + die "tasks.py already exist, aborting (use --force to override)" |
| 84 | + |
| 85 | +-- | "-" causes strings to be printed to stdout |
| 86 | +writer :: FilePath -> String -> IO () |
| 87 | +-- TODO: use Data.Text.IO instead of String functions |
| 88 | +writer outFile = case outFile of |
| 89 | + "-" -> putStrLn |
| 90 | + _ -> writeFile outFile |
| 91 | + |
| 92 | +main :: IO () |
| 93 | +main = run =<< execParser opts |
| 94 | + where |
| 95 | + opts = |
| 96 | + info (helper <*> input) $ |
| 97 | + fullDesc |
| 98 | + <> header "demake -- Makefile to invoke converter" |
| 99 | + <> progDesc "Generate PyInvoke stub from given Makefile" |
0 commit comments