]>
Commit | Line | Data |
---|---|---|
c194f84c | 1 | module PursLoader.Options |
2 | ( pscMakeOptions | |
3 | , pscMakeDefaultOutput | |
4 | , pscMakeOutputOption | |
a72c8af1 | 5 | , loaderSrcOption |
c194f84c | 6 | ) where |
7 | ||
8 | import Data.Either (either) | |
9 | ||
10 | import Data.Foreign (Foreign(), F()) | |
11 | import Data.Foreign.Class (IsForeign, read, readProp) | |
12 | import Data.Foreign.NullOrUndefined (NullOrUndefined(), runNullOrUndefined) | |
13 | ||
14 | import Data.Maybe (Maybe(..), maybe) | |
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 | ||
32 | srcOpt = "src" | |
33 | ||
c194f84c | 34 | pscMakeDefaultOutput = "output" |
35 | ||
36 | newtype Options | |
37 | = Options { noPrelude :: NullOrUndefined Boolean | |
38 | , noOpts :: NullOrUndefined Boolean | |
39 | , noMagicDo :: NullOrUndefined Boolean | |
40 | , noTco :: NullOrUndefined Boolean | |
41 | , verboseErrors :: NullOrUndefined Boolean | |
a72c8af1 | 42 | , comments :: NullOrUndefined Boolean |
c194f84c | 43 | , output :: NullOrUndefined String |
a72c8af1 | 44 | , noPrefix :: NullOrUndefined Boolean |
45 | , src :: NullOrUndefined [String] | |
c194f84c | 46 | } |
47 | ||
48 | instance isForeignOptions :: IsForeign Options where | |
a72c8af1 | 49 | read obj = Options <$> ({ noPrelude: _ |
50 | , noOpts: _ | |
51 | , noMagicDo: _ | |
52 | , noTco: _ | |
53 | , verboseErrors: _ | |
54 | , comments: _ | |
55 | , output: _ | |
56 | , noPrefix: _ | |
57 | , src: _ | |
58 | } <$> readProp noPreludeOpt obj | |
59 | <*> readProp noOptsOpt obj | |
60 | <*> readProp noMagicDoOpt obj | |
61 | <*> readProp noTcoOpt obj | |
62 | <*> readProp verboseErrorsOpt obj | |
63 | <*> readProp commentsOpt obj | |
64 | <*> readProp outputOpt obj | |
65 | <*> readProp noPrefixOpt obj | |
66 | <*> readProp srcOpt obj) | |
67 | ||
68 | class LoaderOption a where | |
69 | opt :: String -> NullOrUndefined a -> [String] | |
70 | ||
71 | instance booleanLoaderOption :: LoaderOption Boolean where | |
72 | opt key opt = maybe [] (\a -> if a then ["--" ++ key] else []) | |
73 | (runNullOrUndefined opt) | |
74 | ||
75 | instance stringLoaderOption :: LoaderOption String where | |
76 | opt key opt = maybe [] (\a -> ["--" ++ key ++ "=" ++ a]) | |
77 | (runNullOrUndefined opt) | |
c194f84c | 78 | |
79 | pscMakeOutputOption :: Foreign -> Maybe String | |
80 | pscMakeOutputOption query = either (const Nothing) | |
81 | (\(Options a) -> runNullOrUndefined a.output) | |
82 | (read query) | |
83 | ||
84 | pscMakeOptions :: Foreign -> [String] | |
85 | pscMakeOptions query = either (const []) fold parsed | |
86 | where parsed = read query :: F Options | |
a72c8af1 | 87 | fold (Options a) = opt noPreludeOpt a.noPrelude <> |
88 | opt noOptsOpt a.noOpts <> | |
89 | opt noMagicDoOpt a.noMagicDo <> | |
90 | opt noTcoOpt a.noTco <> | |
91 | opt verboseErrorsOpt a.verboseErrors <> | |
92 | opt commentsOpt a.comments <> | |
93 | opt outputOpt a.output <> | |
94 | opt noPrefixOpt a.noPrefix | |
95 | ||
96 | loaderSrcOption :: Foreign -> Maybe [String] | |
97 | loaderSrcOption query = either (const Nothing) | |
98 | (\(Options a) -> runNullOrUndefined a.src) | |
99 | (read query) |