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