glob-imports 0.0.5.0 → 0.0.6.0
raw patch · 5 files changed
+95/−22 lines, 5 filesPVP: major bump suggested
API removals or changes: PVP suggests a major version bump
API changes (from Hackage documentation)
- GlobImports.Exe: renderFile :: AllModelsFile -> Affix -> String -> String
+ GlobImports.Exe: renderFile :: AllModelsFile -> Affix -> Bool -> String -> String
- GlobImports.Exe: spliceImports :: Source -> SourceContents -> Destination -> Maybe FilePath -> [String] -> [String] -> Bool -> Affix -> IO ()
+ GlobImports.Exe: spliceImports :: Source -> SourceContents -> Destination -> Maybe FilePath -> [String] -> [String] -> Bool -> Bool -> Affix -> IO ()
Files
- ChangeLog.md +6/−0
- app/Main.hs +7/−0
- glob-imports.cabal +1/−1
- src/GlobImports/Exe.hs +47/−17
- test/GlobImports/ExeSpec.hs +34/−4
ChangeLog.md view
@@ -1,5 +1,11 @@ # Changelog for glob-imports +## 0.0.6.0++- [#11](https://github.com/parsonsmatt/glob-imports/pull/11)+ - `glob-imports` now emits `LINE` pragmas by default to make error messages+ better.+ ## 0.0.5.0 - [#9](https://github.com/parsonsmatt/glob-imports/pull/9)
app/Main.hs view
@@ -12,6 +12,7 @@ , argsPattern :: String , argsExcludedPrefixes :: String , argsDebug :: Bool+ , argsLinePragmas :: Bool , argsImportQualified :: Affix } deriving (Show)@@ -28,6 +29,7 @@ <*> patternParser <*> excludedPrefixesParser <*> debugParser+ <*> linePragmasParser <*> importQualifiedParser ) mempty@@ -56,6 +58,10 @@ switch $ long "debug" <> help "Whether to print debug output"+ linePragmasParser =+ fmap not . switch $+ long "no-line-pragmas"+ <> help "Do not emit {-# LINE #-} pragmas around generated code" stringToAffix x = case x of "pre" -> Just Prefix "post" -> Just Suffix@@ -79,6 +85,7 @@ (splitOn ',' (argsPattern args)) (splitOn ',' (argsExcludedPrefixes args)) (argsDebug args)+ (argsLinePragmas args) (argsImportQualified args) where splitOn _ [] = []
glob-imports.cabal view
@@ -5,7 +5,7 @@ -- see: https://github.com/sol/hpack name: glob-imports-version: 0.0.5.0+version: 0.0.6.0 synopsis: Import modules for metaprogramming description: This package provides an executable for importing modules in a directory and splicing those in. Please see the README on GitHub at <https://github.com/parsonsmatt/glob-imports#readme> category: Metaprogramming
src/GlobImports/Exe.hs view
@@ -124,9 +124,10 @@ -> [String] -> [String] -> Bool+ -> Bool -> Affix -> IO ()-spliceImports (Source src) (SourceContents srcContents) (Destination dest) msearchDir pats prefixes debug affix = do+spliceImports (Source src) (SourceContents srcContents) (Destination dest) msearchDir pats prefixes debug linePragmas affix = do let (sourceDir, _file) = splitFileName src searchDir = fromMaybe sourceDir msearchDir@@ -151,7 +152,7 @@ mapMaybe pathToModule (fmap (searchDir </>) filteredFiles) } output =- renderFile input affix srcContents+ renderFile input affix linePragmas srcContents writeFile dest output where@@ -188,15 +189,15 @@ renderFile :: AllModelsFile -> Affix+ -> Bool -> String -> String-renderFile amf affix originalContents =- concatMap- unlines- [ modulePrior- , newImportLines- , newModuleRest- ]+renderFile amf affix linePragmas originalContents =+ unlines $+ topPragma+ ++ modulePrior+ ++ newImportLines+ ++ newModuleRest where originalLines = lines originalContents@@ -215,6 +216,21 @@ (prior, (_globImportLine : rest)) -> (prior, rest) + modulePath' =+ modulePath (amfModuleBase amf)++ topPragma =+ [linePragma 1 | linePragmas]++ linePragma n =+ "{-# LINE " <> show n <> " \"" <> modulePath' <> "\" #-}"++ spliceLineNumber =+ length modulePrior + 1++ resumeLineNumber =+ length modulePrior + 2+ newModuleRest = let (remainingModule, lastImportLine) =@@ -225,8 +241,8 @@ " [ " <> quoteModuleName mod' mkRestModuleLine mod' = " , " <> quoteModuleName mod'- newLines =- reverse case amfModuleImports amf of+ generatedBlock =+ case amfModuleImports amf of [] -> [] (firstModule : restModules) ->@@ -236,15 +252,29 @@ ] <> map mkRestModuleLine restModules <> [" ]"]+ annotatedNewLines =+ reverse (annotateGenerated generatedBlock)+ remainingWithPragma =+ case (linePragmas, remainingModule) of+ (True, _ : _) ->+ remainingModule <> [linePragma resumeLineNumber]+ _ ->+ remainingModule in- reverse (concat [remainingModule, newLines, lastImportLine])+ reverse (concat [remainingWithPragma, annotatedNewLines, lastImportLine]) + annotateGenerated xs =+ if linePragmas+ then concatMap (\ln -> [linePragma spliceLineNumber, ln]) xs+ else xs+ newImportLines =- map- (\mod' -> case affix of- Prefix -> "import qualified " <> moduleName mod'- Suffix -> "import " <> moduleName mod' <> " qualified")- (amfModuleImports amf)+ annotateGenerated $+ map+ (\mod' -> case affix of+ Prefix -> "import qualified " <> moduleName mod'+ Suffix -> "import " <> moduleName mod' <> " qualified")+ (amfModuleImports amf) data Module = Module { moduleName :: String
test/GlobImports/ExeSpec.hs view
@@ -22,14 +22,14 @@ ] } it "errors without a GLOB_IMPORTS_SPLICE marker" do- (pure $! length (renderFile allModelsFile Prefix (unlines+ (pure $! length (renderFile allModelsFile Prefix False (unlines [ "module Foo.Bar where" , "" , "import Blah" ]))) `shouldThrow` anyErrorCall it "works with a GLOB_IMPORTS_SPLICE marker" do- renderFile allModelsFile Prefix+ renderFile allModelsFile Prefix False (unlines [ "module Foo.Bar where" , ""@@ -48,7 +48,7 @@ , " ]" ] it "works with import qualified post" do- renderFile allModelsFile Suffix+ renderFile allModelsFile Suffix False (unlines [ "module Foo.Bar where" , ""@@ -67,7 +67,7 @@ , " ]" ] it "can handle imports after glob import splice" do- renderFile allModelsFile Prefix+ renderFile allModelsFile Prefix False (unlines [ "module Foo.Bar where" , ""@@ -90,6 +90,36 @@ , " [ \"Foo.Bar.Quux\"" , " ]" , ""+ , "baz :: Int"+ , "baz = 3"+ ]+ it "emits LINE pragmas for the original source" do+ renderFile allModelsFile Prefix True+ (unlines+ [ "module Foo.Bar where"+ , ""+ , "import Blah"+ , "-- GLOB_IMPORTS_SPLICE"+ , "baz :: Int"+ , "baz = 3"+ ])+ `shouldBe`+ unlines+ [ "{-# LINE 1 \"src/Foo/Bar.hs\" #-}"+ , "module Foo.Bar where"+ , ""+ , "import Blah"+ , "{-# LINE 4 \"src/Foo/Bar.hs\" #-}"+ , "import qualified Foo.Bar.Quux"+ , "{-# LINE 4 \"src/Foo/Bar.hs\" #-}"+ , "_importedModules :: [String]"+ , "{-# LINE 4 \"src/Foo/Bar.hs\" #-}"+ , "_importedModules ="+ , "{-# LINE 4 \"src/Foo/Bar.hs\" #-}"+ , " [ \"Foo.Bar.Quux\""+ , "{-# LINE 4 \"src/Foo/Bar.hs\" #-}"+ , " ]"+ , "{-# LINE 5 \"src/Foo/Bar.hs\" #-}" , "baz :: Int" , "baz = 3" ]