Compare commits

..

2 Commits

Author SHA1 Message Date
7b61dd700a Added installation backup functionality 2024-10-19 10:16:53 -05:00
01a838c7f2 Pre change 2024-10-18 22:48:44 -05:00

View File

@ -3,12 +3,14 @@
# BEGIN: Includes & Requires # BEGIN: Includes & Requires
require 'erb' require 'erb'
require 'time'
require 'json' require 'json'
require 'net/http' require 'net/http'
require 'English' require 'English'
require 'rubygems/package' require 'rubygems/package'
require 'fileutils' require 'fileutils'
require 'optparse' require 'optparse'
require 'zlib'
# END: Includes & Requires # END: Includes & Requires
@ -46,6 +48,7 @@ DOWNLOAD_URL='https://discord.com/api/download/stable?platform=linux&format=tar.
$CONFIG = Hash.new $CONFIG = Hash.new
$CONFIG[:icon_dir] = Dir.new(Dir.home) / '.local' / 'share' / 'icons' $CONFIG[:icon_dir] = Dir.new(Dir.home) / '.local' / 'share' / 'icons'
File.mkdir($CONFIG[:icon_dir]) if not File.exist?($CONFIG[:icon_dir]) File.mkdir($CONFIG[:icon_dir]) if not File.exist?($CONFIG[:icon_dir])
$CONFIG[:discord_config_path] = Dir.new(Dir.home) / '.config' / 'discord'
$CONFIG[:local_application_install_dir] = Dir.new(Dir.home) / '.local' / 'share' / 'applications' $CONFIG[:local_application_install_dir] = Dir.new(Dir.home) / '.local' / 'share' / 'applications'
File.mkdir($CONFIG[:local_application_install_dir]) if not File.exist?($CONFIG[:local_application_install_dir]) File.mkdir($CONFIG[:local_application_install_dir]) if not File.exist?($CONFIG[:local_application_install_dir])
$CONFIG[:desktop_path] = Dir.new(Dir.home) / '.local' / 'share' / 'applications' / 'discord.desktop' $CONFIG[:desktop_path] = Dir.new(Dir.home) / '.local' / 'share' / 'applications' / 'discord.desktop'
@ -88,6 +91,15 @@ OptionParser.new do |parser|
$CONFIG[:action] = 'install' $CONFIG[:action] = 'install'
end end
parser.on('-u', '--uninstall', "Uninstall Discord (not-implemented)") do
$CONFIG[:action] = 'uninstall'
end
parser.on('-b', '--backup', "Backup Discord Installation (not-implemented)") do
$CONFIG[:action] = 'backup'
$CONFIG[:backup_filename] = 'discord-backup-' + Time.now.strftime('%Y-%m-%d') + '.tar.gz'
end
parser.separator "" parser.separator ""
parser.separator " General Flags" parser.separator " General Flags"
parser.separator "------------------------------------------------------------------------" parser.separator "------------------------------------------------------------------------"
@ -127,6 +139,7 @@ def get_local_version
end end
end end
def download_installer(version) def download_installer(version)
uri = URI(URL_DOWNLOAD_BASE.gsub('{VERSION}', version.to_s)) uri = URI(URL_DOWNLOAD_BASE.gsub('{VERSION}', version.to_s))
puts "download_installer: Downloading The discord #{version.to_s} tar.gz" puts "download_installer: Downloading The discord #{version.to_s} tar.gz"
@ -148,7 +161,6 @@ def do_install(version)
File.open(installer_file, 'rb') do |file| File.open(installer_file, 'rb') do |file|
Gem::Package.new("").extract_tar_gz(file, $CONFIG[:install_dir]) Gem::Package.new("").extract_tar_gz(file, $CONFIG[:install_dir])
end end
end
if not $CONFIG[:keep_installer] if not $CONFIG[:keep_installer]
puts "do_install: Removing new installer (#{installer_file})" puts "do_install: Removing new installer (#{installer_file})"
@ -156,6 +168,8 @@ def do_install(version)
end end
end end
end
def update_desktop_file def update_desktop_file
file_needs_updating = true file_needs_updating = true
puts "update_desktop_file: Checking if desktop entry needs updating" puts "update_desktop_file: Checking if desktop entry needs updating"
@ -180,6 +194,60 @@ def update_desktop_file
end end
end end
def make_backup
files = {
files: Array.new,
dirs: Array.new,
symlinks: Array.new
}
Dir.glob("#{$CONFIG[:discord_config_path].path}/**/**/**/**").each{ |item|
if File.file?(item)
files[:files] << item
elsif File.symlink?(item)
files[:symlinks] << item
elsif File.directory?(item)
files[:dirs] << item
end
}
Dir.glob("#{$CONFIG[:discord_path].path}/**/**/**/**").each {|item|
if File.file?(item)
files[:files] << item
elsif File.symlink?(item)
files[:symlinks] << item
elsif File.directory?(item)
files[:dirs] << item
end
}
Dir.chdir(Dir.home) do
File.open($CONFIG[:backup_filename], "wb") do |backup_file|
Zlib::GzipWriter.wrap(backup_file) do |gzip|
Gem::Package::TarWriter.new(gzip) do |tar|
files[:files].each do |file|
File.open(file, 'rb') { |file_to_archive|
contents = file_to_archive.read()
tar.add_file_simple(file_to_archive.path.sub("#{Dir.home}/",''), file_to_archive.stat.mode, contents.length) do |io|
io.write(contents)
end
}
end
files[:dirs].each do |dir|
tar.mkdir(dir.sub("#{Dir.home}/",""), File.stat(dir).mode)
end
files[:symlinks].each {|symlink|
tar.add_symlink(symlink.sub("#{Dir.home}/",""), File.readlink(symlink), File.lstat(symlink).mode)
}
end
end
end
end
end
# END: Helper Functions # END: Helper Functions
@ -207,6 +275,8 @@ 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'
make_backup
end end
# END: Work # END: Work