| 
 | 1 | +{-# LANGUAGE DataKinds #-}  | 
1 | 2 | {-# LANGUAGE DeriveDataTypeable #-}  | 
2 | 3 | {-# LANGUAGE DeriveGeneric #-}  | 
3 |  | -{-# LANGUAGE DataKinds #-}  | 
 | 4 | +{-# LANGUAGE FlexibleInstances #-}  | 
 | 5 | +{-# LANGUAGE TypeFamilies #-}  | 
4 | 6 | 
 
  | 
5 | 7 | module Distribution.Types.ExtraSource  | 
6 | 8 |   ( ExtraSource (..)  | 
7 |  | -  , extraSourceFromPath  | 
 | 9 | +  , ExtraSourceClass (..)  | 
8 | 10 |   ) where  | 
9 | 11 | 
 
  | 
10 | 12 | import Distribution.Compat.Prelude  | 
11 | 13 | import Prelude ()  | 
12 | 14 | 
 
  | 
13 | 15 | import Distribution.Parsec  | 
14 | 16 | import Distribution.Pretty  | 
15 |  | -import Distribution.Utils.Path (SymbolicPath, FileOrDir(..), Pkg)  | 
 | 17 | +import Distribution.Utils.Path (Build, FileOrDir (..), Pkg, RelativePath, SymbolicPath, relativeSymbolicPath, unsafeCoerceSymbolicPath)  | 
16 | 18 | 
 
  | 
17 | 19 | import qualified Distribution.Compat.CharParsing as P  | 
18 | 20 | import qualified Text.PrettyPrint as PP  | 
19 |  | -import Distribution.FieldGrammar.Newtypes (SymbolicPathNT(..))  | 
20 | 21 | 
 
  | 
21 |  | -data ExtraSource = ExtraSource  | 
22 |  | -  { extraSourceFile :: SymbolicPath Pkg File  | 
23 |  | -  , extraSourceOpts :: [String]  | 
24 |  | -  }  | 
 | 22 | +data family ExtraSource pkg  | 
 | 23 | + | 
 | 24 | +data instance ExtraSource Pkg = ExtraSourcePkg (SymbolicPath Pkg File) [String]  | 
 | 25 | +  deriving (Generic, Show, Read, Eq, Ord, Typeable, Data)  | 
 | 26 | + | 
 | 27 | +data instance ExtraSource Build = ExtraSourceBuild (RelativePath Build File) [String]  | 
25 | 28 |   deriving (Generic, Show, Read, Eq, Ord, Typeable, Data)  | 
26 | 29 | 
 
  | 
27 |  | -instance Binary ExtraSource  | 
28 |  | -instance Structured ExtraSource  | 
29 |  | -instance NFData ExtraSource where rnf = genericRnf  | 
 | 30 | +class ExtraSourceClass e where  | 
 | 31 | +  extraSourceOpts :: e -> [String]  | 
 | 32 | +  extraSourceFile :: e -> SymbolicPath Pkg 'File  | 
 | 33 | + | 
 | 34 | +instance ExtraSourceClass (ExtraSource Pkg) where  | 
 | 35 | +  extraSourceOpts (ExtraSourcePkg _ opts) = opts  | 
 | 36 | +  extraSourceFile (ExtraSourcePkg f _) = f  | 
 | 37 | + | 
 | 38 | +instance ExtraSourceClass (ExtraSource Build) where  | 
 | 39 | +  extraSourceOpts (ExtraSourceBuild _ opts) = opts  | 
 | 40 | + | 
 | 41 | +  -- FIXME  | 
 | 42 | +  extraSourceFile (ExtraSourceBuild f _) = unsafeCoerceSymbolicPath (relativeSymbolicPath f)  | 
30 | 43 | 
 
  | 
31 |  | -instance Parsec ExtraSource where  | 
 | 44 | +instance Binary (ExtraSource Pkg)  | 
 | 45 | +instance Structured (ExtraSource Pkg)  | 
 | 46 | +instance NFData (ExtraSource Pkg) where rnf = genericRnf  | 
 | 47 | + | 
 | 48 | +instance Binary (ExtraSource Build)  | 
 | 49 | +instance Structured (ExtraSource Build)  | 
 | 50 | +instance NFData (ExtraSource Build) where rnf = genericRnf  | 
 | 51 | + | 
 | 52 | +instance Parsec (ExtraSource Pkg) where  | 
32 | 53 |   parsec = do  | 
33 |  | -    SymbolicPathNT path <- parsec <* P.spaces  | 
34 |  | -    opts <- P.optional (parensLax (P.sepBy p  P.spaces))  | 
35 |  | -    return (ExtraSource path (fromMaybe mempty opts))  | 
 | 54 | +    path <- parsec <* P.spaces  | 
 | 55 | +    opts <- P.optional (parensLax (P.sepBy p P.spaces))  | 
 | 56 | +    return (ExtraSourcePkg path (fromMaybe mempty opts))  | 
36 | 57 |     where  | 
37 | 58 |       p :: P.CharParsing p => p String  | 
38 |  | -      p = some $ P.satisfy (\c -> not (isSpace c) && not (c == ')'))  | 
 | 59 | +      p = some $ P.satisfy (\c -> not (isSpace c) && (c /= ')'))  | 
39 | 60 | 
 
  | 
40 |  | -parensLax :: (P.CharParsing m) => m a -> m a  | 
41 |  | -parensLax p = P.between (P.char '(' *> P.spaces) (P.char ')' *> P.spaces) p  | 
 | 61 | +instance Parsec (ExtraSource Build) where  | 
 | 62 | +  parsec = do  | 
 | 63 | +    path <- parsec <* P.spaces  | 
 | 64 | +    opts <- P.optional (parensLax (P.sepBy p P.spaces))  | 
 | 65 | +    return (ExtraSourceBuild path (fromMaybe mempty opts))  | 
 | 66 | +    where  | 
 | 67 | +      p :: P.CharParsing p => p String  | 
 | 68 | +      p = some $ P.satisfy (\c -> not (isSpace c) && (c /= ')'))  | 
42 | 69 | 
 
  | 
43 |  | -instance Pretty ExtraSource where  | 
44 |  | -  pretty (ExtraSource path opts) =  | 
45 |  | -    pretty (SymbolicPathNT path) <<>> PP.parens (PP.hsep (map PP.text opts))  | 
 | 70 | +instance Pretty (ExtraSource Pkg) where  | 
 | 71 | +  pretty (ExtraSourcePkg path opts) =  | 
 | 72 | +    pretty path <<>> PP.parens (PP.hsep (map PP.text opts))  | 
46 | 73 | 
 
  | 
47 |  | -extraSourceFromPath :: SymbolicPath Pkg File -> ExtraSource  | 
48 |  | -extraSourceFromPath fp = ExtraSource fp mempty  | 
 | 74 | +instance Pretty (ExtraSource Build) where  | 
 | 75 | +  pretty (ExtraSourceBuild path opts) =  | 
 | 76 | +    pretty path <<>> PP.parens (PP.hsep (map PP.text opts))  | 
 | 77 | + | 
 | 78 | +parensLax :: P.CharParsing m => m a -> m a  | 
 | 79 | +parensLax p = P.between (P.char '(' *> P.spaces) (P.char ')' *> P.spaces) p  | 
0 commit comments