Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow using HsYAML instead of yaml #1050

Merged
merged 5 commits into from
Jun 14, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .hlint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
- name: [PackageImports]
- name: [ConstraintKinds, RankNTypes, TypeFamilies]
- name: [TemplateHaskell]
- {name: CPP, within: [HsColour]} # so it can be disabled to avoid GPL code
- {name: CPP, within: [HsColour, Config.Yaml, Test.Annotations]} # so it can be disabled to avoid GPL code

- flags:
- default: false
Expand Down
2 changes: 1 addition & 1 deletion .weeder.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@

- message:
- name: Redundant build-depends entry
- depends: [ghc-lib-parser, ghc, ghc-boot-th, ghc-boot] # Only some subset is required
- depends: [ghc-lib-parser, ghc, ghc-boot-th, ghc-boot, HsYAML, HsYAML-aeson, yaml] # Only some subset is required
14 changes: 13 additions & 1 deletion hlint.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ flag ghc-lib
manual: True
description: Force dependency on ghc-lib-parser even if GHC API in the ghc package is supported

flag hsyaml
default: False
manual: True
description: Use HsYAML instead of yaml

library
default-language: Haskell2010
build-depends:
Expand All @@ -59,7 +64,6 @@ library
data-default >= 0.3,
cpphs >= 1.20.1,
cmdargs >= 0.10,
yaml >= 0.5.0,
uniplate >= 1.5,
ansi-terminal >= 0.6.2,
extra >= 1.7.3,
Expand All @@ -83,6 +87,14 @@ library
else
cpp-options: -DGPL_SCARES_ME

if flag(hsyaml) && flag(gpl)
vaibhavsagar marked this conversation as resolved.
Show resolved Hide resolved
build-depends:
HsYAML >= 0.2,
HsYAML-aeson >= 0.2
cpp-options: -DHS_YAML
else
build-depends: yaml >= 0.5.0

hs-source-dirs: src
exposed-modules:
Language.Haskell.HLint
Expand Down
30 changes: 28 additions & 2 deletions src/Config/Yaml.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE OverloadedStrings, ViewPatterns, RecordWildCards, GeneralizedNewtypeDeriving, TupleSections #-}
{-# LANGUAGE CPP #-}

module Config.Yaml(
ConfigYaml,
Expand All @@ -8,13 +9,11 @@ module Config.Yaml(
) where

import Config.Type
import Data.Yaml
import Data.Either
import Data.Maybe
import Data.List.Extra
import Data.Tuple.Extra
import Control.Monad.Extra
import Control.Exception.Extra
import qualified Data.Text as T
import qualified Data.Vector as V
import qualified Data.ByteString.Char8 as BS
Expand Down Expand Up @@ -42,6 +41,33 @@ import Language.Haskell.GhclibParserEx.GHC.Hs.ExtendInstances
import Language.Haskell.GhclibParserEx.GHC.Types.Name.Reader
import Data.Char

#ifdef HS_YAML

import Data.YAML (Pos)
import Data.YAML.Aeson (encode1Strict, decode1Strict)
import Data.Aeson hiding (encode)
import Data.Aeson.Types (Parser)
import qualified Data.ByteString as BSS

decodeFileEither :: FilePath -> IO (Either (Pos, String) ConfigYaml)
decodeFileEither path = decode1Strict <$> BSS.readFile path

decodeEither' :: BSS.ByteString -> Either (Pos, String) ConfigYaml
decodeEither' = decode1Strict

displayException :: (Pos, String) -> String
displayException = show

encode :: Value -> BSS.ByteString
encode = encode1Strict

#else

import Data.Yaml
import Control.Exception.Extra

#endif


-- | Read a config file in YAML format. Takes a filename, and optionally the contents.
-- Fails if the YAML doesn't parse or isn't valid HLint YAML
Expand Down
18 changes: 16 additions & 2 deletions src/Test/Annotations.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{-# LANGUAGE PatternGuards, RecordWildCards, ViewPatterns #-}
{-# LANGUAGE CPP, PatternGuards, RecordWildCards, ViewPatterns #-}

-- | Check the <TEST> annotations within source and hint files.
module Test.Annotations(testAnnotations) where
Expand All @@ -13,7 +13,6 @@ import Data.Functor
import Data.List.Extra
import Data.Maybe
import Data.Tuple.Extra
import Data.Yaml
import System.Exit
import System.FilePath
import System.IO.Extra
Expand All @@ -34,6 +33,21 @@ import GHC.Util
import SrcLoc
import Language.Haskell.GhclibParserEx.GHC.Utils.Outputable

#ifdef HS_YAML

import Data.YAML.Aeson (decode1Strict)
import Data.YAML (Pos)
import Data.ByteString (ByteString)

decodeEither' :: ByteString -> Either (Pos, String) ConfigYaml
decodeEither' = decode1Strict

#else

import Data.Yaml

#endif

-- Input, Output
-- Output = Nothing, should not match
-- Output = Just xs, should match xs
Expand Down