]> git.immae.eu Git - github/fretlink/purs-loader.git/blame - src/Options.purs
Bumping version number to 0.2.1
[github/fretlink/purs-loader.git] / src / Options.purs
CommitLineData
c194f84c 1module PursLoader.Options
2 ( pscMakeOptions
3 , pscMakeDefaultOutput
4 , pscMakeOutputOption
a72c8af1 5 , loaderSrcOption
c194f84c 6 ) where
7
8import Data.Either (either)
9
10import Data.Foreign (Foreign(), F())
11import Data.Foreign.Class (IsForeign, read, readProp)
12import Data.Foreign.NullOrUndefined (NullOrUndefined(), runNullOrUndefined)
13
14import Data.Maybe (Maybe(..), maybe)
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
32srcOpt = "src"
33
c194f84c 34pscMakeDefaultOutput = "output"
35
36newtype Options
37 = Options { noPrelude :: NullOrUndefined Boolean
38 , noOpts :: NullOrUndefined Boolean
39 , noMagicDo :: NullOrUndefined Boolean
40 , noTco :: NullOrUndefined Boolean
41 , verboseErrors :: NullOrUndefined Boolean
a72c8af1 42 , comments :: NullOrUndefined Boolean
c194f84c 43 , output :: NullOrUndefined String
a72c8af1 44 , noPrefix :: NullOrUndefined Boolean
45 , src :: NullOrUndefined [String]
c194f84c 46 }
47
48instance isForeignOptions :: IsForeign Options where
a72c8af1 49 read obj = Options <$> ({ noPrelude: _
50 , noOpts: _
51 , noMagicDo: _
52 , noTco: _
53 , verboseErrors: _
54 , comments: _
55 , output: _
56 , noPrefix: _
57 , src: _
58 } <$> readProp noPreludeOpt obj
59 <*> readProp noOptsOpt obj
60 <*> readProp noMagicDoOpt obj
61 <*> readProp noTcoOpt obj
62 <*> readProp verboseErrorsOpt obj
63 <*> readProp commentsOpt obj
64 <*> readProp outputOpt obj
65 <*> readProp noPrefixOpt obj
66 <*> readProp srcOpt obj)
67
68class LoaderOption a where
69 opt :: String -> NullOrUndefined a -> [String]
70
71instance booleanLoaderOption :: LoaderOption Boolean where
72 opt key opt = maybe [] (\a -> if a then ["--" ++ key] else [])
73 (runNullOrUndefined opt)
74
75instance stringLoaderOption :: LoaderOption String where
76 opt key opt = maybe [] (\a -> ["--" ++ key ++ "=" ++ a])
77 (runNullOrUndefined opt)
c194f84c 78
79pscMakeOutputOption :: Foreign -> Maybe String
80pscMakeOutputOption query = either (const Nothing)
81 (\(Options a) -> runNullOrUndefined a.output)
82 (read query)
83
84pscMakeOptions :: Foreign -> [String]
85pscMakeOptions query = either (const []) fold parsed
86 where parsed = read query :: F Options
a72c8af1 87 fold (Options a) = opt noPreludeOpt a.noPrelude <>
88 opt noOptsOpt a.noOpts <>
89 opt noMagicDoOpt a.noMagicDo <>
90 opt noTcoOpt a.noTco <>
91 opt verboseErrorsOpt a.verboseErrors <>
92 opt commentsOpt a.comments <>
93 opt outputOpt a.output <>
94 opt noPrefixOpt a.noPrefix
95
96loaderSrcOption :: Foreign -> Maybe [String]
97loaderSrcOption query = either (const Nothing)
98 (\(Options a) -> runNullOrUndefined a.src)
99 (read query)