use options library for command line options

This commit is contained in:
Luite Stegeman 2012-04-05 20:59:59 +02:00
parent 0ee840da44
commit 0fcb55960c
4 changed files with 82 additions and 42 deletions

View File

@ -145,7 +145,7 @@ rebuildGhc bf ld ar = do
rebuildCabal :: String -> IO Bool
rebuildCabal cmd = do
putStrLn "Rebuilding application... (cabal)"
putStrLn $ "Rebuilding application... (" ++ cmd ++ ")"
exit <- rawSystemFilter cmd ["build"]
return $ case exit of
ExitSuccess -> True

24
yesod/Types.hs Normal file
View File

@ -0,0 +1,24 @@
{-# LANGUAGE TemplateHaskell #-}
module Types where
import Options
mkOptCabalDev name = option name (\o -> o
{ optionLongFlags = ["dev", "use-cabal-dev"]
, optionShortFlags = ['d']
, optionType = optionTypeBool
, optionDefault = "false"
, optionDescription = "use cabal-dev to build the package"
})
mkOptNoApi name = option name (\o -> o
{ optionLongFlags = ["no-ghc-api"]
, optionShortFlags = ['n']
, optionType = optionTypeBool
, optionDefault = "false"
, optionDescription = "do not use the GHC API to build, use `cabal build' instead"
})

View File

@ -1,4 +1,4 @@
{-# LANGUAGE CPP #-}
{-# LANGUAGE CPP, TemplateHaskell #-}
import Scaffolding.Scaffolder
import System.Environment (getArgs)
@ -6,48 +6,63 @@ import System.Exit (exitWith)
import System.Process (rawSystem)
import Yesod.Core(yesodVersion)
#ifndef WINDOWS
import Options
import Types
import Build (touch)
#endif
import Devel (devel)
windowsWarning :: String
#ifdef WINDOWS
windowsWarning = "\n (does not work on Windows)"
#else
windowsWarning = ""
#endif
defineOptions "NoOptions" (return ())
defineOptions "DevelOptions" $ do
mkOptNoApi "develOptNoApi"
defineOptions "MainOptions" $ do
mkOptCabalDev "optCabalDev"
type InitOptions = NoOptions
type ConfigureOptions = NoOptions
type BuildOptions = NoOptions
type TouchOptions = NoOptions
type VersionOptions = NoOptions
cabalCommand :: MainOptions -> String
cabalCommand mopt
| optCabalDev mopt = "cabal-dev"
| otherwise = "cabal"
main = runSubcommand
[ subcommand "init" cmdInit
, subcommand "configure" cmdConfigure
#ifndef WINDOWS
, subcommand "build" cmdBuild
, subcommand "touch" cmdTouch
#endif
, subcommand "devel" cmdDevel
, subcommand "version" cmdVersion
]
cmdInit :: MainOptions -> InitOptions -> [String] -> IO ()
cmdInit _ _ _ = scaffold
cmdConfigure :: MainOptions -> ConfigureOptions -> [String] -> IO ()
cmdConfigure mopt opts args = exitWith =<< rawSystem (cabalCommand mopt) ("configure":args)
cmdBuild :: MainOptions -> BuildOptions -> [String] -> IO ()
cmdBuild mopt opts args = do
touch
exitWith =<< rawSystem (cabalCommand mopt) ("build":args)
cmdTouch :: MainOptions -> TouchOptions -> [String] -> IO ()
cmdTouch _ _ _ = touch
cmdDevel :: MainOptions -> DevelOptions -> [String] -> IO ()
cmdDevel mopt opts args = devel (optCabalDev mopt) args
where
forceCabal = develOptNoApi opts
cmdVersion :: MainOptions -> VersionOptions -> [String] -> IO ()
cmdVersion _ _ _ = putStrLn $ "yesod-core version: " ++ yesodVersion
main :: IO ()
main = do
args' <- getArgs
let (isDev, args) =
case args' of
"--dev":rest -> (True, rest)
_ -> (False, args')
let cmd = if isDev then "cabal-dev" else "cabal"
#ifndef WINDOWS
let build rest = rawSystem cmd $ "build":rest
#endif
case args of
["init"] -> scaffold
#ifndef WINDOWS
"build":rest -> touch >> build rest >>= exitWith
["touch"] -> touch
#endif
"devel":rest -> devel isDev rest
["version"] -> putStrLn $ "yesod-core version:" ++ yesodVersion
"configure":rest -> rawSystem cmd ("configure":rest) >>= exitWith
_ -> do
putStrLn "Usage: yesod <command>"
putStrLn "Available commands:"
putStrLn " init Scaffold a new site"
putStrLn " configure Configure a project for building"
putStrLn $ " build Build project (performs TH dependency analysis)"
++ windowsWarning
putStrLn $ " touch Touch any files with altered TH dependencies but do not build"
++ windowsWarning
putStrLn " devel Run project with the devel server"
putStrLn " use --dev devel to build with cabal-dev"
putStrLn " version Print the version of Yesod"

View File

@ -88,6 +88,7 @@ library
, shakespeare-css >= 1.0 && < 1.1
, warp >= 1.2 && < 1.3
, blaze-html >= 0.4.1.3 && < 0.5
, options >= 0.1 && < 0.2
exposed-modules: Yesod
ghc-options: -Wall