Commit ef7e977e authored by Camil Staps's avatar Camil Staps 🍃

Add beautify option

parent f7ebebe1
module saplcg
import StdBool
import StdFile
import StdList
import StdString
......@@ -13,6 +14,7 @@ import System.CommandLine
import System.File
import System.FilePath
import System.Options
import System.Process
import Text
import Sapl.Target.CleanFlavour
......@@ -22,6 +24,7 @@ import Sapl.Target.JS.CodeGeneratorJS
{ sapl_files :: ![FilePath]
, trampoline :: !Bool
, output :: !Maybe FilePath
, beautify :: !Maybe FilePath
}
defaultOptions :: Options
......@@ -29,6 +32,7 @@ defaultOptions =
{ sapl_files = []
, trampoline = False
, output = Nothing
, beautify = Nothing
}
defaultParserState :: ParserState
......@@ -46,19 +50,31 @@ defaultParserState =
Start w
# ([prog:opts],w) = getCommandLine w
# noUsage = Nothing
# usage = Just ("Usage: " +++ prog +++ " [--trampoline] -o OUTPUT INPUT [INPUT..]")
# usage = Just ("Usage: " +++ prog +++ " [-t|--trampoline] [-b|--beautify PROG] -o OUTPUT INPUT [INPUT..]")
# opts = parseOptions optionDescription opts defaultOptions
| isError opts = error noUsage (join "\n" $ fromError opts) w
# opts = fromOk opts
| isEmpty opts.sapl_files = error usage "No sapl files given" w
| isNothing opts.output = error usage "No output file given" w
| isNothing opts.output = error usage "No output file given" w
| isNothing opts.output && isJust opts.beautify = error Nothing "Cannot use beautifier when outputting to stdout" w
# (ok,out,w) = case opts.output of
Nothing -> let (f,w`) = stdio w in (True,f,w`)
Just o -> fopen o FWriteText w
# (out,_,w) = seqSt (handleFile opts) opts.sapl_files (out,defaultParserState,w)
# (_,w) = fclose out w
# w = case opts.beautify of
Nothing -> w
Just beautify
# (err,w) = callProcess beautify [out,"-o",out] Nothing w
with out = fromJust opts.output
| isError err
-> error Nothing ("Error while beautifying: " +++ snd (fromError err)) w
-> w
= w
where
optionDescription :: Option Options
......@@ -67,6 +83,11 @@ where
"--trampoline"
(\opts -> Ok {opts & trampoline=True})
"Turn on trampoline code"
, Shorthand "-b" "--beautify" $ Option
"--beautify"
(\b opts -> Ok {opts & beautify=Just b})
"PROG"
"Name or path of a JavaScript beautifier"
, Shorthand "-o" "--output" $ Option
"--output"
(\f opts -> Ok {opts & output=Just f})
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment