Skip to content

Commit

Permalink
Add autogenerated build config module with an easily usable format. S…
Browse files Browse the repository at this point in the history
…tay DRY!
  • Loading branch information
Nate-Bragg-Bose committed Aug 22, 2014
1 parent 968f087 commit 2e9b2cc
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 7 deletions.
12 changes: 9 additions & 3 deletions Main.hs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
module Main where

import Paths_smudge
import PackageInfo (packageInfo, author, synopsis)
import Backends.Backend
import Backends.GraphViz
import Grammar

import Control.Applicative
import Distribution.Package (packageVersion, packageName, PackageName(..))
import Text.ParserCombinators.Parsec (parse, ParseError)
import System.Console.GetOpt
import System.Environment
Expand Down Expand Up @@ -40,8 +41,13 @@ subcommand name f os = map makeSub os
data SystemOption = Version | Help
deriving (Show, Eq)

app_name :: String
app_name = ((\ (PackageName s) -> s) $ packageName packageInfo)

header :: String
header = "Usage: [OPTIONS] file"
header = "Usage: " ++ app_name ++ " [OPTIONS] file\n" ++
synopsis ++ "\n" ++
"Written by " ++ author ++ "\n"

sysopts :: [OptDescr SystemOption]
sysopts = [Option ['v'] ["version"] (NoArg Version) "Version information.",
Expand All @@ -58,7 +64,7 @@ printUsage :: IO ()
printUsage = putStr $ usageInfo header all_opts

printVersion :: IO ()
printVersion = putStrLn ("Version: " ++ showVersion version)
printVersion = putStrLn (app_name ++ " version: " ++ (showVersion $ packageVersion packageInfo))

main = do
args <- getArgs
Expand Down
40 changes: 38 additions & 2 deletions Setup.hs
Original file line number Diff line number Diff line change
@@ -1,2 +1,38 @@
import Distribution.Simple
main = defaultMain
import Distribution.PackageDescription (PackageDescription(..))
import Distribution.Simple (defaultMainWithHooks, simpleUserHooks, UserHooks(..), Args)
import Distribution.Simple.BuildPaths (autogenModulesDir)
import Distribution.Simple.LocalBuildInfo (LocalBuildInfo)
import Distribution.Simple.Setup (BuildFlags(..), fromFlag)
import Distribution.Simple.Utils (createDirectoryIfMissingVerbose, rewriteFile)
import System.FilePath ((</>), (<.>))

main = defaultMainWithHooks packageInfoUserHooks

packageInfoUserHooks :: UserHooks
packageInfoUserHooks =
simpleUserHooks {
buildHook = genPackageInfoHook
}

genPackageInfoHook :: PackageDescription -> LocalBuildInfo -> UserHooks -> BuildFlags -> IO ()
genPackageInfoHook pkg lbi uhs bfs= do
createDirectoryIfMissingVerbose (fromFlag $ buildVerbosity bfs) True (autogenModulesDir lbi)
let packageInfoModulePath = autogenModulesDir lbi </> cfg_name <.> "hs"
rewriteFile packageInfoModulePath (generate pkg)
buildHook simpleUserHooks pkg lbi uhs bfs
where cfg_name = "PackageInfo"
generate pkg = "module " ++ cfg_name ++ " where\n" ++
"\n" ++
"import Distribution.Package (PackageIdentifier(..), PackageName(..))\n" ++
"import Distribution.Version (Version(..))\n" ++
"\n" ++
"packageInfo = " ++ (show $ package pkg) ++ "\n" ++
"copyright = " ++ (show $ copyright pkg) ++ "\n" ++
"maintainer = " ++ (show $ maintainer pkg) ++ "\n" ++
"author = " ++ (show $ author pkg) ++ "\n" ++
"stability = " ++ (show $ stability pkg) ++ "\n" ++
"homepage = " ++ (show $ homepage pkg) ++ "\n" ++
"pkgUrl = " ++ (show $ pkgUrl pkg) ++ "\n" ++
"bugReports = " ++ (show $ bugReports pkg) ++ "\n" ++
"synopsis = " ++ (show $ synopsis pkg) ++ "\n" ++
"description = " ++ (show $ description pkg) ++ "\n"
4 changes: 2 additions & 2 deletions smudge.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Maintainer: [email protected]

Category: Language

Build-type: Simple
Build-type: Custom

-- Extra files to be distributed with the package, such as examples or
-- a README.
Expand All @@ -48,7 +48,7 @@ Executable smudge
Main-is: Main.hs

-- Packages needed in order to build this package.
Build-depends: base, containers, filepath, fgl, parsec, text, graphviz <= 2999.15.0.1
Build-depends: base, Cabal, containers, filepath, fgl, parsec, text, graphviz <= 2999.15.0.1

-- Modules not exported by this package.
Other-modules: Grammar Backends.Backend Backends.GraphViz
Expand Down

0 comments on commit 2e9b2cc

Please sign in to comment.