Browse Source

Make module handling nicer/more general

Getty Ritter 6 years ago
parent
commit
4a49afff84
4 changed files with 19 additions and 14 deletions
  1. 6 0
      charter/Main.hs
  2. 10 11
      src/Charter.hs
  3. 1 1
      src/Templates.hs
  4. 2 2
      src/Types.hs

+ 6 - 0
charter/Main.hs

@@ -19,6 +19,7 @@ data Option
   | SetRoot T.Text
   | AddDep T.Text
   | AddUsualDeps
+  | AddMod T.Text
     deriving (Eq, Show)
 
 options :: [Opt.OptDescr Option]
@@ -26,6 +27,9 @@ options =
   [ Opt.Option ['b'] ["bin"]
     (Opt.ReqArg (AddBinary . T.pack) "PROGRAM NAME")
     "Add another binary target to this Cabal file"
+  , Opt.Option ['m'] ["module"]
+    (Opt.ReqArg (AddMod . T.pack) "MODULE NAME")
+    "Add another library module to this Cabal file"
   , Opt.Option ['r'] ["root"]
     (Opt.ReqArg (SetRoot . T.pack) "DIRECTORY")
     "Set the root directory for this project"
@@ -62,6 +66,8 @@ process opts p = foldr ($) p (map go opts)
   where
     go (AddBinary n) proj =
       proj & C.binDetails %~ (C.mkBinary n :)
+    go (AddMod m) proj =
+      proj & C.libDetails %~ fmap (& C.libMods %~ (m :))
     go (SetCategory s) proj =
       proj & C.projectDetails . C.projectCategory .~ Just s
     go (SetSynopsis s) proj =

+ 10 - 11
src/Charter.hs

@@ -35,9 +35,9 @@ mkBinary n = ExecutableDetails
   , _execDeps = []
   }
 
-mkLibrary :: T.Text -> LibraryDetails
-mkLibrary n = LibraryDetails
-  { _libExposedModules = [capitalize n]
+mkLibrary :: LibraryDetails
+mkLibrary = LibraryDetails
+  { _libMods = []
   , _libDeps = []
   }
 
@@ -59,8 +59,8 @@ projectBin :: ProjectDetails -> Project
 projectBin deets =
   mkProject deets & binDetails .~ [ bin ]
                   & libDetails .~ Just lib
-  where bin = mkBinary name & execDeps .~ ["name"]
-        lib = mkLibrary name
+  where bin = mkBinary name & execDeps .~ [name]
+        lib = mkLibrary & libMods .~ [capitalize name]
         name = deets^.projectName
 
 usualDeps :: [T.Text]
@@ -73,9 +73,7 @@ usualDeps =
 
 library :: ProjectDetails -> Project
 library deets =
-  mkProject deets & libDetails .~ Just lib
-  where
-    lib = mkLibrary (deets^.projectName)
+  mkProject deets & libDetails .~ Just mkLibrary
 
 mkdirBase :: T.Text -> [T.Text] -> IO ()
 mkdirBase base fp = do
@@ -128,9 +126,10 @@ createProject pr = do
   case (pr^.libDetails) of
     Nothing -> return ()
     Just lib -> do
-      forM_ (lib^.libExposedModules) $ \ m -> do
-        let modPath = "src" : T.splitOn "." (m <> ".hs")
-        write modPath (defaultLib m)
+      forM_ (lib^.libMods) $ \ m -> do
+        let modPath = "src" : T.splitOn "." m
+            modPath' = init modPath ++ [last modPath <> ".hs"]
+        write modPath' (defaultLib m)
 
   forM_ (pr^.binDetails) $ \e -> do
     write [e^.execDir, "Main.hs"] defaultBin

+ 1 - 1
src/Templates.hs

@@ -42,7 +42,7 @@ cabalLibrary lib = T.unlines $
   , "  default-extensions: ScopedTypeVariables"
   ] <> mods
   where
-    mods = case lib^.libExposedModules of
+    mods = case lib^.libMods of
              []     -> []
              (x:xs) ->
                ("  exposed-modules: " <> x) :

+ 2 - 2
src/Types.hs

@@ -17,8 +17,8 @@ data ProjectDetails = ProjectDetails
   }
 
 data LibraryDetails = LibraryDetails
-  { _libExposedModules :: [T.Text]
-  , _libDeps           :: [T.Text]
+  { _libMods :: [T.Text]
+  , _libDeps :: [T.Text]
   }
 
 data ExecutableDetails = ExecutableDetails