Cabal.hs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. , executables :: [CabalExe]
  16. , tests :: [CabalTest]
  17. } deriving (Eq, Show)
  18. data CabalTest =
  19. CabalTest
  20. { test_name :: String
  21. , test_type :: String
  22. , test_hs_source_dirs :: [String]
  23. , test_main_is :: String
  24. , test_build_depends :: [String]
  25. } deriving (Eq, Show)
  26. data CabalExe =
  27. CabalExe
  28. { exe_name :: String
  29. , exe_hs_source_dirs :: [String]
  30. , exe_main_is :: String
  31. , exe_build_depends :: [String]
  32. } deriving (Eq, Show)
  33. defaultCabalFile :: String -> [String] -> [String] -> CabalFile
  34. defaultCabalFile name_ exposed_modules_ build_depends_ = CabalFile
  35. { name = name_
  36. , version = "0.1.0.0"
  37. , author = "Generated by Gidl"
  38. , exposed_modules = exposed_modules_
  39. , build_depends = "base >= 4.7" : build_depends_
  40. , hs_source_dirs = ["src"]
  41. , default_language = "Haskell2010"
  42. , ghc_options = "-Wall"
  43. , executables = []
  44. , tests = []
  45. }
  46. defaultCabalExe :: String -> String -> [String] -> CabalExe
  47. defaultCabalExe name_ main_ build_depends_ = CabalExe
  48. { exe_name = name_
  49. , exe_hs_source_dirs = ["tests"]
  50. , exe_main_is = main_
  51. , exe_build_depends = "base >= 4.7" : build_depends_
  52. }
  53. defaultCabalTest :: String -> String -> [String] -> CabalTest
  54. defaultCabalTest name_ main_ build_depends_ = CabalTest
  55. { test_name = name_
  56. , test_type = "exitcode-stdio-1.0"
  57. , test_hs_source_dirs = ["tests"]
  58. , test_main_is = main_
  59. , test_build_depends = "base >= 4.7" : build_depends_
  60. }
  61. cabalFileArtifact :: CabalFile -> Artifact
  62. cabalFileArtifact CabalFile{..} = artifactText (name ++ ".cabal") $
  63. prettyLazyText 1000 $ stack
  64. [ text "name:" <+> text name
  65. , text "version:" <+> text version
  66. , text "author:" <+> text author
  67. , text "build-type: Simple"
  68. , text "cabal-version: >=1.10"
  69. , empty
  70. , text "library"
  71. , indent 2 $ stack
  72. [ text "exposed-modules:" <+>
  73. align (stack (punctuate comma (map text exposed_modules)))
  74. , text "build-depends:" <+>
  75. align (stack (punctuate comma (map text build_depends)))
  76. , text "hs-source-dirs:" <+> sep (punctuate comma (map text hs_source_dirs))
  77. , text "default-language:" <+> text default_language
  78. , text "ghc-options:" <+> text ghc_options
  79. ]
  80. , stack [ empty </> cabalExeDoc e | e <- executables ]
  81. , stack [ empty </> cabalTestDoc t | t <- tests ]
  82. ]
  83. cabalExeDoc :: CabalExe -> Doc
  84. cabalExeDoc CabalExe{..} =
  85. text "executable" <+> text exe_name </> indent 2 (stack
  86. [ text "main-is:" <+> text exe_main_is
  87. , text "hs-source-dirs:" <+> sep (punctuate comma (map text exe_hs_source_dirs))
  88. , text "build-depends:" <+> align (stack (punctuate comma (map text exe_build_depends)))
  89. , text "default-language: Haskell2010"
  90. , text "ghc-options: -Wall"
  91. ])
  92. cabalTestDoc :: CabalTest -> Doc
  93. cabalTestDoc CabalTest{..} =
  94. text "Test-Suite" <+> text test_name </> indent 2 (stack
  95. [ text "type:" <+> text test_type
  96. , text "hs-source-dirs:" <+> sep (punctuate comma (map text test_hs_source_dirs))
  97. , text "main-is:" <+> text test_main_is
  98. , text "build-depends:" <+> align (stack (punctuate comma (map text test_build_depends)))
  99. ])
  100. filePathToPackage :: String -> String
  101. filePathToPackage ('.':'h':'s':[]) = []
  102. filePathToPackage ('/':as) = '.' : filePathToPackage as
  103. filePathToPackage (a:as) = a : filePathToPackage as
  104. filePathToPackage [] = []