-
Notifications
You must be signed in to change notification settings - Fork 338
/
bundled_gem.rb
88 lines (72 loc) · 2.35 KB
/
bundled_gem.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
module LicenseFinder
class BundledGem
LICENSE_FILE_NAMES = %w(LICENSE License Licence COPYING README Readme ReadMe)
README_FILE_NAMES = %w(README Readme ReadMe)
def initialize(spec, bundler_dependency = nil)
@spec = spec
@bundler_dependency = bundler_dependency
end
def name
"#{dependency_name} #{dependency_version}"
end
def dependency_name
@spec.name
end
def dependency_version
@spec.version.to_s
end
def to_dependency
@dependency ||= LicenseFinder::Dependency.new(
'name' => @spec.name,
'version' => @spec.version.to_s,
'license' => determine_license,
'license_files' => license_files.map(&:full_file_path),
'readme_files' => readme_files.map(&:full_file_path),
'source' => 'bundle',
'bundler_groups' => (@bundler_dependency.groups if @bundler_dependency),
'summary' => @spec.summary,
'description' => @spec.description,
'homepage' => @spec.homepage,
'children' => @spec.dependencies.collect(&:name)
)
end
def determine_license
return @spec.license if @spec.license
license_files.map(&:license).compact.first || 'other'
end
def license_files
paths_with_license_names = find_matching_files(LICENSE_FILE_NAMES)
paths_for_license_files = paths_with_license_names.map do |path|
File.directory?(path) ? paths_for_files_in_license_directory(path) : path
end.flatten.uniq
get_files_for_paths(paths_for_license_files)
end
def readme_files
find_matching_files(README_FILE_NAMES).map do |path|
get_file_for_path(path)
end
end
def install_path
@spec.full_gem_path
end
def sort_order
dependency_name.downcase
end
private
def find_matching_files(names)
Dir.glob(File.join(install_path, '**', "*{#{names.join(',')}}*"))
end
def get_file_for_path(path)
PossibleLicenseFile.new(install_path, path)
end
def paths_for_files_in_license_directory(path)
entries_in_directory = Dir::entries(path).reject { |p| p.match(/^(\.){1,2}$/) }
entries_in_directory.map { |entry_name| File.join(path, entry_name) }
end
def get_files_for_paths(paths_for_license_files)
paths_for_license_files.map do |path|
get_file_for_path(path)
end
end
end
end