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)
response = Net::HTTP.get(uri)
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
raise "Failed to retrieve remote version"
end
end
end
@remote_version
end
def get_local_version
if File.exist? $CONFIG[:discord_version_file]
def local_version
if @local_version.nil?
@local_version = if File.exist? $CONFIG[:discord_version_file]
data = JSON.load_file($CONFIG[:discord_version_file])
Gem::Version.new(data['version'])
else
Gem::Version.new('0.0.0')
end
end
end
@local_version
end
def download_installer(version)
uri = URI(URL_DOWNLOAD_BASE.gsub('{VERSION}', version.to_s))
puts "download_installer: Downloading The discord #{version.to_s} tar.gz"
def download
version = self.remote_version.to_s
uri = URI(URL_DOWNLOAD_BASE.gsub('{VERSION}',version))
puts "download_installer: Downloading The discord #{version} tar.gz"
file_contents = Net::HTTP.get(uri)
output_file = "discord-#{version}.tar.gz"
puts "download_installer: Output file will #{output_file}"
@ -163,10 +176,10 @@ def download_installer(version)
end
end
puts "download_installer: tar.gz was downloaded"
end
end
def do_install(version)
installer_file = "discord-#{version}.tar.gz"
def install
installer_file = "discord-#{self.remote_version}.tar.gz"
puts "do_install: Extracting new installer"
$CONFIG[:install_dir].chdir do
File.open(installer_file, 'rb') do |file|
@ -178,9 +191,15 @@ def do_install(version)
File.unlink(installer_file)
end
end
end
end
# END: Helper Classes
# BEGIN: Helper Functions
def update_desktop_file
file_needs_updating = true
puts "update_desktop_file: Checking if desktop entry needs updating"
@ -274,20 +293,20 @@ end
# BEGIN: Work
local_version = get_local_version
remote_version = get_remote_version
case $CONFIG[:action]
when 'install'
if local_version < remote_version
installer = Installer.new
if installer.needs_update?
puts "main: Installed Version will be updated"
download_installer(remote_version)
do_install(remote_version)
elsif local_version == remote_version
installer.download
installer.install
elsif installer.local_version == installer.remote_version
puts "main: Installed Version is up-to-date"
else
puts "main: What?"
end
when 'check'
puts
puts " Report"
@ -295,8 +314,10 @@ case $CONFIG[:action]
puts " Remote Version: #{remote_version.to_s}"
puts " Local Version: #{local_version.to_s}"
puts " Needs Updating: #{(remote_version > local_version) ? "Yes" : "No"}"
when 'backup'
make_backup
end
# END: Work