aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/PursLoader/Options.purs
diff options
context:
space:
mode:
authoreric thul <thul.eric@gmail.com>2015-08-11 20:27:04 -0400
committereric thul <thul.eric@gmail.com>2015-08-11 20:27:04 -0400
commitfa01c5a4cb42d80ac147dc5ab512a0795dbe14da (patch)
tree8bdb1f4c24a31c7fd5c69fcd804e8428ae6c49b6 /src/PursLoader/Options.purs
parenteae2e182cec2d521166e5775294b52300c42f069 (diff)
downloadpurs-loader-fa01c5a4cb42d80ac147dc5ab512a0795dbe14da.tar.gz
purs-loader-fa01c5a4cb42d80ac147dc5ab512a0795dbe14da.tar.zst
purs-loader-fa01c5a4cb42d80ac147dc5ab512a0795dbe14da.zip
Moving files to match module
Diffstat (limited to 'src/PursLoader/Options.purs')
-rw-r--r--src/PursLoader/Options.purs107
1 files changed, 107 insertions, 0 deletions
diff --git a/src/PursLoader/Options.purs b/src/PursLoader/Options.purs
new file mode 100644
index 0000000..51e9be5
--- /dev/null
+++ b/src/PursLoader/Options.purs
@@ -0,0 +1,107 @@
1module PursLoader.Options
2 ( pscOptions
3 , loaderSrcOption
4 , loaderFFIOption
5 ) where
6
7import Data.Array (concat)
8import Data.Either (either)
9
10import Data.Foreign (Foreign(), F())
11import Data.Foreign.Class (IsForeign, read, readProp)
12import Data.Foreign.NullOrUndefined (NullOrUndefined(..), runNullOrUndefined)
13
14import Data.Maybe (Maybe(..), maybe, fromMaybe)
15
16noPreludeOpt = "no-prelude"
17
18noOptsOpt = "no-opts"
19
20noMagicDoOpt = "no-magic-do"
21
22noTcoOpt = "no-tco"
23
24verboseErrorsOpt = "verbose-errors"
25
26outputOpt = "output"
27
28commentsOpt = "comments"
29
30noPrefixOpt = "no-prefix"
31
32requirePathOpt = "require-path"
33
34srcOpt = "src"
35
36ffiOpt = "ffi"
37
38newtype Options
39 = Options { noPrelude :: NullOrUndefined Boolean
40 , noOpts :: NullOrUndefined Boolean
41 , noMagicDo :: NullOrUndefined Boolean
42 , noTco :: NullOrUndefined Boolean
43 , verboseErrors :: NullOrUndefined Boolean
44 , comments :: NullOrUndefined Boolean
45 , output :: NullOrUndefined String
46 , noPrefix :: NullOrUndefined Boolean
47 , requirePath :: NullOrUndefined String
48 , src :: NullOrUndefined [String]
49 , ffi :: NullOrUndefined [String]
50 }
51
52instance isForeignOptions :: IsForeign Options where
53 read obj = Options <$> ({ noPrelude: _
54 , noOpts: _
55 , noMagicDo: _
56 , noTco: _
57 , verboseErrors: _
58 , comments: _
59 , output: _
60 , noPrefix: _
61 , requirePath: _
62 , src: _
63 , ffi: _
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
72 <*> readProp requirePathOpt obj
73 <*> readProp srcOpt obj
74 <*> readProp ffiOpt obj)
75
76class LoaderOption a where
77 opt :: String -> NullOrUndefined a -> [String]
78
79instance booleanLoaderOption :: LoaderOption Boolean where
80 opt key val = maybe [] (\a -> if a then ["--" ++ key] else []) (runNullOrUndefined val)
81
82instance stringLoaderOption :: LoaderOption String where
83 opt key val = maybe [] (\a -> ["--" ++ key ++ "=" ++ a]) (runNullOrUndefined val)
84
85instance arrayLoaderOption :: (LoaderOption a) => LoaderOption [a] where
86 opt key val = concat (opt key <$> (NullOrUndefined <<< Just)
87 <$> (fromMaybe [] (runNullOrUndefined val)))
88
89pscOptions :: Foreign -> [String]
90pscOptions query = either (const []) fold parsed
91 where parsed = read query :: F Options
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 <>
99 opt noPrefixOpt a.noPrefix <>
100 opt requirePathOpt a.requirePath <>
101 opt ffiOpt a.ffi
102
103loaderSrcOption :: Foreign -> Maybe [String]
104loaderSrcOption query = either (const Nothing) (\(Options a) -> runNullOrUndefined a.src) (read query)
105
106loaderFFIOption :: Foreign -> Maybe [String]
107loaderFFIOption query = either (const Nothing) (\(Options a) -> runNullOrUndefined a.ffi) (read query)