diff options
author | eric <thul.eric@gmail.com> | 2015-04-12 11:35:25 -0400 |
---|---|---|
committer | eric <thul.eric@gmail.com> | 2015-04-12 11:35:25 -0400 |
commit | 7d58cf78c770ef95858f90c6e89bd18653657a82 (patch) | |
tree | ebae7549b1a1b14e11fd5e2ad4dd66d901af81c8 /src/Options.purs | |
parent | 18ced1e0f031df444a5d85d72cd6843d826fef38 (diff) | |
parent | 94a23e744896b4440794de5d6cffedff1a1a2d56 (diff) | |
download | purs-loader-7d58cf78c770ef95858f90c6e89bd18653657a82.tar.gz purs-loader-7d58cf78c770ef95858f90c6e89bd18653657a82.tar.zst purs-loader-7d58cf78c770ef95858f90c6e89bd18653657a82.zip |
Merge pull request #8 from ethul/topic/purescript-rewrite
Topic/purescript rewrite
Diffstat (limited to 'src/Options.purs')
-rw-r--r-- | src/Options.purs | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/src/Options.purs b/src/Options.purs new file mode 100644 index 0000000..b96cddc --- /dev/null +++ b/src/Options.purs | |||
@@ -0,0 +1,72 @@ | |||
1 | module PursLoader.Options | ||
2 | ( pscMakeOptions | ||
3 | , pscMakeDefaultOutput | ||
4 | , pscMakeOutputOption | ||
5 | ) where | ||
6 | |||
7 | import Data.Either (either) | ||
8 | |||
9 | import Data.Foreign (Foreign(), F()) | ||
10 | import Data.Foreign.Class (IsForeign, read, readProp) | ||
11 | import Data.Foreign.NullOrUndefined (NullOrUndefined(), runNullOrUndefined) | ||
12 | |||
13 | import Data.Maybe (Maybe(..), maybe) | ||
14 | |||
15 | noPreludeOpt = "no-prelude" | ||
16 | |||
17 | noOptsOpt = "no-opts" | ||
18 | |||
19 | noMagicDoOpt = "no-magic-do" | ||
20 | |||
21 | noTcoOpt = "no-tco" | ||
22 | |||
23 | verboseErrorsOpt = "verbose-errors" | ||
24 | |||
25 | outputOpt = "output" | ||
26 | |||
27 | pscMakeDefaultOutput = "output" | ||
28 | |||
29 | newtype Options | ||
30 | = Options { noPrelude :: NullOrUndefined Boolean | ||
31 | , noOpts :: NullOrUndefined Boolean | ||
32 | , noMagicDo :: NullOrUndefined Boolean | ||
33 | , noTco :: NullOrUndefined Boolean | ||
34 | , verboseErrors :: NullOrUndefined Boolean | ||
35 | , output :: NullOrUndefined String | ||
36 | } | ||
37 | |||
38 | instance isForeignOptions :: IsForeign Options where | ||
39 | read obj = (\a b c d e f -> | ||
40 | Options { noPrelude: a | ||
41 | , noOpts: b | ||
42 | , noMagicDo: c | ||
43 | , noTco: d | ||
44 | , verboseErrors: e | ||
45 | , output: f | ||
46 | }) <$> readProp noPreludeOpt obj | ||
47 | <*> readProp noOptsOpt obj | ||
48 | <*> readProp noMagicDoOpt obj | ||
49 | <*> readProp noTcoOpt obj | ||
50 | <*> readProp verboseErrorsOpt obj | ||
51 | <*> readProp outputOpt obj | ||
52 | |||
53 | booleanOpt :: String -> NullOrUndefined Boolean -> [String] | ||
54 | booleanOpt key opt = maybe [] (\a -> if a then ["--" ++ key] else []) (runNullOrUndefined opt) | ||
55 | |||
56 | stringOpt :: String -> NullOrUndefined String -> [String] | ||
57 | stringOpt key opt = maybe [] (\a -> ["--" ++ key ++ "=" ++ a]) (runNullOrUndefined opt) | ||
58 | |||
59 | pscMakeOutputOption :: Foreign -> Maybe String | ||
60 | pscMakeOutputOption query = either (const Nothing) | ||
61 | (\(Options a) -> runNullOrUndefined a.output) | ||
62 | (read query) | ||
63 | |||
64 | pscMakeOptions :: Foreign -> [String] | ||
65 | pscMakeOptions query = either (const []) fold parsed | ||
66 | where parsed = read query :: F Options | ||
67 | fold (Options a) = booleanOpt noPreludeOpt a.noPrelude <> | ||
68 | booleanOpt noOptsOpt a.noOpts <> | ||
69 | booleanOpt noMagicDoOpt a.noMagicDo <> | ||
70 | booleanOpt noTcoOpt a.noTco <> | ||
71 | booleanOpt verboseErrorsOpt a.verboseErrors <> | ||
72 | stringOpt outputOpt a.output | ||