Compare commits
11 Commits
89cbb8a4d4
...
master
Author | SHA1 | Date | |
---|---|---|---|
|
e7f6697038 | ||
|
3be7c31aa5 | ||
|
77eb648a9a | ||
|
1e53f004f7 | ||
|
696a71bd8b | ||
|
713ea521c8 | ||
b6226ad3cb | |||
72549ac3bf | |||
eb875363e1 | |||
7b61dd700a | |||
01a838c7f2 |
@@ -3,12 +3,15 @@
|
|||||||
# BEGIN: Includes & Requires
|
# BEGIN: Includes & Requires
|
||||||
|
|
||||||
require 'erb'
|
require 'erb'
|
||||||
|
require 'time'
|
||||||
require 'json'
|
require 'json'
|
||||||
require 'net/http'
|
require 'zlib'
|
||||||
|
require 'find'
|
||||||
require 'English'
|
require 'English'
|
||||||
require 'rubygems/package'
|
require 'net/http'
|
||||||
require 'fileutils'
|
|
||||||
require 'optparse'
|
require 'optparse'
|
||||||
|
require 'fileutils'
|
||||||
|
require 'rubygems/package'
|
||||||
|
|
||||||
# END: Includes & Requires
|
# END: Includes & Requires
|
||||||
|
|
||||||
@@ -20,18 +23,29 @@ require 'optparse'
|
|||||||
|
|
||||||
class Dir
|
class Dir
|
||||||
def / (path)
|
def / (path)
|
||||||
output_path = File.absolute_path(self.path) + '/' + path
|
output_path = File.join(self.path + '/' + path)
|
||||||
if File.directory? output_path
|
if File.directory? output_path
|
||||||
Dir.new(output_path)
|
Dir.new(output_path)
|
||||||
else
|
else
|
||||||
output_path
|
output_path
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
alias to_s path
|
||||||
|
|
||||||
|
## For backwards compat
|
||||||
|
def chdir(dir = nil, &block)
|
||||||
|
if dir.nil?
|
||||||
|
Dir.chdir(self, &block)
|
||||||
|
else
|
||||||
|
Dir.chdir(dir, &block)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class String
|
class String
|
||||||
def / (other)
|
def / (other)
|
||||||
self + '/' + other
|
File.join(self, other)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -46,28 +60,30 @@ 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[:install_dir] = Dir.new(Dir.home) / '.opt'
|
$CONFIG[:install_dir] = Dir.new(Dir.home) / '.opt'
|
||||||
|
$CONFIG[:desktop_path] = $CONFIG[:install_dir] / 'Discord' / 'discord.desktop'
|
||||||
File.mkdir($CONFIG[:install_dir]) if not File.exist?($CONFIG[:install_dir])
|
File.mkdir($CONFIG[:install_dir]) if not File.exist?($CONFIG[:install_dir])
|
||||||
$CONFIG[:discord_path] = $CONFIG[:install_dir] / 'Discord'
|
$CONFIG[:discord_path] = $CONFIG[:install_dir] / 'Discord'
|
||||||
$CONFIG[:desktop_file_path] = $CONFIG[:discord_path] / 'discord.desktop'
|
$CONFIG[:desktop_file_path] = $CONFIG[:local_application_install_dir] / 'discord.desktop'
|
||||||
$CONFIG[:discord_version_file] = $CONFIG[:discord_path] / 'resources' / 'build_info.json'
|
$CONFIG[:discord_version_file] = $CONFIG[:discord_path] / 'resources' / 'build_info.json'
|
||||||
$CONFIG[:action] = 'install'
|
$CONFIG[:action] = 'install'
|
||||||
$CONFIG[:keep_installer] = false
|
$CONFIG[:keep_installer] = false
|
||||||
DISCORD_DESKTOP_CONTENTS=<<EOF
|
$CONFIG[:debug] = false
|
||||||
|
DISCORD_DESKTOP_TEMPLATE = ERB.new(<<ERB, trim_mode:'-')
|
||||||
[Desktop Entry]
|
[Desktop Entry]
|
||||||
Name=Discord
|
Name=Discord
|
||||||
StartupWMClass=discord
|
StartupWMClass=discord
|
||||||
Comment=All-in-one voice and text chat for gamers that's free, secure, and works on both your desktop and phone.
|
Comment=All-in-one voice and text chat for gamers that's free, secure, and works on both your desktop and phone.
|
||||||
GenericName=Internet Messenger
|
GenericName=Internet Messenger
|
||||||
Exec=#{$CONFIG[:install_dir].path}/Discord/Discord
|
Exec=<%= $CONFIG[:discord_path] %>/Discord
|
||||||
Icon=discord
|
Icon=discord
|
||||||
Type=Application
|
Type=Application
|
||||||
Categories=Network;InstantMessaging;
|
Categories=Network;InstantMessaging;
|
||||||
Path=#{$CONFIG[:install_dir].path}/Discord
|
Path=<%= $CONFIG[:discord_path] %>
|
||||||
EOF
|
ERB
|
||||||
# END: Variables
|
# END: Variables
|
||||||
|
|
||||||
|
|
||||||
@@ -88,6 +104,24 @@ OptionParser.new do |parser|
|
|||||||
$CONFIG[:action] = 'install'
|
$CONFIG[:action] = 'install'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
parser.on('-u', '--uninstall', "Uninstall Discord (not-implemented)") do
|
||||||
|
$CONFIG[:action] = 'uninstall'
|
||||||
|
$CONFIG[:backup_filename] = Dir.home / 'discord-backup-' + Time.now.strftime('%Y-%m-%d') + '.tar.gz'
|
||||||
|
end
|
||||||
|
|
||||||
|
parser.on('-b', '--backup [FILENAME]', "Backup Discord Installation") do |backup_filename|
|
||||||
|
$CONFIG[:action] = 'backup'
|
||||||
|
if backup_filename.nil?
|
||||||
|
$CONFIG[:backup_filename] = Dir.home / 'discord-backup-' + Time.now.strftime('%Y-%m-%d') + '.tar.gz'
|
||||||
|
else
|
||||||
|
$CONFIG[:backup_filename] = backup_filename + ".tar.gz"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
parser.on('--update-desktop', "Update desktop file"){
|
||||||
|
$CONFIG[:action] = 'update-desktop'
|
||||||
|
}
|
||||||
|
|
||||||
parser.separator ""
|
parser.separator ""
|
||||||
parser.separator " General Flags"
|
parser.separator " General Flags"
|
||||||
parser.separator "------------------------------------------------------------------------"
|
parser.separator "------------------------------------------------------------------------"
|
||||||
@@ -95,7 +129,12 @@ OptionParser.new do |parser|
|
|||||||
puts parser
|
puts parser
|
||||||
exit
|
exit
|
||||||
end
|
end
|
||||||
parser.on('--keep', 'Keep discord tar.gz file (default: False)') do
|
|
||||||
|
parser.on('-d', '--debug', "Turn on debug logging") do
|
||||||
|
$CONFIG[:debug] = true
|
||||||
|
end
|
||||||
|
|
||||||
|
parser.on('--keep', "Keep discord tar.gz file (default: #{$CONFIG[:keep_installer].to_s})") do
|
||||||
$CONFIG[:keep_installer] = true
|
$CONFIG[:keep_installer] = true
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -105,31 +144,69 @@ end.parse!
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# BEGIN: Helper Classes
|
||||||
|
class DesktopFile
|
||||||
|
@@file = File.open($CONFIG[:desktop_path])
|
||||||
|
@@installed_file = File.open($CONFIG[:desktop_file_path])
|
||||||
|
|
||||||
# BEGIN: Helper Functions
|
def needs_update?
|
||||||
|
@@installed_file.read() != contents
|
||||||
|
end
|
||||||
|
|
||||||
def get_remote_version
|
def update
|
||||||
|
if needs_update?
|
||||||
|
File.open(@@file, 'w') do |file|
|
||||||
|
puts "Updating Desktop File, changes are needed"
|
||||||
|
file.write(contents)
|
||||||
|
end
|
||||||
|
|
||||||
|
puts "update_desktop_file: Triggering desktop to install new desktop entry and refresh entries"
|
||||||
|
%x[ desktop-file-install --dir=#{$CONFIG[:local_application_install_dir]} #{$CONFIG[:desktop_path]} ]
|
||||||
|
else
|
||||||
|
puts "Not updating file. Changes not needed"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
DISCORD_DESKTOP_TEMPLATE.def_method(DesktopFile, 'contents')
|
||||||
|
|
||||||
|
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'
|
||||||
|
@@desktop_file = DesktopFile.new
|
||||||
|
|
||||||
|
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_installer(version)
|
def download
|
||||||
uri = URI(URL_DOWNLOAD_BASE.gsub('{VERSION}', version.to_s))
|
version = self.remote_version.to_s
|
||||||
puts "download_installer: Downloading The discord #{version.to_s} tar.gz"
|
uri = URI(URL_DOWNLOAD_BASE.gsub('{VERSION}',version))
|
||||||
|
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}"
|
||||||
@@ -141,14 +218,13 @@ 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|
|
||||||
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,28 +232,78 @@ def do_install(version)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def update_desktop_file
|
@@desktop_file.update
|
||||||
file_needs_updating = true
|
end
|
||||||
puts "update_desktop_file: Checking if desktop entry needs updating"
|
end
|
||||||
if File.exist?($CONFIG[:desktop_path])
|
|
||||||
puts "update_desktop_file: Desktop file exists, checking if it matches template"
|
|
||||||
File.open($CONFIG[:desktop_path], 'r') do |file|
|
# END: Helper Classes
|
||||||
if file.read() == DISCORD_DESKTOP_CONTENTS
|
|
||||||
puts "update_desktop_file: Desktop file matches template. Not updating"
|
|
||||||
file_needs_updating = false
|
|
||||||
|
# BEGIN: Helper Functions
|
||||||
|
|
||||||
|
def make_backup
|
||||||
|
files = {
|
||||||
|
files: Array.new,
|
||||||
|
dirs: Array.new,
|
||||||
|
symlinks: Array.new
|
||||||
|
}
|
||||||
|
|
||||||
|
[ $CONFIG[:discord_config_path].path, $CONFIG[:discord_path].path ].each do |backup_dir|
|
||||||
|
puts "make_backup: Getting Files from #{backup_dir})"
|
||||||
|
Find.find("#{backup_dir}").each{ |item|
|
||||||
|
puts("#{item} was retrieved from #{backup_dir}") if $CONFIG[:debug]
|
||||||
|
if File.file?(item)
|
||||||
|
puts("#{item} was found to be a file") if $CONFIG[:debug]
|
||||||
|
files[:files] << item
|
||||||
|
elsif File.symlink?(item)
|
||||||
|
puts("#{item} was found to be a symlink") if $CONFIG[:debug]
|
||||||
|
files[:symlinks] << item
|
||||||
|
elsif File.directory?(item)
|
||||||
|
puts("#{item} was found to be a directory") if $CONFIG[:debug]
|
||||||
|
files[:dirs] << item
|
||||||
|
end
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
Dir.chdir(Dir.home) do
|
||||||
|
puts "make_backup: Beginning backup process (OUTPUT_FILE = #{$CONFIG[:backup_filename]})"
|
||||||
|
File.open($CONFIG[:backup_filename], "wb") do |backup_file|
|
||||||
|
Zlib::GzipWriter.wrap(backup_file) do |gzip|
|
||||||
|
Gem::Package::TarWriter.new(gzip) do |tar|
|
||||||
|
|
||||||
|
puts "\nmake_backup : files : Beginning backup process for files (Count: #{files[:files].count})"
|
||||||
|
file_count = files[:files].count
|
||||||
|
files[:files].each_with_index { |file, i|
|
||||||
|
puts "make_backup : files : Backing up #{file} (#{i+1}/#{file_count})" if $CONFIG[:debug]
|
||||||
|
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) { |io|
|
||||||
|
io.write(contents)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
puts "\nmake_backup : directories : Beginning backup process for directories (Count: #{files[:dirs].count})"
|
||||||
|
dir_count = files[:dirs].count
|
||||||
|
files[:dirs].each_with_index {|dir,i|
|
||||||
|
puts "make_backup : directories : Backing up #{dir} (#{i+1}/#{dir_count})" if $CONFIG[:debug]
|
||||||
|
tar.mkdir(dir.sub("#{Dir.home}/",""), File.stat(dir).mode)
|
||||||
|
}
|
||||||
|
|
||||||
|
puts "\nmake_backup : symlinks : Beginning backup process for symlinks (Count: #{files[:symlinks].count})"
|
||||||
|
symlink_count = files[:symlinks].count
|
||||||
|
files[:symlinks].each_with_index {|symlink,i|
|
||||||
|
puts "make_backup : symlinks : Backing up #{symlink} (#{i+1}/#{symlink_count})" if $CONFIG[:debug]
|
||||||
|
tar.add_symlink(symlink.sub("#{Dir.home}/",""), File.readlink(symlink), File.lstat(symlink).mode)
|
||||||
|
}
|
||||||
|
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if file_needs_updating
|
|
||||||
File.open($CONFIG[:desktop_path], 'w') do |file|
|
|
||||||
puts "update_desktop_file: Desktop File was written"
|
|
||||||
file.write(DISCORD_DESKTOP_CONTENTS)
|
|
||||||
end
|
|
||||||
|
|
||||||
puts "update_desktop_file: Triggering desktop to install new desktop entry and refresh entries"
|
|
||||||
%x[ desktop-file-install --dir=#{$CONFIG[:local_application_install_dir].path} #{$CONFIG[:desktop_path]} ]
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# END: Helper Functions
|
# END: Helper Functions
|
||||||
@@ -186,27 +312,34 @@ end
|
|||||||
|
|
||||||
# BEGIN: Work
|
# BEGIN: Work
|
||||||
|
|
||||||
local_version = get_local_version
|
installer = Installer.new
|
||||||
remote_version = get_remote_version
|
|
||||||
|
|
||||||
case $CONFIG[:action]
|
case $CONFIG[:action]
|
||||||
|
|
||||||
when 'install'
|
when 'install'
|
||||||
if local_version < remote_version
|
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"
|
||||||
puts "-------------------------------------"
|
puts "-------------------------------------"
|
||||||
puts " Remote Version: #{remote_version.to_s}"
|
puts " Remote Version: #{installer.remote_version.to_s}"
|
||||||
puts " Local Version: #{local_version.to_s}"
|
puts " Local Version: #{installer.local_version.to_s}"
|
||||||
puts " Needs Updating: #{(remote_version > local_version) ? "Yes" : "No"}"
|
puts " Needs Updating: #{installer.needs_update? ? "Yes" : "No"}"
|
||||||
|
|
||||||
|
when 'backup'
|
||||||
|
make_backup
|
||||||
|
|
||||||
|
when 'update-desktop'
|
||||||
|
DesktopFile.new.update
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# END: Work
|
# END: Work
|
||||||
|
Reference in New Issue
Block a user