summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorJasper Van der Jeugt <jaspervdj@gmail.com>2017-12-26 14:25:59 +0100
committerGitHub <noreply@github.com>2017-12-26 14:25:59 +0100
commit02c93a9c34ff9bed0b536f5f7a92c6f802d54480 (patch)
tree76432cbd4e90c8f7601db067132b3fb3da2000fb
parent336220ca9f3e1ab9091651d60a9491386f77b7ed (diff)
downloadstylish-haskell-02c93a9c34ff9bed0b536f5f7a92c6f802d54480.tar.gz
Use file-embed for default configuration
-rw-r--r--lib/Language/Haskell/Stylish/Config.hs42
-rw-r--r--src/Main.hs9
-rw-r--r--stylish-haskell.cabal70
3 files changed, 56 insertions, 65 deletions
diff --git a/lib/Language/Haskell/Stylish/Config.hs b/lib/Language/Haskell/Stylish/Config.hs
index 8952790..f88aef0 100644
--- a/lib/Language/Haskell/Stylish/Config.hs
+++ b/lib/Language/Haskell/Stylish/Config.hs
@@ -1,9 +1,10 @@
--------------------------------------------------------------------------------
{-# LANGUAGE OverloadedStrings #-}
+{-# LANGUAGE TemplateHaskell #-}
module Language.Haskell.Stylish.Config
( Extensions
, Config (..)
- , defaultConfigFilePath
+ , defaultConfigBytes
, configFilePath
, loadConfig
) where
@@ -14,12 +15,13 @@ import Control.Monad (forM, mzero)
import Data.Aeson (FromJSON (..))
import qualified Data.Aeson as A
import qualified Data.Aeson.Types as A
-import Data.Maybe (fromMaybe)
import qualified Data.ByteString as B
+import qualified Data.FileEmbed as FileEmbed
import Data.List (inits,
intercalate)
import Data.Map (Map)
import qualified Data.Map as M
+import Data.Maybe (fromMaybe)
import Data.Yaml (decodeEither)
import System.Directory
import System.FilePath (joinPath,
@@ -38,7 +40,6 @@ import qualified Language.Haskell.Stylish.Step.Tabs as Tabs
import qualified Language.Haskell.Stylish.Step.TrailingWhitespace as TrailingWhitespace
import qualified Language.Haskell.Stylish.Step.UnicodeSyntax as UnicodeSyntax
import Language.Haskell.Stylish.Verbose
-import Paths_stylish_haskell (getDataFileName)
--------------------------------------------------------------------------------
@@ -65,27 +66,22 @@ configFileName = ".stylish-haskell.yaml"
--------------------------------------------------------------------------------
-defaultConfigFilePath :: IO FilePath
-defaultConfigFilePath = getDataFileName "data/stylish-haskell.yaml"
+defaultConfigBytes :: B.ByteString
+defaultConfigBytes = $(FileEmbed.embedFile "data/stylish-haskell.yaml")
--------------------------------------------------------------------------------
-configFilePath :: Verbose -> Maybe FilePath -> IO FilePath
-configFilePath _ (Just userSpecified) = return userSpecified
+configFilePath :: Verbose -> Maybe FilePath -> IO (Maybe FilePath)
+configFilePath _ (Just userSpecified) = return (Just userSpecified)
configFilePath verbose Nothing = do
- current <- getCurrentDirectory
- configPath <- getXdgDirectory XdgConfig "stylish-haskell"
- home <- getHomeDirectory
- def <- defaultConfigFilePath
- mbConfig <- search $
+ current <- getCurrentDirectory
+ configPath <- getXdgDirectory XdgConfig "stylish-haskell"
+ home <- getHomeDirectory
+ mbConfig <- search $
[d </> configFileName | d <- ancestors current] ++
- [configPath </> "config.yaml", home </> configFileName, def]
+ [configPath </> "config.yaml", home </> configFileName]
- case mbConfig of
- Just config -> return config
- Nothing -> fail $
- "Language.Haskell.Stylish.Config.configFilePath: " ++
- "could not load default configuration at: " ++ def
+ return mbConfig
where
-- All ancestors of a dir (including that dir)
ancestors :: FilePath -> [FilePath]
@@ -102,11 +98,11 @@ configFilePath verbose Nothing = do
--------------------------------------------------------------------------------
loadConfig :: Verbose -> Maybe FilePath -> IO Config
-loadConfig verbose mfp = do
- fp <- configFilePath verbose mfp
- verbose $ "Loading configuration at " ++ fp
- bs <- B.readFile fp
- case decodeEither bs of
+loadConfig verbose userSpecified = do
+ mbFp <- configFilePath verbose userSpecified
+ verbose $ "Loading configuration at " ++ fromMaybe "<embedded>" mbFp
+ bytes <- maybe (return defaultConfigBytes) B.readFile mbFp
+ case decodeEither bytes of
Left err -> error $
"Language.Haskell.Stylish.Config.loadConfig: " ++ err
Right config -> return config
diff --git a/src/Main.hs b/src/Main.hs
index 8eeb7ab..e71c795 100644
--- a/src/Main.hs
+++ b/src/Main.hs
@@ -6,10 +6,10 @@ module Main
--------------------------------------------------------------------------------
import Control.Monad (forM_, unless)
+import qualified Data.ByteString.Char8 as BC8
import Data.Monoid ((<>))
import Data.Version (showVersion)
import qualified Options.Applicative as OA
-import qualified Paths_stylish_haskell
import System.Exit (exitFailure)
import qualified System.IO as IO
import qualified System.IO.Strict as IO.Strict
@@ -70,7 +70,7 @@ parseStylishArgs = StylishArgs
--------------------------------------------------------------------------------
stylishHaskellVersion :: String
-stylishHaskellVersion = "stylish-haskell " <> showVersion Paths_stylish_haskell.version
+stylishHaskellVersion = "stylish-haskell " <> showVersion version
--------------------------------------------------------------------------------
@@ -94,9 +94,8 @@ stylishHaskell sa = do
putStrLn stylishHaskellVersion
else if saDefaults sa then do
- fileName <- defaultConfigFilePath
- verbose' $ "Dumping config from " ++ fileName
- readUTF8File fileName >>= putStr
+ verbose' "Dumping embedded config..."
+ BC8.putStr defaultConfigBytes
else do
conf <- loadConfig verbose' (saConfig sa)
diff --git a/stylish-haskell.cabal b/stylish-haskell.cabal
index 4938334..441af68 100644
--- a/stylish-haskell.cabal
+++ b/stylish-haskell.cabal
@@ -18,9 +18,6 @@ Description:
<https://github.com/jaspervdj/stylish-haskell/blob/master/README.markdown>
-Data-files:
- data/stylish-haskell.yaml
-
Extra-source-files:
CHANGELOG,
README.markdown
@@ -50,40 +47,39 @@ Library
Paths_stylish_haskell
Build-depends:
- aeson >= 0.6 && < 1.3,
- base >= 4.8 && < 5,
- bytestring >= 0.9 && < 0.11,
- containers >= 0.3 && < 0.6,
- directory >= 1.1 && < 1.4,
- filepath >= 1.1 && < 1.5,
- haskell-src-exts >= 1.18 && < 1.21,
- mtl >= 2.0 && < 2.3,
- syb >= 0.3 && < 0.8,
- yaml >= 0.7 && < 0.9
+ aeson >= 0.6 && < 1.3,
+ base >= 4.8 && < 5,
+ bytestring >= 0.9 && < 0.11,
+ containers >= 0.3 && < 0.6,
+ directory >= 1.1 && < 1.4,
+ filepath >= 1.1 && < 1.5,
+ file-embed >= 0.0.10 && < 0.1,
+ haskell-src-exts >= 1.18 && < 1.21,
+ mtl >= 2.0 && < 2.3,
+ syb >= 0.3 && < 0.8,
+ yaml >= 0.7 && < 0.9
Executable stylish-haskell
Ghc-options: -Wall
Hs-source-dirs: src
Main-is: Main.hs
- Other-modules:
- Paths_stylish_haskell
-
Build-depends:
stylish-haskell,
strict >= 0.3 && < 0.4,
optparse-applicative >= 0.12 && < 0.15,
-- Copied from regular dependencies...
- aeson >= 0.6 && < 1.3,
- base >= 4.8 && < 5,
- bytestring >= 0.9 && < 0.11,
- containers >= 0.3 && < 0.6,
- directory >= 1.1 && < 1.4,
- filepath >= 1.1 && < 1.5,
- haskell-src-exts >= 1.18 && < 1.21,
- mtl >= 2.0 && < 2.3,
- syb >= 0.3 && < 0.8,
- yaml >= 0.7 && < 0.9
+ aeson >= 0.6 && < 1.3,
+ base >= 4.8 && < 5,
+ bytestring >= 0.9 && < 0.11,
+ containers >= 0.3 && < 0.6,
+ directory >= 1.1 && < 1.4,
+ filepath >= 1.1 && < 1.5,
+ file-embed >= 0.0.10 && < 0.1,
+ haskell-src-exts >= 1.18 && < 1.21,
+ mtl >= 2.0 && < 2.3,
+ syb >= 0.3 && < 0.8,
+ yaml >= 0.7 && < 0.9
Test-suite stylish-haskell-tests
Ghc-options: -Wall
@@ -114,23 +110,23 @@ Test-suite stylish-haskell-tests
Language.Haskell.Stylish.Tests.Util
Language.Haskell.Stylish.Util
Language.Haskell.Stylish.Verbose
- Paths_stylish_haskell
Build-depends:
HUnit >= 1.2 && < 1.7,
test-framework >= 0.4 && < 0.9,
test-framework-hunit >= 0.2 && < 0.4,
-- Copied from regular dependencies...
- aeson >= 0.6 && < 1.3,
- base >= 4.8 && < 5,
- bytestring >= 0.9 && < 0.11,
- containers >= 0.3 && < 0.6,
- directory >= 1.2.3 && < 1.4,
- filepath >= 1.1 && < 1.5,
- haskell-src-exts >= 1.18 && < 1.21,
- mtl >= 2.0 && < 2.3,
- syb >= 0.3 && < 0.8,
- yaml >= 0.7 && < 0.9
+ aeson >= 0.6 && < 1.3,
+ base >= 4.8 && < 5,
+ bytestring >= 0.9 && < 0.11,
+ containers >= 0.3 && < 0.6,
+ directory >= 1.1 && < 1.4,
+ filepath >= 1.1 && < 1.5,
+ file-embed >= 0.0.10 && < 0.1,
+ haskell-src-exts >= 1.18 && < 1.21,
+ mtl >= 2.0 && < 2.3,
+ syb >= 0.3 && < 0.8,
+ yaml >= 0.7 && < 0.9
Source-repository head
Type: git