aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/program/aedifex/command/clean.lux
blob: 9e55ccc83a8b1c044a771d9e150ad383777f17b6 (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
(.module:
  [library
   [lux #*
    [abstract
     ["." monad (#+ do)]]
    [control
     [try (#+ Try)]
     [concurrency
      ["." async (#+ Async)]]]
    [data
     [text
      ["%" format (#+ format)]]]
    [world
     ["." file (#+ Path)]
     ["." console (#+ Console)]]]]
  ["." /// #_
   [command (#+ Command)]
   ["#" profile]
   ["#." action (#+ Action)]])

(def: (clean_files! fs root)
  (-> (file.System Async) Path (Async (Try Any)))
  (let [! ///action.monad]
    (|> root
        (\ fs directory_files)
        (\ ! map (monad.map ! (\ fs delete)))
        (\ ! join))))

(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 (value@ #///.target profile)]
     ? (\ fs directory? target)
     _ (let [! ///action.monad]
         (if ?
           (loop [root target]
             (do !
               [_ (..clean_files! fs root)
                _ (|> root
                      (\ fs sub_directories)
                      (\ ! map (monad.map ! recur))
                      (\ ! join))]
               (\ fs delete root)))
           (\ ! in [])))]
    (console.write_line (..success target) console)))