/
dependency.rb
184 lines (159 loc) · 5.02 KB
/
dependency.rb
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
# encoding: UTF-8
require "erb"
module LicenseFinder
class Dependency
attr_accessor :name, :version, :license, :approved, :license_url, :notes, :license_files,
:readme_files, :source, :bundler_groups, :homepage, :children, :parents
attr_reader :summary, :description
def self.from_hash(attrs)
attrs['license_files'] = attrs['license_files'].map { |lf| lf['path'] } if attrs['license_files']
attrs['readme_files'] = attrs['readme_files'].map { |rf| rf['path'] } if attrs['readme_files']
new(attrs)
end
def initialize(attributes = {})
@source = attributes['source']
@name = attributes['name']
@version = attributes['version']
@license = attributes['license']
@approved = attributes['approved'] || LicenseFinder.config.whitelist.include?(attributes['license'])
@notes = attributes['notes'] || ''
@license_files = attributes['license_files'] || []
@readme_files = attributes['readme_files'] || []
@bundler_groups = attributes['bundler_groups'] || []
@summary = attributes['summary']
@description = attributes['description']
@homepage = attributes['homepage']
@children = attributes.fetch('children', [])
@parents = attributes.fetch('parents', [])
end
def license_url
LicenseFinder::LicenseUrl.find_by_name license
end
def merge(other)
raise "Cannot merge dependencies with different names. Expected #{name}, was #{other.name}." unless other.name == name
merged = self.class.new(
'name' => name,
'version' => other.version,
'license_files' => other.license_files,
'readme_files' => other.readme_files,
'license_url' => other.license_url,
'notes' => notes,
'source' => other.source,
'summary' => other.summary,
'description' => other.description,
'bundler_groups' => other.bundler_groups,
'homepage' => other.homepage,
'children' => other.children,
'parents' => other.parents
)
case other.license
when license, 'other'
merged.license = license
merged.approved = approved
else
merged.license = other.license
merged.approved = other.approved
end
merged
end
def as_yaml
attrs = {
'name' => name,
'version' => version,
'license' => license,
'approved' => approved,
'source' => source,
'license_url' => license_url,
'homepage' => homepage,
'notes' => notes,
'license_files' => nil,
'readme_files' => nil
}
unless license_files.empty?
attrs['license_files'] = license_files.map do |file|
{'path' => file}
end
end
unless readme_files.empty?
attrs['readme_files'] = readme_files.map do |file|
{'path' => file}
end
end
attrs
end
def to_yaml
as_yaml.to_yaml
end
def to_html
css_class = approved ? "approved" : "unapproved"
template = ERB.new <<-ERB
<div id="<%=name%>" class="<%=css_class%>">
<h2>
<% if homepage && !homepage.empty? %>
<a href="<%=homepage%>"><%=name%></a>
<% else %>
<%= name %>
<% end %>
v<%=version%>
<% if bundler_groups.any? %>
(<%= bundler_groups.join(", ") %>)
<% end%>
</h2>
<table class="table table-striped table-bordered">
<thead>
<tr>
<th>Summary</th>
<th>Description</th>
<th>License</th>
</tr>
</thead>
<tbody>
<tr>
<td><%= summary %></td>
<td><%= description %></td>
<td>
<% if license_url && !license_url.empty? %>
<a href="<%= license_url %>"><%= license %></a>
<% else %>
<%= license %>
<% end %>
</td>
</td>
</tr>
</tbody>
</table>
<% if parents.any? %>
<dl>
<dt>Parents</dt>
<% parents.each do |parent| %>
<dd><%= parent.name %></dd>
<% end %>
</dl>
<% end %>
<% if children.any? %>
<dl>
<dt>Children</dt>
<% children.each do |child| %>
<dd><%= child.name %></dd>
<% end %>
</dl>
<% end %>
</div>
ERB
template.result binding
end
def to_s
[name, version, license].join ", "
end
private
def constantize(string)
names = string.split('::')
names.shift if names.empty? || names.first.empty?
constant = Object
names.each do |name|
constant = constant.const_defined?(name, false) ? constant.const_get(name) : constant.const_missing(name)
end
constant
end
end
end