aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/program/aedifex/repository/local.lux
blob: dcc604306ad769d2993362df38d4d5dde6e6c042 (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
(.require
 [library
  [lux (.except)
   [abstract
    [monad (.only do)]]
   [control
    ["[0]" try (.only Try)]
    [concurrency
     ["[0]" async (.only Async)]]]
   [data
    ["[0]" text (.only)
     ["%" \\format (.only format)]]]
   [world
    [environment (.only Environment)]
    ["[0]" file]
    [net
     ["[0]" uri (.only URI)]]]]]
 ["[0]" // (.only)
  ["/[1]" //
   ["[1][0]" local]
   ["[1][0]" metadata]]])

(def .public (root environment fs)
  (-> (Environment Async) (file.System Async) file.Path)
  (let [/ (at fs separator)]
    (|> ///local.repository
        (text.replaced uri.separator /)
        (format (at environment home) /))))

(def (path /)
  (-> Text (-> URI file.Path))
  (text.replaced uri.separator /))

(def (absolute_path environment fs)
  (-> (Environment Async) (file.System Async) (-> URI file.Path))
  (let [/ (at fs separator)]
    (|>> ///metadata.local_uri
         (..path /)
         (format (..root environment fs) /))))

(def .public (repository environment fs)
  (-> (Environment Async) (file.System Async) (//.Repository Async))
  (implementation
   (def description
     (..root environment fs))

   (def download
     (|>> (..absolute_path environment fs)
          (at fs read)))

   (def (upload uri content)
     (do [! async.monad]
       [.let [absolute_path (..absolute_path environment fs uri)]
        ? (at fs file? absolute_path)
        _ (is (Async (Try Any))
              (if ?
                (in {try.#Success []})
                (when (file.parent fs absolute_path)
                  {.#Some parent}
                  (file.make_directories async.monad fs parent)

                  _
                  (in {try.#Success []}))))]
       (at fs write absolute_path content)))))