servant-serf 0.0.3 → 0.1.0
raw patch · 4 files changed
+32/−46 lines, 4 filesdep −tomland
Dependencies removed: tomland
Files
- app/Main.hs +15/−11
- app/Options.hs +14/−17
- app/Regex.hs +1/−15
- servant-serf.cabal +2/−3
app/Main.hs view
@@ -2,6 +2,7 @@ {-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE RecordWildCards #-} {-# LANGUAGE OverloadedStrings #-}+{-# LANGUAGE CPP #-} module Main where @@ -16,7 +17,6 @@ import qualified Data.Text as T import qualified Data.Text.IO as T import qualified Hpack.Config as Hpack-import qualified Toml type Preprocessor = ExceptT PreprocessorException IO @@ -32,17 +32,11 @@ main :: IO () main = do Options{..} <- execParser options- configTomlRes <- Toml.decodeFileEither configCodec ".servant-serf.toml"- config <- case configTomlRes of- Left errs -> do - logErrLn $ Toml.prettyTomlDecodeErrors errs- exitFailure- Right config -> pure config- [origInputFile, input, output] <- getArgs+ (origInputFile : input : output : _) <- getArgs contents <- liftIO $ T.readFile input apiModule@ApiModule{..} <- case decodeApiModule contents of Right apiModule -> pure apiModule- Left err -> do + Left err -> do logErrLn $ "Parse error: " <> T.pack err exitFailure mException <- runExceptT $ mainPP apiModule config@@ -65,7 +59,7 @@ Nothing -> throwError NoLibrary Just section -> let lib = Hpack.sectionData section- in pure $ T.pack <$> Hpack.libraryExposedModules lib <> Hpack.libraryOtherModules lib+ in pure $ getAllModules lib let handlerModules = filterPattern isHandlerModule allModules case handlerModules of [] -> throwError $ EmptyHandlerModules allModules@@ -75,6 +69,17 @@ [] -> pure () -- all good, discovered modules are in scope xs -> throwError $ MissingImports xs +getAllModules :: Hpack.Library -> [Text]+getAllModules lib = T.pack . unModule <$> Hpack.libraryExposedModules lib <> Hpack.libraryOtherModules lib++#if MIN_VERSION_hpack(0, 34, 3)+unModule :: Hpack.Module -> String+unModule = Hpack.unModule+#else+unModule :: String -> String+unModule = id+#endif+ renderException :: String -> Module -> PreprocessorException -> Text renderException origInputFile (Module modName) exception = case exception of PackageYamlParseFailure errMsg ->@@ -93,4 +98,3 @@ , " or updating the is_handler_module regular expression in your .servant-serf.toml" ] <> importStatements-
app/Options.hs view
@@ -11,23 +11,19 @@ , options , Options.Applicative.execParser , Config(..)- , defaultConfig- , configCodec , Pattern' ) where +import Data.Bifunctor import Data.Text (Text)-import GHC.Generics (Generic(..)) import Options.Applicative import Regex-import Toml ((.=))-import qualified Data.Text as T-import qualified Toml data Options = Options { originalInputFile :: FilePath , inputFile :: FilePath , outputFile :: FilePath+ , config :: Config } options :: ParserInfo Options@@ -48,19 +44,20 @@ <*> strArgument ( metavar "OUTPUT_FILEPATH" )+ <*> configParser +configParser :: Parser Config+configParser = Config+ <$> option str+ ( long "package-name"+ <> short 'p'+ )+ <*> option (eitherReader $ first show . parseRegex)+ ( long "is-handler-module"+ <> short 'm'+ )+ data Config = Config { packageName :: Text , isHandlerModule :: Pattern'- } deriving (Generic)--configCodec :: Toml.TomlCodec Config-configCodec = Config- <$> Toml.text "package_name" .= packageName- <*> patternCodec "is_handler_module" .= isHandlerModule--defaultConfig :: Config-defaultConfig = Config- { packageName = "example"- , isHandlerModule = either (error . T.unpack) id $ parseRegex' ".*Handler.*" }
app/Regex.hs view
@@ -1,8 +1,7 @@ module Regex ( Pattern'- , patternCodec , filterPattern- , parseRegex'+ , parseRegex ) where import Data.Text (Text)@@ -11,21 +10,8 @@ import Text.Regex.TDFA.ReadRegex (parseRegex) import Text.Regex.TDFA.TDFA ( patternToRegex ) import Text.Regex.TDFA.Text-import qualified Data.Text as T-import qualified Toml type Pattern' = (Pattern, (GroupIndex, DoPa))--parseRegex' :: Text -> Either Text Pattern'-parseRegex' inp = case parseRegex (T.unpack inp) of- Left e -> Left $ T.pack $ show e- Right pattern -> Right pattern--_Pattern :: Toml.TomlBiMap Pattern' Toml.AnyValue-_Pattern = Toml._TextBy (T.pack . showPattern . fst) parseRegex'--patternCodec :: Toml.Key -> Toml.TomlCodec Pattern'-patternCodec key = Toml.match _Pattern key filterPattern :: Pattern' -> [Text] -> [Text] filterPattern pattern strings = filter matchPattern strings
servant-serf.cabal view
@@ -1,6 +1,6 @@ cabal-version: 1.12 name: servant-serf-version: 0.0.3+version: 0.1.0 license: MIT license-file: LICENSE maintainer: Zachary Churchill <zachary@itpro.tv>@@ -48,8 +48,7 @@ parser-combinators >=1.2.1, regex-base >=0.94.0.0, regex-tdfa >=1.3.1.0,- text >=1.2.4.0,- tomland >=1.3.1.0+ text >=1.2.4.0 if False other-modules: Paths_servant_serf