]> git.immae.eu Git - github/fretlink/purs-loader.git/blame - src/PursLoader/Options.purs
Moving files to match module
[github/fretlink/purs-loader.git] / src / PursLoader / Options.purs
CommitLineData
c194f84c 1module PursLoader.Options
1983893b 2 ( pscOptions
a72c8af1 3 , loaderSrcOption
0e1221d7 4 , loaderFFIOption
c194f84c 5 ) where
6
1983893b 7import Data.Array (concat)
c194f84c 8import Data.Either (either)
9
10import Data.Foreign (Foreign(), F())
11import Data.Foreign.Class (IsForeign, read, readProp)
1983893b 12import Data.Foreign.NullOrUndefined (NullOrUndefined(..), runNullOrUndefined)
c194f84c 13
1983893b 14import Data.Maybe (Maybe(..), maybe, fromMaybe)
c194f84c 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
a72c8af1 28commentsOpt = "comments"
29
30noPrefixOpt = "no-prefix"
31
1983893b 32requirePathOpt = "require-path"
33
a72c8af1 34srcOpt = "src"
35
1983893b 36ffiOpt = "ffi"
c194f84c 37
38newtype Options
39 = Options { noPrelude :: NullOrUndefined Boolean
40 , noOpts :: NullOrUndefined Boolean
41 , noMagicDo :: NullOrUndefined Boolean
42 , noTco :: NullOrUndefined Boolean
43 , verboseErrors :: NullOrUndefined Boolean
a72c8af1 44 , comments :: NullOrUndefined Boolean
c194f84c 45 , output :: NullOrUndefined String
a72c8af1 46 , noPrefix :: NullOrUndefined Boolean
1983893b 47 , requirePath :: NullOrUndefined String
a72c8af1 48 , src :: NullOrUndefined [String]
1983893b 49 , ffi :: NullOrUndefined [String]
c194f84c 50 }
51
52instance isForeignOptions :: IsForeign Options where
a72c8af1 53 read obj = Options <$> ({ noPrelude: _
54 , noOpts: _
55 , noMagicDo: _
56 , noTco: _
57 , verboseErrors: _
58 , comments: _
59 , output: _
60 , noPrefix: _
1983893b 61 , requirePath: _
a72c8af1 62 , src: _
1983893b 63 , ffi: _
a72c8af1 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
1983893b 72 <*> readProp requirePathOpt obj
73 <*> readProp srcOpt obj
74 <*> readProp ffiOpt obj)
a72c8af1 75
76class LoaderOption a where
77 opt :: String -> NullOrUndefined a -> [String]
78
79instance booleanLoaderOption :: LoaderOption Boolean where
1983893b 80 opt key val = maybe [] (\a -> if a then ["--" ++ key] else []) (runNullOrUndefined val)
a72c8af1 81
82instance stringLoaderOption :: LoaderOption String where
1983893b 83 opt key val = maybe [] (\a -> ["--" ++ key ++ "=" ++ a]) (runNullOrUndefined val)
c194f84c 84
1983893b 85instance arrayLoaderOption :: (LoaderOption a) => LoaderOption [a] where
86 opt key val = concat (opt key <$> (NullOrUndefined <<< Just)
87 <$> (fromMaybe [] (runNullOrUndefined val)))
c194f84c 88
1983893b 89pscOptions :: Foreign -> [String]
90pscOptions query = either (const []) fold parsed
c194f84c 91 where parsed = read query :: F Options
a72c8af1 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 <>
1983893b 99 opt noPrefixOpt a.noPrefix <>
100 opt requirePathOpt a.requirePath <>
101 opt ffiOpt a.ffi
a72c8af1 102
103loaderSrcOption :: Foreign -> Maybe [String]
1983893b 104loaderSrcOption query = either (const Nothing) (\(Options a) -> runNullOrUndefined a.src) (read query)
0e1221d7 105
106loaderFFIOption :: Foreign -> Maybe [String]
107loaderFFIOption query = either (const Nothing) (\(Options a) -> runNullOrUndefined a.ffi) (read query)