Skip to content

Commit 129b879

Browse files
committed
Add a script to store download data in hugo.yml
We will use that information in the download pages. This script is a transmogrified version of `app/services/download_service.rb`. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 3741988 commit 129b879

File tree

4 files changed

+83
-54
lines changed

4 files changed

+83
-54
lines changed

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
source "https://rubygems.org"
44

55
gem "octokit"
6+
gem "rss"

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,9 @@ Or you can do it from GitHub (much slower) like this:
8282

8383
## Update the `Downloads` pages
8484

85-
(TODO!)
8685
Now you need to get the latest downloads for the downloads pages:
8786

88-
$ rake downloads
87+
$ ruby ./script/update-download-data.rb
8988

9089
## Update the ProGit book
9190

hugo.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,29 @@ params:
1717
latest_version: 2.46.0
1818
latest_relnote_url: https://raw.github.com/git/git/master/Documentation/RelNotes/2.46.0.txt
1919
latest_release_date: '2024-07-29'
20+
macos_installer:
21+
url: https://sourceforge.net/projects/git-osx-installer/files/git-2.33.0-intel-universal-mavericks.dmg/download?use_mirror=autoselect
22+
version: 2.33.0
23+
release_date: '2021-08-30'
24+
filename: git-2.33.0-intel-universal-mavericks.dmg
25+
windows_installer:
26+
portable32:
27+
filename: PortableGit-2.45.2-32-bit.7z.exe
28+
release_date: '2024-06-03'
29+
version: 2.45.2
30+
url: https://github.com/git-for-windows/git/releases/download/v2.45.2.windows.1/PortableGit-2.45.2-32-bit.7z.exe
31+
portable64:
32+
filename: PortableGit-2.45.2-64-bit.7z.exe
33+
release_date: '2024-06-03'
34+
version: 2.45.2
35+
url: https://github.com/git-for-windows/git/releases/download/v2.45.2.windows.1/PortableGit-2.45.2-64-bit.7z.exe
36+
installer32:
37+
filename: Git-2.45.2-32-bit.exe
38+
release_date: '2024-06-03'
39+
version: 2.45.2
40+
url: https://github.com/git-for-windows/git/releases/download/v2.45.2.windows.1/Git-2.45.2-32-bit.exe
41+
installer64:
42+
filename: Git-2.45.2-64-bit.exe
43+
release_date: '2024-06-03'
44+
version: 2.45.2
45+
url: https://github.com/git-for-windows/git/releases/download/v2.45.2.windows.1/Git-2.45.2-64-bit.exe
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
1-
# frozen_string_literal: true
1+
#!/usr/bin/env ruby
22

3+
# This is a simple script to update the download data in hugo.yml
4+
5+
require "logger"
6+
require "octokit"
37
require "rss"
8+
require "yaml"
9+
10+
$logger = Logger.new(STDERR)
411

5-
class DownloadService
12+
class DownloadData
613
# [OvD] note that Google uses Atom & Sourceforge uses RSS
714
# however this isn't relevant when parsing the feeds for
815
# name, version, url & date with Feedzirra
@@ -16,7 +23,7 @@ def sourceforge_project_download_url(project, filename)
1623
"https://sourceforge.net/projects/#{project}/files/#{filename}/download?use_mirror=autoselect"
1724
end
1825

19-
def download_windows_versions
26+
def update_download_windows_versions(config)
2027
files_from_github(GIT_FOR_WINDOWS_NAME_WITH_OWNER).each do |name, date, url|
2128
# Git for Windows uses the following naming system
2229
# [Portable]Git-#.#.#.#[-dev-preview]-32/64-bit[.7z].exe
@@ -29,45 +36,50 @@ def download_windows_versions
2936

3037
# Git for windows sometimes creates extra releases all based off of the same upstream Git version
3138
# so we want to crop versions like 2.16.1.4 to just 2.16.1
32-
version_name = match[2].slice(/^\d+\.\d+\.\d+/)
33-
version = find_version_by_name(version_name)
39+
version = match[2].slice(/^\d+\.\d+\.\d+/)
3440

3541
if version
36-
find_or_create_download(
37-
filename: name,
38-
platform: "windows#{bitness}#{portable}",
39-
release_date: date,
40-
version: version,
41-
url: url
42-
)
42+
config["windows_installer"] = {} if config["windows_installer"].nil?
43+
win_config = config["windows_installer"]
44+
if portable.empty?
45+
key = "installer#{bitness}"
46+
else
47+
key = "portable#{bitness}"
48+
end
49+
win_config[key] = {} if win_config[key].nil?
50+
return if version_compare(version, win_config[key]["version"]) < 0
51+
win_config[key]["filename"] = name
52+
win_config[key]["release_date"] = date.strftime("%Y-%m-%d")
53+
win_config[key]["version"] = version
54+
win_config[key]["url"] = url
4355
else
44-
Rails.logger.info("Could not find version #{version_name}")
56+
$logger.info("Could not find version #{version_name}")
4557
end
4658
end
4759
end
4860

49-
def download_mac_versions
61+
def update_download_mac_versions(config)
5062
files_from_sourceforge(SOURCEFORGE_URL).each do |url, date|
51-
name = url.split("/")[-2]
52-
match = /git-(.*?)-/.match(name)
63+
filename = url.split("/")[-2]
64+
match = /git-(.*?)-/.match(filename)
5365

5466
next unless match
5567

56-
url = sourceforge_project_download_url("git-osx-installer", name)
68+
url = sourceforge_project_download_url("git-osx-installer", filename)
5769
name = match[1]
5870

59-
version = find_version_by_name(name)
71+
version = name
6072

6173
if version
62-
find_or_create_download(
63-
filename: name,
64-
platform: "mac",
65-
release_date: Time.parse(date.iso8601),
66-
version: version,
67-
url: url
68-
)
74+
config["macos_installer"] = {} if config["macos_installer"].nil?
75+
mac_config = config["macos_installer"]
76+
return if version_compare(version, mac_config["version"]) < 0
77+
mac_config["filename"] = filename
78+
mac_config["release_date"] = Time.parse(date.iso8601).strftime("%Y-%m-%d")
79+
mac_config["version"] = version
80+
mac_config["url"] = url
6981
else
70-
Rails.logger.info("Could not find version #{name}")
82+
$logger.info("Could not find version #{name}")
7183
end
7284
end
7385
end
@@ -106,33 +118,24 @@ def files_from_sourceforge(repository)
106118
downloads
107119
end
108120

109-
def find_or_create_download(filename:, platform:, release_date:, version:, url:)
110-
options = {
111-
filename: filename,
112-
platform: platform,
113-
release_date: release_date,
114-
version: version,
115-
url: url
116-
}
117-
118-
if (download = Download.find_by(options))
119-
Rails.logger.info("Download record found #{download.inspect}")
120-
else
121-
begin
122-
download = Download.create!(options)
123-
Rails.logger.info("Download record created #{download.inspect}")
124-
rescue ActiveRecord::RecordInvalid => e
125-
Rail.logger.error(e.message.to_s)
126-
end
121+
def version_compare(a, b)
122+
a = a.nil? ? [] : a.gsub(/^v/, "").split(/\./)
123+
b = b.nil? ? [] : b.gsub(/^v/, "").split(/\./)
124+
while true
125+
a0 = a.shift
126+
b0 = b.shift
127+
return b0.nil? ? 0 : -1 if a0.nil?
128+
return +1 if b0.nil?
129+
diff = a0.to_i - b0.to_i
130+
return diff unless diff == 0
127131
end
128132
end
129-
130-
def find_version_by_name(name)
131-
# We assume the preindex rake task ran previously and saved possible new versions into the storage.
132-
# Otherwise this code should also create the versions, while the preindex task needs to be updated to deal
133-
# with versions imported by other tasks without importing the docs.
134-
# More details at https://github.com/git/git-scm.com/pull/1207.
135-
Version.find_by(name: name)
136-
end
137133
end
138134
end
135+
136+
config = YAML.load_file("hugo.yml")
137+
config["params"] = {} if config["params"].nil?
138+
DownloadData.update_download_windows_versions(config["params"])
139+
DownloadData.update_download_mac_versions(config["params"])
140+
yaml = YAML.dump(config).gsub(/ *$/, "")
141+
File.write("hugo.yml", yaml)

0 commit comments

Comments
 (0)