diff options
Diffstat (limited to 'src/Options.purs')
-rw-r--r-- | src/Options.purs | 50 |
1 files changed, 29 insertions, 21 deletions
diff --git a/src/Options.purs b/src/Options.purs index c47bebc..51e9be5 100644 --- a/src/Options.purs +++ b/src/Options.purs | |||
@@ -1,17 +1,17 @@ | |||
1 | module PursLoader.Options | 1 | module PursLoader.Options |
2 | ( pscMakeOptions | 2 | ( pscOptions |
3 | , pscMakeDefaultOutput | ||
4 | , pscMakeOutputOption | ||
5 | , loaderSrcOption | 3 | , loaderSrcOption |
4 | , loaderFFIOption | ||
6 | ) where | 5 | ) where |
7 | 6 | ||
7 | import Data.Array (concat) | ||
8 | import Data.Either (either) | 8 | import Data.Either (either) |
9 | 9 | ||
10 | import Data.Foreign (Foreign(), F()) | 10 | import Data.Foreign (Foreign(), F()) |
11 | import Data.Foreign.Class (IsForeign, read, readProp) | 11 | import Data.Foreign.Class (IsForeign, read, readProp) |
12 | import Data.Foreign.NullOrUndefined (NullOrUndefined(), runNullOrUndefined) | 12 | import Data.Foreign.NullOrUndefined (NullOrUndefined(..), runNullOrUndefined) |
13 | 13 | ||
14 | import Data.Maybe (Maybe(..), maybe) | 14 | import Data.Maybe (Maybe(..), maybe, fromMaybe) |
15 | 15 | ||
16 | noPreludeOpt = "no-prelude" | 16 | noPreludeOpt = "no-prelude" |
17 | 17 | ||
@@ -29,9 +29,11 @@ commentsOpt = "comments" | |||
29 | 29 | ||
30 | noPrefixOpt = "no-prefix" | 30 | noPrefixOpt = "no-prefix" |
31 | 31 | ||
32 | requirePathOpt = "require-path" | ||
33 | |||
32 | srcOpt = "src" | 34 | srcOpt = "src" |
33 | 35 | ||
34 | pscMakeDefaultOutput = "output" | 36 | ffiOpt = "ffi" |
35 | 37 | ||
36 | newtype Options | 38 | newtype Options |
37 | = Options { noPrelude :: NullOrUndefined Boolean | 39 | = Options { noPrelude :: NullOrUndefined Boolean |
@@ -42,7 +44,9 @@ newtype Options | |||
42 | , comments :: NullOrUndefined Boolean | 44 | , comments :: NullOrUndefined Boolean |
43 | , output :: NullOrUndefined String | 45 | , output :: NullOrUndefined String |
44 | , noPrefix :: NullOrUndefined Boolean | 46 | , noPrefix :: NullOrUndefined Boolean |
47 | , requirePath :: NullOrUndefined String | ||
45 | , src :: NullOrUndefined [String] | 48 | , src :: NullOrUndefined [String] |
49 | , ffi :: NullOrUndefined [String] | ||
46 | } | 50 | } |
47 | 51 | ||
48 | instance isForeignOptions :: IsForeign Options where | 52 | instance isForeignOptions :: IsForeign Options where |
@@ -54,7 +58,9 @@ instance isForeignOptions :: IsForeign Options where | |||
54 | , comments: _ | 58 | , comments: _ |
55 | , output: _ | 59 | , output: _ |
56 | , noPrefix: _ | 60 | , noPrefix: _ |
61 | , requirePath: _ | ||
57 | , src: _ | 62 | , src: _ |
63 | , ffi: _ | ||
58 | } <$> readProp noPreludeOpt obj | 64 | } <$> readProp noPreludeOpt obj |
59 | <*> readProp noOptsOpt obj | 65 | <*> readProp noOptsOpt obj |
60 | <*> readProp noMagicDoOpt obj | 66 | <*> readProp noMagicDoOpt obj |
@@ -63,26 +69,25 @@ instance isForeignOptions :: IsForeign Options where | |||
63 | <*> readProp commentsOpt obj | 69 | <*> readProp commentsOpt obj |
64 | <*> readProp outputOpt obj | 70 | <*> readProp outputOpt obj |
65 | <*> readProp noPrefixOpt obj | 71 | <*> readProp noPrefixOpt obj |
66 | <*> readProp srcOpt obj) | 72 | <*> readProp requirePathOpt obj |
73 | <*> readProp srcOpt obj | ||
74 | <*> readProp ffiOpt obj) | ||
67 | 75 | ||
68 | class LoaderOption a where | 76 | class LoaderOption a where |
69 | opt :: String -> NullOrUndefined a -> [String] | 77 | opt :: String -> NullOrUndefined a -> [String] |
70 | 78 | ||
71 | instance booleanLoaderOption :: LoaderOption Boolean where | 79 | instance booleanLoaderOption :: LoaderOption Boolean where |
72 | opt key opt = maybe [] (\a -> if a then ["--" ++ key] else []) | 80 | opt key val = maybe [] (\a -> if a then ["--" ++ key] else []) (runNullOrUndefined val) |
73 | (runNullOrUndefined opt) | ||
74 | 81 | ||
75 | instance stringLoaderOption :: LoaderOption String where | 82 | instance stringLoaderOption :: LoaderOption String where |
76 | opt key opt = maybe [] (\a -> ["--" ++ key ++ "=" ++ a]) | 83 | opt key val = maybe [] (\a -> ["--" ++ key ++ "=" ++ a]) (runNullOrUndefined val) |
77 | (runNullOrUndefined opt) | ||
78 | 84 | ||
79 | pscMakeOutputOption :: Foreign -> Maybe String | 85 | instance arrayLoaderOption :: (LoaderOption a) => LoaderOption [a] where |
80 | pscMakeOutputOption query = either (const Nothing) | 86 | opt key val = concat (opt key <$> (NullOrUndefined <<< Just) |
81 | (\(Options a) -> runNullOrUndefined a.output) | 87 | <$> (fromMaybe [] (runNullOrUndefined val))) |
82 | (read query) | ||
83 | 88 | ||
84 | pscMakeOptions :: Foreign -> [String] | 89 | pscOptions :: Foreign -> [String] |
85 | pscMakeOptions query = either (const []) fold parsed | 90 | pscOptions query = either (const []) fold parsed |
86 | where parsed = read query :: F Options | 91 | where parsed = read query :: F Options |
87 | fold (Options a) = opt noPreludeOpt a.noPrelude <> | 92 | fold (Options a) = opt noPreludeOpt a.noPrelude <> |
88 | opt noOptsOpt a.noOpts <> | 93 | opt noOptsOpt a.noOpts <> |
@@ -91,9 +96,12 @@ pscMakeOptions query = either (const []) fold parsed | |||
91 | opt verboseErrorsOpt a.verboseErrors <> | 96 | opt verboseErrorsOpt a.verboseErrors <> |
92 | opt commentsOpt a.comments <> | 97 | opt commentsOpt a.comments <> |
93 | opt outputOpt a.output <> | 98 | opt outputOpt a.output <> |
94 | opt noPrefixOpt a.noPrefix | 99 | opt noPrefixOpt a.noPrefix <> |
100 | opt requirePathOpt a.requirePath <> | ||
101 | opt ffiOpt a.ffi | ||
95 | 102 | ||
96 | loaderSrcOption :: Foreign -> Maybe [String] | 103 | loaderSrcOption :: Foreign -> Maybe [String] |
97 | loaderSrcOption query = either (const Nothing) | 104 | loaderSrcOption query = either (const Nothing) (\(Options a) -> runNullOrUndefined a.src) (read query) |
98 | (\(Options a) -> runNullOrUndefined a.src) | 105 | |
99 | (read query) | 106 | loaderFFIOption :: Foreign -> Maybe [String] |
107 | loaderFFIOption query = either (const Nothing) (\(Options a) -> runNullOrUndefined a.ffi) (read query) | ||