Skip to content

Commit 0a3359f

Browse files
committed
turn PackageBuildStatus into a type and JSON coding model
1 parent 7ff3273 commit 0a3359f

File tree

8 files changed

+172
-31
lines changed

8 files changed

+172
-31
lines changed

Package.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ let package:Package = .init(
5656

5757
.library(name: "Unidoc", targets: ["Unidoc"]),
5858
.library(name: "UnidocAnalysis", targets: ["UnidocAnalysis"]),
59+
.library(name: "UnidocAutomation", targets: ["UnidocAutomation"]),
5960
.library(name: "UnidocDB", targets: ["UnidocDB"]),
6061
.library(name: "UnidocDiagnostics", targets: ["UnidocDiagnostics"]),
6162
.library(name: "UnidocLinker", targets: ["UnidocLinker"]),
@@ -65,6 +66,7 @@ let package:Package = .init(
6566
.library(name: "UnidocSelectors", targets: ["UnidocSelectors"]),
6667
.library(name: "URI", targets: ["URI"]),
6768

69+
.executable(name: "UnidocBuild", targets: ["UnidocBuild"]),
6870
.executable(name: "UnidocServer", targets: ["UnidocServer"]),
6971
],
7072
dependencies:
@@ -381,6 +383,11 @@ let package:Package = .init(
381383
.target(name: "UnidocSelectors"),
382384
]),
383385

386+
.target(name: "UnidocAutomation", dependencies:
387+
[
388+
.target(name: "JSON"),
389+
]),
390+
384391
.target(name: "UnidocDB", dependencies:
385392
[
386393
.target(name: "GitHubAPI"),
@@ -409,6 +416,7 @@ let package:Package = .init(
409416
.target(name: "GitHubAPI"),
410417
.target(name: "HTTP"),
411418
.target(name: "MarkdownRendering"),
419+
.target(name: "UnidocAutomation"),
412420
.target(name: "UnidocQueries"),
413421
.target(name: "URI"),
414422
]),
@@ -452,6 +460,13 @@ let package:Package = .init(
452460
.target(name: "SymbolGraphBuilder"),
453461
]),
454462

463+
.executableTarget(name: "UnidocBuild", dependencies:
464+
[
465+
.target(name: "HTTPClient"),
466+
.target(name: "UnidocAutomation"),
467+
.target(name: "SymbolGraphBuilder"),
468+
]),
469+
455470
.executableTarget(name: "UnidocServer", dependencies:
456471
[
457472
.target(name: "GitHubClient"),
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import JSONAST
2+
3+
public
4+
protocol JSONObjectEncodable<CodingKey>:JSONEncodable
5+
{
6+
associatedtype CodingKey:RawRepresentable<String> = JSON.Key
7+
8+
func encode(to json:inout JSON.ObjectEncoder<CodingKey>)
9+
}
10+
extension JSONObjectEncodable
11+
{
12+
@inlinable public
13+
func encode(to json:inout JSON)
14+
{
15+
self.encode(to: &json[as: JSON.ObjectEncoder<CodingKey>.self])
16+
}
17+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import JSON
2+
3+
extension PackageBuildStatus
4+
{
5+
@frozen public
6+
struct Edition:Equatable, Sendable
7+
{
8+
public
9+
let graphs:Int
10+
public
11+
let tag:String
12+
13+
@inlinable public
14+
init(graphs:Int, tag:String)
15+
{
16+
self.graphs = graphs
17+
self.tag = tag
18+
}
19+
}
20+
}
21+
extension PackageBuildStatus.Edition
22+
{
23+
@frozen public
24+
enum CodingKey:String
25+
{
26+
case graphs
27+
case tag
28+
}
29+
}
30+
extension PackageBuildStatus.Edition:JSONObjectEncodable
31+
{
32+
public
33+
func encode(to json:inout JSON.ObjectEncoder<CodingKey>)
34+
{
35+
json[.graphs] = self.graphs
36+
json[.tag] = self.tag
37+
}
38+
}
39+
extension PackageBuildStatus.Edition:JSONObjectDecodable
40+
{
41+
public
42+
init(json:JSON.ObjectDecoder<CodingKey>) throws
43+
{
44+
self.init(
45+
graphs: try json[.graphs].decode(),
46+
tag: try json[.tag].decode())
47+
}
48+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import JSON
2+
3+
@frozen public
4+
struct PackageBuildStatus
5+
{
6+
public
7+
let repo:String
8+
9+
public
10+
var release:Edition
11+
public
12+
var prerelease:Edition?
13+
14+
@inlinable public
15+
init(repo:String, release:Edition, prerelease:Edition? = nil)
16+
{
17+
self.repo = repo
18+
self.release = release
19+
self.prerelease = prerelease
20+
}
21+
}
22+
extension PackageBuildStatus
23+
{
24+
@frozen public
25+
enum CodingKey:String
26+
{
27+
case repo
28+
case release
29+
case prerelease
30+
}
31+
}
32+
extension PackageBuildStatus:JSONObjectEncodable
33+
{
34+
public
35+
func encode(to json:inout JSON.ObjectEncoder<CodingKey>)
36+
{
37+
json[.repo] = self.repo
38+
json[.release] = self.release
39+
json[.prerelease] = self.prerelease
40+
}
41+
}
42+
extension PackageBuildStatus:JSONObjectDecodable
43+
{
44+
public
45+
init(json:JSON.ObjectDecoder<CodingKey>) throws
46+
{
47+
self.init(repo: try json[.repo].decode(),
48+
release: try json[.release].decode(),
49+
prerelease: try json[.prerelease]?.decode())
50+
}
51+
}

Sources/UnidocBuild/Main.swift

Whitespace-only changes.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import UnidocAutomation
2+
import UnidocDB
3+
import UnidocQueries
4+
5+
extension PackageBuildStatus
6+
{
7+
init?(from output:PackageEditionsQuery.Output)
8+
{
9+
guard
10+
let repo:PackageRepo = output.record.repo,
11+
let release:PackageEditionsQuery.Facet = output.releases.first,
12+
let release:Edition = .init(from: release)
13+
else
14+
{
15+
return nil
16+
}
17+
18+
self.init(
19+
repo: "https://\(repo.origin)",
20+
release: release,
21+
prerelease: output.prereleases.first.flatMap(Edition.init(from:)))
22+
}
23+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import UnidocAutomation
2+
import UnidocQueries
3+
4+
extension PackageBuildStatus.Edition
5+
{
6+
init?(from output:PackageEditionsQuery.Facet)
7+
{
8+
self.init(graphs: output.graphs?.count ?? 0, tag: output.edition.name)
9+
}
10+
}

Sources/UnidocPages/Responses/PackageEditionsQuery.Output (ext).swift

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import HTTP
22
import JSON
33
import Media
4-
import UnidocDB
4+
import UnidocAutomation
55
import UnidocQueries
66
import URI
77

@@ -13,39 +13,16 @@ extension PackageEditionsQuery.Output:ServerResponseFactory
1313
switch type
1414
{
1515
case .application(.json):
16-
let json:JSON = .object
16+
guard
17+
let status:PackageBuildStatus = .init(from: self)
18+
else
1719
{
18-
guard
19-
let repo:PackageRepo = self.record.repo,
20-
let release:PackageEditionsQuery.Facet = self.releases.first
21-
else
22-
{
23-
return
24-
}
25-
26-
$0["repo"] = "https://\(repo.origin)"
27-
28-
$0["release"]
29-
{
30-
$0["graphs"] = release.graphs?.count ?? 0
31-
$0["tag"] = release.edition.name
32-
}
33-
34-
guard
35-
let prerelease:PackageEditionsQuery.Facet = self.prereleases.first,
36-
prerelease.edition.patch > release.edition.patch
37-
else
38-
{
39-
return
40-
}
41-
42-
$0["prerelease"]
43-
{
44-
$0["graphs"] = prerelease.graphs?.count ?? 0
45-
$0["tag"] = prerelease.edition.name
46-
}
20+
return .notFound(.init(content: .string(""),
21+
type: .text(.plain, charset: .utf8)))
4722
}
4823

24+
let json:JSON = .object(with: status.encode(to:))
25+
4926
return .ok(.init(
5027
content: .binary(json.utf8),
5128
type: .application(.json, charset: .utf8)))

0 commit comments

Comments
 (0)