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