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"
37require "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
138134end
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