Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 55 additions & 4 deletions ShaderMinifier/main.fs
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,65 @@ let run (options: Options.Options) filenames =
printfn "%s" (exn.ToString())
1

let splitCommandLine (input: string) : string list =
// " a \\\" b c\"\\d \" c\"\\\d\\\"\"e " -> ["a"; """; "b"; "cd "; "c\d"e"]
let args = ResizeArray<string>()
let sb = System.Text.StringBuilder()
let mutable inQuotes = false
let mutable escapeNext = false
for c in input do
match c with
| _ when escapeNext ->
sb.Append(c) |> ignore
escapeNext <- false
| '\\' ->
escapeNext <- true
| '"' ->
inQuotes <- not inQuotes
| ' ' when not inQuotes ->
if sb.Length > 0 then
args.Add(sb.ToString())
sb.Clear() |> ignore
| _ ->
sb.Append(c) |> ignore
if sb.Length > 0 then
args.Add(sb.ToString())
args |> List.ofSeq
let x = splitCommandLine " a \\\" b\"\\c \" z\"\\d\\\"\"e "
in if x <> ["a"; "\""; "bc "; "zd\"e"] then failwithf "unit test failed: %A" x

let runServer () =
use stdin = new StreamReader(System.Console.OpenStandardInput())
let mutable exit = false
while not exit do
let line = stdin.ReadLine()
printfn "> %s" line
match line with
| null | "#exit" -> exit <- true
| _ when System.String.IsNullOrWhiteSpace(line) -> ()
| _ when line.StartsWith "#" -> ()
| _ ->
let argv = splitCommandLine line |> Seq.toArray
printfn "[%s]" (argv |> Array.map (sprintf "\"%s\"") |> String.concat ", ")
let options, files = Options.initFiles argv
if files |> Seq.contains "stdin" then
printfn "Usage of stdin is not supported in server mode."
else
run options files |> ignore
printfn "ok"
0

[<EntryPoint>]
let main argv =
let err =
try
let options, files = Options.initFiles argv
if options.verbose then
printfn "Shader Minifier %s - https://github.com/laurentlb/Shader_Minifier" Options.version
run options files
if argv = [|"--server"|] then
runServer ()
else
let options, files = Options.initFiles argv
if options.verbose then
printfn "Shader Minifier %s - https://github.com/laurentlb/Shader_Minifier" Options.version
run options files
with
| :? Argu.ArguParseException as ex ->
printfn "%s" ex.Message
Expand Down