on master help sign in
Merge pull request #79 from obsidiansystems/ss-run-static-io-logging

Allow run and run-static-io to run in ObeliskT

commit89669c73d90ddd7313b0f5e0a6e77297888c0bd2 authorLuigy Leon <luichi.19@gmail.com> dateTue 15 May 2018, 17:38 (8 years ago) parent14a9380791c3f6
3 files changed+33−17
lib/command/src/Obelisk/App.hs+1−1 lib/command/src/Obelisk/Command.hs+5−3 lib/command/src/Obelisk/Command/Run.hs+27−13
lib/command/src/Obelisk/App.hs+1−1
@@ -18,7 +18,7 @@ data Obelisk = Obelisk
1818newtype ObeliskT m a = ObeliskT
1919 { unObeliskT :: ReaderT Obelisk (LoggingT Output m) a
2020 }
21− deriving (Functor, Applicative, Monad, MonadIO, MonadThrow, MonadCatch, MonadMask)
21+ deriving (Functor, Applicative, Monad, MonadIO, MonadThrow, MonadCatch, MonadMask, MonadReader Obelisk, MonadLog Output)
2222
2323runObelisk :: MonadIO m => Obelisk -> ObeliskT m a -> m a
2424runObelisk c =
lib/command/src/Obelisk/Command.hs+5−3
@@ -91,7 +91,7 @@ data ObInternal
9191 | ObInternal_CLIDemo
9292 deriving Show
9393
94−inNixShell' :: MonadObelisk m => StaticPtr (IO ()) -> m ()
94+inNixShell' :: MonadObelisk m => StaticPtr (ObeliskT IO ()) -> m ()
9595inNixShell' p = withProjectRoot "." $ \root -> do
9696 cmd <- liftIO $ unwords <$> mkCmd -- TODO: shell escape instead of unwords
9797 projectShell root False "ghc" cmd
@@ -284,9 +284,11 @@ ob = \case
284284 ObCommand_Repl component -> runRepl component
285285 ObCommand_Watch component -> watch component
286286 ObCommand_Internal icmd -> case icmd of
287− ObInternal_RunStaticIO k -> liftIO (unsafeLookupStaticPtr @(IO ()) k) >>= \case
287+ ObInternal_RunStaticIO k -> liftIO (unsafeLookupStaticPtr @(ObeliskT IO ()) k) >>= \case
288288 Nothing -> failWith $ "ObInternal_RunStaticIO: no such StaticKey: " <> T.pack (show k)
289− Just p -> liftIO $ deRefStaticPtr p
289+ Just p -> do
290+ c <- ask
291+ liftIO $ runObelisk c $ deRefStaticPtr p
290292 ObInternal_CLIDemo -> cliDemo
291293
292294--TODO: Clean up all the magic strings throughout this codebase
lib/command/src/Obelisk/Command/Run.hs+27−13
@@ -1,12 +1,16 @@
11{-# LANGUAGE OverloadedStrings #-}
2+{-# LANGUAGE FlexibleContexts #-}
23module Obelisk.Command.Run where
34
45import qualified Control.Exception as E
56import Control.Monad
67import Data.Either
78import Data.List
9+import Control.Monad.Reader (ask)
810import Data.Maybe
11+import Control.Monad.IO.Class (liftIO)
912import Data.Monoid
13+import qualified Data.Text as T
1014import Data.List.NonEmpty as NE
1115import Distribution.Utils.Generic (withUTF8FileContents)
1216import Distribution.PackageDescription.Parse (parseGenericPackageDescription, ParseResult(..))
@@ -20,10 +24,14 @@ import System.FilePath
2024import System.IO.Temp (withSystemTempDirectory)
2125import System.Process (callCommand)
2226
23−run :: IO ()
27+import Obelisk.App (MonadObelisk, ObeliskT, runObelisk)
28+import Obelisk.CLI (putLog, failWith, Severity(..))
29+
30+-- NOTE: `run` is not polymorphic like the rest because we use StaticPtr to invoke it.
31+run :: ObeliskT IO ()
2432run = do
25− freePort <- getFreePort
26− pkgs <- getLocalPkgs
33+ freePort <- liftIO getFreePort
34+ pkgs <- liftIO getLocalPkgs
2735 (pkgDirErrs, hsSrcDirs) <- fmap partitionEithers $ forM pkgs $ \pkg -> do
2836 let cabalFp = pkg </> pkg <.> "cabal"
2937 xs <- parseHsSrcDir cabalFp
@@ -31,9 +39,9 @@ run = do
3139 Nothing -> Left pkg
3240 Just hsSrcDirs -> Right $ toList $ fmap (pkg </>) hsSrcDirs
3341 when (null hsSrcDirs) $
34− fail $ "No valid pkgs found in " <> intercalate ", " pkgs
42+ failWith $ T.pack $ "No valid pkgs found in " <> intercalate ", " pkgs
3543 when (not (null pkgDirErrs)) $
36− putStrLn $ "Failed to find pkgs in " <> intercalate ", " pkgDirErrs
44+ putLog Warning $ T.pack $ "Failed to find pkgs in " <> intercalate ", " pkgDirErrs
3745 let dotGhci = unlines
3846 [ ":set args --quiet --port " <> show freePort
3947 , ":set -i" <> intercalate ":" (mconcat hsSrcDirs)
@@ -50,28 +58,34 @@ run = do
5058 ]
5159 withSystemTempDirectory "ob-ghci" $ \fp -> do
5260 let dotGhciPath = fp </> ".ghci"
53− writeFile dotGhciPath dotGhci
54− runDev dotGhciPath $ Just testCmd
61+ liftIO $ do
62+ writeFile dotGhciPath dotGhci
63+ runDev dotGhciPath $ Just testCmd
5564
5665-- | Relative paths to local packages of an obelisk project
5766-- TODO a way to query this
5867getLocalPkgs :: IO [FilePath]
5968getLocalPkgs = return ["backend", "common", "frontend"]
6069
61−parseHsSrcDir :: FilePath -- ^ package cabal file path
62− -> IO (Maybe (NE.NonEmpty FilePath)) -- ^ List of hs src dirs of the library component
70+parseHsSrcDir
71+ :: MonadObelisk m
72+ => FilePath -- ^ package cabal file path
73+ -> m (Maybe (NE.NonEmpty FilePath)) -- ^ List of hs src dirs of the library component
6374parseHsSrcDir cabalFp = do
64− exists <- doesFileExist cabalFp
75+ exists <- liftIO $ doesFileExist cabalFp
6576 if exists
6677 then do
67− withUTF8FileContents cabalFp $ \cabal -> do
78+ c <- ask
79+ liftIO $ withUTF8FileContents cabalFp $ \cabal -> runObelisk c $ do
6880 case parseGenericPackageDescription cabal of
6981 ParseOk warnings gpkg -> do
70− mapM_ print warnings
82+ mapM_ (putLog Warning) $ fmap (T.pack . show) warnings
7183 return $ do
7284 (_, lib) <- simplifyCondTree (const $ pure True) <$> condLibrary gpkg
7385 pure $ fromMaybe (pure ".") $ NE.nonEmpty $ hsSourceDirs $ libBuildInfo lib
74− ParseFailed _ -> return Nothing
86+ ParseFailed e -> do
87+ putLog Error $ T.pack $ "Failed to parse cabal file: " <> show e
88+ return Nothing
7589 else return Nothing
7690
7791-- | Dev