aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/program/aedifex/metadata/artifact.lux
blob: f4971546f883710db5576dce262ca8bcd6e652f5 (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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
(.module:
  [library
   [lux (#- Name)
    [abstract
     [monad (#+ do)]
     [equivalence (#+ Equivalence)]]
    [control
     [pipe (#+ do>)]
     ["." try (#+ Try)]
     ["<>" parser
      ["<.>" xml (#+ Parser)]
      ["<.>" text]]
     [concurrency
      ["." async (#+ Async)]]]
    [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: .public Metadata
  {#group Group
   #name Name
   #versions (List Version)
   #last_updated Instant})

(def: (pad value)
  (-> Nat Text)
  (if (n.< 10 value)
    (%.format "0" (%.nat value))
    (%.nat value)))

(def: (date_format value)
  (%.Format Date)
  (%.format (|> value date.year year.value .nat %.nat)
            (|> value date.month month.number ..pad)
            (|> value date.day_of_month ..pad)))

(def: (time_format value)
  (%.Format Time)
  (let [(^slots [#time.hour #time.minute #time.second]) (time.clock value)]
    (%.format (..pad hour)
              (..pad minute)
              (..pad second))))

(def: (instant_format value)
  (%.Format Instant)
  (%.format (..date_format (instant.date value))
            (..time_format (instant.time value))))

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

  [<group> "groupId"]
  [<name> "artifactId"]
  [<version> "version"]
  [<versioning> "versioning"]
  [<versions> "versions"]
  [<last_updated> "lastUpdated"]
  [<metadata> "metadata"]
  )

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

  [group_format Group ..<group> (|>)]
  [name_format Name ..<name> (|>)]
  [version_format Version ..<version> (|>)]
  [last_updated_format Instant ..<last_updated> ..instant_format]
  )

(def: versions_format
  (-> (List Version) XML)
  (|>> (list\map ..version_format) (#xml.Node ..<versions> xml.attributes)))

(def: .public (format value)
  (-> Metadata XML)
  (#xml.Node ..<metadata>
             xml.attributes
             (list (..group_format (get@ #group value))
                   (..name_format (get@ #name value))
                   (#xml.Node ..<versioning>
                              xml.attributes
                              (list (..versions_format (get@ #versions value))
                                    (..last_updated_format (get@ #last_updated value)))))))

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

(def: date_parser
  (<text>.Parser Date)
  (do <>.monad
    [year (<>.codec n.decimal (<text>.exactly 4 <text>.decimal))
     year (<>.lift (year.year (.int year)))
     month (<>.codec n.decimal (<text>.exactly 2 <text>.decimal))
     month (<>.lift (month.by_number month))
     day_of_month (<>.codec n.decimal (<text>.exactly 2 <text>.decimal))]
    (<>.lift (date.date year month day_of_month))))

(def: time_parser
  (<text>.Parser Time)
  (do <>.monad
    [hour (<>.codec n.decimal (<text>.exactly 2 <text>.decimal))
     minute (<>.codec n.decimal (<text>.exactly 2 <text>.decimal))
     second (<>.codec n.decimal (<text>.exactly 2 <text>.decimal))]
    (<>.lift (time.time
              {#time.hour hour
               #time.minute minute
               #time.second second
               #time.milli_second 0}))))

(def: last_updated_parser
  (Parser Instant)
  (<text>.then (do <>.monad
                 [date ..date_parser
                  time ..time_parser]
                 (in (instant.of_date_time date time)))
               (..text ..<last_updated>)))

(def: .public parser
  (Parser Metadata)
  (<| (<xml>.node ..<metadata>)
      ($_ <>.and
          (<xml>.somewhere (..text ..<group>))
          (<xml>.somewhere (..text ..<name>))
          (<| (<xml>.node ..<versioning>)
              ($_ <>.and
                  (<| <xml>.somewhere
                      (<xml>.node ..<versions>)
                      (<>.many (..text ..<version>)))
                  (<xml>.somewhere ..last_updated_parser)
                  )))))

(def: .public equivalence
  (Equivalence Metadata)
  ($_ product.equivalence
      text.equivalence
      text.equivalence
      (list.equivalence text.equivalence)
      instant.equivalence
      ))

(def: .public uri
  (-> Artifact URI)
  //.remote_project_uri)

(def: epoch
  Instant
  (instant.of_millis +0))

(def: .public (read repository artifact)
  (-> (Repository Async) Artifact (Async (Try Metadata)))
  (do async.monad
    [project (\ repository download (..uri artifact))]
    (case project
      (#try.Success project)
      (in (|> project
              (do> try.monad
                   [(\ utf8.codec decode)]
                   [(\ xml.codec decode)]
                   [list (<xml>.result ..parser)])))
      
      (#try.Failure error)
      (in (#try.Success
           (let [(^slots [#///artifact.group #///artifact.name]) artifact]
             {#group group
              #name name
              #versions (list)
              #last_updated ..epoch}))))))

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