module Spigoter::Utils
Module with some methods commons to everyone. @author Daniel Ramos Acosta <danielramosacosta@hotmail.com>
Public Class Methods
default_opts(opts)
click to toggle source
# File lib/spigoter/utils.rb, line 54 def self.default_opts(opts) opts[:build_dir] = 'build' if opts[:build_dir].nil? opts[:plugins_dir] = 'plugins' if opts[:plugins_dir].nil? opts[:javaparams] = '-Xms1G -Xmx2G' if opts[:javaparams].nil? opts[:spigot_version] = Spigoter::SPIGOT_VERSION if opts[:spigot_version].nil? opts end
download(url)
click to toggle source
# File lib/spigoter/utils.rb, line 7 def self.download(url) open(url).read rescue raise "Can't download anything from #{url}, check internet or URL?" end
fill_opts_config()
click to toggle source
# File lib/spigoter/utils.rb, line 43 def self.fill_opts_config raise "spigoter.yml doesn't exists, do 'spigoter init'" unless File.exist?('spigoter.yml') opts = loadyaml('spigoter.yml') opts = {} if opts.nil? opts['Spigoter'] = {} if opts['Spigoter'].nil? opts = Spigoter::Utils.symbolize(opts['Spigoter']) default_opts(opts) end
get_plugins(opts = {})
click to toggle source
# File lib/spigoter/utils.rb, line 62 def self.get_plugins(opts = {}) raise "spigoter.yml doesn't exists, do 'spigoter init'" unless File.exist?('plugins.yml') plugins_data = loadyaml('plugins.yml')['Plugins'] plugins_data = normalize_plugins(plugins_data) truncate_to_list(plugins_data, opts) end
loadyaml(path)
click to toggle source
# File lib/spigoter/utils.rb, line 13 def self.loadyaml(path) raise "File #{path} doesn't exists" unless File.exist?(path) opts = {} File.open(path, 'r') do |f| opts = YAML.load(f) end raise "Malformed YAML file #{path}" unless opts.class == Hash opts end
normalize_plugins(plugins)
click to toggle source
# File lib/spigoter/utils.rb, line 70 def self.normalize_plugins(plugins) plugins = symbolize(plugins) plugins.each do |key, plugin| plugins[key] = symbolize(plugin) plugins[key][:type] = plugins[key][:type].to_sym end end
symbolize(hash)
click to toggle source
# File lib/spigoter/utils.rb, line 37 def self.symbolize(hash) new_hash = {} hash.each { |k, v| new_hash[k.to_sym] = v } new_hash end
truncate_to_list(plugin, opts)
click to toggle source
# File lib/spigoter/utils.rb, line 79 def self.truncate_to_list(plugin, opts) leftovers = {} if !opts[:list].nil? && !opts[:list].empty? list = opts[:list] # If a list cames in opts, use it list.each do |key| leftovers[key] = plugin[key] end else leftovers = plugin end leftovers end
which(cmd)
click to toggle source
# File lib/spigoter/utils.rb, line 25 def self.which(cmd) # http://stackoverflow.com/questions/2108727/which-in-ruby-checking-if-program-exists-in-path-from-ruby exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : [''] ENV['PATH'].split(File::PATH_SEPARATOR).each do |path| exts.each do |ext| exe = File.join(path, "#{cmd}#{ext}") return exe if File.executable?(exe) && !File.directory?(exe) end end nil end