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

(def: (root /)
  (-> Text Path)
  (text.replace_all uri.separator / ///local.repository))

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

(def: (file program system create? uri)
  (-> (Program Promise)
      (file.System Promise)
      Bit
      URI
      (Promise (Try (File Promise))))
  (do {! promise.monad}
    [#let [uri (text.replace_once ///metadata.remote_file ///metadata.local_file uri)]
     home (\ program home [])
     #let [/ (\ system separator)
           absolute_path (format home / (..root /) / (..path / uri))]]
    (if create?
      (do {! (try.with !)}
        [_ (: (Promise (Try Path))
              (file.make_directories promise.monad system (file.parent system absolute_path)))]
        (: (Promise (Try (File Promise)))
           (file.get_file promise.monad system absolute_path)))
      (: (Promise (Try (File Promise)))
         (!.use (\ system file) absolute_path)))))

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

  (def: (download uri)
    (do {! (try.with promise.monad)}
      [file (..file program system false uri)]
      (!.use (\ file content) [])))

  (def: (upload uri content)
    (do {! (try.with promise.monad)}
      [file (..file program system true uri)]
      (!.use (\ file over_write) [content]))))