aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/program/aedifex/command/clean.lux
blob: 40609f4d4d8650b083cb87fc16659547710ee40c (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
(.require
 [library
  [lux (.except)
   [abstract
    ["[0]" monad (.only do)]]
   [control
    [try (.only Try)]
    [concurrency
     ["[0]" async (.only Async)]]]
   [data
    [text
     ["%" \\format (.only format)]]]
   [world
    ["[0]" file (.only Path)]
    ["[0]" console (.only Console)]]]]
 ["[0]" ///
  [command (.only Command)]
  ["[1]" profile]
  ["[1][0]" action (.only Action)]])

(def (clean_files! fs root)
  (-> (file.System Async) Path (Async (Try Any)))
  (do [! ///action.monad]
    [_ (|> root
           (at fs directory_files)
           (at ! each (monad.each ! (at fs delete)))
           (at ! conjoint))]
    (in [])))

(def .public success
  (-> ///.Target Text)
  (|>> (format "Successfully cleaned target directory: ")))

(def .public (do! console fs profile)
  (-> (Console Async) (file.System Async) (Command Any))
  (do [! async.monad]
    [.let [target (the ///.#target profile)]
     ? (at fs directory? target)
     _ (let [! ///action.monad]
         (if ?
           (loop (again [root target])
             (do !
               [_ (..clean_files! fs root)
                _ (|> root
                      (at fs sub_directories)
                      (at ! each (monad.each ! again))
                      (at ! conjoint))]
               (at fs delete root)))
           (at ! in [])))]
    (console.write_line (..success target) console)))