Module: Drakkon::Gems::GemHelpers

Included in:
Gem
Defined in:
lib/drakkon/gem/gem.rb,
lib/drakkon/gem/helpers/check.rb,
lib/drakkon/gem/helpers/files.rb,
lib/drakkon/gem/helpers/source.rb,
lib/drakkon/gem/helpers/general.rb,
lib/drakkon/gem/helpers/modules.rb

Overview

General Helpers for Gem Class

Instance Method Summary collapse

Instance Method Details

#config_fileObject



13
14
15
# File 'lib/drakkon/gem/helpers/general.rb', line 13

def config_file
  "#{data[:path]}/drakkon.json"
end

#do_the_check(installing: true) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/drakkon/gem/helpers/check.rb', line 5

def do_the_check(installing: true)
  unless File.exist? config_file
    LogBot.fatal('Gem', "Drakkon config file not found! #{config_file}")
    exit 0
  end

  # Validate Config Structure
  read_config

  # Valid Structure - Name
  valid_structure?

  # Validate / Find Valid Modules
  load_modules

  # Check for Conflict
  return if installing == false

  if Settings.config[:gems].key?(name)
    LogBot.fatal('Gem', 'Duplicate Gem already installed')
    exit 0
  end

  # I hate the other syntax
  :return
end

#file_editObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/drakkon/gem/helpers/files.rb', line 10

def file_edit
  # Select File
  file = prompt_file
  file_id = file.to_sym

  if data[:files].key?(file_id) && prompt.yes?("Remove File? #{file.pastel(:bright_blue)}")
    data[:files].delete file_id
    return
  end

  # Fine Weight
  weight = prompt.ask("File: #{file.pastel(:bright_blue)}, What weight?", convert: :integer, default: 10)

  # Datas
  file_data = {
    weight: weight,
    file: file
  }

  # Save
  data[:files][file_id] = file_data
end

#files_exist?(files) ⇒ Boolean

Returns:

  • (Boolean)


82
83
84
85
86
87
88
89
90
91
# File 'lib/drakkon/gem/helpers/check.rb', line 82

def files_exist?(files)
  files.all? do |file|
    if File.exist?("#{data[:path]}/#{file}.rb")
      true
    else
      LogBot.fatal('Gem', "Missing File: #{file.pastel(:red)}")
      exit 1
    end
  end
end

#load_modulesObject



57
58
59
60
61
62
63
64
65
66
# File 'lib/drakkon/gem/helpers/check.rb', line 57

def load_modules
  @modules = config[:modules].select do |mod|
    valid_mod?(mod)
  end

  return unless module_duplicates?

  LogBot.fatal('Gem', "Duplicate Modules: #{module_duplicates}")
  exit 0
end

#module_duplicatesObject

Helper to make easier to identify dupes



98
99
100
# File 'lib/drakkon/gem/helpers/check.rb', line 98

def module_duplicates
  module_index.tally.select { |_, count| count > 1 }.keys
end

#module_duplicates?Boolean

Returns:

  • (Boolean)


102
103
104
# File 'lib/drakkon/gem/helpers/check.rb', line 102

def module_duplicates?
  module_index.count != module_index.uniq.count
end

#module_indexObject



93
94
95
# File 'lib/drakkon/gem/helpers/check.rb', line 93

def module_index
  modules.map(&:name)
end

#path?(path) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
31
32
33
34
35
36
37
38
# File 'lib/drakkon/gem/helpers/source.rb', line 28

def path?(path)
  return path unless path.nil?

  loop do
    dir = prompt.ask('Local Path? (e.g. /data/gem/location)')

    return dir if File.directory?(dir)

    LogBot.fatal('Gem', "Invalid Directory / Not found! #{dir.pastel(:red)}")
  end
end

#path_filesObject



33
34
35
# File 'lib/drakkon/gem/helpers/files.rb', line 33

def path_files
  Dir["#{data[:path]}/**/*.rb"].map { |x| x.gsub("#{data[:path]}/", '') }
end

#promptObject



5
6
7
# File 'lib/drakkon/gem/helpers/general.rb', line 5

def prompt
  TTY::Prompt.new(active_color: :cyan, interrupt: :exit)
end

#prompt_fileObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/drakkon/gem/helpers/files.rb', line 37

def prompt_file
  prompt.select('Select File:', filter: true, per_page: 25) do |menu|
    path_files.each do |file|
      name = if data[:files].key? file.to_sym
               "#{file.pastel(:bright_blue)} #{''.pastel(:bright_green)}"
             else
               file
             end

      menu.choice name: name, value: file
    end
  end

  # Return
end

#prompt_modulesObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/drakkon/gem/helpers/modules.rb', line 26

def prompt_modules
  prompt.multi_select('Select Modules:', filter: true, per_page: modules.count) do |menu|
    # menu.default *data[:modules].map(&:name) if data[:modules]
    # menu.default 'Core - Daemon Core Modules'

    defaults = []

    modules.each do |mod|
      # Format
      name = mod.name.pastel(:bright_blue)
      name += " - #{mod[:description]}".pastel(:bright_black) if mod.key?(:description)

      # Preselect Previous ones if exists
      defaults.push(name) if data[:modules]&.any? { |x| x[:name] == mod&.name }

      # Choice
      menu.choice name: name, value: mod[:name]
    end

    # Defaults
    menu.default(*defaults)
  end

  # Return
end

#read_configObject



9
10
11
# File 'lib/drakkon/gem/helpers/general.rb', line 9

def read_config
  @config = JSON.parse(File.read(config_file), { symbolize_names: true })
end

#refresh_modulesObject



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/drakkon/gem/helpers/modules.rb', line 13

def refresh_modules
  read_config
  valid_structure?
  load_modules

  # Ignore if no modules
  return if data[:modules].nil? || data[:modules].empty?

  data[:modules] = data[:modules].map(&:name).map do |selected|
    modules.find { |x| x[:name] == selected }
  end
end

#select_filesObject



5
6
7
8
# File 'lib/drakkon/gem/helpers/files.rb', line 5

def select_files
  data[:files] ||= {}
  file_edit
end

#select_modulesObject



5
6
7
8
9
10
11
# File 'lib/drakkon/gem/helpers/modules.rb', line 5

def select_modules
  return if (data[:modules].nil? || data[:modules].empty?) && modules.empty?

  data[:modules] = prompt_modules.map do |selected|
    modules.find { |x| x[:name] == selected }
  end
end

#source?(source) ⇒ Boolean

Returns:

  • (Boolean)


16
17
18
19
20
# File 'lib/drakkon/gem/helpers/source.rb', line 16

def source?(source)
  return source if sources.include?(source)

  prompt.select('Gem Source?', sources, filter: true)
end

#source_setupObject



5
6
7
8
9
10
11
12
13
14
# File 'lib/drakkon/gem/helpers/source.rb', line 5

def source_setup
  @source = source?(args.shift).to_sym

  case source
  when :local
    data[:path] = path?(args.shift)
    # Using Name within package
    # @id = "#{source}--#{data[:path]}"
  end
end

#sourcesObject



22
23
24
25
26
# File 'lib/drakkon/gem/helpers/source.rb', line 22

def sources
  [
    'local'
  ]
end

#valid_mod?(mod) ⇒ Boolean

Returns:

  • (Boolean)


68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/drakkon/gem/helpers/check.rb', line 68

def valid_mod?(mod)
  unless mod.key?(:name)
    LogBot.fatal('Gem', "Invalid Mod. No name found: #{mod}")
    return false
  end

  unless files_exist?(mod[:files])
    LogBot.fatal('Gem', "Invalid Mod: Not all files found: #{mod}")
    return false
  end

  true
end

#valid_structure?Boolean

Returns:

  • (Boolean)


32
33
34
35
36
37
38
39
40
41
42
# File 'lib/drakkon/gem/helpers/check.rb', line 32

def valid_structure?
  unless config.key? :name
    LogBot.fatal('Gem', 'Name not found!')
    exit 0
  end

  @name = config[:name].to_sym

  # Validate Versioning
  valid_version?
end

#valid_version?Boolean

Returns:

  • (Boolean)


44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/drakkon/gem/helpers/check.rb', line 44

def valid_version?
  unless config.key? :version
    LogBot.fatal('Gem', 'Version not found')
    exit 0
  end

  Semantic::Version.new config[:version]
rescue StandardError => e
  LogBot.fatal('Gem',
               "Invalid Version: #{config[:version].pastel.to_s.pastel(:green)}; #{e.message.pastel(:yellow)}")
  exit 0
end