on master help sign in
Handle empty/missing local packages
commit7d2218c170b754af45ec616baa402093c18d943e authorLuigy Leon <luigy@outlook.com> dateThu 26 Apr 2018, 14:50 (8 years ago) parent588bae6
1 file changed+21−8
lib/command/src/Obelisk/Command.hs+21−8
lib/command/src/Obelisk/Command.hs+21−8
@@ -3,7 +3,10 @@
33module Obelisk.Command where
44
55import Control.Monad
6+import Data.Either
7+import Data.Foldable
68import Data.List
9+import qualified Data.List.NonEmpty as NE
710import Data.Maybe
811import Data.Monoid
912import Distribution.Utils.Generic (withUTF8FileContents)
@@ -142,15 +145,20 @@ ob = \case
142145 ObCommand_Init source -> initProject source
143146 ObCommand_Run -> do
144147 freePort <- getFreePort
145− let pkgs = ["backend", "common", "frontend"]
146− hsSrcDirs <- forM pkgs $ \pkg -> do
148+ pkgs <- getLocalPkgs
149+ (pkgDirErrs, hsSrcDirs) <- fmap partitionEithers $ forM pkgs $ \pkg -> do
147150 let cabalFp = pkg </> pkg <.> "cabal"
148− mHsSrcDirs <- parseHsSrcDir cabalFp
149− return $ flip fmap mHsSrcDirs $ \hsSrcDirs -> do
150− if null hsSrcDirs then pure pkg else map (pkg </>) hsSrcDirs
151+ xs <- parseHsSrcDir cabalFp
152+ return $ case xs of
153+ Nothing -> Left pkg
154+ Just hsSrcDirs -> Right $ toList $ fmap (pkg </>) hsSrcDirs
155+ when (null hsSrcDirs) $
156+ fail $ "No valid pkgs found in " <> intercalate ", " pkgs
157+ when (not (null pkgDirErrs)) $
158+ putStrLn $ "Failed to find pkgs in " <> intercalate ", " pkgDirErrs
151159 let dotGhci = unlines
152160 [ ":set args --port " <> show freePort
153− , ":set -i" <> intercalate ":" (mconcat $ catMaybes hsSrcDirs)
161+ , ":set -i" <> intercalate ":" (mconcat hsSrcDirs)
154162 , ":add Common.Api Backend.App Frontend.App"
155163 , ":module + Control.Concurrent Obelisk.Widget.Run Common.Api Frontend.App Backend.App"
156164 ]
@@ -171,8 +179,13 @@ ob = \case
171179 ObCommand_Repl component -> runRepl component
172180 ObCommand_Watch component -> watch component
173181
182+-- | Relative paths to local packages of an obelisk project
183+-- TODO a way to query this
184+getLocalPkgs :: IO [FilePath]
185+getLocalPkgs = return ["backend", "common", "frontend"]
186+
174187parseHsSrcDir :: FilePath -- ^ package cabal file path
175− -> IO (Maybe [FilePath]) -- ^ List of hs src dirs of the library component
188+ -> IO (Maybe (NE.NonEmpty FilePath)) -- ^ List of hs src dirs of the library component
176189parseHsSrcDir cabalFp = do
177190 exists <- doesFileExist cabalFp
178191 if exists
@@ -183,7 +196,7 @@ parseHsSrcDir cabalFp = do
183196 mapM_ print warnings
184197 return $ do
185198 (_, lib) <- simplifyCondTree (const $ pure True) <$> condLibrary gpkg
186− return $ hsSourceDirs $ libBuildInfo lib
199+ pure $ fromMaybe (pure ".") $ NE.nonEmpty $ hsSourceDirs $ libBuildInfo lib
187200 ParseFailed _ -> return Nothing
188201 else return Nothing
189202