aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/program/aedifex/command/auto.lux
blob: 61ff6c78f39f3f0673f1ce1b483e273d017d0958 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
(.require
 [library
  [lux (.except)
   [abstract
    ["[0]" monad (.only do)]]
   [control
    ["[0]" try (.only Try)]
    [concurrency
     ["[0]" async (.only Async)]]]
   [data
    [collection
     ["[0]" list]
     ["[0]" set]]]
   [world
    [environment (.only Environment)]
    [shell (.only Exit Shell)]
    [console (.only Console)]
    ["[0]" file (.only)
     ["[0]" watch (.only Watcher)]]]]]
 ["[0]" //
  ["/[1]" //
   [command (.only Command)]
   ["[1]" profile]
   ["[1][0]" action]
   [dependency
    [resolution (.only Resolution)]]]])

(def (targets fs path)
  (-> (file.System Async) file.Path (Async (List file.Path)))
  (let [! async.monad]
    (|> path
        (of fs sub_directories)
        (of ! each (|>> (try.else (list))
                        (monad.each ! (targets fs))))
        (of ! conjoint)
        (of ! each (|>> list.together (list.partial path))))))

(def .public delay
  Nat
  1,000)

(def (pause delay)
  (-> Nat (Async (Try Any)))
  (async.after delay {try.#Success []}))

(def .public (do! delay watcher command)
  (All (_ a)
    (-> Nat (Watcher Async)
        (-> (Console Async) (Environment Async) (file.System Async) (Shell Async) Resolution (Command [Exit a]))
        (-> (Console Async) (Environment Async) (file.System Async) (Shell Async) Resolution (Command [Exit Any]))))
  (function (_ console environment fs shell resolution)
    (function (_ profile)
      (with_expansions [<call> ((command console environment fs shell resolution) profile)]
        (do [! async.monad]
          [targets (|> profile
                       (the ///.#sources)
                       set.list
                       (monad.each ! (..targets fs))
                       (of ! each list.together))]
          (do [! ///action.monad]
            [_ (monad.each ! (of watcher start watch.modification) targets)
             _ <call>]
            (loop (again [_ []])
              (do !
                [_ (..pause delay)
                 events (of watcher poll [])]
                (when events
                  {.#Item _}
                  (do !
                    [_ <call>]
                    (again []))

                  {.#End}
                  (again []))))))))))