-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathCli.hs
More file actions
176 lines (160 loc) · 4.85 KB
/
Copy pathCli.hs
File metadata and controls
176 lines (160 loc) · 4.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
{-# LANGUAGE QuasiQuotes #-}
module Cli where
import Data.Char qualified as Char
import Data.List.NonEmpty qualified as NE
import Data.Tagged (Tagged (..))
import Data.Text (splitOn)
import Data.Text qualified as Text
import Language.PureScript.Backend.AppOrModule (AppOrModule (..))
import Language.PureScript.CoreFn
import Options.Applicative
( Parser
, eitherReader
, execParser
, flag
, fullDesc
, header
, helpDoc
, helper
, info
, long
, metavar
, option
, progDesc
, short
, value
)
import Path
( Dir
, File
, SomeBase (..)
, parseSomeDir
, parseSomeFile
, reldir
, relfile
)
import Prettyprinter (Doc, annotate, flatAlt, indent, line, vsep, (<+>))
import Prettyprinter qualified as PP
import Prettyprinter.Render.Terminal (AnsiStyle, Color (..))
import Prettyprinter.Render.Terminal qualified as PT
data Args = Args
{ foreignPath ∷ Tagged "foreign" (SomeBase Dir)
, psOutputPath ∷ Tagged "output" (SomeBase Dir)
, luaOutputFile ∷ Tagged "output-lua" (SomeBase File)
, outputIR ∷ Maybe ExtraOutput
, outputLuaAst ∷ Maybe ExtraOutput
, appOrModule ∷ AppOrModule
}
deriving stock (Show)
data ExtraOutput = OutputIR | OutputLuaAst
deriving stock (Eq, Show)
options ∷ Parser Args
options = do
foreignPath ←
option (eitherReader (bimap displayException Tagged . parseSomeDir)) $
fold
[ metavar "FOREIGN-PATH"
, long "foreign-path"
, value $ Tagged $ Rel [reldir|foreign|]
, helpDoc . Just $
"Path to a directory containing foreign files."
<> linebreak
<> bold "Default: foreign"
]
psOutputPath ←
option (eitherReader (bimap displayException Tagged . parseSomeDir)) $
fold
[ metavar "PS-PATH"
, long "ps-output"
, value $ Tagged $ Rel [reldir|output|]
, helpDoc . Just $
"Path to purs output directory."
<> linebreak
<> bold "Default: output"
]
luaOutputFile ←
option (eitherReader (bimap displayException Tagged . parseSomeFile)) $
fold
[ metavar "LUA-OUT-FILE"
, long "lua-output-file"
, value $ Tagged $ Rel [relfile|main.lua|]
, helpDoc . Just $
"Path to write compiled Lua file to."
<> linebreak
<> bold "Default: main.lua"
]
outputLuaAst ←
flag Nothing (Just OutputLuaAst) . fold $
[ long "output-lua-ast"
, helpDoc . Just $
"Output Lua AST."
<> linebreak
<> bold "Default: false"
]
outputIR ←
flag Nothing (Just OutputIR) . fold $
[ long "output-ir"
, helpDoc . Just $
"Output IR."
<> linebreak
<> bold "Default: false"
]
appOrModule ←
option (eitherReader parseAppOrModule) . fold $
[ metavar "ENTRY"
, short 'e'
, long "entry"
, value $
AsApplication
(unsafeModuleNameFromText "Main")
(Ident "main")
, helpDoc . Just $
vsep
[ "Where to start compilation."
<> softbreak
<> "Could be one of the following formats:"
, "- Application format:" <+> magenta "<Module>.<binding>"
, green $ indent 2 "Example: Acme.App.main"
, "- Module format:" <+> magenta "<Module>"
, green $ indent 2 "Example: Acme.Lib"
, bold "Default: Main.main"
]
]
pure Args {..}
parseAppOrModule ∷ String → Either String AppOrModule
parseAppOrModule s = case splitOn "." (toText s) of
[] → Left "Invalid entry point format"
[name] | isModule name → maybeToRight "Invalid module name" do
AsModule <$> moduleNameFromText name
(NE.fromList → segments) →
case moduleNameFromText (Text.intercalate "." (init segments)) of
Nothing → Left $ "Invalid module name: " <> s
Just mn →
let segment = last segments
in Right
if isModule segment
then AsModule mn
else AsApplication mn (Ident segment)
where
isModule = Char.isAsciiUpper . Text.head
parseArguments ∷ IO Args
parseArguments =
execParser $
info
(options <**> helper)
( fullDesc
<> progDesc "Compile PureScript's CoreFn to Lua"
<> header "pslua - a PureScript backend for Lua"
)
--------------------------------------------------------------------------------
-- Helpers for pretty-printing -------------------------------------------------
linebreak ∷ Doc AnsiStyle
linebreak = flatAlt line mempty
softbreak ∷ Doc AnsiStyle
softbreak = PP.group linebreak
green ∷ Doc AnsiStyle → Doc AnsiStyle
green = annotate (PT.color Green)
magenta ∷ Doc AnsiStyle → Doc AnsiStyle
magenta = annotate (PT.color Magenta)
bold ∷ Doc AnsiStyle → Doc AnsiStyle
bold = annotate PT.bold