]>
Commit | Line | Data |
---|---|---|
c194f84c | 1 | module PursLoader.Options |
1983893b | 2 | ( pscOptions |
a72c8af1 | 3 | , loaderSrcOption |
0e1221d7 | 4 | , loaderFFIOption |
c194f84c | 5 | ) where |
6 | ||
03b840cb | 7 | import Prelude (Unit(), (<>), (<$>), (<<<), (++), (<*>), const) |
8 | ||
1983893b | 9 | import Data.Array (concat) |
c194f84c | 10 | import Data.Either (either) |
11 | ||
12 | import Data.Foreign (Foreign(), F()) | |
13 | import Data.Foreign.Class (IsForeign, read, readProp) | |
1983893b | 14 | import Data.Foreign.NullOrUndefined (NullOrUndefined(..), runNullOrUndefined) |
c194f84c | 15 | |
1983893b | 16 | import Data.Maybe (Maybe(..), maybe, fromMaybe) |
c194f84c | 17 | |
18 | noPreludeOpt = "no-prelude" | |
19 | ||
20 | noOptsOpt = "no-opts" | |
21 | ||
22 | noMagicDoOpt = "no-magic-do" | |
23 | ||
24 | noTcoOpt = "no-tco" | |
25 | ||
26 | verboseErrorsOpt = "verbose-errors" | |
27 | ||
28 | outputOpt = "output" | |
29 | ||
a72c8af1 | 30 | commentsOpt = "comments" |
31 | ||
32 | noPrefixOpt = "no-prefix" | |
33 | ||
1983893b | 34 | requirePathOpt = "require-path" |
35 | ||
a72c8af1 | 36 | srcOpt = "src" |
37 | ||
1983893b | 38 | ffiOpt = "ffi" |
c194f84c | 39 | |
40 | newtype Options | |
41 | = Options { noPrelude :: NullOrUndefined Boolean | |
42 | , noOpts :: NullOrUndefined Boolean | |
43 | , noMagicDo :: NullOrUndefined Boolean | |
44 | , noTco :: NullOrUndefined Boolean | |
45 | , verboseErrors :: NullOrUndefined Boolean | |
a72c8af1 | 46 | , comments :: NullOrUndefined Boolean |
c194f84c | 47 | , output :: NullOrUndefined String |
a72c8af1 | 48 | , noPrefix :: NullOrUndefined Boolean |
1983893b | 49 | , requirePath :: NullOrUndefined String |
03b840cb | 50 | , src :: NullOrUndefined (Array String) |
51 | , ffi :: NullOrUndefined (Array String) | |
c194f84c | 52 | } |
53 | ||
54 | instance isForeignOptions :: IsForeign Options where | |
a72c8af1 | 55 | read obj = Options <$> ({ noPrelude: _ |
56 | , noOpts: _ | |
57 | , noMagicDo: _ | |
58 | , noTco: _ | |
59 | , verboseErrors: _ | |
60 | , comments: _ | |
61 | , output: _ | |
62 | , noPrefix: _ | |
1983893b | 63 | , requirePath: _ |
a72c8af1 | 64 | , src: _ |
1983893b | 65 | , ffi: _ |
a72c8af1 | 66 | } <$> readProp noPreludeOpt obj |
67 | <*> readProp noOptsOpt obj | |
68 | <*> readProp noMagicDoOpt obj | |
69 | <*> readProp noTcoOpt obj | |
70 | <*> readProp verboseErrorsOpt obj | |
71 | <*> readProp commentsOpt obj | |
72 | <*> readProp outputOpt obj | |
73 | <*> readProp noPrefixOpt obj | |
1983893b | 74 | <*> readProp requirePathOpt obj |
75 | <*> readProp srcOpt obj | |
76 | <*> readProp ffiOpt obj) | |
a72c8af1 | 77 | |
78 | class LoaderOption a where | |
03b840cb | 79 | opt :: String -> NullOrUndefined a -> Array String |
a72c8af1 | 80 | |
81 | instance booleanLoaderOption :: LoaderOption Boolean where | |
1983893b | 82 | opt key val = maybe [] (\a -> if a then ["--" ++ key] else []) (runNullOrUndefined val) |
a72c8af1 | 83 | |
84 | instance stringLoaderOption :: LoaderOption String where | |
1983893b | 85 | opt key val = maybe [] (\a -> ["--" ++ key ++ "=" ++ a]) (runNullOrUndefined val) |
c194f84c | 86 | |
03b840cb | 87 | instance arrayLoaderOption :: (LoaderOption a) => LoaderOption (Array a) where |
1983893b | 88 | opt key val = concat (opt key <$> (NullOrUndefined <<< Just) |
89 | <$> (fromMaybe [] (runNullOrUndefined val))) | |
c194f84c | 90 | |
03b840cb | 91 | pscOptions :: Foreign -> Array String |
1983893b | 92 | pscOptions query = either (const []) fold parsed |
c194f84c | 93 | where parsed = read query :: F Options |
a72c8af1 | 94 | fold (Options a) = opt noPreludeOpt a.noPrelude <> |
95 | opt noOptsOpt a.noOpts <> | |
96 | opt noMagicDoOpt a.noMagicDo <> | |
97 | opt noTcoOpt a.noTco <> | |
98 | opt verboseErrorsOpt a.verboseErrors <> | |
99 | opt commentsOpt a.comments <> | |
100 | opt outputOpt a.output <> | |
1983893b | 101 | opt noPrefixOpt a.noPrefix <> |
102 | opt requirePathOpt a.requirePath <> | |
103 | opt ffiOpt a.ffi | |
a72c8af1 | 104 | |
03b840cb | 105 | loaderSrcOption :: Foreign -> Maybe (Array String) |
1983893b | 106 | loaderSrcOption query = either (const Nothing) (\(Options a) -> runNullOrUndefined a.src) (read query) |
0e1221d7 | 107 | |
03b840cb | 108 | loaderFFIOption :: Foreign -> Maybe (Array String) |
0e1221d7 | 109 | loaderFFIOption query = either (const Nothing) (\(Options a) -> runNullOrUndefined a.ffi) (read query) |