aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/program/aedifex/metadata/snapshot.lux
blob: f6878a02338a158be32b1bf03e72b4ab946dae23 (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
(.module:
  [lux (#- Name Type)
   [abstract
    [monad (#+ do)]
    [equivalence (#+ Equivalence)]]
   [control
    [pipe (#+ do> case>)]
    ["." try (#+ Try)]
    ["." exception (#+ exception:)]
    ["<>" parser
     ["<.>" xml (#+ Parser)]
     ["<.>" text]]
    [concurrency
     ["." promise (#+ Promise)]]]
   [data
    ["." product]
    ["." text
     ["%" format]
     [encoding
      ["." utf8]]]
    [format
     ["." xml (#+ XML)]]
    [collection
     ["." list ("#\." functor)]]]
   [math
    [number
     ["n" nat]]]
   ["." time (#+ Time)
    ["." instant (#+ Instant)]
    ["." date (#+ Date)]
    ["." year]
    ["." month]]
   [world
    [net
     ["." uri (#+ URI)]]]]
  ["." //
   ["/#" // #_
    [repository (#+ Repository)]
    ["#." artifact (#+ Group Name Version Artifact)
     ["#/." type (#+ Type)]
     ["#/." versioning (#+ Versioning)]
     ["#/." snapshot
      ["#/." version]]]]])

(type: #export Metadata
  {#artifact Artifact
   #versioning Versioning})

(template [<definition> <tag>]
  [(def: <definition> xml.Tag ["" <tag>])]

  [<group> "groupId"]
  [<name> "artifactId"]
  [<version> "version"]
  [<metadata> "metadata"]
  )

(template [<name> <type> <tag> <pre>]
  [(def: <name>
     (-> <type> XML)
     (|>> <pre> #xml.Text list (#xml.Node <tag> xml.attributes)))]

  [format_group Group ..<group> (|>)]
  [format_name Name ..<name> (|>)]
  [format_version Version ..<version> (|>)]
  )

(def: #export (format (^slots [#artifact #versioning]))
  (-> Metadata XML)
  (let [(^slots [#///artifact.group #///artifact.name #///artifact.version]) artifact]
    (#xml.Node ..<metadata>
               xml.attributes
               (list (..format_group group)
                     (..format_name name)
                     (..format_version version)
                     (///artifact/versioning.format versioning)))))

(def: (sub tag parser)
  (All [a] (-> xml.Tag (Parser a) (Parser a)))
  (do <>.monad
    [_ (<xml>.node tag)]
    (<xml>.children parser)))

(def: (text tag)
  (-> xml.Tag (Parser Text))
  (..sub tag <xml>.text))

(def: #export parser
  (Parser Metadata)
  (<| (..sub ..<metadata>)
      (do {! <>.monad}
        [group (<xml>.somewhere (..text ..<group>))
         name (<xml>.somewhere (..text ..<name>))
         version (<xml>.somewhere (..text ..<version>))
         versioning (\ ! map
                       (update@ #///artifact/versioning.versions
                                (: (-> (List ///artifact/snapshot/version.Version)
                                       (List ///artifact/snapshot/version.Version))
                                   (|>> (case> (^ (list))
                                               (list {#///artifact/snapshot/version.extension ///artifact/type.jvm_library
                                                      #///artifact/snapshot/version.value version
                                                      #///artifact/snapshot/version.updated instant.epoch})

                                               versions
                                               versions))))
                       (<xml>.somewhere ///artifact/versioning.parser))]
        (wrap {#artifact {#///artifact.group group
                          #///artifact.name name
                          #///artifact.version version}
               #versioning versioning}))))

(def: #export equivalence
  (Equivalence Metadata)
  ($_ product.equivalence
      ///artifact.equivalence
      ///artifact/versioning.equivalence
      ))

(def: #export (uri artifact)
  (-> Artifact URI)
  (let [/ uri.separator
        group (|> artifact
                  (get@ #///artifact.group)
                  (///artifact.directory /))
        name (get@ #///artifact.name artifact)
        version (get@ #///artifact.version artifact)]
    (%.format group / name / version / //.remote_file)))

(def: #export (read repository artifact)
  (-> (Repository Promise) Artifact (Promise (Try Metadata)))
  (do promise.monad
    [project (\ repository download (..uri artifact))]
    (case project
      (#try.Success project)
      (wrap (|> project
                (do> try.monad
                     [(\ utf8.codec decode)]
                     [(\ xml.codec decode)]
                     [list (<xml>.run ..parser)])))
      
      (#try.Failure error)
      (wrap (#try.Success
             {#artifact artifact
              #versioning ///artifact/versioning.init})))))

(def: #export (write repository artifact metadata)
  (-> (Repository Promise) Artifact Metadata (Promise (Try Any)))
  (|> metadata
      ..format
      (\ xml.codec encode)
      (\ utf8.codec encode)
      (\ repository upload (..uri artifact))))