11module LambdaBuffers.Compiler.Cli.Compile (CompileOpts (.. ), compile ) where
22
3- import Control.Lens (makeLenses )
3+ import Control.Lens (makeLenses , (&) , (.~) )
44import Control.Lens.Getter ((^.) )
55import Data.ByteString qualified as BS
6+ import Data.ProtoLens (Message (defMessage ))
67import Data.ProtoLens qualified as Pb
78import Data.ProtoLens.TextFormat qualified as PbText
89import Data.Text.Lazy qualified as Text
910import Data.Text.Lazy.IO qualified as Text
10- import LambdaBuffers.Compiler.KindCheck (check )
11- import LambdaBuffers.Compiler.ProtoCompat (
12- FromProtoErr (NamingError , ProtoError ),
13- IsMessage (fromProto , toProto ),
14- )
15- import LambdaBuffers.Compiler.ProtoCompat.Types qualified as ProtoCompat
16- import Proto.Compiler as ProtoLib (CompilerInput , CompilerOutput )
11+ import LambdaBuffers.Compiler (runCompiler )
12+ import Proto.Compiler (CompilerError , CompilerInput , CompilerOutput )
13+ import Proto.Compiler_Fields (compilerError , compilerResult )
1714import System.FilePath.Lens (extension )
1815
1916data CompileOpts = CompileOpts
@@ -24,20 +21,19 @@ data CompileOpts = CompileOpts
2421
2522makeLenses ''CompileOpts
2623
27- -- NOTE(cstml) - let 's use Katip instead of print.
24+ -- NOTE(cstml): Let 's use Katip instead of print.
2825
2926-- | Compile LambdaBuffers modules
3027compile :: CompileOpts -> IO ()
3128compile opts = do
32- compIn <- readCompilerInput (opts ^. input)
33- case fromProto @ CompilerInput @ ProtoCompat. CompilerInput compIn of
34- Left err -> case err of
35- NamingError ne -> print $ " Encountered a naming error " <> show ne
36- ProtoError pe -> print $ " Encountered a proto error " <> show pe
37- Right compIn' -> do
38- print @ String " Successfully processed the CompilerInput"
39- let result = check compIn'
40- writeCompilerOutput (opts ^. output) (toProto result)
29+ compInp <- readCompilerInput (opts ^. input)
30+ case runCompiler compInp of
31+ Left compErr -> do
32+ putStrLn " Encountered errors during Compilation"
33+ writeCompilerError (opts ^. output) compErr
34+ Right compRes -> do
35+ putStrLn " Compilation succeeded"
36+ writeCompilerOutput (opts ^. output) (defMessage & compilerResult .~ compRes)
4137 return ()
4238
4339readCompilerInput :: FilePath -> IO CompilerInput
@@ -52,7 +48,10 @@ readCompilerInput fp = do
5248 return $ PbText. readMessageOrDie content
5349 _ -> error $ " Unknown CompilerInput format " <> ext
5450
55- writeCompilerOutput :: FilePath -> ProtoLib. CompilerOutput -> IO ()
51+ writeCompilerError :: FilePath -> CompilerError -> IO ()
52+ writeCompilerError fp err = writeCompilerOutput fp (defMessage & compilerError .~ err)
53+
54+ writeCompilerOutput :: FilePath -> CompilerOutput -> IO ()
5655writeCompilerOutput fp cr = do
5756 let ext = fp ^. extension
5857 case ext of
0 commit comments