Module: Spigoter::Utils

Defined in:
lib/spigoter/utils.rb

Overview

Module with some methods commons to everyone.

Author:

Class Method Summary collapse

Class Method Details

.default_opts(opts) ⇒ Object



54
55
56
57
58
59
60
# 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) ⇒ Object



7
8
9
10
11
# 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_configObject



43
44
45
46
47
48
49
50
51
52
# 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 = {}) ⇒ Object



62
63
64
65
66
67
68
# 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) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
# 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) ⇒ Object



70
71
72
73
74
75
76
77
# 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) ⇒ Object



37
38
39
40
41
# 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) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
# 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) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# 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