diff options
author | eric <thul.eric@gmail.com> | 2015-08-11 21:23:46 -0400 |
---|---|---|
committer | eric <thul.eric@gmail.com> | 2015-08-11 21:23:46 -0400 |
commit | 7226ff961c104037e8e5d3705949e2e9d58a6727 (patch) | |
tree | 555547366ccadfe71689b95a6b16b855b6173364 /src/PursLoader/Options.purs | |
parent | eae2e182cec2d521166e5775294b52300c42f069 (diff) | |
parent | 07de44be15efb0ca9a2d1443ab1e91076a25409c (diff) | |
download | purs-loader-7226ff961c104037e8e5d3705949e2e9d58a6727.tar.gz purs-loader-7226ff961c104037e8e5d3705949e2e9d58a6727.tar.zst purs-loader-7226ff961c104037e8e5d3705949e2e9d58a6727.zip |
Merge pull request #27 from ethul/topic/issue-26
Topic/issue 26
Diffstat (limited to 'src/PursLoader/Options.purs')
-rw-r--r-- | src/PursLoader/Options.purs | 109 |
1 files changed, 109 insertions, 0 deletions
diff --git a/src/PursLoader/Options.purs b/src/PursLoader/Options.purs new file mode 100644 index 0000000..e3957eb --- /dev/null +++ b/src/PursLoader/Options.purs | |||
@@ -0,0 +1,109 @@ | |||
1 | module PursLoader.Options | ||
2 | ( pscOptions | ||
3 | , loaderSrcOption | ||
4 | , loaderFFIOption | ||
5 | ) where | ||
6 | |||
7 | import Prelude (Unit(), (<>), (<$>), (<<<), (++), (<*>), const) | ||
8 | |||
9 | import Data.Array (concat) | ||
10 | import Data.Either (either) | ||
11 | |||
12 | import Data.Foreign (Foreign(), F()) | ||
13 | import Data.Foreign.Class (IsForeign, read, readProp) | ||
14 | import Data.Foreign.NullOrUndefined (NullOrUndefined(..), runNullOrUndefined) | ||
15 | |||
16 | import Data.Maybe (Maybe(..), maybe, fromMaybe) | ||
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 | |||
30 | commentsOpt = "comments" | ||
31 | |||
32 | noPrefixOpt = "no-prefix" | ||
33 | |||
34 | requirePathOpt = "require-path" | ||
35 | |||
36 | srcOpt = "src" | ||
37 | |||
38 | ffiOpt = "ffi" | ||
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 | ||
46 | , comments :: NullOrUndefined Boolean | ||
47 | , output :: NullOrUndefined String | ||
48 | , noPrefix :: NullOrUndefined Boolean | ||
49 | , requirePath :: NullOrUndefined String | ||
50 | , src :: NullOrUndefined (Array String) | ||
51 | , ffi :: NullOrUndefined (Array String) | ||
52 | } | ||
53 | |||
54 | instance isForeignOptions :: IsForeign Options where | ||
55 | read obj = Options <$> ({ noPrelude: _ | ||
56 | , noOpts: _ | ||
57 | , noMagicDo: _ | ||
58 | , noTco: _ | ||
59 | , verboseErrors: _ | ||
60 | , comments: _ | ||
61 | , output: _ | ||
62 | , noPrefix: _ | ||
63 | , requirePath: _ | ||
64 | , src: _ | ||
65 | , ffi: _ | ||
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 | ||
74 | <*> readProp requirePathOpt obj | ||
75 | <*> readProp srcOpt obj | ||
76 | <*> readProp ffiOpt obj) | ||
77 | |||
78 | class LoaderOption a where | ||
79 | opt :: String -> NullOrUndefined a -> Array String | ||
80 | |||
81 | instance booleanLoaderOption :: LoaderOption Boolean where | ||
82 | opt key val = maybe [] (\a -> if a then ["--" ++ key] else []) (runNullOrUndefined val) | ||
83 | |||
84 | instance stringLoaderOption :: LoaderOption String where | ||
85 | opt key val = maybe [] (\a -> ["--" ++ key ++ "=" ++ a]) (runNullOrUndefined val) | ||
86 | |||
87 | instance arrayLoaderOption :: (LoaderOption a) => LoaderOption (Array a) where | ||
88 | opt key val = concat (opt key <$> (NullOrUndefined <<< Just) | ||
89 | <$> (fromMaybe [] (runNullOrUndefined val))) | ||
90 | |||
91 | pscOptions :: Foreign -> Array String | ||
92 | pscOptions query = either (const []) fold parsed | ||
93 | where parsed = read query :: F Options | ||
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 <> | ||
101 | opt noPrefixOpt a.noPrefix <> | ||
102 | opt requirePathOpt a.requirePath <> | ||
103 | opt ffiOpt a.ffi | ||
104 | |||
105 | loaderSrcOption :: Foreign -> Maybe (Array String) | ||
106 | loaderSrcOption query = either (const Nothing) (\(Options a) -> runNullOrUndefined a.src) (read query) | ||
107 | |||
108 | loaderFFIOption :: Foreign -> Maybe (Array String) | ||
109 | loaderFFIOption query = either (const Nothing) (\(Options a) -> runNullOrUndefined a.ffi) (read query) | ||