aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/program/aedifex/repository/local.lux
blob: e7dbb7d4d87bdf2c46a1e822c3cb404cf7c677a7 (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
(.module:
  [library
   [lux #*
    [abstract
     [monad (#+ do)]]
    [control
     ["." try]
     [concurrency
      ["." promise (#+ Promise)]]]
    [data
     ["." text
      ["%" format (#+ format)]]]
    [world
     [program (#+ Program)]
     ["." file]
     [net
      ["." uri (#+ URI)]]]]]
  ["." //
   ["/#" // #_
    ["#." local]
    ["#." metadata]]])

(def: (root program /)
  (-> (Program Promise) Text file.Path)
  (|> ///local.repository
      (text.replace_all uri.separator /)
      (format (\ program home) /)))

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

(def: (absolute_path program /)
  (-> (Program Promise) Text (-> URI file.Path))
  (|>> ///metadata.local_uri
       (..path /)
       (format (..root program /) /)))

(implementation: #export (repository program fs)
  (-> (Program Promise) (file.System Promise) (//.Repository Promise))

  (def: description
    (..root program (\ fs separator)))

  (def: download
    (|>> (..absolute_path program (\ fs separator))
         (\ fs read)))

  (def: (upload uri content)
    (do {! promise.monad}
      [#let [absolute_path (..absolute_path program (\ fs separator) uri)]
       ? (\ fs file? absolute_path)
       _ (if ?
           (wrap [])
           (case (file.parent fs absolute_path)
             (#.Some parent)
             (file.make_directories promise.monad fs parent)

             _
             (let [! (try.with promise.monad)]
               (\ ! wrap []))))]
      (\ fs write content absolute_path))))