@@ -18,8 +18,10 @@ import Protolude qualified as P
1818
1919import Control.Arrow ((>>>) )
2020import Data.Aeson qualified as Aeson
21+ import Data.ByteString.Lazy qualified as BL
2122import Data.Text (Text )
2223import Data.Text qualified as T
24+ import Data.Text.Encoding qualified as TE
2325import Options.Applicative.Arrows (left )
2426import Prettyprinter (Doc , annotate , pretty )
2527import Prettyprinter.Render.Terminal (AnsiStyle , Color (Red , Yellow ))
@@ -28,6 +30,7 @@ import System.Process (readProcess)
2830
2931import Config (Config , Hook (body , filePath , interpreter ))
3032import ImportTask (ImportTask )
33+ import LuaRunner (runLuaHook )
3134import Utils (colr , (<!!>) )
3235
3336
@@ -91,8 +94,6 @@ executeHooks stdinText hooks = do
9194 if
9295 | s `P.elem` [" javascript" , " js" , " node" , " node.js" ] ->
9396 (" node" , [" -e" ], ExecStdin )
94- | s `P.elem` [" lua" ] ->
95- (" lua" , [" -e" ], ExecStdin )
9697 | s `P.elem` [" python" , " python3" , " py" ] ->
9798 (" python3" , [" -c" ], ExecStdin )
9899 | s `P.elem` [" ruby" , " rb" ] ->
@@ -103,6 +104,24 @@ executeHooks stdinText hooks = do
103104 | otherwise ->
104105 (" " , [" " ], ExecFile )
105106
107+ -- Execute a Lua hook using the embedded interpreter
108+ runEmbeddedLua :: Text -> IO String
109+ runEmbeddedLua luaCode = do
110+ result <- runLuaHook luaCode stdinText
111+ case result of
112+ P. Right output -> pure $ T. unpack output
113+ P. Left err ->
114+ pure $
115+ T. unpack $
116+ TE. decodeUtf8 $
117+ BL. toStrict $
118+ Aeson. encode $
119+ Aeson. object [" error" Aeson. .= err]
120+
121+ -- Check if a file extension or interpreter name indicates Lua
122+ isLua :: String -> P. Bool
123+ isLua s = s `P.elem` [" lua" ]
124+
106125 hookToResult <-
107126 P. sequence $
108127 hooks <&> \ hook -> do
@@ -112,29 +131,38 @@ executeHooks stdinText hooks = do
112131 " " ->
113132 -- Is executed with shell
114133 readProcess fPath [] stdinStr
115- ext -> do
116- let (interpreter, cliFlags, execMode) = getInterpreter ext
117- case execMode of
118- ExecStdin -> do
134+ ext
135+ -- Use embedded Lua interpreter for .lua files
136+ | isLua ext -> do
119137 fileContent <- P. readFile fPath
120- readProcess
121- interpreter
122- (P. concat [cliFlags, [T. unpack fileContent]])
123- stdinStr
124- ExecFile -> do
125- readProcess
126- interpreter
127- (P. concat [cliFlags, [fPath]])
128- stdinStr
138+ runEmbeddedLua fileContent
139+ | otherwise -> do
140+ let (interpreterCmd, cliFlags, execMode) = getInterpreter ext
141+ case execMode of
142+ ExecStdin -> do
143+ fileContent <- P. readFile fPath
144+ readProcess
145+ interpreterCmd
146+ (P. concat [cliFlags, [T. unpack fileContent]])
147+ stdinStr
148+ ExecFile -> do
149+ readProcess
150+ interpreterCmd
151+ (P. concat [cliFlags, [fPath]])
152+ stdinStr
129153 ---
130154 Nothing -> do
131- let
132- (interpreter, cliFlags, _) =
133- getInterpreter (T. unpack hook. interpreter)
134- readProcess
135- interpreter
136- (P. concat [cliFlags, [T. unpack hook. body]])
137- stdinStr
155+ -- Use embedded Lua interpreter for inline Lua hooks
156+ if isLua (T. unpack hook. interpreter)
157+ then runEmbeddedLua hook. body
158+ else do
159+ let
160+ (interpreterCmd, cliFlags, _) =
161+ getInterpreter (T. unpack hook. interpreter)
162+ readProcess
163+ interpreterCmd
164+ (P. concat [cliFlags, [T. unpack hook. body]])
165+ stdinStr
138166
139167 let parsedHookResults :: [P. Either Text HookResult ] =
140168 hookToResult
0 commit comments