Objectized the installer

This commit is contained in:
Tristan Ancelet 2024-10-19 11:13:45 -05:00
parent eb875363e1
commit 72549ac3bf

View File

@ -128,32 +128,45 @@ end.parse!
# BEGIN: Helper Classes
# BEGIN: Helper Functions class Installer
URL_DOWNLOAD_BASE='https://dl.discordapp.net/apps/linux/{VERSION}/discord-{VERSION}.tar.gz'
DOWNLOAD_URL='https://discord.com/api/download/stable?platform=linux&format=tar.gz'
def get_remote_version def needs_update?
local_version < remote_version
end
def remote_version
if @remote_version.nil?
uri = URI(DOWNLOAD_URL) uri = URI(DOWNLOAD_URL)
response = Net::HTTP.get(uri) response = Net::HTTP.get(uri)
if response =~ /discord-(?<version>\d+\.\d+\.\d+).tar.gz/ if response =~ /discord-(?<version>\d+\.\d+\.\d+).tar.gz/
Gem::Version.new($LAST_MATCH_INFO['version']) @remote_version = Gem::Version.new($LAST_MATCH_INFO['version'])
else else
raise "Failed to retrieve remote version" raise "Failed to retrieve remote version"
end end
end end
@remote_version
end
def get_local_version def local_version
if File.exist? $CONFIG[:discord_version_file] if @local_version.nil?
@local_version = if File.exist? $CONFIG[:discord_version_file]
data = JSON.load_file($CONFIG[:discord_version_file]) data = JSON.load_file($CONFIG[:discord_version_file])
Gem::Version.new(data['version']) Gem::Version.new(data['version'])
else else
Gem::Version.new('0.0.0') Gem::Version.new('0.0.0')
end end
end end
@local_version
end
def download
def download_installer(version) version = self.remote_version.to_s
uri = URI(URL_DOWNLOAD_BASE.gsub('{VERSION}', version.to_s)) uri = URI(URL_DOWNLOAD_BASE.gsub('{VERSION}',version))
puts "download_installer: Downloading The discord #{version.to_s} tar.gz" puts "download_installer: Downloading The discord #{version} tar.gz"
file_contents = Net::HTTP.get(uri) file_contents = Net::HTTP.get(uri)
output_file = "discord-#{version}.tar.gz" output_file = "discord-#{version}.tar.gz"
puts "download_installer: Output file will #{output_file}" puts "download_installer: Output file will #{output_file}"
@ -165,8 +178,8 @@ def download_installer(version)
puts "download_installer: tar.gz was downloaded" puts "download_installer: tar.gz was downloaded"
end end
def do_install(version) def install
installer_file = "discord-#{version}.tar.gz" installer_file = "discord-#{self.remote_version}.tar.gz"
puts "do_install: Extracting new installer" puts "do_install: Extracting new installer"
$CONFIG[:install_dir].chdir do $CONFIG[:install_dir].chdir do
File.open(installer_file, 'rb') do |file| File.open(installer_file, 'rb') do |file|
@ -178,8 +191,14 @@ def do_install(version)
File.unlink(installer_file) File.unlink(installer_file)
end end
end end
end end
end
# END: Helper Classes
# BEGIN: Helper Functions
def update_desktop_file def update_desktop_file
file_needs_updating = true file_needs_updating = true
@ -274,20 +293,20 @@ end
# BEGIN: Work # BEGIN: Work
local_version = get_local_version
remote_version = get_remote_version
case $CONFIG[:action] case $CONFIG[:action]
when 'install' when 'install'
if local_version < remote_version installer = Installer.new
if installer.needs_update?
puts "main: Installed Version will be updated" puts "main: Installed Version will be updated"
download_installer(remote_version) installer.download
do_install(remote_version) installer.install
elsif local_version == remote_version elsif installer.local_version == installer.remote_version
puts "main: Installed Version is up-to-date" puts "main: Installed Version is up-to-date"
else else
puts "main: What?" puts "main: What?"
end end
when 'check' when 'check'
puts puts
puts " Report" puts " Report"
@ -295,8 +314,10 @@ case $CONFIG[:action]
puts " Remote Version: #{remote_version.to_s}" puts " Remote Version: #{remote_version.to_s}"
puts " Local Version: #{local_version.to_s}" puts " Local Version: #{local_version.to_s}"
puts " Needs Updating: #{(remote_version > local_version) ? "Yes" : "No"}" puts " Needs Updating: #{(remote_version > local_version) ? "Yes" : "No"}"
when 'backup' when 'backup'
make_backup make_backup
end end
# END: Work # END: Work