aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/program/aedifex/command/auto.lux
blob: afce4d6ff1543e82abfe3c11572b5cf4e8b4fc20 (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
75
76
77
78
(.module:
  [lux #*
   [abstract
    ["." monad (#+ do)]]
   [control
    ["." try (#+ Try)]
    [concurrency
     ["." promise (#+ Promise)]]
    [security
     ["!" capability]]]
   [data
    [collection
     ["." list]
     ["." set]]]
   [world
    [program (#+ Program)]
    [shell (#+ Shell)]
    ["." console (#+ Console)]
    ["." file (#+ Path)
     ["." watch (#+ Watcher)]]]]
  ["." // #_
   ["/#" // #_
    [command (#+ Command)]
    ["#" profile]
    ["#." action (#+ Action)]
    [dependency
     [resolution (#+ Resolution)]]]])

(def: (targets fs path)
  (-> (file.System Promise) Path (Promise (List Path)))
  (do {! promise.monad}
    [?root (!.use (\ fs directory) [path])]
    (case ?root
      (#try.Success root)
      (loop [root root]
        (do !
          [subs (\ ! map (|>> (try.default (list)))
                   (!.use (\ root directories) []))]
          (\ ! map (|>> list.concat (list& (!.use (\ root scope) [])))
             (monad.map ! recur subs))))

      (#try.Failure error)
      (wrap (list)))))

(def: (pause _)
  (-> Any (Promise (Try Any)))
  (promise.delay 1,000 (#try.Success [])))

(def: #export (do! watcher command)
  (All [a]
    (-> (Watcher Promise)
        (-> (Console Promise) (Program Promise) (file.System Promise) (Shell Promise) Resolution (Command a))
        (-> (Console Promise) (Program Promise) (file.System Promise) (Shell Promise) Resolution (Command Any))))
  (function (_ console program fs shell resolution)
    (function (_ profile)
      (with_expansions [<call> ((command console program fs shell resolution) profile)]
        (do {! promise.monad}
          [targets (|> profile
                       (get@ #///.sources)
                       set.to_list
                       (monad.map ! (..targets fs))
                       (\ ! map list.concat))]
          (do {! ///action.monad}
            [_ (monad.map ! (\ watcher start watch.all) targets)
             _ <call>]
            (loop [_ []]
              (do !
                [_ (..pause [])
                 events (\ watcher poll [])
                 _ (case events
                     (#.Cons _)
                     (do !
                       [_ <call>]
                       (wrap []))

                     #.Nil
                     (wrap []))]
                (recur [])))))))))