]> gitweb.factorcode.org Git - factor.git/blob - basis/tools/vocabs/monitor/monitor.factor
ed2e486ecccc86282d20b4d8ed3f5f73bb102f14
[factor.git] / basis / tools / vocabs / monitor / monitor.factor
1 ! Copyright (C) 2008 Slava Pestov.\r
2 ! See http://factorcode.org/license.txt for BSD license.\r
3 USING: threads io.files io.monitors init kernel\r
4 vocabs vocabs.loader tools.vocabs namespaces continuations\r
5 sequences splitting assocs command-line concurrency.messaging\r
6 io.backend sets tr ;\r
7 IN: tools.vocabs.monitor\r
8 \r
9 TR: convert-separators "/\\" ".." ;\r
10 \r
11 : vocab-dir>vocab-name ( path -- vocab )\r
12     trim-left-separators\r
13     trim-right-separators\r
14     convert-separators ;\r
15 \r
16 : path>vocab-name ( path -- vocab )\r
17     dup ".factor" tail? [ parent-directory ] when ;\r
18 \r
19 : chop-vocab-root ( path -- path' )\r
20     "resource:" prepend-path normalize-path\r
21     dup vocab-roots get\r
22     [ normalize-path ] map\r
23     [ head? ] with find nip\r
24     ?head drop ;\r
25 \r
26 : path>vocab ( path -- vocab )\r
27     chop-vocab-root path>vocab-name vocab-dir>vocab-name ;\r
28 \r
29 : monitor-loop ( -- )\r
30     #! On OS X, monitors give us the full path, so we chop it\r
31     #! off if its there.\r
32     receive first path>vocab changed-vocab\r
33     reset-cache\r
34     monitor-loop ;\r
35 \r
36 : add-monitor-for-path ( path -- )\r
37     dup exists? [ t my-mailbox (monitor) ] when drop ;\r
38 \r
39 : monitor-thread ( -- )\r
40     [\r
41         [\r
42             vocab-roots get prune [ add-monitor-for-path ] each\r
43 \r
44             H{ } clone changed-vocabs set-global\r
45             vocabs [ changed-vocab ] each\r
46 \r
47             monitor-loop\r
48         ] with-monitors\r
49     ] ignore-errors ;\r
50 \r
51 : start-monitor-thread ( -- )\r
52     #! Silently ignore errors during monitor creation since\r
53     #! monitors are not supported on all platforms.\r
54     [ monitor-thread ] "Vocabulary monitor" spawn drop ;\r
55 \r
56 [\r
57     "-no-monitors" cli-args member? [\r
58         start-monitor-thread\r
59     ] unless\r
60 ] "tools.vocabs.monitor" add-init-hook\r