on master help sign in
Add ob hoogle command
commit9adf09e14033c14439ea0cff9134cdd200ef2077 authorElliot Cameron <eacameron@gmail.com> dateTue 28 Jan 2020, 11:23 (7 years ago) parent796fac5
2 files changed+51−22
lib/command/src/Obelisk/Command.hs+7 lib/command/src/Obelisk/Command/Project.hs+44−22
lib/command/src/Obelisk/Command.hs+7
@@ -95,6 +95,7 @@ data ObCommand
9595 | ObCommand_Watch
9696 | ObCommand_Shell ShellOpts
9797 | ObCommand_Doc String [String] -- shell and list of packages
98+ | ObCommand_Hoogle String Int -- shell and port
9899 | ObCommand_Internal ObInternal
99100 deriving Show
100101
@@ -121,6 +122,7 @@ obCommand cfg = hsubparser
121122 <> footerDoc (Just $
122123 text "Hint: To open the documentation you can pipe the output of this command like"
123124 <$$> text "ob doc reflex reflex-dom-core | xargs -n1 xdg-open")
125+ , command "hoogle" $ info (ObCommand_Hoogle <$> shellFlags <*> portOpt 8080) $ progDesc "Run a hoogle server locally for your project's dependency tree"
124126 ])
125127 <|> subparser
126128 (mconcat
@@ -254,6 +256,9 @@ shellOpts = ShellOpts
254256 <$> shellFlags
255257 <*> optional (strArgument (metavar "COMMAND"))
256258
259+portOpt :: Int -> Parser Int
260+portOpt dfault = option auto (long "port" <> short 'p' <> help "Port number for server" <> showDefault <> value dfault <> metavar "INT")
261+
257262parserPrefs :: ParserPrefs
258263parserPrefs = defaultPrefs
259264 { prefShowHelpOnEmpty = True
@@ -387,6 +392,8 @@ ob = \case
387392 projectShell root False (_shellOpts_shell so) (_shellOpts_command so)
388393 ObCommand_Doc shell' pkgs -> withProjectRoot "." $ \root ->
389394 projectShell root False shell' (Just $ haddockCommand pkgs)
395+ ObCommand_Hoogle shell' port -> withProjectRoot "." $ \root -> do
396+ nixShellWithHoogle root True shell' $ Just $ "hoogle server -p " <> show port <> " --local"
390397 ObCommand_Internal icmd -> case icmd of
391398 ObInternal_RunStaticIO k -> liftIO (unsafeLookupStaticPtr @(ObeliskT IO ()) k) >>= \case -- TODO: DELETE
392399 Nothing -> failWith $ "ObInternal_RunStaticIO: no such StaticKey: " <> T.pack (show k)
lib/command/src/Obelisk/Command/Project.hs+44−22
@@ -6,6 +6,9 @@ module Obelisk.Command.Project
66 , findProjectObeliskCommand
77 , findProjectRoot
88 , initProject
9+ , nixShellRunConfig
10+ , nixShellRunProc
11+ , nixShellWithHoogle
912 , nixShellWithPkgs
1013 , obeliskDirName
1114 , projectShell
@@ -253,31 +256,50 @@ toNixPath :: FilePath -> FilePath
253256toNixPath root | "/" `isInfixOf` root = root
254257 | otherwise = "./" <> root
255258
256−nixShellWithPkgs :: MonadObelisk m => FilePath -> Bool -> Map Text FilePath -> Maybe String -> m ()
257−nixShellWithPkgs root isPure packageNamesAndPaths command = do
258− packageNamesAndAbsPaths <- liftIO $ for packageNamesAndPaths makeAbsolute
259+nixShellRunConfig :: MonadObelisk m => FilePath -> Bool -> Maybe String -> m NixShellConfig
260+nixShellRunConfig root isPure command = do
259261 nixpkgsPath <- fmap T.strip $ readProcessAndLogStderr Debug $ setCwd (Just root) $
260262 proc nixExePath ["eval", "(import .obelisk/impl {}).nixpkgs.path"]
261263 nixRemote <- liftIO $ lookupEnv "NIX_REMOTE"
262− (_, _, _, ph) <- createProcess_ "runNixShellAttr" $ setDelegateCtlc True $ setCwd (Just root) $ proc "nix-shell" $
263− runNixShellConfig $ def
264− & nixShellConfig_pure .~ isPure
265− & nixShellConfig_common . nixCmdConfig_target .~ (def
266− & target_path .~ Nothing
267− & target_expr ?~
268− "{root, pkgs}: ((import root {}).passthru.__unstable__.self.extend (self: super: {\
269− \shellPackages = builtins.fromJSON pkgs; })\
270− \).project.shells.ghc"
271− )
272− & nixShellConfig_common . nixCmdConfig_args .~
273− [ rawArg "root" $ toNixPath root
274− , strArg "pkgs" (T.unpack $ decodeUtf8 $ BSL.toStrict $ Json.encode packageNamesAndAbsPaths)
275− ]
276− & nixShellConfig_run .~ (command <&> \c -> mconcat
277− [ "export NIX_PATH=nixpkgs=", T.unpack nixpkgsPath, "; "
278− , maybe "" (\v -> "export NIX_REMOTE=" <> v <> "; ") nixRemote
279− , c
280− ])
264+ pure $ def
265+ & nixShellConfig_pure .~ isPure
266+ & nixShellConfig_common . nixCmdConfig_target .~ (def & target_path .~ Nothing)
267+ & nixShellConfig_run .~ (command <&> \c -> mconcat
268+ [ "export NIX_PATH=nixpkgs=", T.unpack nixpkgsPath, "; "
269+ , maybe "" (\v -> "export NIX_REMOTE=" <> v <> "; ") nixRemote
270+ , c
271+ ])
272+
273+nixShellRunProc :: FilePath -> NixShellConfig -> ProcessSpec
274+nixShellRunProc root cfg = setDelegateCtlc True $ setCwd (Just root) $ proc "nix-shell" $ runNixShellConfig cfg
275+
276+nixShellWithPkgs :: MonadObelisk m => FilePath -> Bool -> Map Text FilePath -> Maybe String -> m ()
277+nixShellWithPkgs root isPure packageNamesAndPaths command = do
278+ packageNamesAndAbsPaths <- liftIO $ for packageNamesAndPaths makeAbsolute
279+ defShellConfig <- nixShellRunConfig root isPure command
280+ (_, _, _, ph) <- createProcess_ "nixShellWithPkgs" $ nixShellRunProc root $ defShellConfig
281+ & nixShellConfig_common . nixCmdConfig_target . target_expr ?~
282+ "{root, pkgs}: ((import root {}).passthru.__unstable__.self.extend (_: _: {\
283+ \shellPackages = builtins.fromJSON pkgs;\
284+ \})).project.shells.ghc"
285+ & nixShellConfig_common . nixCmdConfig_args .~
286+ [ rawArg "root" $ toNixPath root
287+ , strArg "pkgs" (T.unpack $ decodeUtf8 $ BSL.toStrict $ Json.encode packageNamesAndAbsPaths)
288+ ]
289+ void $ liftIO $ waitForProcess ph
290+
291+nixShellWithHoogle :: MonadObelisk m => FilePath -> Bool -> String -> Maybe String -> m ()
292+nixShellWithHoogle root isPure shell' command = do
293+ defShellConfig <- nixShellRunConfig root isPure command
294+ (_, _, _, ph) <- createProcess_ "nixShellWithHoogle" $ nixShellRunProc root $ defShellConfig
295+ & nixShellConfig_common . nixCmdConfig_target . target_expr ?~
296+ "{root, shell}: ((import root {}).passthru.__unstable__.self.extend (_: super: {\
297+ \userSettings = super.userSettings // { withHoogle = true; };\
298+ \})).project.shells.${shell}"
299+ & nixShellConfig_common . nixCmdConfig_args .~
300+ [ rawArg "root" $ toNixPath root
301+ , strArg "shell" shell'
302+ ]
281303 void $ liftIO $ waitForProcess ph
282304
283305projectShell :: MonadObelisk m => FilePath -> Bool -> String -> Maybe String -> m ()