From b50d9cf08e409d4ca20abf37fb541baebff60391 Mon Sep 17 00:00:00 2001 From: Avi Dessauer Date: Sat, 4 Jan 2020 05:48:29 -0500 Subject: [PATCH] Embed Files --- hlint.cabal | 3 +++ src/CmdLine.hs | 7 +++---- src/Config/Read.hs | 3 ++- src/EmbedData.hs | 20 ++++++++++++++++++++ src/HLint.hs | 7 +++---- src/Report.hs | 6 ++---- 6 files changed, 33 insertions(+), 13 deletions(-) create mode 100644 src/EmbedData.hs diff --git a/hlint.cabal b/hlint.cabal index aeb7729e4..f0151d337 100644 --- a/hlint.cabal +++ b/hlint.cabal @@ -54,6 +54,8 @@ library base == 4.*, process, filepath, directory, containers, unordered-containers, vector, text, bytestring, transformers, + file-embed, + utf8-string, data-default >= 0.3, cpphs >= 1.20.1, cmdargs >= 0.10, @@ -100,6 +102,7 @@ library Refact Timing CC + EmbedData Config.Compute Config.Haskell Config.Read diff --git a/src/CmdLine.hs b/src/CmdLine.hs index effa70f84..f18e04296 100644 --- a/src/CmdLine.hs +++ b/src/CmdLine.hs @@ -217,12 +217,11 @@ mode = cmdArgsMode $ modes -- We want more important hints to go last, since they override cmdHintFiles :: Cmd -> IO [FilePath] cmdHintFiles cmd = do - let explicit1 = [cmdDataDir cmd "hlint.yaml" | null $ cmdWithHints cmd] let explicit2 = cmdGivenHints cmd - bad <- filterM (notM . doesFileExist) $ explicit1 ++ explicit2 + bad <- filterM (notM . doesFileExist) explicit2 when (bad /= []) $ fail $ unlines $ "Failed to find requested hint files:" : map (" "++) bad - if cmdWithHints cmd /= [] then return $ explicit1 ++ explicit2 else do + if cmdWithHints cmd /= [] then return explicit2 else do -- we follow the stylish-haskell config file search policy -- 1) current directory or its ancestors; 2) home directory curdir <- getCurrentDirectory @@ -231,7 +230,7 @@ cmdHintFiles cmd = do implicit <- findM doesFileExist $ map ( ".hlint.yaml") (ancestors curdir ++ home) -- to match Stylish Haskell ++ ["HLint.hs"] -- the default in HLint 1.* - return $ explicit1 ++ maybeToList implicit ++ explicit2 + return $ maybeToList implicit ++ explicit2 where ancestors = init . map joinPath . reverse . inits . splitPath diff --git a/src/Config/Read.hs b/src/Config/Read.hs index 9f3cec9e7..b3f3c4d5c 100644 --- a/src/Config/Read.hs +++ b/src/Config/Read.hs @@ -6,11 +6,12 @@ import Config.Haskell import Config.Yaml import Data.List.Extra import System.FilePath +import EmbedData readFilesConfig :: [(FilePath, Maybe String)] -> IO [Setting] readFilesConfig files = do - yaml <- mapM (uncurry readFileConfigYaml) yaml + yaml <- mapM (uncurry readFileConfigYaml) (hlintYaml : yaml) haskell <- mapM (uncurry readFileConfigHaskell) haskell return $ concat haskell ++ settingsFromConfigYaml yaml where diff --git a/src/EmbedData.hs b/src/EmbedData.hs new file mode 100644 index 000000000..89d69320b --- /dev/null +++ b/src/EmbedData.hs @@ -0,0 +1,20 @@ +{-# LANGUAGE TemplateHaskell #-} + +module EmbedData + ( hlintYaml, + defaultYaml, + reportTemplate, + ) +where + +import Data.ByteString.UTF8 +import Data.FileEmbed + +hlintYaml :: (FilePath, Maybe String) +hlintYaml = ("data/hlint.yaml", Just $ toString $(embedFile "data/hlint.yaml")) + +defaultYaml :: String +defaultYaml = toString $(embedFile "data/default.yaml") + +reportTemplate :: String +reportTemplate = toString $(embedFile "data/report_template.html") diff --git a/src/HLint.hs b/src/HLint.hs index a467fa535..8b33f69d0 100644 --- a/src/HLint.hs +++ b/src/HLint.hs @@ -20,7 +20,6 @@ import Data.Version.Extra import System.Process.Extra import Data.Maybe import System.Directory -import System.FilePath import CmdLine import Config.Read @@ -37,6 +36,7 @@ import Test.Proof import Parallel import HSE.All import CC +import EmbedData -- | This function takes a list of command line arguments, and returns the given hints. @@ -119,9 +119,8 @@ hlintMain args cmd@CmdMain{..} ideas <- if null cmdFiles then return [] else withVerbosity Quiet $ runHlintMain args cmd{cmdJson=False,cmdSerialise=False,cmdRefactor=False} Nothing let bad = nubOrd $ map ideaHint ideas - src <- readFile $ cmdDataDir "default.yaml" - if null bad then putStr src else do - let group1:groups = splitOn ["",""] $ lines src + if null bad then putStr defaultYaml else do + let group1:groups = splitOn ["",""] $ lines defaultYaml let group2 = "# Warnings currently triggered by your code" : ["- ignore: {name: " ++ show x ++ "}" | x <- bad] putStr $ unlines $ intercalate ["",""] $ group1:group2:groups diff --git a/src/Report.hs b/src/Report.hs index 25ad6eac9..b1ea93d27 100644 --- a/src/Report.hs +++ b/src/Report.hs @@ -7,18 +7,16 @@ import Data.Tuple.Extra import Data.List.Extra import Data.Maybe import Data.Version -import System.FilePath -import System.IO.Extra import HSE.All import Timing import Paths_hlint import HsColour +import EmbedData writeTemplate :: FilePath -> [(String,[String])] -> FilePath -> IO () writeTemplate dataDir content to = do - src <- readFile' $ dataDir "report_template.html" - writeFile to $ unlines $ concatMap f $ lines src + writeFile to $ unlines $ concatMap f $ lines reportTemplate where f ('$':xs) = fromMaybe ['$':xs] $ lookup xs content f x = [x]