@@ -5,8 +5,10 @@ module Obelisk.Command.Project
55 , initProject
66 , findProjectObeliskCommand
77 , findProjectRoot
8+ , withProjectRoot
89 , inProjectShell
910 , inImpureProjectShell
11+ , projectShell
1012 ) where
1113
1214import Control.Monad
@@ -110,6 +112,11 @@ findProjectRoot target = do
110112 (result, _) <- runStateT (walkToProjectRoot target targetStat myUid) []
111113 return result
112114
115+withProjectRoot :: FilePath -> (FilePath -> IO ()) -> IO ()
116+withProjectRoot target f = findProjectRoot target >>= \case
117+ Nothing -> fail "Must be used inside of an Obelisk project"
118+ Just root -> f root
119+
113120-- | Walk from the current directory to the containing project's root directory,
114121-- if there is one, accumulating potentially insecure directories that were
115122-- traversed in the process. Return the project root directory, if found.
@@ -153,24 +160,23 @@ isWritableOnlyBy s uid = fileOwner s == uid && fileMode s .&. 0o22 == 0
153160
154161-- | Run a command in the given shell for the current project
155162inProjectShell :: String -> String -> IO ()
156−inProjectShell = inProjectShell' ["--pure"]
157−
158−inProjectShell' :: [String] -> String -> String -> IO ()
159−inProjectShell' args shellName command = do
160− findProjectRoot "." >>= \case
161− Nothing -> putStrLn "Must be used inside of an Obelisk project"
162− Just root -> do
163− (_, _, _, ph) <- createProcess_ "runNixShellAttr" $ setCwd (Just root) $ proc "nix-shell" $
164− args <>
165− [ "-A"
166− , "shells." <> shellName
167− , "--run", command
168− ]
169− _ <- waitForProcess ph
170− return ()
163+inProjectShell shellName command = withProjectRoot "." $ \root ->
164+ projectShell root True shellName command
171165
172166inImpureProjectShell :: String -> String -> IO ()
173−inImpureProjectShell = inProjectShell' []
167+inImpureProjectShell shellName command = withProjectRoot "." $ \root ->
168+ projectShell root False shellName command
169+
170+projectShell :: FilePath -> Bool -> String -> String -> IO ()
171+projectShell root isPure shellName command = do
172+ (_, _, _, ph) <- createProcess_ "runNixShellAttr" $ setCwd (Just root) $ proc "nix-shell" $
173+ [ "--pure" | isPure ] <>
174+ [ "-A"
175+ , "shells." <> shellName
176+ , "--run", command
177+ ]
178+ _ <- waitForProcess ph
179+ return ()
174180
175181setCwd :: Maybe FilePath -> CreateProcess -> CreateProcess
176182setCwd fp cp = cp { cwd = fp }