Skip to content

Commit

Permalink
Added --watch flag to Coffee.exe
Browse files Browse the repository at this point in the history
  • Loading branch information
tomlokhorst committed Apr 17, 2011
1 parent 3295986 commit 5549bf0
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 11 deletions.
9 changes: 4 additions & 5 deletions Coffee/OptionParser.fs
Expand Up @@ -13,7 +13,7 @@ type Config =
{ action : Action;
outputDir : string option;
joinFiles : bool;
// watchDir : string option;
watch : bool;
print : bool;
stdio : bool;
bare : bool;
Expand All @@ -24,7 +24,7 @@ let defaultConfig =
{ action = Interactive;
outputDir = None;
joinFiles = false;
// watchDir = None;
watch = false;
print = false;
stdio = false;
bare = false;
Expand All @@ -36,7 +36,7 @@ let options =
(Some 'i', "interactive" , "run an interactive CoffeeScript REPL");
(Some 'o', "output" , "set the directory for compiled JavaScript");
(Some 'j', "join" , "concatenate the scripts before compiling");
// (Some 'w', "watch" , "watch scripts for changes, and recompile");
(Some 'w', "watch" , "watch scripts for changes, and recompile");
(Some 'p', "print" , "print the compiled JavaScript to stdout");
// (Some 'l', "lint" , "pipe the compiled JavaScript through JSLint");
(Some 's', "stdio" , "listen for and compile scripts over stdio");
Expand Down Expand Up @@ -89,8 +89,7 @@ let rec parse cfg args =
| ("interactive" :: opts, args , rest) -> { parse cfg (opts, args, rest) with action = Interactive }
| ("output" :: opts, [] , rest) -> { parse cfg (opts, [] , rest) with outputDir = None }
| ("output" :: opts, a::args, rest) -> { parse cfg (opts, args, rest) with outputDir = Some a }
// | ("watch" :: opts, [] , rest) -> { parse cfg (opts, [] , rest) with watchDir = None }
// | ("watch" :: opts, a::args, rest) -> { parse cfg (opts, args, rest) with watchDir = Some a }
| ("watch" :: opts, args , rest) -> { parse cfg (opts, args, rest) with watch = true }
| ("join" :: opts, args , rest) -> { parse cfg (opts, args, rest) with joinFiles = true }
| ("print" :: opts, args , rest) -> { parse cfg (opts, args, rest) with print = true }
| ("stdio" :: opts, args , rest) -> { parse cfg (opts, args, rest) with stdio = true }
Expand Down
38 changes: 32 additions & 6 deletions Coffee/Program.fs
Expand Up @@ -49,7 +49,7 @@ let compile code bare globals filename =
let c = lazy (cse.Compile (code, bare, globals, toNull filename))
printException c

let compileScripts sources bare print outputDir =
let compileScript dir fn bare print outputDir log =
let c fn code = compile code bare false (Some fn)
let filename dir fn =
let fn' = Path.Combine(Option.fold (fun _ s -> s) dir outputDir, fn)
Expand All @@ -61,10 +61,31 @@ let compileScripts sources bare print outputDir =
if print
then printfn "%s" s
else File.WriteAllText(filename dir fn, s)
let f (dir, fns) =
List.map (fun fn -> Path.Combine(dir, fn) |> File.ReadAllText |> c fn |> Option.iter (output dir fn))
fns
List.map f sources |> ignore
Path.Combine(dir, fn)
|> File.ReadAllText
|> c fn
|> Option.iter (output dir fn)
if log
then printfn "%s - compiled %s" (System.DateTime.Now.ToLongTimeString()) fn
else ()

let compileScripts sources bare print outputDir log =
List.map (fun (dir, fns) -> List.map (fun fn -> compileScript dir fn bare print outputDir log) fns) sources
|> ignore

let watch (dir, fns) bare print outputDir =
let onChange (e: FileSystemEventArgs) =
if List.exists (fun i -> i = e.Name) fns
then compileScript dir e.Name bare print outputDir true
else ()
let fsw = (new FileSystemWatcher(dir))
fsw.IncludeSubdirectories <- true
fsw.Changed.Add onChange
fsw.EnableRaisingEvents <- true
let rec loop () =
System.Console.ReadLine () |> ignore
loop ()
loop ()

let files (src : string) : string * string list =
let rec files' root path =
Expand Down Expand Up @@ -112,7 +133,12 @@ let main args =
else Option.iter (fun c -> eval c false false None |> ignore) code
| Compile srcs -> if config.stdio
then compile (stdin()) config.bare false None |> Option.iter (printfn "%s")
else compileScripts (List.map files srcs) config.bare config.print config.outputDir
else compileScripts (List.map files srcs) config.bare config.print config.outputDir config.watch
if config.watch
then List.map (fun src -> watch (files src) config.bare config.print config.outputDir) srcs
|> ignore
else ()

| Tokens osrc -> if config.stdio
then stdin() |> tokens
else Option.iter (sources tokens >> ignore) osrc
Expand Down

0 comments on commit 5549bf0

Please sign in to comment.