-
Notifications
You must be signed in to change notification settings - Fork 202
/
hackage.rb
83 lines (68 loc) · 2.59 KB
/
hackage.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
# frozen_string_literal: true
module PackageManager
class Hackage < Base
HAS_VERSIONS = true
HAS_DEPENDENCIES = false
BIBLIOTHECARY_SUPPORT = true
URL = "http://hackage.haskell.org"
COLOR = "#29b544"
def self.package_link(db_project, version = nil)
"http://hackage.haskell.org/package/#{db_project.name}" + (version ? "-#{version}" : "")
end
def self.download_url(db_project, version = nil)
"http://hackage.haskell.org/package/#{db_project.name}-#{version}/#{db_project.name}-#{version}.tar.gz"
end
def self.install_instructions(db_project, version = nil)
"cabal install #{db_project.name}" + (version ? "-#{version}" : "")
end
def self.project_names
get_json("http://hackage.haskell.org/packages/").map { |h| h["packageName"] }
end
def self.recent_names
u = "http://hackage.haskell.org/packages/recent.rss"
titles = SimpleRSS.parse(get_raw(u)).items.map(&:title)
titles.map { |t| t.split.first }.uniq
end
def self.project(name)
{
name: name,
page: get_html("http://hackage.haskell.org/package/#{name}"),
}
end
def self.mapping(raw_project)
MappingBuilder.build_hash(
name: raw_project[:name],
keywords_array: Array(raw_project[:page].css("#content div:first a")[1..].map(&:text)),
description: description(raw_project[:page]),
licenses: find_attribute(raw_project[:page], "License"),
homepage: find_attribute(raw_project[:page], "Home page"),
repository_url: repo_fallback(repository_url(find_attribute(raw_project[:page], "Source repository")), find_attribute(raw_project[:page], "Home page"))
)
end
def self.versions(raw_project, _name)
versions = find_attribute(raw_project[:page], "Versions")
versions = find_attribute(raw_project[:page], "Version") if versions.nil?
versions.delete("(info)").split(",").map(&:strip).map do |v|
VersionBuilder.build_hash(
number: v
)
end
end
def self.find_attribute(page, name)
tr = page.css("#content tr").select { |t| t.css("th").text.to_s.start_with?(name) }.first
tr&.css("td")&.text
end
def self.description(page)
contents = page.css("#content p, #content hr").map(&:text)
index = contents.index ""
return "" unless index
contents[0..(index - 1)].join("\n\n")
end
def self.repository_url(text)
return nil unless text.present?
match = text.match(/github.com\/(.+?)\.git/)
return nil unless match
"https://github.com/#{match[1]}"
end
end
end