{-# LANGUAGE RecordWildCards #-} module Gidl.Backend.Cabal where import Ivory.Artifact import Text.PrettyPrint.Mainland data CabalFile = CabalFile { name :: String , version :: String , author :: String , exposed_modules :: [String] , build_depends :: [String] , hs_source_dirs :: [String] , default_language :: String , ghc_options :: String , tests :: [CabalTest] } deriving (Eq, Show) data CabalTest = CabalTest { test_name :: String , test_type :: String , test_hs_source_dirs :: [String] , test_main_is :: String , test_build_depends :: [String] } deriving (Eq, Show) defaultCabalFile :: String -> [String] -> [String] -> CabalFile defaultCabalFile name_ exposed_modules_ build_depends_ = CabalFile { name = name_ , version = "0.1.0.0" , author = "Generated by Gidl" , exposed_modules = exposed_modules_ , build_depends = "base >= 4.7" : build_depends_ , hs_source_dirs = ["src"] , default_language = "Haskell2010" , ghc_options = "-Wall" , tests = [] } defaultCabalTest :: String -> String -> [String] -> CabalTest defaultCabalTest name_ main_ build_depends_ = CabalTest { test_name = name_ , test_type = "exitcode-stdio-1.0" , test_hs_source_dirs = ["tests"] , test_main_is = main_ , test_build_depends = "base >= 4.7" : build_depends_ } cabalFileArtifact :: CabalFile -> Artifact cabalFileArtifact CabalFile{..} = artifactText (name ++ ".cabal") $ prettyLazyText 80 $ stack [ text "name:" <+> text name , text "version:" <+> text version , text "author:" <+> text author , text "build-type: Simple" , text "cabal-version: >=1.10" , empty , text "library" , indent 2 $ stack [ text "exposed-modules:" <+> align (stack (punctuate comma (map text exposed_modules))) , text "build-depends:" <+> align (stack (punctuate comma (map text build_depends))) , text "hs-source-dirs:" <+> sep (punctuate comma (map text hs_source_dirs)) , text "default-language:" <+> text default_language , text "ghc-options:" <+> text ghc_options ] , stack [ cabalTestDoc t | t <- tests ] ] cabalTestDoc :: CabalTest -> Doc cabalTestDoc CabalTest{..} = text "Test-Suite" <+> text test_name indent 2 (stack [ text "type:" <+> text test_type , text "hs-source-dirs:" <+> sep (punctuate comma (map text test_hs_source_dirs)) , text "main-is:" <+> text test_main_is , text "build-depends:" <+> align (stack (punctuate comma (map text test_build_depends))) ]) filePathToPackage :: String -> String filePathToPackage ('.':'h':'s':[]) = [] filePathToPackage ('/':as) = '.' : filePathToPackage as filePathToPackage (a:as) = a : filePathToPackage as filePathToPackage [] = [] makefile :: Artifact makefile = artifactText "Makefile" $ prettyLazyText 80 $ stack [ text "default:" , text "\tcabal build" , empty , text "create-sandbox:" , text "\tcabal sandbox init" , text "\tcabal install --enable-tests --dependencies-only" , empty , text "test:" , text "\tcabal test" , empty ]