aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/program/aedifex/command/clean.lux
blob: b966fe85e0b3d19878c461c6e92518e1c9e26dee (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
(.module:
  [lux #*
   [abstract
    ["." monad (#+ do)]]
   [control
    ["." try (#+ Try)]
    ["." exception]
    [security
     ["!" capability]]
    [concurrency
     ["." promise (#+ Promise)]]]
   [data
    [text
     ["%" format (#+ format)]]]
   [world
    ["." file (#+ Path File Directory)]
    ["." console (#+ Console)]]]
  ["." /// #_
   [command (#+ Command)]
   ["#" profile]
   ["#." action (#+ Action)]])

(def: (clean_files! root)
  (-> (Directory Promise) (Promise (Try Any)))
  (do {! ///action.monad}
    [nodes (: (Promise (Try (List (File Promise))))
              (!.use (\ root files) []))
     _ (monad.map ! (function (_ node)
                      (!.use (\ node delete) []))
                  nodes)]
    (wrap [])))

(def: #export (success path)
  (-> ///.Target Text)
  (format "Successfully cleaned target directory: " path))

(def: #export (do! console fs profile)
  (-> (Console Promise) (file.System Promise) (Command Any))
  (do promise.monad
    [#let [target (get@ #///.target profile)]
     root (: (Promise (Try (Directory Promise)))
             (!.use (\ fs directory) target))]
    (case root
      (#try.Success root)
      (do {! ///action.monad}
        [_ (loop [root root]
             (do !
               [_ (..clean_files! root)
                subs (: (Promise (Try (List (Directory Promise))))
                        (!.use (\ root directories) []))
                _ (monad.map ! recur subs)]
               (!.use (\ root discard) [])))]
        (console.write_line (..success target) console))
      
      (#try.Failure error)
      (if (exception.match? file.cannot_find_directory error)
        (console.write_line (..success target) console)
        (\ promise.monad wrap (#try.Failure error))))))