Cabal.hs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. {-# LANGUAGE RecordWildCards #-}
  2. module Gidl.Backend.Cabal where
  3. import Ivory.Artifact
  4. import Text.PrettyPrint.Mainland
  5. data CabalFile =
  6. CabalFile
  7. { name :: String
  8. , version :: String
  9. , author :: String
  10. , exposed_modules :: [String]
  11. , build_depends :: [String]
  12. , hs_source_dirs :: [String]
  13. , default_language :: String
  14. , ghc_options :: String
  15. , tests :: [CabalTest]
  16. } deriving (Eq, Show)
  17. data CabalTest =
  18. CabalTest
  19. { test_name :: String
  20. , test_type :: String
  21. , test_hs_source_dirs :: [String]
  22. , test_main_is :: String
  23. , test_build_depends :: [String]
  24. } deriving (Eq, Show)
  25. defaultCabalFile :: String -> [String] -> [String] -> CabalFile
  26. defaultCabalFile name_ exposed_modules_ build_depends_ = CabalFile
  27. { name = name_
  28. , version = "0.1.0.0"
  29. , author = "Generated by Gidl"
  30. , exposed_modules = exposed_modules_
  31. , build_depends = "base >= 4.7" : build_depends_
  32. , hs_source_dirs = ["src"]
  33. , default_language = "Haskell2010"
  34. , ghc_options = "-Wall"
  35. , tests = []
  36. }
  37. defaultCabalTest :: String -> String -> [String] -> CabalTest
  38. defaultCabalTest name_ main_ build_depends_ = CabalTest
  39. { test_name = name_
  40. , test_type = "exitcode-stdio-1.0"
  41. , test_hs_source_dirs = ["tests"]
  42. , test_main_is = main_
  43. , test_build_depends = "base >= 4.7" : build_depends_
  44. }
  45. cabalFileArtifact :: CabalFile -> Artifact
  46. cabalFileArtifact CabalFile{..} = artifactText (name ++ ".cabal") $
  47. prettyLazyText 80 $ stack
  48. [ text "name:" <+> text name
  49. , text "version:" <+> text version
  50. , text "author:" <+> text author
  51. , text "build-type: Simple"
  52. , text "cabal-version: >=1.10"
  53. , empty
  54. , text "library"
  55. , indent 2 $ stack
  56. [ text "exposed-modules:" <+>
  57. align (stack (punctuate comma (map text exposed_modules)))
  58. , text "build-depends:" <+>
  59. align (stack (punctuate comma (map text build_depends)))
  60. , text "hs-source-dirs:" <+> sep (punctuate comma (map text hs_source_dirs))
  61. , text "default-language:" <+> text default_language
  62. , text "ghc-options:" <+> text ghc_options
  63. ]
  64. , stack [ cabalTestDoc t | t <- tests ]
  65. ]
  66. cabalTestDoc :: CabalTest -> Doc
  67. cabalTestDoc CabalTest{..} =
  68. text "Test-Suite" <+> text test_name </> indent 2 (stack
  69. [ text "type:" <+> text test_type
  70. , text "hs-source-dirs:" <+> sep (punctuate comma (map text test_hs_source_dirs))
  71. , text "main-is:" <+> text test_main_is
  72. , text "build-depends:" <+> align (stack (punctuate comma (map text test_build_depends)))
  73. ])
  74. filePathToPackage :: String -> String
  75. filePathToPackage ('.':'h':'s':[]) = []
  76. filePathToPackage ('/':as) = '.' : filePathToPackage as
  77. filePathToPackage (a:as) = a : filePathToPackage as
  78. filePathToPackage [] = []
  79. makefile :: Artifact
  80. makefile = artifactText "Makefile" $
  81. prettyLazyText 80 $ stack
  82. [ text "default:"
  83. , text "\tcabal build"
  84. , empty
  85. , text "create-sandbox:"
  86. , text "\tcabal sandbox init"
  87. , text "\tcabal install --enable-tests --dependencies-only"
  88. , empty
  89. , text "test:"
  90. , text "\tcabal test"
  91. , empty
  92. ]