]> git.immae.eu Git - github/fretlink/purs-loader.git/blob - src/PursLoader/Options.purs
Fixing compiler warnings
[github/fretlink/purs-loader.git] / src / PursLoader / Options.purs
1 module PursLoader.Options
2 ( pscOptions
3 , loaderSrcOption
4 , loaderFFIOption
5 , Options()
6 , output
7 ) where
8
9 import Prelude ((<>), (<$>), (<<<), (++), (<*>), ($), const, id)
10
11 import Data.Array (concat)
12 import Data.Either (either)
13
14 import Data.Foreign (Foreign())
15 import Data.Foreign.Class (IsForeign, read, readProp)
16 import Data.Foreign.NullOrUndefined (NullOrUndefined(..), runNullOrUndefined)
17
18 import Data.Maybe (Maybe(..), maybe, fromMaybe)
19
20 noPreludeOpt :: String
21 noPreludeOpt = "no-prelude"
22
23 noOptsOpt :: String
24 noOptsOpt = "no-opts"
25
26 noMagicDoOpt :: String
27 noMagicDoOpt = "no-magic-do"
28
29 noTcoOpt :: String
30 noTcoOpt = "no-tco"
31
32 verboseErrorsOpt :: String
33 verboseErrorsOpt = "verbose-errors"
34
35 outputOpt :: String
36 outputOpt = "output"
37
38 commentsOpt :: String
39 commentsOpt = "comments"
40
41 noPrefixOpt :: String
42 noPrefixOpt = "no-prefix"
43
44 requirePathOpt :: String
45 requirePathOpt = "require-path"
46
47 srcOpt :: String
48 srcOpt = "src"
49
50 ffiOpt :: String
51 ffiOpt = "ffi"
52
53 newtype Options
54 = Options { noPrelude :: NullOrUndefined Boolean
55 , noOpts :: NullOrUndefined Boolean
56 , noMagicDo :: NullOrUndefined Boolean
57 , noTco :: NullOrUndefined Boolean
58 , verboseErrors :: NullOrUndefined Boolean
59 , comments :: NullOrUndefined Boolean
60 , output :: String
61 , noPrefix :: NullOrUndefined Boolean
62 , requirePath :: String
63 , src :: NullOrUndefined (Array String)
64 , ffi :: NullOrUndefined (Array String)
65 }
66
67 output :: Options -> String
68 output (Options o) = o.output
69
70 instance isForeignOptions :: IsForeign Options where
71 read obj = Options <$> ({ noPrelude: _
72 , noOpts: _
73 , noMagicDo: _
74 , noTco: _
75 , verboseErrors: _
76 , comments: _
77 , output: _
78 , noPrefix: _
79 , requirePath: "../"
80 , src: _
81 , ffi: _
82 } <$> readProp noPreludeOpt obj
83 <*> readProp noOptsOpt obj
84 <*> readProp noMagicDoOpt obj
85 <*> readProp noTcoOpt obj
86 <*> readProp verboseErrorsOpt obj
87 <*> readProp commentsOpt obj
88 <*> (maybe "output" id <<< runNullOrUndefined <$> readProp outputOpt obj)
89 <*> readProp noPrefixOpt obj
90 <*> readProp srcOpt obj
91 <*> readProp ffiOpt obj)
92
93 class LoaderOption a where
94 opt :: String -> NullOrUndefined a -> Array String
95
96 instance booleanLoaderOption :: LoaderOption Boolean where
97 opt key val = maybe [] (\a -> if a then ["--" ++ key] else []) (runNullOrUndefined val)
98
99 instance stringLoaderOption :: LoaderOption String where
100 opt key val = maybe [] (\a -> ["--" ++ key ++ "=" ++ a]) (runNullOrUndefined val)
101
102 instance arrayLoaderOption :: (LoaderOption a) => LoaderOption (Array a) where
103 opt key val = concat (opt key <$> (NullOrUndefined <<< Just)
104 <$> (fromMaybe [] (runNullOrUndefined val)))
105
106 pscOptions :: Options -> Array String
107 pscOptions (Options a) = opt noPreludeOpt a.noPrelude <>
108 opt noOptsOpt a.noOpts <>
109 opt noMagicDoOpt a.noMagicDo <>
110 opt noTcoOpt a.noTco <>
111 opt verboseErrorsOpt a.verboseErrors <>
112 opt commentsOpt a.comments <>
113 opt outputOpt (NullOrUndefined $ Just a.output) <>
114 opt noPrefixOpt a.noPrefix <>
115 opt requirePathOpt (NullOrUndefined $ Just a.requirePath) <>
116 opt ffiOpt a.ffi
117
118 loaderSrcOption :: Foreign -> Maybe (Array String)
119 loaderSrcOption query = either (const Nothing) (\(Options a) -> runNullOrUndefined a.src) (read query)
120
121 loaderFFIOption :: Foreign -> Maybe (Array String)
122 loaderFFIOption query = either (const Nothing) (\(Options a) -> runNullOrUndefined a.ffi) (read query)