Commit 43ee471f authored by Taddeüs Kroes's avatar Taddeüs Kroes

Changed main function for minimal command-line usage (just simple minification operations, for now)

parent 28178a42
...@@ -2,63 +2,106 @@ open Lexing ...@@ -2,63 +2,106 @@ open Lexing
open Types open Types
type args = { type args = {
mutable infiles : string list; infiles : string list;
mutable outfile : string option; outfile : string option;
mutable verbose : bool; verbose : bool;
mutable prune : bool; whitespace : bool;
mutable unfold : bool; color : bool;
mutable upto : int option; font : bool;
shorthands : bool;
duplicates : bool;
echo : bool;
} }
(* Parse command-line arguments *)
let parse_args () = let parse_args () =
let args = { let usage =
"Usage: " ^ Sys.argv.(0) ^
" [<options>] [<file> ...]\n\
\n\
Generic options:\n \
-h, --help Show this help message\n \
-v, --verbose Verbose mode: show compression rate\n \
-o <file> Output file (defaults to stdout)\n \
<file> ... Input files (default is to read from stdin)\n\
\n\
Optimization flags (if none are specified, all are enabled):\n \
-w, --whitespace Eliminate unnecessary whitespaces (has the greatest \
effect, omit for pretty-printing)\n \
-c, --color Shorten colors\n \
-f, --font Shorten font weights\n \
-s, --shorthands Generate shorthand properties\n \
-d, --duplicates Prune duplicate properties (WARNING: may affect \
cross-browser hacks)\n \
-p, --pretty Shorthand for -c -f -s -d\n \
-e, --echo Just parse and pretty-print, no optimizations\n"
in
let default_args = {
infiles = []; infiles = [];
outfile = None; outfile = None;
verbose = false; verbose = false;
prune = true; whitespace = false;
unfold = true; color = false;
upto = None; font = false;
shorthands = false;
duplicates = false;
echo = false;
} in } in
let args_spec = [ let rec handle args = function
("<file> ...", Arg.Rest (fun _ -> ()), | ("-v" | "--verbose") :: tl ->
" Input files (default is to read from stdin)"); handle {args with verbose = true} tl
| ("-w" | "--whitespace") :: tl ->
("-o", Arg.String (fun s -> args.outfile <- Some s), handle {args with whitespace = true} tl
"<file> Output file (defaults to stdout)"); | ("-c" | "--color") :: tl ->
handle {args with color = true} tl
("-v", Arg.Unit (fun _ -> args.verbose <- true), | ("-f" | "--font") :: tl ->
" Verbose mode: show compression rate"); handle {args with font = true} tl
| ("-s" | "--shorthands") :: tl ->
("-no-prune", Arg.Unit (fun _ -> args.prune <- false), handle {args with shorthands = true} tl
" Don't prune duplicate properties (skip step 5 below)"); | ("-d" | "-duplicates") :: tl ->
handle {args with duplicates = true} tl
("-no-unfold", Arg.Unit (fun _ -> args.unfold <- false), | ("-p" | "--pretty") :: tl ->
" Only minify whitespace, colors and shorthands \ handle args ("-c" :: "-f" :: "-s" :: "-d" :: tl)
(skip steps 2-7 below)"); | ("-e" | "--echo") :: tl ->
handle {args with echo = true} tl
("-upto", Arg.Int (fun i -> args.upto <- Some i),
"<step> Stop after the specified step (for debugging): \ | ("-h" | "--help") :: tl ->
\n \ prerr_string usage;
1: parse \n \ raise Exit_success
2: unfold shorthands \n \
3: unfold selectors \n \ | ["-o"] ->
4: unfold blocks \n \ raise (Failure ("missing output file name"))
5: prune duplicates \n \ | "-o" :: next :: tl when next.[0] = '-' ->
6: combine selectors \n \ raise (Failure ("missing output file name"))
7: concatenate blocks \n \ | "-o" :: filename :: tl ->
8: optimize blocks \n \ handle {args with outfile = Some filename} tl
9: minify");
] in | arg :: tl when arg.[0] = '-' ->
prerr_string usage;
raise (Failure ("unknown option " ^ arg))
let usage = | filename :: tl ->
"Usage: " ^ Sys.argv.(0) ^ " [-o <file>] [-v] [-no-prune] [-upto <step>] \ handle {args with infiles = args.infiles @ [filename]} tl
[<file> ...] "
| [] -> args
in in
Arg.parse args_spec (fun f -> args.infiles <- args.infiles @ [f]) usage; match handle default_args (List.tl (Array.to_list Sys.argv)) with
args | { whitespace = false;
color = false;
font = false;
shorthands = false;
duplicates = false;
echo = false;
_ } as args ->
{ args with
whitespace = true;
color = true;
font = true;
shorthands = true;
duplicates = true }
| args -> args
let parse_files = function let parse_files = function
| [] -> | [] ->
...@@ -76,21 +119,6 @@ let parse_files = function ...@@ -76,21 +119,6 @@ let parse_files = function
(String.concat "" inputs, List.concat stylesheets) (String.concat "" inputs, List.concat stylesheets)
let handle_args args = let handle_args args =
let steps =
(*let switch flag fn = if flag then fn else fun s -> s in*)
[
(*
switch args.unfold Unfold.unfold_shorthands;
switch args.unfold Unfold.unfold_selectors;
switch args.unfold Unfold.unfold_blocks;
switch (args.unfold && args.prune) Unfold.prune_duplicates;
switch args.unfold Combine.combine_selectors;
switch args.unfold Concat.concat_blocks;
Optimize.optimize_blocks;
*)
]
in
let write_output = let write_output =
match args.outfile with match args.outfile with
| None -> print_endline | None -> print_endline
...@@ -98,16 +126,20 @@ let handle_args args = ...@@ -98,16 +126,20 @@ let handle_args args =
fun css -> let f = open_out name in output_string f css; close_out f fun css -> let f = open_out name in output_string f css; close_out f
in in
let upto = match args.upto with Some i -> i | None -> 0 in let switch flag fn = if flag then fn else fun x -> x in
let input, stylesheet = parse_files args.infiles in
let rec do_steps i stylesheet = function let input, css = parse_files args.infiles in
| _ when i = upto -> let css = css
write_output (Stringify.string_of_stylesheet stylesheet) |> (switch args.color Color.compress)
(*|> (switch args.font Font.compress)*)
| [] -> |> (switch args.shorthands Shorthand.compress)
let output = Stringify.minify_stylesheet stylesheet in (*|> (switch args.duplicates Duplicates.compress)*)
in
let output =
if args.whitespace
then Stringify.minify_stylesheet css
else Stringify.string_of_stylesheet css
in
write_output output; write_output output;
if args.verbose then begin if args.verbose then begin
...@@ -117,13 +149,6 @@ let handle_args args = ...@@ -117,13 +149,6 @@ let handle_args args =
il ol (int_of_float (float_of_int ol /. float_of_int il *. 100.)) il ol (int_of_float (float_of_int ol /. float_of_int il *. 100.))
end end
| step :: tl ->
do_steps (i + 1) (step stylesheet) tl
in
do_steps 1 stylesheet steps
(* Main function, returns exit status *) (* Main function, returns exit status *)
let main () = let main () =
begin begin
...@@ -137,6 +162,8 @@ let main () = ...@@ -137,6 +162,8 @@ let main () =
prerr_endline ("Error: " ^ msg ^ ": " ^ Stringify.string_of_box box); prerr_endline ("Error: " ^ msg ^ ": " ^ Stringify.string_of_box box);
| Failure msg -> | Failure msg ->
prerr_endline ("Error: " ^ msg); prerr_endline ("Error: " ^ msg);
| Exit_success ->
exit 0
end; end;
exit 1 exit 1
......
...@@ -86,3 +86,5 @@ exception Syntax_error of string ...@@ -86,3 +86,5 @@ exception Syntax_error of string
exception Loc_error of loc * string exception Loc_error of loc * string
exception Box_error of box * string exception Box_error of box * string
exception Exit_success
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