-
-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathScore.swift
More file actions
187 lines (159 loc) · 5.83 KB
/
Score.swift
File metadata and controls
187 lines (159 loc) · 5.83 KB
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
// Copyright Dave Verwer, Sven A. Schmidt, and other contributors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import Foundation
import Dependencies
enum Score {
struct Details: Codable, Equatable {
var licenseKind: License.Kind
var releaseCount: Int
var likeCount: Int
var isArchived: Bool
var numberOfDependencies: Int?
var lastActivityAt: Date
var hasDocumentation: Bool
var hasReadme: Bool
var numberOfContributors: Int
var hasTestTargets: Bool
var scoreBreakdown: [Category: Int] { Score.computeBreakdown(self) }
var score: Int { scoreBreakdown.score }
}
enum Category: CaseIterable {
case archive
case license
case releases
case stars
case dependencies
case maintenance
case documentation
case readme
case contributors
case tests
}
static func computeBreakdown(_ candidate: Details) -> [Category: Int] {
var scoreBreakdown: [Category: Int] = [:]
// Is the package archived and no longer receiving updates?
if candidate.isArchived == false {
scoreBreakdown[.archive] = 20
}
// Is the license a known open-source license?
switch candidate.licenseKind {
case .known:
scoreBreakdown[.license] = 10
default: break;
}
// Number of releases
switch candidate.releaseCount {
case ..<5:
break
case 5..<20:
scoreBreakdown[.releases] = 10
default:
scoreBreakdown[.releases] = 20
}
// Stars count
switch candidate.likeCount {
case ..<25:
break
case 25..<100:
scoreBreakdown[.stars] = 10
case 100..<500:
scoreBreakdown[.stars] = 20
case 500..<5_000:
scoreBreakdown[.stars] = 30
case 5_000..<10_000:
scoreBreakdown[.stars] = 35
default:
scoreBreakdown[.stars] = 37
}
// Number of resolved dependencies
switch candidate.numberOfDependencies {
case .some(..<3):
scoreBreakdown[.dependencies] = 5
case .some(3..<5):
scoreBreakdown[.dependencies] = 2
default: break
}
// Last maintenance activity
// Note: This is not the most accurate method to calculate the number of days between
// two dates, but is more than good enough for the purposes of this calculation.
@Dependency(\.date.now) var now
let dateDifference = Calendar.current.dateComponents([.day], from: candidate.lastActivityAt, to: now)
switch dateDifference.day {
case .some(..<30):
scoreBreakdown[.maintenance] = 15
case .some(30..<180):
scoreBreakdown[.maintenance] = 10
case .some(180..<360):
scoreBreakdown[.maintenance] = 5
default: break
}
// Documentation and README checks
if candidate.hasDocumentation {
scoreBreakdown[.documentation] = 15
}
if candidate.hasReadme {
scoreBreakdown[.readme] = 15
}
// Collaboration checks
switch candidate.numberOfContributors {
case ..<5: break
case 5..<20:
scoreBreakdown[.contributors] = 5
default:
scoreBreakdown[.contributors] = 10
}
// Target/product checks
if candidate.hasTestTargets {
scoreBreakdown[.tests] = 5
}
return scoreBreakdown
}
static func computeDetails(repo: Repository?, versions: [Version], targets: [(String, TargetType)]? = []) -> Details? {
guard
let defaultVersion = versions.latest(for: .defaultBranch),
let repo
else { return nil }
let hasDocumentation = [
defaultVersion,
versions.latest(for: .release),
versions.latest(for: .preRelease)
].hasDocumentation()
let numberOfContributors = {
guard let authors = repo.authors else { return 0 }
return authors.authors.count + authors.numberOfContributors
}()
let hasTestTargets = !(targets?.filter { $0.1 == App.TargetType.test }.isEmpty ?? false)
return .init(
licenseKind: repo.license.licenseKind,
releaseCount: versions.releases.count,
likeCount: repo.stars,
isArchived: repo.isArchived,
numberOfDependencies: defaultVersion.resolvedDependencies?.count,
lastActivityAt: repo.lastActivityAt ?? Date(timeIntervalSince1970: 0),
hasDocumentation: hasDocumentation,
hasReadme: repo.readmeHtmlUrl != nil,
numberOfContributors: numberOfContributors,
hasTestTargets: hasTestTargets
)
}
}
private extension Array where Element == Version {
func latest(for kind: Version.Kind) -> Version? {
first { $0.latest == kind }
}
var releases: Self { filter { $0.reference.isTag } }
}
extension [Score.Category: Int] {
var score: Int { compactMap { $0.value }.reduce(0, +) }
}