Objectized the installer
This commit is contained in:
parent
eb875363e1
commit
72549ac3bf
@ -128,59 +128,78 @@ end.parse!
|
||||
|
||||
|
||||
|
||||
# BEGIN: Helper Classes
|
||||
|
||||
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 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/
|
||||
@remote_version = Gem::Version.new($LAST_MATCH_INFO['version'])
|
||||
else
|
||||
raise "Failed to retrieve remote version"
|
||||
end
|
||||
end
|
||||
@remote_version
|
||||
end
|
||||
|
||||
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
|
||||
@local_version
|
||||
end
|
||||
|
||||
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}"
|
||||
$CONFIG[:install_dir].chdir do
|
||||
File.open(output_file, 'wb') do |file|
|
||||
file.write(file_contents)
|
||||
end
|
||||
end
|
||||
puts "download_installer: tar.gz was downloaded"
|
||||
end
|
||||
|
||||
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|
|
||||
Gem::Package.new("").extract_tar_gz(file, $CONFIG[:install_dir])
|
||||
end
|
||||
|
||||
if not $CONFIG[:keep_installer]
|
||||
puts "do_install: Removing new installer (#{installer_file})"
|
||||
File.unlink(installer_file)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# END: Helper Classes
|
||||
|
||||
|
||||
|
||||
# BEGIN: Helper Functions
|
||||
|
||||
def get_remote_version
|
||||
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'])
|
||||
else
|
||||
raise "Failed to retrieve remote version"
|
||||
end
|
||||
end
|
||||
|
||||
def get_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
|
||||
|
||||
|
||||
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"
|
||||
file_contents = Net::HTTP.get(uri)
|
||||
output_file = "discord-#{version}.tar.gz"
|
||||
puts "download_installer: Output file will #{output_file}"
|
||||
$CONFIG[:install_dir].chdir do
|
||||
File.open(output_file, 'wb') do |file|
|
||||
file.write(file_contents)
|
||||
end
|
||||
end
|
||||
puts "download_installer: tar.gz was downloaded"
|
||||
end
|
||||
|
||||
def do_install(version)
|
||||
installer_file = "discord-#{version}.tar.gz"
|
||||
puts "do_install: Extracting new installer"
|
||||
$CONFIG[:install_dir].chdir do
|
||||
File.open(installer_file, 'rb') do |file|
|
||||
Gem::Package.new("").extract_tar_gz(file, $CONFIG[:install_dir])
|
||||
end
|
||||
|
||||
if not $CONFIG[:keep_installer]
|
||||
puts "do_install: Removing new installer (#{installer_file})"
|
||||
File.unlink(installer_file)
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
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
|
||||
|
Loading…
Reference in New Issue
Block a user