Skip to content

Commit

Permalink
Avoid using maybe for filepath in ob profile
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewbauer committed Feb 11, 2020
1 parent f882d63 commit 9ec5d2b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 22 deletions.
28 changes: 17 additions & 11 deletions lib/command/src/Obelisk/Command.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@ import qualified Data.ByteString.Base16 as Base16
import qualified Data.ByteString.Lazy as LBS
import Data.Foldable (for_)
import Data.List
import Data.Maybe
import qualified Data.Text as T
import Data.Text.Encoding
import Data.Text.Encoding.Error (lenientDecode)
import Data.Time.Clock
import Data.Time.Format (formatTime, defaultTimeLocale)
import GHC.StaticPtr
import Options.Applicative
import Options.Applicative.Help.Pretty (text, (<$$>))
Expand Down Expand Up @@ -50,8 +53,8 @@ newtype ArgsConfig = ArgsConfig
{ _argsConfig_enableVmBuilderByDefault :: Bool
}

args :: ArgsConfig -> Parser Args
args cfg = Args <$> noHandoff <*> verbose <*> obCommand cfg
args :: ArgsConfig -> UTCTime -> Maybe FilePath -> Parser Args
args cfg time root = Args <$> noHandoff <*> verbose <*> obCommand cfg time root

noHandoff :: Parser Bool
noHandoff = flag False True $ mconcat
Expand All @@ -67,8 +70,8 @@ verbose = flag False True $ mconcat
, help "Be more verbose"
]

argsInfo :: ArgsConfig -> ParserInfo Args
argsInfo cfg = info (args cfg <**> helper) $ mconcat
argsInfo :: ArgsConfig -> UTCTime -> Maybe FilePath -> ParserInfo Args
argsInfo cfg time root = info (args cfg time root <**> helper) $ mconcat
[ fullDesc
, progDesc "Manage Obelisk projects"
]
Expand All @@ -87,7 +90,7 @@ data ObCommand
= ObCommand_Init InitSource Bool
| ObCommand_Deploy DeployCommand
| ObCommand_Run
| ObCommand_Profile (Maybe FilePath)
| ObCommand_Profile FilePath
| ObCommand_Thunk ThunkCommand
| ObCommand_Repl
| ObCommand_Watch
Expand All @@ -103,13 +106,13 @@ data ObInternal
= ObInternal_ApplyPackages String String String [String]
deriving Show

obCommand :: ArgsConfig -> Parser ObCommand
obCommand cfg = hsubparser
obCommand :: ArgsConfig -> UTCTime -> Maybe FilePath -> Parser ObCommand
obCommand cfg time root = hsubparser
(mconcat
[ command "init" $ info (ObCommand_Init <$> initSource <*> initForce) $ progDesc "Initialize an Obelisk project"
, command "deploy" $ info (ObCommand_Deploy <$> deployCommand cfg) $ progDesc "Prepare a deployment for an Obelisk project"
, command "run" $ info (pure ObCommand_Run) $ progDesc "Run current project in development mode"
, command "profile" $ info (ObCommand_Profile <$> optional (strOption ( long "output" <> short 'o' <> metavar "BASEPATH" ))) $ progDesc "Run current project with profiling enabled"
, command "profile" $ info (ObCommand_Profile <$> option auto (long "output" <> short 'o' <> help "Base output to use for profiling output. Suffixes are added to this based on the profiling type." <> showDefault <> value ((fromMaybe "" root) </> "profile" </> formatTime defaultTimeLocale "%Y-%m-%dT%H:%M:%S" time) <> metavar "PATH")) $ progDesc "Run current project with profiling enabled"

This comment has been minimized.

Copy link
@3noch

3noch Feb 12, 2020

Collaborator

I don't think we want to actually pick a time here since that's obviously not going to be the right default when we run the command a moment or two later.

This comment has been minimized.

Copy link
@3noch

3noch Feb 12, 2020

Collaborator

I'm still pretty suspicious of bothering to find root here. What's the problem if we just use CWD again?

, command "thunk" $ info (ObCommand_Thunk <$> thunkCommand) $ progDesc "Manipulate thunk directories"
, command "repl" $ info (pure ObCommand_Repl) $ progDesc "Open an interactive interpreter"
, command "watch" $ info (pure ObCommand_Watch) $ progDesc "Watch current project for errors and warnings"
Expand Down Expand Up @@ -250,8 +253,8 @@ parserPrefs = defaultPrefs
{ prefShowHelpOnEmpty = True
}

parseCLIArgs :: ArgsConfig -> [String] -> IO Args
parseCLIArgs cfg = handleParseResult . execParserPure parserPrefs (argsInfo cfg)
parseCLIArgs :: ArgsConfig -> UTCTime -> Maybe FilePath -> [String] -> IO Args
parseCLIArgs cfg time root = handleParseResult . execParserPure parserPrefs (argsInfo cfg time root)

-- | Create an Obelisk config for the current process.
mkObeliskConfig :: IO Obelisk
Expand Down Expand Up @@ -306,12 +309,15 @@ main' argsCfg = do
, "logging-level=" <> show logLevel
]

time <- liftIO $ getCurrentTime
root <- findProjectRoot "."

--TODO: We'd like to actually use the parser to determine whether to hand off,
--but in the case where this implementation of 'ob' doesn't support all
--arguments being passed along, this could fail. For now, we don't bother
--with optparse-applicative until we've done the handoff.
let go as = do
args' <- liftIO $ handleParseResult (execParserPure parserPrefs (argsInfo argsCfg) as)
args' <- liftIO $ handleParseResult (execParserPure parserPrefs (argsInfo argsCfg time root) as)
case _args_noHandOffPassed args' of
False -> return ()
True -> putLog Warning "--no-handoff should only be passed once and as the first argument; ignoring"
Expand Down
14 changes: 3 additions & 11 deletions lib/command/src/Obelisk/Command/Run.hs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ import Data.Maybe
import qualified Data.Set as Set
import Data.Text (Text)
import qualified Data.Text as T
import Data.Time (getCurrentTime)
import Data.Time.Format (formatTime, defaultTimeLocale)
import Data.Traversable (for)
import Debug.Trace (trace)
import Distribution.Compiler (CompilerFlavor(..))
Expand Down Expand Up @@ -83,20 +81,14 @@ obRunImports = [ "import qualified Obelisk.Run"

profile
:: MonadObelisk m
=> Maybe FilePath
=> FilePath
-> m ()
profile mProfileBaseName = withProjectRoot "." $ \root -> do
profile profileBaseName = withProjectRoot "." $ \root -> do
freePort <- getFreePort
assets <- findProjectAssets root
putLog Debug $ "Assets impurely loaded from: " <> assets
putLog Debug "Using profiled build of project."
time <- liftIO $ formatTime defaultTimeLocale "%Y-%m-%dT%H:%M:%S" <$> getCurrentTime
profileBaseName <- case mProfileBaseName of
Just baseName -> pure baseName
Nothing -> do
let profileDirectory = root </> "profile"
liftIO $ createDirectoryIfMissing False profileDirectory
pure $ profileDirectory </> time
liftIO $ createDirectoryIfMissing False $ takeDirectory profileBaseName
let -- Sane flags to enable by default, enable time profiling +
-- closure heap profiling.
rtsFlags = [ "+RTS", "-p", "-po" <> profileBaseName, "-hc", "-RTS" ]
Expand Down

0 comments on commit 9ec5d2b

Please sign in to comment.