From 0fcb55960c6d5b71ea1e26dbca4a8801bc2be481 Mon Sep 17 00:00:00 2001 From: Luite Stegeman Date: Thu, 5 Apr 2012 20:59:59 +0200 Subject: [PATCH] use options library for command line options --- yesod/Devel.hs | 2 +- yesod/Types.hs | 24 ++++++++++++ yesod/main.hs | 97 +++++++++++++++++++++++++++-------------------- yesod/yesod.cabal | 1 + 4 files changed, 82 insertions(+), 42 deletions(-) create mode 100644 yesod/Types.hs diff --git a/yesod/Devel.hs b/yesod/Devel.hs index c8afc71a..ca504b5f 100644 --- a/yesod/Devel.hs +++ b/yesod/Devel.hs @@ -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 diff --git a/yesod/Types.hs b/yesod/Types.hs new file mode 100644 index 00000000..5c23705c --- /dev/null +++ b/yesod/Types.hs @@ -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" + }) + + diff --git a/yesod/main.hs b/yesod/main.hs index 2c854df3..ad822bbb 100755 --- a/yesod/main.hs +++ b/yesod/main.hs @@ -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 " - 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" diff --git a/yesod/yesod.cabal b/yesod/yesod.cabal index ef48dff8..dfc12039 100644 --- a/yesod/yesod.cabal +++ b/yesod/yesod.cabal @@ -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