aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/PursLoader/Options.purs
diff options
context:
space:
mode:
authoreric <thul.eric@gmail.com>2015-08-11 21:23:46 -0400
committereric <thul.eric@gmail.com>2015-08-11 21:23:46 -0400
commit7226ff961c104037e8e5d3705949e2e9d58a6727 (patch)
tree555547366ccadfe71689b95a6b16b855b6173364 /src/PursLoader/Options.purs
parenteae2e182cec2d521166e5775294b52300c42f069 (diff)
parent07de44be15efb0ca9a2d1443ab1e91076a25409c (diff)
downloadpurs-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.purs109
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 @@
1module PursLoader.Options
2 ( pscOptions
3 , loaderSrcOption
4 , loaderFFIOption
5 ) where
6
7import Prelude (Unit(), (<>), (<$>), (<<<), (++), (<*>), const)
8
9import Data.Array (concat)
10import Data.Either (either)
11
12import Data.Foreign (Foreign(), F())
13import Data.Foreign.Class (IsForeign, read, readProp)
14import Data.Foreign.NullOrUndefined (NullOrUndefined(..), runNullOrUndefined)
15
16import Data.Maybe (Maybe(..), maybe, fromMaybe)
17
18noPreludeOpt = "no-prelude"
19
20noOptsOpt = "no-opts"
21
22noMagicDoOpt = "no-magic-do"
23
24noTcoOpt = "no-tco"
25
26verboseErrorsOpt = "verbose-errors"
27
28outputOpt = "output"
29
30commentsOpt = "comments"
31
32noPrefixOpt = "no-prefix"
33
34requirePathOpt = "require-path"
35
36srcOpt = "src"
37
38ffiOpt = "ffi"
39
40newtype 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
54instance 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
78class LoaderOption a where
79 opt :: String -> NullOrUndefined a -> Array String
80
81instance booleanLoaderOption :: LoaderOption Boolean where
82 opt key val = maybe [] (\a -> if a then ["--" ++ key] else []) (runNullOrUndefined val)
83
84instance stringLoaderOption :: LoaderOption String where
85 opt key val = maybe [] (\a -> ["--" ++ key ++ "=" ++ a]) (runNullOrUndefined val)
86
87instance arrayLoaderOption :: (LoaderOption a) => LoaderOption (Array a) where
88 opt key val = concat (opt key <$> (NullOrUndefined <<< Just)
89 <$> (fromMaybe [] (runNullOrUndefined val)))
90
91pscOptions :: Foreign -> Array String
92pscOptions 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
105loaderSrcOption :: Foreign -> Maybe (Array String)
106loaderSrcOption query = either (const Nothing) (\(Options a) -> runNullOrUndefined a.src) (read query)
107
108loaderFFIOption :: Foreign -> Maybe (Array String)
109loaderFFIOption query = either (const Nothing) (\(Options a) -> runNullOrUndefined a.ffi) (read query)