1 module PursLoader.Options
9 import Prelude ((<>), (<$>), (<<<), (++), (<*>), ($), const, id)
11 import Data.Array (concat)
12 import Data.Either (either)
14 import Data.Foreign (Foreign())
15 import Data.Foreign.Class (IsForeign, read, readProp)
16 import Data.Foreign.NullOrUndefined (NullOrUndefined(..), runNullOrUndefined)
18 import Data.Maybe (Maybe(..), maybe, fromMaybe)
20 noPreludeOpt :: String
21 noPreludeOpt = "no-prelude"
26 noMagicDoOpt :: String
27 noMagicDoOpt = "no-magic-do"
32 verboseErrorsOpt :: String
33 verboseErrorsOpt = "verbose-errors"
39 commentsOpt = "comments"
42 noPrefixOpt = "no-prefix"
44 requirePathOpt :: String
45 requirePathOpt = "require-path"
54 = Options { noPrelude :: NullOrUndefined Boolean
55 , noOpts :: NullOrUndefined Boolean
56 , noMagicDo :: NullOrUndefined Boolean
57 , noTco :: NullOrUndefined Boolean
58 , verboseErrors :: NullOrUndefined Boolean
59 , comments :: NullOrUndefined Boolean
61 , noPrefix :: NullOrUndefined Boolean
62 , requirePath :: String
63 , src :: NullOrUndefined (Array String)
64 , ffi :: NullOrUndefined (Array String)
67 output :: Options -> String
68 output (Options o) = o.output
70 instance isForeignOptions :: IsForeign Options where
71 read obj = Options <$> ({ noPrelude: _
82 } <$> readProp noPreludeOpt obj
83 <*> readProp noOptsOpt obj
84 <*> readProp noMagicDoOpt obj
85 <*> readProp noTcoOpt obj
86 <*> readProp verboseErrorsOpt obj
87 <*> readProp commentsOpt obj
88 <*> (maybe "output" id <<< runNullOrUndefined <$> readProp outputOpt obj)
89 <*> readProp noPrefixOpt obj
90 <*> readProp srcOpt obj
91 <*> readProp ffiOpt obj)
93 class LoaderOption a where
94 opt :: String -> NullOrUndefined a -> Array String
96 instance booleanLoaderOption :: LoaderOption Boolean where
97 opt key val = maybe [] (\a -> if a then ["--" ++ key] else []) (runNullOrUndefined val)
99 instance stringLoaderOption :: LoaderOption String where
100 opt key val = maybe [] (\a -> ["--" ++ key ++ "=" ++ a]) (runNullOrUndefined val)
102 instance arrayLoaderOption :: (LoaderOption a) => LoaderOption (Array a) where
103 opt key val = concat (opt key <$> (NullOrUndefined <<< Just)
104 <$> (fromMaybe [] (runNullOrUndefined val)))
106 pscOptions :: Options -> Array String
107 pscOptions (Options a) = opt noPreludeOpt a.noPrelude <>
108 opt noOptsOpt a.noOpts <>
109 opt noMagicDoOpt a.noMagicDo <>
110 opt noTcoOpt a.noTco <>
111 opt verboseErrorsOpt a.verboseErrors <>
112 opt commentsOpt a.comments <>
113 opt outputOpt (NullOrUndefined $ Just a.output) <>
114 opt noPrefixOpt a.noPrefix <>
115 opt requirePathOpt (NullOrUndefined $ Just a.requirePath) <>
118 loaderSrcOption :: Foreign -> Maybe (Array String)
119 loaderSrcOption query = either (const Nothing) (\(Options a) -> runNullOrUndefined a.src) (read query)
121 loaderFFIOption :: Foreign -> Maybe (Array String)
122 loaderFFIOption query = either (const Nothing) (\(Options a) -> runNullOrUndefined a.ffi) (read query)