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