Skip to content

Commit

Permalink
Embed Files
Browse files Browse the repository at this point in the history
  • Loading branch information
Avi-D-coder committed Jan 4, 2020
1 parent 2bb95c8 commit b50d9cf
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 13 deletions.
3 changes: 3 additions & 0 deletions hlint.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -100,6 +102,7 @@ library
Refact
Timing
CC
EmbedData
Config.Compute
Config.Haskell
Config.Read
Expand Down
7 changes: 3 additions & 4 deletions src/CmdLine.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
3 changes: 2 additions & 1 deletion src/Config/Read.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions src/EmbedData.hs
Original file line number Diff line number Diff line change
@@ -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")
7 changes: 3 additions & 4 deletions src/HLint.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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
Expand Down
6 changes: 2 additions & 4 deletions src/Report.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit b50d9cf

Please sign in to comment.