1 module PursLoader.Options
7 import Data.Array (concat)
8 import Data.Either (either)
10 import Data.Foreign (Foreign(), F())
11 import Data.Foreign.Class (IsForeign, read, readProp)
12 import Data.Foreign.NullOrUndefined (NullOrUndefined(..), runNullOrUndefined)
14 import Data.Maybe (Maybe(..), maybe, fromMaybe)
16 noPreludeOpt = "no-prelude"
20 noMagicDoOpt = "no-magic-do"
24 verboseErrorsOpt = "verbose-errors"
28 commentsOpt = "comments"
30 noPrefixOpt = "no-prefix"
32 requirePathOpt = "require-path"
39 = Options { noPrelude :: NullOrUndefined Boolean
40 , noOpts :: NullOrUndefined Boolean
41 , noMagicDo :: NullOrUndefined Boolean
42 , noTco :: NullOrUndefined Boolean
43 , verboseErrors :: NullOrUndefined Boolean
44 , comments :: NullOrUndefined Boolean
45 , output :: NullOrUndefined String
46 , noPrefix :: NullOrUndefined Boolean
47 , requirePath :: NullOrUndefined String
48 , src :: NullOrUndefined [String]
49 , ffi :: NullOrUndefined [String]
52 instance isForeignOptions :: IsForeign Options where
53 read obj = Options <$> ({ noPrelude: _
64 } <$> readProp noPreludeOpt obj
65 <*> readProp noOptsOpt obj
66 <*> readProp noMagicDoOpt obj
67 <*> readProp noTcoOpt obj
68 <*> readProp verboseErrorsOpt obj
69 <*> readProp commentsOpt obj
70 <*> readProp outputOpt obj
71 <*> readProp noPrefixOpt obj
72 <*> readProp requirePathOpt obj
73 <*> readProp srcOpt obj
74 <*> readProp ffiOpt obj)
76 class LoaderOption a where
77 opt :: String -> NullOrUndefined a -> [String]
79 instance booleanLoaderOption :: LoaderOption Boolean where
80 opt key val = maybe [] (\a -> if a then ["--" ++ key] else []) (runNullOrUndefined val)
82 instance stringLoaderOption :: LoaderOption String where
83 opt key val = maybe [] (\a -> ["--" ++ key ++ "=" ++ a]) (runNullOrUndefined val)
85 instance arrayLoaderOption :: (LoaderOption a) => LoaderOption [a] where
86 opt key val = concat (opt key <$> (NullOrUndefined <<< Just)
87 <$> (fromMaybe [] (runNullOrUndefined val)))
89 pscOptions :: Foreign -> [String]
90 pscOptions query = either (const []) fold parsed
91 where parsed = read query :: F Options
92 fold (Options a) = opt noPreludeOpt a.noPrelude <>
93 opt noOptsOpt a.noOpts <>
94 opt noMagicDoOpt a.noMagicDo <>
95 opt noTcoOpt a.noTco <>
96 opt verboseErrorsOpt a.verboseErrors <>
97 opt commentsOpt a.comments <>
98 opt outputOpt a.output <>
99 opt noPrefixOpt a.noPrefix <>
100 opt requirePathOpt a.requirePath <>
103 loaderSrcOption :: Foreign -> Maybe [String]
104 loaderSrcOption query = either (const Nothing) (\(Options a) -> runNullOrUndefined a.src) (read query)
106 loaderFFIOption :: Foreign -> Maybe [String]
107 loaderFFIOption query = either (const Nothing) (\(Options a) -> runNullOrUndefined a.ffi) (read query)