-
-
Notifications
You must be signed in to change notification settings - Fork 214
Expand file tree
/
Copy pathmirrorlist_generator.rb
More file actions
132 lines (108 loc) · 3.79 KB
/
mirrorlist_generator.rb
File metadata and controls
132 lines (108 loc) · 3.79 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
module MirrorlistGeneratorPlugin
class MirrorlistGenerator < Jekyll::Generator
safe true
def generate(site)
puts "Generating mirrorlist pages..."
versions = 0
site.data["versions"].each do |version|
version_name = version["name"]
versions += 1
# Generate files for the current main flavor (stable or latest pre-release).
site.pages << MirrorlistPage.new(site, version, false)
site.pages << MirrorlistPage.new(site, version, true)
# Iterate through previous releases and generate files for them as well.
if version.key?("releases")
version["releases"].each do |release|
version_name = release.key?("release_version") ? release["release_version"] : version["name"]
prerelease = {
'name' => version_name,
'flavor' => release["name"]
}
site.pages << MirrorlistPage.new(site, prerelease, false)
site.pages << MirrorlistPage.new(site, prerelease, true)
end
end
end
puts "Finished generating the mirrorlist (#{versions} versions generated)."
end
end
class MirrorlistPage < Jekyll::Page
def initialize(site, version, mono)
# Configure static information and path.
@site = site
@base = site.source
@dir = 'mirrorlist'
# Generate the version identificator.
version_name = version["name"]
version_flavor = version["flavor"]
version_id = "#{version_name}.#{version_flavor}"
if version_name == "3.0" # Hack for the only version before the naming scheme has been changed.
version_id = "#{version_name}-#{version_flavor}"
end
if mono
version_id = "#{version_id}.mono"
end
version_bits = version_name.split(".")
version_majmin = "#{version_bits[0]}.#{version_bits[1]}"
# Configure the permalink information.
@basename = version_id
@ext = '.json'
@name = "#{version_id}.json"
# Generate the list of mirrors.
mirrors = []
mirrorlist_configs = site.data["mirrorlist_configs"]
mirrorlist_hosts = mirrorlist_configs["hosts"]
version_defaults = mirrorlist_configs["defaults"].find { |config| config["name"] == version_majmin }
unless version_defaults.nil?
mirror_hosts = version_defaults["stable"]
unless version_flavor == "stable"
mirror_hosts = version_defaults["preview"]
end
mirror_hosts.each do |host_name|
mirror_host = mirrorlist_hosts.find { |host| host["name"] == host_name }
mirror_url = make_download(version, mono, host_name)
unless mirror_url == "#"
mirror = { 'name' => mirror_host["title"], 'url' => mirror_url }
mirrors.push(mirror)
end
end
else
mirror_host = mirrorlist_hosts.find { |host| host["name"] == "tuxfamily" }
mirror_url = make_download(version, mono, "tuxfamily")
unless mirror_url == "#"
mirror = { 'name' => mirror_host["title"], 'url' => mirror_url }
mirrors.push(mirror)
end
end
# Initialize data hash to pass objects to the template.
@data = {
'version_mirrors' => mirrors
}
# Defaults can be configured via the `_config.yml`, same as with normal
# collections.
data.default_proc = proc do |_, key|
site.frontmatter_defaults.find(relative_path, :mirrorlist, key)
end
end
# This is a hack to generate the URL from code. I don't know a better way to hook into Liquid and use its
# registered filters. But it's short and sweet enough for now (or for... ever).
def make_download(version, mono, host)
template = Liquid::Template.parse('{{ version | make_download: "templates", mono, host }}')
assigns = {
'version' => version,
'mono' => mono,
'host' => host
}
# We need to pass the site reference to registers because make_downloads relies on this context being
# present.
return template.render(assigns, registers: { site: @site })
end
def url_placeholders
{
:path => @dir,
:basename => basename,
:output_ext => output_ext,
}
end
end
end