on master help sign in
Move ob-preprocessor into ob command via ObeliskInternal
commitd6cbbf7234a7aaa88fef21a96979c795607174c1 authorleon.smith@obsidian.systems <leon.smith@obsidian.systems> dateFri 17 Jan 2020, 11:54 (7 years ago) parent2a564e0
5 files changed+22−22
lib/command/obelisk-command.cabal−5 lib/command/src-bin/ob-preprocessor.hs−4 lib/command/src/Obelisk/Command.hs+11−1 lib/command/src/Obelisk/Command/Preprocessor.hs+10−11 lib/command/src/Obelisk/Command/Run.hs+1−1
lib/command/obelisk-command.cabal−5
@@ -66,8 +66,3 @@ executable ob
6666 main-is: src-bin/ob.hs
6767 build-depends: base, obelisk-command
6868 ghc-options: -Wall -O2 -threaded
69−
70−executable ob-preprocessor
71− main-is: src-bin/ob-preprocessor.hs
72− build-depends: base, obelisk-command
73− ghc-options: -Wall -O2 -threaded
lib/command/src-bin/ob-preprocessor.hsdeleted−4
@@ -1,4 +0,0 @@
1−import qualified Obelisk.Command.Preprocessor
2−
3−main :: IO ()
4−main = Obelisk.Command.Preprocessor.main
lib/command/src/Obelisk/Command.hs+11−1
@@ -38,6 +38,7 @@ import Obelisk.Command.Run
3838import Obelisk.Command.Thunk
3939import Obelisk.Command.Utils
4040import qualified Obelisk.Command.VmBuilder as VmBuilder
41+import qualified Obelisk.Command.Preprocessor as Preprocessor
4142
4243
4344data Args = Args
@@ -102,6 +103,9 @@ data ObCommand
102103data ObInternal
103104 = ObInternal_RunStaticIO StaticKey
104105 | ObInternal_CLIDemo
106+ -- the "apply-packages" preprocessor argument syntax is also handled outside
107+ -- optparse-applicative, but it shouldn't ever conflict with another syntax
108+ | ObInternal_ApplyPackages String String String [String]
105109 deriving Show
106110
107111inNixShell' :: MonadObelisk m => StaticPtr (ObeliskT IO ()) -> m ()
@@ -276,7 +280,11 @@ parserPrefs = defaultPrefs
276280 }
277281
278282parseCLIArgs :: ArgsConfig -> [String] -> IO Args
279−parseCLIArgs cfg as = pure as >>= handleParseResult . execParserPure parserPrefs (argsInfo cfg)
283+parseCLIArgs cfg = \case
284+ (origPath:inPath:outPath:"apply-packages":packagePaths)
285+ | any (\c -> c == '.' || c == '/') origPath ->
286+ pure $ Args False False $ ObCommand_Internal $ ObInternal_ApplyPackages origPath inPath outPath packagePaths
287+ as -> pure as >>= handleParseResult . execParserPure parserPrefs (argsInfo cfg)
280288
281289-- | Create an Obelisk config for the current process.
282290mkObeliskConfig :: IO Obelisk
@@ -407,6 +415,8 @@ ob = \case
407415 c <- getObelisk
408416 liftIO $ runObelisk c $ deRefStaticPtr p
409417 ObInternal_CLIDemo -> cliDemo
418+ ObInternal_ApplyPackages origPath inPath outPath packagePaths -> do
419+ liftIO $ Preprocessor.applyPackages origPath inPath outPath packagePaths
410420
411421haddockCommand :: [String] -> String
412422haddockCommand pkgs = unwords
lib/command/src/Obelisk/Command/Preprocessor.hs+10−11
@@ -11,28 +11,27 @@ import qualified Data.Text.Lazy.Builder as TL
1111import qualified Data.Text.Lazy.Encoding as TL
1212import Distribution.Compiler (CompilerFlavor (..))
1313import Language.Haskell.Extension (Extension (..))
14−import System.Environment (getArgs)
1514import System.IO (IOMode (..), hClose, hPutStrLn, openFile, stderr)
1615import System.FilePath (hasTrailingPathSeparator, joinPath, normalise, splitPath)
1716
1817import Obelisk.Command.Run (CabalPackageInfo (..), parseCabalPackage')
1918
20−main :: IO ()
21−main = do
22− -- The command line argumens are passed in via ghci.
23− -- ghci dictates the first three options and meanings
24− -- TODO: perform some validation and print a nice error/help message, if we don't see something plausible.
19+applyPackages :: FilePath -> FilePath -> FilePath -> [FilePath] -> IO ()
20+applyPackages origPath inPath outPath packagePaths = do
21+ -- This code is intended to be executed via ghci's -pgmF preprocessor option
22+ -- The command line arguments are passed in via ghc, which dictates the first three options and meanings
23+ -- In order for this code to execute, origPath must contain either a '.' character or a '/' character.
24+ -- (This is to avoid the possibility of the command line syntax conflicting with another ob command)
2525 -- We do have control over the remaining arguments, but they must be the same for all files.
26+ -- Thus, the fourth command line argument must be "apply-packages", which has already been handled.
2627 -- We assume all the remaining arguments passed in are paths to cabal or hpack package specifications.
2728
28− args@(origPath:inPath:outPath:packagePaths) <- getArgs
29−
3029 outFile <- openFile outPath WriteMode
3130 let hPutTextBuilder h = BU.hPutBuilder h . TL.encodeUtf8Builder . TL.toLazyText
3231
33− putStr "--------------------------------------------------------------------------------\n"
34− print args
35− putStr "--------------------------------------------------------------------------------\n"
32+ -- putStr "--------------------------------------------------------------------------------\n"
33+ -- print args
34+ -- putStr "--------------------------------------------------------------------------------\n"
3635
3736 -- Thus we must select among the packagePaths for the file we are going to parse.
3837
lib/command/src/Obelisk/Command/Run.hs+1−1
@@ -257,7 +257,7 @@ withGhciScript dirs f = do
257257 -- NOTE when no default-language is present cabal sets Haskell98
258258 language = NE.toList $ fromMaybe (Haskell98 NE.:| []) $ NE.nonEmpty languageFromPkgs
259259 dotGhci = unlines
260− [ ":set -pgmF ob-preprocessor " <> unwords (map (("-optF " <>) . _cabalPackageInfo_packageRoot) packageInfos)
260+ [ ":set -pgmF ob -optF apply-packages " <> unwords (map (("-optF " <>) . _cabalPackageInfo_packageRoot) packageInfos)
261261 , ":set -i" <> intercalate ":" (packageInfos >>= rootedSourceDirs)
262262 , ":set " <> unwords (("-X" <>) . prettyShow <$> language)
263263 , ":load Backend Frontend"