]> gitweb.factorcode.org Git - factor.git/blob - basis/vocabs/refresh/monitor/monitor.factor
Fix comments to be ! not #!.
[factor.git] / basis / vocabs / refresh / monitor / monitor.factor
1 ! Copyright (C) 2008, 2010 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors assocs command-line concurrency.messaging
4 continuations init io.backend io.files io.monitors io.pathnames
5 kernel libc namespaces sequences sets splitting threads fry
6 tr vocabs vocabs.loader vocabs.refresh vocabs.cache
7 io.files.links ;
8 IN: vocabs.refresh.monitor
9
10 TR: convert-separators "/\\" ".." ;
11
12 : vocab-dir>vocab-name ( path -- vocab )
13     trim-head-separators
14     trim-tail-separators
15     convert-separators ;
16
17 : path>vocab-name ( path -- vocab )
18     dup ".factor" tail? [ parent-directory ] when ;
19
20 : chop-vocab-root ( path -- path' )
21     "resource:" prepend-path normalize-path
22     dup vocab-roots get
23     [ [ normalize-path ] map ]
24     [ [ resolve-symlinks ] map ] bi append
25     [ head? ] with find nip
26     ?head drop ;
27
28 : path>vocab ( path -- vocab )
29     chop-vocab-root path>vocab-name vocab-dir>vocab-name ;
30
31 : monitor-loop ( monitor -- )
32     ! On OS X, monitors give us the full path, so we chop it
33     ! off if its there.
34     [
35         next-change path>> path>vocab
36         [ changed-vocab ] [ reset-cache ] bi
37     ]
38     [ monitor-loop ]
39     bi ;
40
41 : (start-vocab-monitor) ( vocab-root -- )
42     dup exists?
43     [ [ t <monitor> monitor-loop ] with-monitors ] [ drop ] if ;
44
45 : start-vocab-monitor ( vocab-root -- )
46     [ '[ [ _ (start-vocab-monitor) ] ignore-errors ] ]
47     [ "Root monitor: " prepend ]
48     bi spawn drop ;
49
50 : init-vocab-monitor ( -- )
51     H{ } clone changed-vocabs set-global
52     loaded-vocab-names [ changed-vocab ] each ;
53
54 [
55     "-no-monitors" (command-line) member? [
56         [ drop ] add-vocab-root-hook set-global
57         f changed-vocabs set-global
58     ] [
59         init-vocab-monitor
60         vocab-roots get [ start-vocab-monitor ] each
61         [ start-vocab-monitor ] add-vocab-root-hook set-global
62     ] if
63 ] "vocabs.refresh.monitor" add-startup-hook