]> git.immae.eu Git - github/fretlink/purs-loader.git/blob - src/PursLoader/Options.purs
51e9be53cbecaaee4730caf7e1096ddb026afcb4
[github/fretlink/purs-loader.git] / src / PursLoader / Options.purs
1 module PursLoader.Options
2 ( pscOptions
3 , loaderSrcOption
4 , loaderFFIOption
5 ) where
6
7 import Data.Array (concat)
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, fromMaybe)
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
28 commentsOpt = "comments"
29
30 noPrefixOpt = "no-prefix"
31
32 requirePathOpt = "require-path"
33
34 srcOpt = "src"
35
36 ffiOpt = "ffi"
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
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
52 instance 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
76 class LoaderOption a where
77 opt :: String -> NullOrUndefined a -> [String]
78
79 instance booleanLoaderOption :: LoaderOption Boolean where
80 opt key val = maybe [] (\a -> if a then ["--" ++ key] else []) (runNullOrUndefined val)
81
82 instance stringLoaderOption :: LoaderOption String where
83 opt key val = maybe [] (\a -> ["--" ++ key ++ "=" ++ a]) (runNullOrUndefined val)
84
85 instance arrayLoaderOption :: (LoaderOption a) => LoaderOption [a] where
86 opt key val = concat (opt key <$> (NullOrUndefined <<< Just)
87 <$> (fromMaybe [] (runNullOrUndefined val)))
88
89 pscOptions :: Foreign -> [String]
90 pscOptions 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
103 loaderSrcOption :: Foreign -> Maybe [String]
104 loaderSrcOption query = either (const Nothing) (\(Options a) -> runNullOrUndefined a.src) (read query)
105
106 loaderFFIOption :: Foreign -> Maybe [String]
107 loaderFFIOption query = either (const Nothing) (\(Options a) -> runNullOrUndefined a.ffi) (read query)