module LHMake.Data.Feature (
FeatureName, ModuleName, Feature (..), feature,
Registry, registry,
FeatureSet, resolve, superimpose,
list, listAll, listDeps,
combos, generate, generateCombos,
Dependency (..),
(/\), (\/),
cnf, dnf
) where
import Control.Monad (forM, forM_, unless, when)
import Data.Bifunctor (first)
import Data.Functor (void)
import Data.List (intercalate)
import Data.Map (Map)
import Data.Maybe (maybeToList)
import Data.Set (Set)
import System.Directory (createDirectoryIfMissing, doesFileExist)
import System.Exit (die)
import System.FilePath (takeDirectory, (</>), (<.>))
import System.IO (readFile')
import Happy.Frontend (parseYFileContents)
import Happy.Frontend.AbsSyn
import qualified Language.Haskell.Exts as H
import qualified Data.Graph as Graph
import qualified Data.Map as Map
import qualified Data.Set as Set
import LHMake.Control.Superimposition ((-->), parseMode)
import LHMake.Data.Error (LHsMakeError)
import LHMake.Text.Happy.Pretty (pprHappy)
import LHMake.Text.Label (infoLabel, errLabel)
type FeatureName = String
type ModuleName = String
data Feature = Feature
{ Feature -> FeatureName
descrip :: String
, Feature -> [FeatureName]
modules :: [FilePath]
, Feature -> Maybe FeatureName
grammar :: Maybe FilePath
, Feature -> Set (Set FeatureName)
dependsOn :: Set (Set FeatureName)
, Feature -> Set (Set FeatureName)
impliedBy :: Set (Set FeatureName)
, Feature -> Bool
hidden :: Bool
}
feature
:: String
-> [FilePath]
-> Maybe FilePath
-> Dependency
-> Dependency
-> Bool
-> Feature
feature :: FeatureName
-> [FeatureName]
-> Maybe FeatureName
-> Dependency
-> Dependency
-> Bool
-> Feature
feature FeatureName
desc [FeatureName]
modls Maybe FeatureName
gram Dependency
depends Dependency
implied Bool
hide =
FeatureName
-> [FeatureName]
-> Maybe FeatureName
-> Set (Set FeatureName)
-> Set (Set FeatureName)
-> Bool
-> Feature
Feature FeatureName
desc ((FeatureName -> FeatureName) -> [FeatureName] -> [FeatureName]
forall a b. (a -> b) -> [a] -> [b]
map FeatureName -> FeatureName
qualify [FeatureName]
modls) ((FeatureName -> FeatureName)
-> Maybe FeatureName -> Maybe FeatureName
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap FeatureName -> FeatureName
qualify Maybe FeatureName
gram) (Dependency -> Set (Set FeatureName)
cnf Dependency
depends) (Dependency -> Set (Set FeatureName)
dnf Dependency
implied) Bool
hide
where qualify :: FeatureName -> FeatureName
qualify FeatureName
file = FeatureName
"LayeredHaskell-make" FeatureName -> FeatureName -> FeatureName
</> FeatureName
"src" FeatureName -> FeatureName -> FeatureName
</> FeatureName
"LHMake" FeatureName -> FeatureName -> FeatureName
</> FeatureName
"Feature" FeatureName -> FeatureName -> FeatureName
</> FeatureName
file
type Registry = Map FeatureName Feature
registry :: Registry
registry :: Registry
registry = [(FeatureName, Feature)] -> Registry
forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList
[ (FeatureName
"E", FeatureName
-> [FeatureName]
-> Maybe FeatureName
-> Dependency
-> Dependency
-> Bool
-> Feature
feature FeatureName
"Expressions"
[FeatureName
"E" FeatureName -> FeatureName -> FeatureName
</> FeatureName
"AbsSyn" FeatureName -> FeatureName -> FeatureName
<.> FeatureName
"hs"]
(FeatureName -> Maybe FeatureName
forall a. a -> Maybe a
Just (FeatureName
"E" FeatureName -> FeatureName -> FeatureName
</> FeatureName
"Parser" FeatureName -> FeatureName -> FeatureName
<.> FeatureName
"y"))
Dependency
None
Dependency
None
Bool
True)
, (FeatureName
"B", FeatureName
-> [FeatureName]
-> Maybe FeatureName
-> Dependency
-> Dependency
-> Bool
-> Feature
feature FeatureName
"Basic types"
[FeatureName
"B" FeatureName -> FeatureName -> FeatureName
</> FeatureName
"AbsSyn" FeatureName -> FeatureName -> FeatureName
<.> FeatureName
"hs"]
(FeatureName -> Maybe FeatureName
forall a. a -> Maybe a
Just (FeatureName
"B" FeatureName -> FeatureName -> FeatureName
</> FeatureName
"Parser" FeatureName -> FeatureName -> FeatureName
<.> FeatureName
"y"))
(FeatureName -> Dependency
Has FeatureName
"E")
Dependency
None
Bool
False)
, (FeatureName
"D", FeatureName
-> [FeatureName]
-> Maybe FeatureName
-> Dependency
-> Dependency
-> Bool
-> Feature
feature FeatureName
"Monomorphic algebraic datatypes"
[FeatureName
"D" FeatureName -> FeatureName -> FeatureName
</> FeatureName
"AbsSyn" FeatureName -> FeatureName -> FeatureName
<.> FeatureName
"hs"]
(FeatureName -> Maybe FeatureName
forall a. a -> Maybe a
Just (FeatureName
"D" FeatureName -> FeatureName -> FeatureName
</> FeatureName
"Parser" FeatureName -> FeatureName -> FeatureName
<.> FeatureName
"y"))
(FeatureName -> Dependency
Has FeatureName
"E")
Dependency
None
Bool
False)
, (FeatureName
"F", FeatureName
-> [FeatureName]
-> Maybe FeatureName
-> Dependency
-> Dependency
-> Bool
-> Feature
feature FeatureName
"Functions"
[FeatureName
"F" FeatureName -> FeatureName -> FeatureName
</> FeatureName
"AbsSyn" FeatureName -> FeatureName -> FeatureName
<.> FeatureName
"hs"]
(FeatureName -> Maybe FeatureName
forall a. a -> Maybe a
Just (FeatureName
"F" FeatureName -> FeatureName -> FeatureName
</> FeatureName
"Parser" FeatureName -> FeatureName -> FeatureName
<.> FeatureName
"y"))
(FeatureName -> Dependency
Has FeatureName
"B" Dependency -> Dependency -> Dependency
\/ FeatureName -> Dependency
Has FeatureName
"D")
Dependency
None
Bool
False)
, (FeatureName
"C", FeatureName
-> [FeatureName]
-> Maybe FeatureName
-> Dependency
-> Dependency
-> Bool
-> Feature
feature FeatureName
"Case expressions"
[FeatureName
"C" FeatureName -> FeatureName -> FeatureName
</> FeatureName
"AbsSyn" FeatureName -> FeatureName -> FeatureName
<.> FeatureName
"hs"]
(FeatureName -> Maybe FeatureName
forall a. a -> Maybe a
Just (FeatureName
"C" FeatureName -> FeatureName -> FeatureName
</> FeatureName
"Parser" FeatureName -> FeatureName -> FeatureName
<.> FeatureName
"y"))
Dependency
None
(FeatureName -> Dependency
Has FeatureName
"D" Dependency -> Dependency -> Dependency
/\ FeatureName -> Dependency
Has FeatureName
"F")
Bool
True)
, (FeatureName
"P", FeatureName
-> [FeatureName]
-> Maybe FeatureName
-> Dependency
-> Dependency
-> Bool
-> Feature
feature FeatureName
"Prenex polymorphism"
[FeatureName
"P" FeatureName -> FeatureName -> FeatureName
</> FeatureName
"AbsSyn" FeatureName -> FeatureName -> FeatureName
<.> FeatureName
"hs"]
(FeatureName -> Maybe FeatureName
forall a. a -> Maybe a
Just (FeatureName
"P" FeatureName -> FeatureName -> FeatureName
</> FeatureName
"Parser" FeatureName -> FeatureName -> FeatureName
<.> FeatureName
"y"))
(FeatureName -> Dependency
Has FeatureName
"D")
Dependency
None
Bool
False)
, (FeatureName
"L", FeatureName
-> [FeatureName]
-> Maybe FeatureName
-> Dependency
-> Dependency
-> Bool
-> Feature
feature FeatureName
"List and tuple syntax"
[FeatureName
"L" FeatureName -> FeatureName -> FeatureName
</> FeatureName
"AbsSyn" FeatureName -> FeatureName -> FeatureName
<.> FeatureName
"hs"]
(FeatureName -> Maybe FeatureName
forall a. a -> Maybe a
Just (FeatureName
"L" FeatureName -> FeatureName -> FeatureName
</> FeatureName
"Parser" FeatureName -> FeatureName -> FeatureName
<.> FeatureName
"y"))
(FeatureName -> Dependency
Has FeatureName
"D" Dependency -> Dependency -> Dependency
/\ FeatureName -> Dependency
Has FeatureName
"F")
Dependency
None
Bool
False)
]
type FeatureSet = Set FeatureName
select :: Registry -> FeatureSet -> FeatureSet
select :: Registry -> Set FeatureName -> Set FeatureName
select Registry
reg Set FeatureName
names
| Set FeatureName -> Int
forall a. Set a -> Int
Set.size Set FeatureName
added Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Set FeatureName -> Int
forall a. Set a -> Int
Set.size Set FeatureName
names = Set FeatureName
names
| Bool
otherwise = Registry -> Set FeatureName -> Set FeatureName
select Registry
reg Set FeatureName
added
where
added :: Set FeatureName
added = [Set FeatureName] -> Set FeatureName
forall (f :: * -> *) a. (Foldable f, Ord a) => f (Set a) -> Set a
Set.unions [Set FeatureName
names, Set FeatureName
dependencies, Set FeatureName
implications]
dependencies :: Set FeatureName
dependencies = (Feature -> Set FeatureName) -> Registry -> Set FeatureName
forall m a. Monoid m => (a -> m) -> Map FeatureName a -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap (Set (Set FeatureName) -> Set FeatureName
forall (f :: * -> *) a. (Foldable f, Ord a) => f (Set a) -> Set a
Set.unions (Set (Set FeatureName) -> Set FeatureName)
-> (Feature -> Set (Set FeatureName)) -> Feature -> Set FeatureName
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Set FeatureName -> Bool)
-> Set (Set FeatureName) -> Set (Set FeatureName)
forall a. (a -> Bool) -> Set a -> Set a
Set.filter ((Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
1) (Int -> Bool)
-> (Set FeatureName -> Int) -> Set FeatureName -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Set FeatureName -> Int
forall a. Set a -> Int
Set.size) (Set (Set FeatureName) -> Set (Set FeatureName))
-> (Feature -> Set (Set FeatureName))
-> Feature
-> Set (Set FeatureName)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Feature -> Set (Set FeatureName)
dependsOn)
(Registry -> Set FeatureName -> Registry
forall k a. Ord k => Map k a -> Set k -> Map k a
Map.restrictKeys Registry
reg Set FeatureName
names)
implications :: Set FeatureName
implications = Registry -> Set FeatureName
forall k a. Map k a -> Set k
Map.keysSet ((Feature -> Bool) -> Registry -> Registry
forall a k. (a -> Bool) -> Map k a -> Map k a
Map.filter ((Set FeatureName -> Bool) -> Set (Set FeatureName) -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any (Set FeatureName -> Set FeatureName -> Bool
forall a. Ord a => Set a -> Set a -> Bool
`Set.isSubsetOf` Set FeatureName
names) (Set (Set FeatureName) -> Bool)
-> (Feature -> Set (Set FeatureName)) -> Feature -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Feature -> Set (Set FeatureName)
impliedBy) Registry
reg)
order :: Registry -> FeatureSet -> LHsMakeError [FeatureName]
order :: Registry -> Set FeatureName -> LHsMakeError [FeatureName]
order Registry
reg Set FeatureName
names = ([FeatureName] -> [FeatureName])
-> LHsMakeError [FeatureName] -> LHsMakeError [FeatureName]
forall a b.
(a -> b) -> Either FeatureName a -> Either FeatureName b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [FeatureName] -> [FeatureName]
forall a. [a] -> [a]
reverse ((SCC FeatureName -> Either FeatureName FeatureName)
-> [SCC FeatureName] -> LHsMakeError [FeatureName]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> [a] -> f [b]
traverse SCC FeatureName -> Either FeatureName FeatureName
acyclic ([(FeatureName, FeatureName, [FeatureName])] -> [SCC FeatureName]
forall key node. Ord key => [(node, key, [key])] -> [SCC node]
Graph.stronglyConnComp [(FeatureName, FeatureName, [FeatureName])]
nodes))
where
nodes :: [(FeatureName, FeatureName, [FeatureName])]
nodes = [ (FeatureName
name, FeatureName
name, Set FeatureName -> [FeatureName]
forall a. Set a -> [a]
Set.toList (Set (Set FeatureName) -> Set FeatureName
forall (f :: * -> *) a. (Foldable f, Ord a) => f (Set a) -> Set a
Set.unions (Feature -> Set (Set FeatureName)
dependsOn Feature
feat Set (Set FeatureName)
-> Set (Set FeatureName) -> Set (Set FeatureName)
forall a. Semigroup a => a -> a -> a
<> Feature -> Set (Set FeatureName)
impliedBy Feature
feat)))
| (FeatureName
name, Feature
feat) <- Registry -> [(FeatureName, Feature)]
forall k a. Map k a -> [(k, a)]
Map.toList (Registry -> Set FeatureName -> Registry
forall k a. Ord k => Map k a -> Set k -> Map k a
Map.restrictKeys Registry
reg Set FeatureName
names) ]
acyclic :: SCC FeatureName -> Either FeatureName FeatureName
acyclic (Graph.AcyclicSCC FeatureName
name) = FeatureName -> Either FeatureName FeatureName
forall a b. b -> Either a b
Right FeatureName
name
acyclic (Graph.CyclicSCC [FeatureName]
members) = FeatureName -> Either FeatureName FeatureName
forall a b. a -> Either a b
Left (FeatureName
"These features have cyclic dependencies: " FeatureName -> FeatureName -> FeatureName
forall a. [a] -> [a] -> [a]
++ [FeatureName] -> FeatureName
unwords [FeatureName]
members)
unknown :: Registry -> FeatureSet -> [String]
unknown :: Registry -> Set FeatureName -> [FeatureName]
unknown Registry
reg Set FeatureName
names =
[ FeatureName
"Unknown feature: " FeatureName -> FeatureName -> FeatureName
forall a. [a] -> [a] -> [a]
++ FeatureName
name
| FeatureName
name <- Set FeatureName -> [FeatureName]
forall a. Set a -> [a]
Set.toList ((FeatureName -> Bool) -> Set FeatureName -> Set FeatureName
forall a. (a -> Bool) -> Set a -> Set a
Set.filter (FeatureName -> Registry -> Bool
forall k a. Ord k => k -> Map k a -> Bool
`Map.notMember` Registry
reg) Set FeatureName
names) ]
check :: Registry -> FeatureSet -> [String]
check :: Registry -> Set FeatureName -> [FeatureName]
check Registry
reg Set FeatureName
names =
Registry -> Set FeatureName -> [FeatureName]
unknown Registry
reg Set FeatureName
names [FeatureName] -> [FeatureName] -> [FeatureName]
forall a. [a] -> [a] -> [a]
++
[ FeatureName
"Can't automatically resolve dependencies of " FeatureName -> FeatureName -> FeatureName
forall a. [a] -> [a] -> [a]
++ FeatureName
name FeatureName -> FeatureName -> FeatureName
forall a. [a] -> [a] -> [a]
++ FeatureName
", explicitly enable one of: " FeatureName -> FeatureName -> FeatureName
forall a. [a] -> [a] -> [a]
++ [FeatureName] -> FeatureName
unwords (Set FeatureName -> [FeatureName]
forall a. Set a -> [a]
Set.toList Set FeatureName
clause)
| (FeatureName
name, Feature
feat) <- Registry -> [(FeatureName, Feature)]
forall k a. Map k a -> [(k, a)]
Map.toList (Registry -> Set FeatureName -> Registry
forall k a. Ord k => Map k a -> Set k -> Map k a
Map.restrictKeys Registry
reg Set FeatureName
names)
, Set FeatureName
clause <- Set (Set FeatureName) -> [Set FeatureName]
forall a. Set a -> [a]
Set.toList (Feature -> Set (Set FeatureName)
dependsOn Feature
feat)
, Set FeatureName -> Set FeatureName -> Bool
forall a. Ord a => Set a -> Set a -> Bool
Set.disjoint Set FeatureName
clause Set FeatureName
names ]
resolve :: Registry -> FeatureSet -> LHsMakeError [FeatureName]
resolve :: Registry -> Set FeatureName -> LHsMakeError [FeatureName]
resolve Registry
reg Set FeatureName
names = do
let hide :: [FeatureName]
hide = Registry -> [FeatureName]
forall k a. Map k a -> [k]
Map.keys ((Feature -> Bool) -> Registry -> Registry
forall a k. (a -> Bool) -> Map k a -> Map k a
Map.filter Feature -> Bool
hidden (Registry -> Set FeatureName -> Registry
forall k a. Ord k => Map k a -> Set k -> Map k a
Map.restrictKeys Registry
reg Set FeatureName
names))
selected :: Set FeatureName
selected = Registry -> Set FeatureName -> Set FeatureName
select Registry
reg Set FeatureName
names
errs :: [FeatureName]
errs = Registry -> Set FeatureName -> [FeatureName]
check Registry
reg Set FeatureName
selected
Bool -> Either FeatureName () -> Either FeatureName ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless ([FeatureName] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [FeatureName]
hide) (Either FeatureName () -> Either FeatureName ())
-> Either FeatureName () -> Either FeatureName ()
forall a b. (a -> b) -> a -> b
$
FeatureName -> Either FeatureName ()
forall a b. a -> Either a b
Left (FeatureName
"Hidden features cannot be selected directly: " FeatureName -> FeatureName -> FeatureName
forall a. [a] -> [a] -> [a]
++ [FeatureName] -> FeatureName
unwords [FeatureName]
hide)
Bool -> Either FeatureName () -> Either FeatureName ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless ([FeatureName] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [FeatureName]
errs) (Either FeatureName () -> Either FeatureName ())
-> Either FeatureName () -> Either FeatureName ()
forall a b. (a -> b) -> a -> b
$
FeatureName -> Either FeatureName ()
forall a b. a -> Either a b
Left (FeatureName -> [FeatureName] -> FeatureName
forall a. [a] -> [[a]] -> [a]
intercalate FeatureName
"\n" [FeatureName]
errs)
Registry -> Set FeatureName -> LHsMakeError [FeatureName]
order Registry
reg Set FeatureName
selected
load :: Feature -> IO (LHsMakeError (Map ModuleName (H.Module ()), Map ModuleName BookendedAbsSyn))
load :: Feature
-> IO
(LHsMakeError
(Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn))
load Feature
feat = do
[(FeatureName, FeatureName)]
haskellSrcs <- (FeatureName -> IO (FeatureName, FeatureName))
-> [FeatureName] -> IO [(FeatureName, FeatureName)]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> [a] -> f [b]
traverse FeatureName -> IO (FeatureName, FeatureName)
loadFileWithPath (Feature -> [FeatureName]
modules Feature
feat)
[(FeatureName, FeatureName)]
happySrcs <- (FeatureName -> IO (FeatureName, FeatureName))
-> [FeatureName] -> IO [(FeatureName, FeatureName)]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> [a] -> f [b]
traverse FeatureName -> IO (FeatureName, FeatureName)
loadFileWithPath (Maybe FeatureName -> [FeatureName]
forall a. Maybe a -> [a]
maybeToList (Feature -> Maybe FeatureName
grammar Feature
feat))
LHsMakeError
(Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn)
-> IO
(LHsMakeError
(Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn))
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (LHsMakeError
(Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn)
-> IO
(LHsMakeError
(Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn)))
-> LHsMakeError
(Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn)
-> IO
(LHsMakeError
(Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn))
forall a b. (a -> b) -> a -> b
$ do
[(FeatureName, Module ())]
haskell <- [(FeatureName, FeatureName)]
-> ((FeatureName, FeatureName)
-> Either FeatureName (FeatureName, Module ()))
-> Either FeatureName [(FeatureName, Module ())]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
forM [(FeatureName, FeatureName)]
haskellSrcs (\(FeatureName
file, FeatureName
src) -> do
Module ()
modl <- FeatureName -> FeatureName -> Either FeatureName (Module ())
parse FeatureName
file FeatureName
src
FeatureName
name <- FeatureName -> Module () -> Either FeatureName FeatureName
forall {l}.
FeatureName -> Module l -> Either FeatureName FeatureName
moduleName FeatureName
file Module ()
modl
(FeatureName, Module ())
-> Either FeatureName (FeatureName, Module ())
forall a. a -> Either FeatureName a
forall (m :: * -> *) a. Monad m => a -> m a
return (FeatureName
name, Module ()
modl))
[(FeatureName, BookendedAbsSyn)]
happy <- [(FeatureName, FeatureName)]
-> ((FeatureName, FeatureName)
-> Either FeatureName (FeatureName, BookendedAbsSyn))
-> Either FeatureName [(FeatureName, BookendedAbsSyn)]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
forM [(FeatureName, FeatureName)]
happySrcs (\(FeatureName
file, FeatureName
src) -> do
BookendedAbsSyn
gram <- (FeatureName -> FeatureName)
-> Either FeatureName BookendedAbsSyn
-> Either FeatureName BookendedAbsSyn
forall a b c. (a -> b) -> Either a c -> Either b c
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first ((FeatureName
file FeatureName -> FeatureName -> FeatureName
forall a. [a] -> [a] -> [a]
++ FeatureName
": ") FeatureName -> FeatureName -> FeatureName
forall a. [a] -> [a] -> [a]
++) (FeatureName -> Either FeatureName BookendedAbsSyn
parseYFileContents FeatureName
src)
FeatureName
header <- case BookendedAbsSyn
gram of
BookendedAbsSyn Maybe FeatureName
_ (Just FeatureName
header) AbsSyn FeatureName
_ Maybe FeatureName
_ -> FeatureName -> Either FeatureName FeatureName
forall a b. b -> Either a b
Right FeatureName
header
BookendedAbsSyn
_ -> FeatureName -> Either FeatureName FeatureName
forall a b. a -> Either a b
Left (FeatureName
file FeatureName -> FeatureName -> FeatureName
forall a. [a] -> [a] -> [a]
++ FeatureName
": The grammar header is empty, so I can't identify it")
Module ()
modl <- FeatureName -> FeatureName -> Either FeatureName (Module ())
parse FeatureName
file FeatureName
header
FeatureName
name <- FeatureName -> Module () -> Either FeatureName FeatureName
forall {l}.
FeatureName -> Module l -> Either FeatureName FeatureName
moduleName FeatureName
file Module ()
modl
(FeatureName, BookendedAbsSyn)
-> Either FeatureName (FeatureName, BookendedAbsSyn)
forall a. a -> Either FeatureName a
forall (m :: * -> *) a. Monad m => a -> m a
return (FeatureName
name, BookendedAbsSyn
gram))
(Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn)
-> LHsMakeError
(Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn)
forall a. a -> Either FeatureName a
forall (m :: * -> *) a. Monad m => a -> m a
return ([(FeatureName, Module ())] -> Map FeatureName (Module ())
forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList [(FeatureName, Module ())]
haskell, [(FeatureName, BookendedAbsSyn)] -> Map FeatureName BookendedAbsSyn
forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList [(FeatureName, BookendedAbsSyn)]
happy)
where
loadFileWithPath :: FeatureName -> IO (FeatureName, FeatureName)
loadFileWithPath FeatureName
file = do
FeatureName
src <- FeatureName -> IO FeatureName
readFile FeatureName
file
(FeatureName, FeatureName) -> IO (FeatureName, FeatureName)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (FeatureName
file, FeatureName
src)
parse :: FeatureName -> FeatureName -> Either FeatureName (Module ())
parse FeatureName
file FeatureName
src = case ParseMode -> FeatureName -> ParseResult (Module SrcSpanInfo)
H.parseModuleWithMode ParseMode
parseMode FeatureName
src of
H.ParseOk Module SrcSpanInfo
modl -> Module () -> Either FeatureName (Module ())
forall a b. b -> Either a b
Right (Module SrcSpanInfo -> Module ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void Module SrcSpanInfo
modl)
H.ParseFailed (H.SrcLoc FeatureName
_ Int
l Int
c) FeatureName
err ->
FeatureName -> Either FeatureName (Module ())
forall a b. a -> Either a b
Left (FeatureName
file FeatureName -> FeatureName -> FeatureName
forall a. [a] -> [a] -> [a]
++ FeatureName
"@" FeatureName -> FeatureName -> FeatureName
forall a. [a] -> [a] -> [a]
++ Int -> FeatureName
forall a. Show a => a -> FeatureName
show Int
l FeatureName -> FeatureName -> FeatureName
forall a. [a] -> [a] -> [a]
++ FeatureName
":" FeatureName -> FeatureName -> FeatureName
forall a. [a] -> [a] -> [a]
++ Int -> FeatureName
forall a. Show a => a -> FeatureName
show Int
c FeatureName -> FeatureName -> FeatureName
forall a. [a] -> [a] -> [a]
++ FeatureName
": " FeatureName -> FeatureName -> FeatureName
forall a. [a] -> [a] -> [a]
++ FeatureName
err)
moduleName :: FeatureName -> Module l -> Either FeatureName FeatureName
moduleName FeatureName
_ (H.Module l
_ (Just (H.ModuleHead l
_ (H.ModuleName l
_ FeatureName
name) Maybe (WarningText l)
_ Maybe (ExportSpecList l)
_)) [ModulePragma l]
_ [ImportDecl l]
_ [Decl l]
_) = FeatureName -> Either FeatureName FeatureName
forall a b. b -> Either a b
Right FeatureName
name
moduleName FeatureName
file Module l
_ = FeatureName -> Either FeatureName FeatureName
forall a b. a -> Either a b
Left (FeatureName
file FeatureName -> FeatureName -> FeatureName
forall a. [a] -> [a] -> [a]
++ FeatureName
": The module has no header, so I can't identify it")
merge :: Registry -> FeatureSet -> IO (LHsMakeError (Map ModuleName (H.Module ()), Map ModuleName BookendedAbsSyn))
merge :: Registry
-> Set FeatureName
-> IO
(LHsMakeError
(Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn))
merge Registry
reg Set FeatureName
names = case Registry -> Set FeatureName -> LHsMakeError [FeatureName]
resolve Registry
reg Set FeatureName
names of
Left FeatureName
err -> LHsMakeError
(Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn)
-> IO
(LHsMakeError
(Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn))
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (FeatureName
-> LHsMakeError
(Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn)
forall a b. a -> Either a b
Left (FeatureName -> FeatureName
errLabel FeatureName
err))
Right [FeatureName]
enabled -> do
FeatureName -> IO ()
putStrLn (FeatureName -> IO ()) -> FeatureName -> IO ()
forall a b. (a -> b) -> a -> b
$ FeatureName -> FeatureName
infoLabel FeatureName
"These features are enabled: " FeatureName -> FeatureName -> FeatureName
forall a. [a] -> [a] -> [a]
++ [FeatureName] -> FeatureName
unwords (Set FeatureName -> [FeatureName]
forall a. Set a -> [a]
Set.toList Set FeatureName
names)
let automatic :: [FeatureName]
automatic = (FeatureName -> Bool) -> [FeatureName] -> [FeatureName]
forall a. (a -> Bool) -> [a] -> [a]
filter (FeatureName -> Set FeatureName -> Bool
forall a. Ord a => a -> Set a -> Bool
`Set.notMember` Set FeatureName
names) [FeatureName]
enabled
Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless ([FeatureName] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [FeatureName]
automatic) (FeatureName -> IO ()
putStrLn
(FeatureName -> IO ()) -> FeatureName -> IO ()
forall a b. (a -> b) -> a -> b
$ FeatureName -> FeatureName
infoLabel FeatureName
"These features were automatically enabled: "
FeatureName -> FeatureName -> FeatureName
forall a. [a] -> [a] -> [a]
++ [FeatureName] -> FeatureName
unwords [FeatureName]
automatic)
[LHsMakeError
(Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn)]
loaded <- (FeatureName
-> IO
(LHsMakeError
(Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn)))
-> [FeatureName]
-> IO
[LHsMakeError
(Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn)]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> [a] -> f [b]
traverse (Feature
-> IO
(LHsMakeError
(Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn))
load (Feature
-> IO
(LHsMakeError
(Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn)))
-> (FeatureName -> Feature)
-> FeatureName
-> IO
(LHsMakeError
(Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Registry
reg Registry -> FeatureName -> Feature
forall k a. Ord k => Map k a -> k -> a
Map.!)) [FeatureName]
enabled
let mergeAll :: [Map k a] -> Either FeatureName (Map k a)
mergeAll [Map k a]
maps = Map k (Either FeatureName a) -> Either FeatureName (Map k a)
forall (t :: * -> *) (m :: * -> *) a.
(Traversable t, Monad m) =>
t (m a) -> m (t a)
forall (m :: * -> *) a. Monad m => Map k (m a) -> m (Map k a)
sequence ((Either FeatureName a
-> Either FeatureName a -> Either FeatureName a)
-> [Map k (Either FeatureName a)] -> Map k (Either FeatureName a)
forall (f :: * -> *) k a.
(Foldable f, Ord k) =>
(a -> a -> a) -> f (Map k a) -> Map k a
Map.unionsWith (\Either FeatureName a
x Either FeatureName a
y -> Either FeatureName a
x Either FeatureName a
-> (a -> Either FeatureName a) -> Either FeatureName a
forall a b.
Either FeatureName a
-> (a -> Either FeatureName b) -> Either FeatureName b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \a
a -> Either FeatureName a
y Either FeatureName a
-> (a -> Either FeatureName a) -> Either FeatureName a
forall a b.
Either FeatureName a
-> (a -> Either FeatureName b) -> Either FeatureName b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (a
a a -> a -> Either FeatureName a
forall a. Superimposable a => a -> a -> LHsMakeError a
-->))
((Map k a -> Map k (Either FeatureName a))
-> [Map k a] -> [Map k (Either FeatureName a)]
forall a b. (a -> b) -> [a] -> [b]
map ((a -> Either FeatureName a)
-> Map k a -> Map k (Either FeatureName a)
forall a b. (a -> b) -> Map k a -> Map k b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> Either FeatureName a
forall a b. b -> Either a b
Right) [Map k a]
maps))
LHsMakeError
(Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn)
-> IO
(LHsMakeError
(Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn))
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (LHsMakeError
(Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn)
-> IO
(LHsMakeError
(Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn)))
-> LHsMakeError
(Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn)
-> IO
(LHsMakeError
(Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn))
forall a b. (a -> b) -> a -> b
$ (FeatureName -> FeatureName)
-> LHsMakeError
(Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn)
-> LHsMakeError
(Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn)
forall a b c. (a -> b) -> Either a c -> Either b c
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first FeatureName -> FeatureName
errLabel (LHsMakeError
(Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn)
-> LHsMakeError
(Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn))
-> LHsMakeError
(Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn)
-> LHsMakeError
(Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn)
forall a b. (a -> b) -> a -> b
$ do
([Map FeatureName (Module ())]
modls, [Map FeatureName BookendedAbsSyn]
grams) <- ([(Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn)]
-> ([Map FeatureName (Module ())],
[Map FeatureName BookendedAbsSyn]))
-> Either
FeatureName
[(Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn)]
-> Either
FeatureName
([Map FeatureName (Module ())], [Map FeatureName BookendedAbsSyn])
forall a b.
(a -> b) -> Either FeatureName a -> Either FeatureName b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [(Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn)]
-> ([Map FeatureName (Module ())],
[Map FeatureName BookendedAbsSyn])
forall a b. [(a, b)] -> ([a], [b])
unzip ([LHsMakeError
(Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn)]
-> Either
FeatureName
[(Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn)]
forall (t :: * -> *) (m :: * -> *) a.
(Traversable t, Monad m) =>
t (m a) -> m (t a)
forall (m :: * -> *) a. Monad m => [m a] -> m [a]
sequence [LHsMakeError
(Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn)]
loaded)
(,) (Map FeatureName (Module ())
-> Map FeatureName BookendedAbsSyn
-> (Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn))
-> Either FeatureName (Map FeatureName (Module ()))
-> Either
FeatureName
(Map FeatureName BookendedAbsSyn
-> (Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Map FeatureName (Module ())]
-> Either FeatureName (Map FeatureName (Module ()))
forall {k} {a}.
(Ord k, Superimposable a) =>
[Map k a] -> Either FeatureName (Map k a)
mergeAll [Map FeatureName (Module ())]
modls Either
FeatureName
(Map FeatureName BookendedAbsSyn
-> (Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn))
-> Either FeatureName (Map FeatureName BookendedAbsSyn)
-> LHsMakeError
(Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn)
forall a b.
Either FeatureName (a -> b)
-> Either FeatureName a -> Either FeatureName b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> [Map FeatureName BookendedAbsSyn]
-> Either FeatureName (Map FeatureName BookendedAbsSyn)
forall {k} {a}.
(Ord k, Superimposable a) =>
[Map k a] -> Either FeatureName (Map k a)
mergeAll [Map FeatureName BookendedAbsSyn]
grams
superimpose :: [FeatureName] -> FilePath -> IO ()
superimpose :: [FeatureName] -> FeatureName -> IO ()
superimpose [FeatureName]
names FeatureName
dir = do
FeatureName -> IO ()
putStrLn (FeatureName -> IO ()) -> FeatureName -> IO ()
forall a b. (a -> b) -> a -> b
$ FeatureName -> FeatureName
infoLabel FeatureName
"LayeredHaskell-make is starting up..."
(Map FeatureName (Module ())
haskell, Map FeatureName BookendedAbsSyn
happy) <- Registry
-> Set FeatureName
-> IO
(LHsMakeError
(Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn))
merge Registry
registry ([FeatureName] -> Set FeatureName
forall a. Ord a => [a] -> Set a
Set.fromList [FeatureName]
names) IO
(LHsMakeError
(Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn))
-> (LHsMakeError
(Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn)
-> IO
(Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn))
-> IO
(Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn)
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (FeatureName
-> IO
(Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn))
-> ((Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn)
-> IO
(Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn))
-> LHsMakeError
(Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn)
-> IO
(Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn)
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either FeatureName
-> IO
(Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn)
forall a. FeatureName -> IO a
die (Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn)
-> IO
(Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return
[(FeatureName, Module ())]
-> ((FeatureName, Module ()) -> IO ()) -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ (Map FeatureName (Module ()) -> [(FeatureName, Module ())]
forall k a. Map k a -> [(k, a)]
Map.toList Map FeatureName (Module ())
haskell) (((FeatureName, Module ()) -> IO ()) -> IO ())
-> ((FeatureName, Module ()) -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \(FeatureName
name, Module ()
modl) ->
FeatureName -> FeatureName -> IO ()
writeIfChanged
(FeatureName
dir FeatureName -> FeatureName -> FeatureName
</> FeatureName -> FeatureName
end FeatureName
name FeatureName -> FeatureName -> FeatureName
<.> FeatureName
"hs")
(Module () -> FeatureName
forall a. Pretty a => a -> FeatureName
H.prettyPrint Module ()
modl)
[(FeatureName, BookendedAbsSyn)]
-> ((FeatureName, BookendedAbsSyn) -> IO ()) -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ (Map FeatureName BookendedAbsSyn -> [(FeatureName, BookendedAbsSyn)]
forall k a. Map k a -> [(k, a)]
Map.toList Map FeatureName BookendedAbsSyn
happy) (((FeatureName, BookendedAbsSyn) -> IO ()) -> IO ())
-> ((FeatureName, BookendedAbsSyn) -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \(FeatureName
name, BookendedAbsSyn
gram) ->
FeatureName -> FeatureName -> IO ()
writeIfChanged
(FeatureName
dir FeatureName -> FeatureName -> FeatureName
</> FeatureName -> FeatureName
end FeatureName
name FeatureName -> FeatureName -> FeatureName
<.> FeatureName
"y")
(BookendedAbsSyn -> FeatureName
pprHappy BookendedAbsSyn
gram)
FeatureName -> IO ()
putStrLn (FeatureName -> IO ()) -> FeatureName -> IO ()
forall a b. (a -> b) -> a -> b
$ FeatureName -> FeatureName
infoLabel FeatureName
"Done!"
where
end :: FeatureName -> FeatureName
end = FeatureName -> FeatureName
forall a. [a] -> [a]
reverse (FeatureName -> FeatureName)
-> (FeatureName -> FeatureName) -> FeatureName -> FeatureName
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Char -> Bool) -> FeatureName -> FeatureName
forall a. (a -> Bool) -> [a] -> [a]
takeWhile (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
/= Char
'.') (FeatureName -> FeatureName)
-> (FeatureName -> FeatureName) -> FeatureName -> FeatureName
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FeatureName -> FeatureName
forall a. [a] -> [a]
reverse
writeIfChanged :: FilePath -> String -> IO ()
writeIfChanged :: FeatureName -> FeatureName -> IO ()
writeIfChanged FeatureName
path FeatureName
new = FeatureName -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length FeatureName
new Int -> IO () -> IO ()
forall a b. a -> b -> b
`seq` do
Bool -> FeatureName -> IO ()
createDirectoryIfMissing Bool
True (FeatureName -> FeatureName
takeDirectory FeatureName
path)
Bool
exists <- FeatureName -> IO Bool
doesFileExist FeatureName
path
Bool
same <- if Bool
exists then (FeatureName -> Bool) -> IO FeatureName -> IO Bool
forall a b. (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (FeatureName -> FeatureName -> Bool
forall a. Eq a => a -> a -> Bool
== FeatureName
new) (FeatureName -> IO FeatureName
readFile' FeatureName
path) else Bool -> IO Bool
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless Bool
same (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ do
FeatureName -> FeatureName -> IO ()
writeFile FeatureName
path FeatureName
new
FeatureName -> IO ()
putStrLn (FeatureName -> IO ()) -> FeatureName -> IO ()
forall a b. (a -> b) -> a -> b
$ FeatureName -> FeatureName
infoLabel FeatureName
"Updated source file " FeatureName -> FeatureName -> FeatureName
forall a. [a] -> [a] -> [a]
++ FeatureName
path
colored :: String -> String
colored :: FeatureName -> FeatureName
colored FeatureName
s = FeatureName
"\ESC[35m" FeatureName -> FeatureName -> FeatureName
forall a. [a] -> [a] -> [a]
++ FeatureName
s FeatureName -> FeatureName -> FeatureName
forall a. [a] -> [a] -> [a]
++ FeatureName
"\ESC[0m"
printFeatures :: String -> Registry -> (Feature -> String) -> IO ()
printFeatures :: FeatureName -> Registry -> (Feature -> FeatureName) -> IO ()
printFeatures FeatureName
heading Registry
reg Feature -> FeatureName
det = do
FeatureName -> IO ()
putStrLn (FeatureName -> IO ()) -> FeatureName -> IO ()
forall a b. (a -> b) -> a -> b
$ FeatureName -> FeatureName
infoLabel FeatureName
heading
Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when ((Feature -> Bool) -> Registry -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any Feature -> Bool
hidden Registry
reg) (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$
FeatureName -> IO ()
putStrLn (FeatureName -> IO ()) -> FeatureName -> IO ()
forall a b. (a -> b) -> a -> b
$ FeatureName -> FeatureName
infoLabel (FeatureName
"Note that only " FeatureName -> FeatureName -> FeatureName
forall a. [a] -> [a] -> [a]
++ FeatureName -> FeatureName
colored FeatureName
"colored" FeatureName -> FeatureName -> FeatureName
forall a. [a] -> [a] -> [a]
++ FeatureName
" features can be enabled manually.")
[(FeatureName, Feature)]
-> ((FeatureName, Feature) -> IO ()) -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ (Registry -> [(FeatureName, Feature)]
forall k a. Map k a -> [(k, a)]
Map.toList Registry
reg) (((FeatureName, Feature) -> IO ()) -> IO ())
-> ((FeatureName, Feature) -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \(FeatureName
name, Feature
feat) ->
FeatureName -> IO ()
putStrLn (FeatureName -> IO ()) -> FeatureName -> IO ()
forall a b. (a -> b) -> a -> b
$ FeatureName -> FeatureName
infoLabel ((if Feature -> Bool
hidden Feature
feat then FeatureName
name else FeatureName -> FeatureName
colored FeatureName
name) FeatureName -> FeatureName -> FeatureName
forall a. [a] -> [a] -> [a]
++ FeatureName
": " FeatureName -> FeatureName -> FeatureName
forall a. [a] -> [a] -> [a]
++ Feature -> FeatureName
det Feature
feat)
list :: IO ()
list :: IO ()
list = FeatureName -> Registry -> (Feature -> FeatureName) -> IO ()
printFeatures FeatureName
"Listing features..." ((Feature -> Bool) -> Registry -> Registry
forall a k. (a -> Bool) -> Map k a -> Map k a
Map.filter (Bool -> Bool
not (Bool -> Bool) -> (Feature -> Bool) -> Feature -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Feature -> Bool
hidden) Registry
registry) Feature -> FeatureName
descrip
listAll :: IO ()
listAll :: IO ()
listAll = FeatureName -> Registry -> (Feature -> FeatureName) -> IO ()
printFeatures FeatureName
"Listing all features..." Registry
registry Feature -> FeatureName
descrip
listDeps :: IO ()
listDeps :: IO ()
listDeps = FeatureName -> Registry -> (Feature -> FeatureName) -> IO ()
printFeatures FeatureName
"Listing all dependencies and joint implications..." Registry
registry Feature -> FeatureName
detail
where
detail :: Feature -> FeatureName
detail Feature
feat
| [FeatureName] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [FeatureName]
parts = FeatureName
"none"
| Bool
otherwise = FeatureName -> [FeatureName] -> FeatureName
forall a. [a] -> [[a]] -> [a]
intercalate FeatureName
", " [FeatureName]
parts
where parts :: [FeatureName]
parts = FeatureName
-> FeatureName
-> FeatureName
-> Set (Set FeatureName)
-> [FeatureName]
describe FeatureName
"depends on " FeatureName
" or " FeatureName
" and " (Feature -> Set (Set FeatureName)
dependsOn Feature
feat)
[FeatureName] -> [FeatureName] -> [FeatureName]
forall a. [a] -> [a] -> [a]
++ FeatureName
-> FeatureName
-> FeatureName
-> Set (Set FeatureName)
-> [FeatureName]
describe FeatureName
"implied by " FeatureName
" and " FeatureName
" or " (Feature -> Set (Set FeatureName)
impliedBy Feature
feat)
describe :: FeatureName
-> FeatureName
-> FeatureName
-> Set (Set FeatureName)
-> [FeatureName]
describe FeatureName
prefix FeatureName
inner FeatureName
outer Set (Set FeatureName)
xss =
[ FeatureName
prefix FeatureName -> FeatureName -> FeatureName
forall a. [a] -> [a] -> [a]
++ FeatureName -> [FeatureName] -> FeatureName
forall a. [a] -> [[a]] -> [a]
intercalate FeatureName
outer
[ if Set FeatureName -> Int
forall a. Set a -> Int
Set.size Set FeatureName
xs Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
1 Bool -> Bool -> Bool
&& Set (Set FeatureName) -> Int
forall a. Set a -> Int
Set.size Set (Set FeatureName)
xss Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
1 then FeatureName
"(" FeatureName -> FeatureName -> FeatureName
forall a. [a] -> [a] -> [a]
++ FeatureName
s FeatureName -> FeatureName -> FeatureName
forall a. [a] -> [a] -> [a]
++ FeatureName
")" else FeatureName
s
| Set FeatureName
xs <- Set (Set FeatureName) -> [Set FeatureName]
forall a. Set a -> [a]
Set.toList Set (Set FeatureName)
xss, let s :: FeatureName
s = FeatureName -> [FeatureName] -> FeatureName
forall a. [a] -> [[a]] -> [a]
intercalate FeatureName
inner (Set FeatureName -> [FeatureName]
forall a. Set a -> [a]
Set.toList Set FeatureName
xs) ]
| Bool -> Bool
not (Set (Set FeatureName) -> Bool
forall a. Set a -> Bool
Set.null Set (Set FeatureName)
xss) ]
combos :: LHsMakeError [[(FeatureName, Bool)]]
combos :: LHsMakeError [[(FeatureName, Bool)]]
combos = do
(FeatureName -> Either FeatureName Any)
-> [FeatureName] -> Either FeatureName ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ FeatureName -> Either FeatureName Any
forall a b. a -> Either a b
Left (Registry -> Set FeatureName -> [FeatureName]
unknown Registry
registry ((Feature -> Set FeatureName) -> Registry -> Set FeatureName
forall m a. Monoid m => (a -> m) -> Map FeatureName a -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap (Set (Set FeatureName) -> Set FeatureName
forall (f :: * -> *) a. (Foldable f, Ord a) => f (Set a) -> Set a
Set.unions (Set (Set FeatureName) -> Set FeatureName)
-> (Feature -> Set (Set FeatureName)) -> Feature -> Set FeatureName
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Feature -> Set (Set FeatureName)
dependsOn) Registry
registry))
([[(FeatureName, Bool)]] -> [[(FeatureName, Bool)]])
-> LHsMakeError [[(FeatureName, Bool)]]
-> LHsMakeError [[(FeatureName, Bool)]]
forall a b.
(a -> b) -> Either FeatureName a -> Either FeatureName b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (([(FeatureName, Bool)] -> Bool)
-> [[(FeatureName, Bool)]] -> [[(FeatureName, Bool)]]
forall a. (a -> Bool) -> [a] -> [a]
filter (Bool -> Bool
not (Bool -> Bool)
-> ([(FeatureName, Bool)] -> Bool) -> [(FeatureName, Bool)] -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [(FeatureName, Bool)] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null)) (LHsMakeError [[(FeatureName, Bool)]]
-> LHsMakeError [[(FeatureName, Bool)]])
-> LHsMakeError [[(FeatureName, Bool)]]
-> LHsMakeError [[(FeatureName, Bool)]]
forall a b. (a -> b) -> a -> b
$
[Set FeatureName]
-> (Set FeatureName -> Either FeatureName [(FeatureName, Bool)])
-> LHsMakeError [[(FeatureName, Bool)]]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
forM ((Set FeatureName -> Bool) -> [Set FeatureName] -> [Set FeatureName]
forall a. (a -> Bool) -> [a] -> [a]
filter ([FeatureName] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null ([FeatureName] -> Bool)
-> (Set FeatureName -> [FeatureName]) -> Set FeatureName -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Registry -> Set FeatureName -> [FeatureName]
check Registry
registry)
(Set (Set FeatureName) -> [Set FeatureName]
forall a. Set a -> [a]
Set.toList
((Set FeatureName -> Set FeatureName)
-> Set (Set FeatureName) -> Set (Set FeatureName)
forall b a. Ord b => (a -> b) -> Set a -> Set b
Set.map (Registry -> Set FeatureName -> Set FeatureName
select Registry
registry)
(Set FeatureName -> Set (Set FeatureName)
forall a. Set a -> Set (Set a)
Set.powerSet (Registry -> Set FeatureName
forall k a. Map k a -> Set k
Map.keysSet ((Feature -> Bool) -> Registry -> Registry
forall a k. (a -> Bool) -> Map k a -> Map k a
Map.filter (Bool -> Bool
not (Bool -> Bool) -> (Feature -> Bool) -> Feature -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Feature -> Bool
hidden) Registry
registry))))))
(\Set FeatureName
names -> ([FeatureName] -> [(FeatureName, Bool)])
-> LHsMakeError [FeatureName]
-> Either FeatureName [(FeatureName, Bool)]
forall a b.
(a -> b) -> Either FeatureName a -> Either FeatureName b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((FeatureName -> (FeatureName, Bool))
-> [FeatureName] -> [(FeatureName, Bool)]
forall a b. (a -> b) -> [a] -> [b]
map (\FeatureName
name -> (FeatureName
name, Set FeatureName -> FeatureName -> Bool
manual Set FeatureName
names FeatureName
name)))
(Registry -> Set FeatureName -> LHsMakeError [FeatureName]
order Registry
registry Set FeatureName
names))
where
manual :: Set FeatureName -> FeatureName -> Bool
manual Set FeatureName
names FeatureName
name = Bool -> Bool
not (FeatureName
name FeatureName -> Set FeatureName -> Bool
forall a. Ord a => a -> Set a -> Bool
`Set.member` Registry -> Set FeatureName -> Set FeatureName
select Registry
registry (FeatureName -> Set FeatureName -> Set FeatureName
forall a. Ord a => a -> Set a -> Set a
Set.delete FeatureName
name Set FeatureName
names))
generate :: IO ()
generate :: IO ()
generate = do
FeatureName -> IO ()
putStrLn (FeatureName -> IO ()) -> FeatureName -> IO ()
forall a b. (a -> b) -> a -> b
$ FeatureName -> FeatureName
infoLabel FeatureName
"Listing all (non-redundant) feature combinations..."
FeatureName -> IO ()
putStrLn (FeatureName -> IO ()) -> FeatureName -> IO ()
forall a b. (a -> b) -> a -> b
$ FeatureName -> FeatureName
infoLabel (FeatureName
"Note that only " FeatureName -> FeatureName -> FeatureName
forall a. [a] -> [a] -> [a]
++ FeatureName -> FeatureName
colored FeatureName
"colored" FeatureName -> FeatureName -> FeatureName
forall a. [a] -> [a] -> [a]
++ FeatureName
" features need to be enabled manually. ")
FeatureName -> FeatureName -> FeatureName
forall a. [a] -> [a] -> [a]
++ FeatureName
"The rest can be automatically resolved."
(FeatureName -> IO ())
-> ([[(FeatureName, Bool)]] -> IO ())
-> LHsMakeError [[(FeatureName, Bool)]]
-> IO ()
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (FeatureName -> IO ()
putStrLn (FeatureName -> IO ())
-> (FeatureName -> FeatureName) -> FeatureName -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FeatureName -> FeatureName
errLabel) [[(FeatureName, Bool)]] -> IO ()
forall {t :: * -> *}.
Foldable t =>
t [(FeatureName, Bool)] -> IO ()
report LHsMakeError [[(FeatureName, Bool)]]
combos
where
report :: t [(FeatureName, Bool)] -> IO ()
report t [(FeatureName, Bool)]
entries = do
([(FeatureName, Bool)] -> IO ())
-> t [(FeatureName, Bool)] -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (FeatureName -> IO ()
putStrLn (FeatureName -> IO ())
-> ([(FeatureName, Bool)] -> FeatureName)
-> [(FeatureName, Bool)]
-> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FeatureName -> FeatureName
infoLabel (FeatureName -> FeatureName)
-> ([(FeatureName, Bool)] -> FeatureName)
-> [(FeatureName, Bool)]
-> FeatureName
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [FeatureName] -> FeatureName
unwords ([FeatureName] -> FeatureName)
-> ([(FeatureName, Bool)] -> [FeatureName])
-> [(FeatureName, Bool)]
-> FeatureName
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((FeatureName, Bool) -> FeatureName)
-> [(FeatureName, Bool)] -> [FeatureName]
forall a b. (a -> b) -> [a] -> [b]
map (FeatureName, Bool) -> FeatureName
paint) t [(FeatureName, Bool)]
entries
FeatureName -> IO ()
putStrLn (FeatureName -> IO ()) -> FeatureName -> IO ()
forall a b. (a -> b) -> a -> b
$ FeatureName -> FeatureName
infoLabel (Int -> FeatureName
forall a. Show a => a -> FeatureName
show (t [(FeatureName, Bool)] -> Int
forall a. t a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length t [(FeatureName, Bool)]
entries)) FeatureName -> FeatureName -> FeatureName
forall a. [a] -> [a] -> [a]
++ FeatureName
" feature combinations found!"
paint :: (FeatureName, Bool) -> FeatureName
paint (FeatureName
name, Bool
manual) = if Bool
manual then FeatureName -> FeatureName
colored FeatureName
name else FeatureName
name
generateCombos :: IO [[FeatureName]]
generateCombos :: IO [[FeatureName]]
generateCombos =
(FeatureName -> IO [[FeatureName]])
-> ([[(FeatureName, Bool)]] -> IO [[FeatureName]])
-> LHsMakeError [[(FeatureName, Bool)]]
-> IO [[FeatureName]]
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (FeatureName -> IO [[FeatureName]]
forall a. FeatureName -> IO a
die (FeatureName -> IO [[FeatureName]])
-> (FeatureName -> FeatureName)
-> FeatureName
-> IO [[FeatureName]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FeatureName -> FeatureName
errLabel)
([[FeatureName]] -> IO [[FeatureName]]
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ([[FeatureName]] -> IO [[FeatureName]])
-> ([[(FeatureName, Bool)]] -> [[FeatureName]])
-> [[(FeatureName, Bool)]]
-> IO [[FeatureName]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([FeatureName] -> Bool) -> [[FeatureName]] -> [[FeatureName]]
forall a. (a -> Bool) -> [a] -> [a]
filter (Bool -> Bool
not (Bool -> Bool) -> ([FeatureName] -> Bool) -> [FeatureName] -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [FeatureName] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null) ([[FeatureName]] -> [[FeatureName]])
-> ([[(FeatureName, Bool)]] -> [[FeatureName]])
-> [[(FeatureName, Bool)]]
-> [[FeatureName]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([(FeatureName, Bool)] -> [FeatureName])
-> [[(FeatureName, Bool)]] -> [[FeatureName]]
forall a b. (a -> b) -> [a] -> [b]
map (((FeatureName, Bool) -> FeatureName)
-> [(FeatureName, Bool)] -> [FeatureName]
forall a b. (a -> b) -> [a] -> [b]
map (FeatureName, Bool) -> FeatureName
forall a b. (a, b) -> a
fst ([(FeatureName, Bool)] -> [FeatureName])
-> ([(FeatureName, Bool)] -> [(FeatureName, Bool)])
-> [(FeatureName, Bool)]
-> [FeatureName]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((FeatureName, Bool) -> Bool)
-> [(FeatureName, Bool)] -> [(FeatureName, Bool)]
forall a. (a -> Bool) -> [a] -> [a]
filter (FeatureName, Bool) -> Bool
forall a b. (a, b) -> b
snd))
LHsMakeError [[(FeatureName, Bool)]]
combos
data Dependency
= None
| Has FeatureName
| All [Dependency]
| Any [Dependency]
deriving (Dependency -> Dependency -> Bool
(Dependency -> Dependency -> Bool)
-> (Dependency -> Dependency -> Bool) -> Eq Dependency
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Dependency -> Dependency -> Bool
== :: Dependency -> Dependency -> Bool
$c/= :: Dependency -> Dependency -> Bool
/= :: Dependency -> Dependency -> Bool
Eq, Int -> Dependency -> FeatureName -> FeatureName
[Dependency] -> FeatureName -> FeatureName
Dependency -> FeatureName
(Int -> Dependency -> FeatureName -> FeatureName)
-> (Dependency -> FeatureName)
-> ([Dependency] -> FeatureName -> FeatureName)
-> Show Dependency
forall a.
(Int -> a -> FeatureName -> FeatureName)
-> (a -> FeatureName)
-> ([a] -> FeatureName -> FeatureName)
-> Show a
$cshowsPrec :: Int -> Dependency -> FeatureName -> FeatureName
showsPrec :: Int -> Dependency -> FeatureName -> FeatureName
$cshow :: Dependency -> FeatureName
show :: Dependency -> FeatureName
$cshowList :: [Dependency] -> FeatureName -> FeatureName
showList :: [Dependency] -> FeatureName -> FeatureName
Show)
(/\) :: Dependency -> Dependency -> Dependency
Dependency
a /\ :: Dependency -> Dependency -> Dependency
/\ Dependency
b = [Dependency] -> Dependency
All [Dependency
a, Dependency
b]
infixr 3 /\
(\/) :: Dependency -> Dependency -> Dependency
Dependency
a \/ :: Dependency -> Dependency -> Dependency
\/ Dependency
b = [Dependency] -> Dependency
Any [Dependency
a, Dependency
b]
infixr 2 \/
combine :: Ord a => Set (Set a) -> Set (Set a) -> Set (Set a)
combine :: forall a. Ord a => Set (Set a) -> Set (Set a) -> Set (Set a)
combine Set (Set a)
xs Set (Set a)
ys = ((Set a, Set a) -> Set a) -> Set (Set a, Set a) -> Set (Set a)
forall b a. Ord b => (a -> b) -> Set a -> Set b
Set.map ((Set a -> Set a -> Set a) -> (Set a, Set a) -> Set a
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Set a -> Set a -> Set a
forall a. Ord a => Set a -> Set a -> Set a
Set.union) (Set (Set a) -> Set (Set a) -> Set (Set a, Set a)
forall a b. Set a -> Set b -> Set (a, b)
Set.cartesianProduct Set (Set a)
xs Set (Set a)
ys)
cnf :: Dependency -> Set (Set FeatureName)
cnf :: Dependency -> Set (Set FeatureName)
cnf Dependency
None = Set (Set FeatureName)
forall a. Set a
Set.empty
cnf (Has FeatureName
name) = Set FeatureName -> Set (Set FeatureName)
forall a. a -> Set a
Set.singleton (FeatureName -> Set FeatureName
forall a. a -> Set a
Set.singleton FeatureName
name)
cnf (Any [Dependency]
deps) = (Dependency -> Set (Set FeatureName) -> Set (Set FeatureName))
-> Set (Set FeatureName) -> [Dependency] -> Set (Set FeatureName)
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (Set (Set FeatureName)
-> Set (Set FeatureName) -> Set (Set FeatureName)
forall a. Ord a => Set (Set a) -> Set (Set a) -> Set (Set a)
combine (Set (Set FeatureName)
-> Set (Set FeatureName) -> Set (Set FeatureName))
-> (Dependency -> Set (Set FeatureName))
-> Dependency
-> Set (Set FeatureName)
-> Set (Set FeatureName)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Dependency -> Set (Set FeatureName)
cnf) (Set FeatureName -> Set (Set FeatureName)
forall a. a -> Set a
Set.singleton Set FeatureName
forall a. Set a
Set.empty) [Dependency]
deps
cnf (All [Dependency]
deps) = (Dependency -> Set (Set FeatureName))
-> [Dependency] -> Set (Set FeatureName)
forall m a. Monoid m => (a -> m) -> [a] -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap Dependency -> Set (Set FeatureName)
cnf [Dependency]
deps
dnf :: Dependency -> Set (Set FeatureName)
dnf :: Dependency -> Set (Set FeatureName)
dnf Dependency
None = Set (Set FeatureName)
forall a. Set a
Set.empty
dnf (Has FeatureName
name) = Set FeatureName -> Set (Set FeatureName)
forall a. a -> Set a
Set.singleton (FeatureName -> Set FeatureName
forall a. a -> Set a
Set.singleton FeatureName
name)
dnf (Any [Dependency]
deps) = (Dependency -> Set (Set FeatureName))
-> [Dependency] -> Set (Set FeatureName)
forall m a. Monoid m => (a -> m) -> [a] -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap Dependency -> Set (Set FeatureName)
dnf [Dependency]
deps
dnf (All [Dependency]
deps) = (Dependency -> Set (Set FeatureName) -> Set (Set FeatureName))
-> Set (Set FeatureName) -> [Dependency] -> Set (Set FeatureName)
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (Set (Set FeatureName)
-> Set (Set FeatureName) -> Set (Set FeatureName)
forall a. Ord a => Set (Set a) -> Set (Set a) -> Set (Set a)
combine (Set (Set FeatureName)
-> Set (Set FeatureName) -> Set (Set FeatureName))
-> (Dependency -> Set (Set FeatureName))
-> Dependency
-> Set (Set FeatureName)
-> Set (Set FeatureName)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Dependency -> Set (Set FeatureName)
dnf) (Set FeatureName -> Set (Set FeatureName)
forall a. a -> Set a
Set.singleton Set FeatureName
forall a. Set a
Set.empty) [Dependency]
deps