on master help sign in
added projectProc commands
commit0c9778648b861b0d1dd96e0db18ee30d36c3dff0 authorOwen Shepherd <414owen@gmail.com> dateWed 18 Dec 2019, 21:19 (7 years ago) parent7c7b8d5
7 files changed+51−12
lib/cliapp/obelisk-cliapp.cabal+2−1 lib/cliapp/src/Obelisk/CliApp/Process.hs+4−2 lib/cliapp/src/Obelisk/CliApp/ShellEscape.hs+18 lib/command/obelisk-command.cabal+2−1 lib/command/src/Obelisk/Command.hs+5−4 lib/command/src/Obelisk/Command/Project.hs+15−1 lib/command/src/Obelisk/Command/Run.hs+5−3
lib/cliapp/obelisk-cliapp.cabal+2−1
@@ -16,12 +16,13 @@ library
1616 , logging-effect
1717 , monad-loops
1818 , mtl
19− , posix-escape
19+ , shell-escape
2020 , process
2121 , terminal-size
2222 , text
2323 , time
2424 , transformers
25+ , utf8-string
2526 exposed-modules:
2627 Obelisk.CliApp
2728 Obelisk.CliApp.Demo
lib/cliapp/src/Obelisk/CliApp/Process.hs+4−2
@@ -29,6 +29,7 @@ import Control.Monad.IO.Class (MonadIO, liftIO)
2929import Control.Lens (Prism', review)
3030import qualified Data.ByteString as BS
3131import qualified Data.ByteString.Char8 as BSC
32+import qualified Data.ByteString.UTF8 as BSU
3233import Data.Function (fix)
3334import Data.Monoid ((<>))
3435import Data.Text (Text)
@@ -40,10 +41,10 @@ import System.IO (Handle)
4041import System.IO.Streams (InputStream, handleToInputStream)
4142import qualified System.IO.Streams as Streams
4243import System.IO.Streams.Concurrent (concurrentMerge)
43−import System.Posix.Escape (escape)
4444import System.Process (CreateProcess, ProcessHandle, StdStream (CreatePipe), cmdspec, std_err, std_out,
4545 waitForProcess)
4646import qualified System.Process as Process
47+import Text.ShellEscape (bytes, bash)
4748
4849import Control.Monad.Log (Severity (..))
4950import Obelisk.CliApp.Logging (putLog, putLogRaw)
@@ -183,4 +184,5 @@ reconstructCommand :: Process.CmdSpec -> Text
183184reconstructCommand (Process.ShellCommand str) = T.pack str
184185reconstructCommand (Process.RawCommand c as) = processToShellString c as
185186 where
186− processToShellString cmd args = T.pack $ unwords $ map escape (cmd : args)
187+ processToShellString cmd args = T.pack $ unwords $
188+ map (BSU.toString . bytes . bash . BSU.fromString) (cmd : args)
lib/cliapp/src/Obelisk/CliApp/ShellEscape.hsnew file+18
@@ -0,0 +1,18 @@
1+module Obelisk.CliApp.ShellEscape
2+ ( escape
3+ , fromEscaped
4+ , toEscaped
5+ ) where
6+
7+import Shell.Utility.Quote (minimal)
8+
9+newtype ShellEscaped = ShellEscaped { fromEscaped :: String }
10+
11+escape :: String -> String
12+escape = minimal
13+
14+toEscaped :: String -> ShellEscaped
15+toEscaped = ShellEscape . escape
16+
17+escapeCommand :: [String] -> ShellEscape
18+escapeCommand = ShellEscaped . unwords . fmap escape
lib/command/obelisk-command.cabal+2−1
@@ -39,7 +39,7 @@ library
3939 , optparse-applicative
4040 , placeholders
4141 , process
42− , posix-escape
42+ , shell-escape
4343 , temporary
4444 , terminal-size
4545 , text
@@ -47,6 +47,7 @@ library
4747 , transformers
4848 , unix
4949 , unordered-containers
50+ , utf8-string
5051 , which
5152 , yaml
5253 exposed-modules:
lib/command/src/Obelisk/Command.hs+5−4
@@ -13,6 +13,7 @@ import qualified Data.Binary as Binary
1313import Data.Bool (bool)
1414import qualified Data.ByteString.Base16 as Base16
1515import qualified Data.ByteString.Lazy as LBS
16+import qualified Data.ByteString.UTF8 as BSU
1617import Data.List
1718import Data.Maybe (catMaybes)
1819import qualified Data.Text as T
@@ -26,7 +27,7 @@ import System.Environment
2627import System.FilePath
2728import qualified System.Info
2829import System.IO (hIsTerminalDevice, stdout)
29−import System.Posix.Escape (escapeMany)
30+import Text.ShellEscape (bash)
3031import System.Posix.Process (executeFile)
3132
3233import Obelisk.App
@@ -106,8 +107,8 @@ data ObInternal
106107
107108inNixShell' :: MonadObelisk m => StaticPtr (ObeliskT IO ()) -> m ()
108109inNixShell' p = withProjectRoot "." $ \root -> do
109− cmd <- liftIO $ escapeMany <$> mkCmd
110− projectShell root True "ghc" (Just cmd)
110+ cmd <- liftIO $ fmap (bash . BSU.fromString) <$> mkCmd
111+ projectProc root True "ghc" (Just cmd)
111112 where
112113 mkCmd = do
113114 argsCfg <- getArgsConfig
@@ -382,7 +383,7 @@ ob = \case
382383 ObCommand_Repl -> runRepl
383384 ObCommand_Watch -> inNixShell' $ static runWatch
384385 ObCommand_Shell so -> withProjectRoot "." $ \root ->
385− projectShell root False (_shellOpts_shell so) (_shellOpts_command so)
386+ projectProc root False (_shellOpts_shell so) (pure . bash . BSU.fromString <$> _shellOpts_command so)
386387 ObCommand_Doc shell pkgs -> withProjectRoot "." $ \root ->
387388 projectShell root False shell (Just $ haddockCommand pkgs)
388389 ObCommand_Internal icmd -> case icmd of
lib/command/src/Obelisk/Command/Project.hs+15−1
@@ -7,8 +7,10 @@ module Obelisk.Command.Project
77 , findProjectObeliskCommand
88 , findProjectRoot
99 , withProjectRoot
10+ , inProjectProc
1011 , inProjectShell
1112 , inImpureProjectShell
13+ , projectProc
1214 , projectShell
1315
1416 , toObeliskDir
@@ -18,6 +20,7 @@ module Obelisk.Command.Project
1820import Control.Monad
1921import Control.Monad.IO.Class (liftIO)
2022import Control.Monad.State
23+import qualified Data.ByteString.UTF8 as BSU
2124import Data.Bits
2225import Data.Function (on)
2326import qualified Data.Text as T
@@ -27,6 +30,7 @@ import System.IO.Temp
2730import System.Posix (FileStatus, UserID, deviceID, fileID, fileMode, fileOwner, getFileStatus, getRealUserID)
2831import System.Posix.Files
2932import System.Process (CreateProcess, cwd, proc, waitForProcess, delegate_ctlc)
33+import Text.ShellEscape (Bash, bytes)
3034
3135import GitHub.Data.GitData (Branch)
3236import GitHub.Data.Name (Name)
@@ -193,6 +197,10 @@ walkToImplDir projectRoot myUid = do
193197isWritableOnlyBy :: FileStatus -> UserID -> Bool
194198isWritableOnlyBy s uid = fileOwner s == uid && fileMode s .&. 0o22 == 0
195199
200+inProjectProc :: MonadObelisk m => String -> [Bash] -> m ()
201+inProjectProc shellName command = withProjectRoot "." $ \root ->
202+ projectProc root True shellName (Just command)
203+
196204-- | Run a command in the given shell for the current project
197205inProjectShell :: MonadObelisk m => String -> String -> m ()
198206inProjectShell shellName command = withProjectRoot "." $ \root ->
@@ -202,6 +210,12 @@ inImpureProjectShell :: MonadObelisk m => String -> String -> m ()
202210inImpureProjectShell shellName command = withProjectRoot "." $ \root ->
203211 projectShell root False shellName (Just command)
204212
213+projectProc :: MonadObelisk m => FilePath -> Bool -> String -> Maybe [Bash] -> m ()
214+projectProc root isPure shellName command =
215+ projectShell root isPure shellName $ commandToString <$> command
216+ where
217+ commandToString = unwords . fmap (BSU.toString . bytes)
218+
205219projectShell :: MonadObelisk m => FilePath -> Bool -> String -> Maybe String -> m ()
206220projectShell root isPure shellName command = do
207221 (_, _, _, ph) <- createProcess_ "runNixShellAttr" $ setCtlc $ setCwd (Just root) $ proc "nix-shell" $
@@ -213,7 +227,7 @@ projectShell root isPure shellName command = do
213227 , "shells." <> shellName
214228 ] <> case command of
215229 Nothing -> []
216− Just c -> ["--run", c]
230+ Just c -> ["--run", c]
217231 void $ liftIO $ waitForProcess ph
218232
219233setCtlc :: CreateProcess -> CreateProcess
lib/command/src/Obelisk/Command/Run.hs+5−3
@@ -10,6 +10,8 @@ import Control.Exception (bracket)
1010import Control.Monad
1111import Control.Monad.IO.Class (liftIO)
1212import Control.Monad.Reader (MonadIO)
13+import Data.ByteString (ByteString)
14+import qualified Data.ByteString.UTF8 as BSU
1315import Data.Either
1416import qualified Data.List as L
1517import qualified Data.List.NonEmpty as NE
@@ -34,14 +36,14 @@ import System.FilePath
3436import System.Process (proc)
3537import System.IO.Temp (withSystemTempDirectory)
3638import System.Which (staticWhich)
37−import Data.ByteString (ByteString)
39+import Text.ShellEscape (bash)
3840
3941import Obelisk.App (MonadObelisk, ObeliskT)
4042import Obelisk.CliApp
4143 ( CliT (..), HasCliConfig, Severity (..)
4244 , callCommand, failWith, getCliConfig, putLog
4345 , readProcessAndLogStderr, runCli)
44−import Obelisk.Command.Project (inProjectShell, withProjectRoot)
46+import Obelisk.Command.Project (inProjectProc, withProjectRoot)
4547
4648data CabalPackageInfo = CabalPackageInfo
4749 { _cabalPackageInfo_packageRoot :: FilePath
@@ -219,7 +221,7 @@ runGhciRepl
219221 :: MonadObelisk m
220222 => FilePath -- ^ Path to .ghci
221223 -> m ()
222−runGhciRepl dotGhci = inProjectShell "ghc" $ unwords $ "ghci" : ["-no-user-package-db", "-ghci-script", dotGhci]
224+runGhciRepl dotGhci = inProjectProc "ghc" $ fmap (bash . BSU.fromString) $ "ghci" : ["-no-user-package-db", "-ghci-script", dotGhci]
223225
224226-- | Run ghcid
225227runGhcid