Haskell.hs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. module Gidl.Backend.Haskell where
  2. import Gidl.Types
  3. import Gidl.Interface
  4. import Gidl.Backend.Cabal
  5. import Gidl.Backend.Haskell.Types
  6. import Gidl.Backend.Haskell.Test
  7. import Gidl.Backend.Haskell.Interface
  8. import Ivory.Artifact
  9. import Data.Char (isSpace)
  10. import Text.PrettyPrint.Mainland
  11. haskellBackend :: TypeEnv -> InterfaceEnv -> String -> String -> [Artifact]
  12. haskellBackend (TypeEnv te) (InterfaceEnv ie) pkgname namespace_raw =
  13. [ cabalFileArtifact cf
  14. , makefile
  15. , artifactPath "tests" serializeTestMod
  16. ] ++
  17. [ artifactPath "src" m | m <- sourceMods
  18. ]
  19. where
  20. tmods = [ typeModule (namespace ++ ["Types"]) t
  21. | (_tn, t) <- te
  22. , isUserDefined t
  23. ]
  24. imods = [ interfaceModule (namespace ++ ["Interface"]) i
  25. | (_iname, i) <- ie
  26. ]
  27. sourceMods = tmods ++ imods
  28. cf = (defaultCabalFile pkgname cabalmods deps) { tests = [ serializeTest ] }
  29. cabalmods = [ filePathToPackage (artifactFileName m) | m <- sourceMods ]
  30. deps = [ "cereal", "QuickCheck" ]
  31. serializeTest = defaultCabalTest "serialize-test" "SerializeTest.hs"
  32. (pkgname:deps)
  33. serializeTestMod = serializeTestModule namespace (map snd ie)
  34. namespace = dotwords namespace_raw
  35. dotwords :: String -> [String]
  36. dotwords s = case dropWhile isDot s of
  37. "" -> []
  38. s' -> let (w, s'') = break isDot s' in w : dotwords s''
  39. isDot c = (c == '.') || isSpace c
  40. makefile :: Artifact
  41. makefile = artifactText "Makefile" $
  42. prettyLazyText 80 $ stack
  43. [ text "default:"
  44. , text "\tcabal build"
  45. , empty
  46. , text "create-sandbox:"
  47. , text "\tcabal sandbox init"
  48. , text "\tcabal install --enable-tests --dependencies-only"
  49. , empty
  50. , text "test:"
  51. , text "\tcabal test"
  52. , empty
  53. ]