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