Module: Drakkon::Skeleton::InstallHelpers

Included in:
Install
Defined in:
lib/drakkon/skeleton/helpers/check.rb,
lib/drakkon/skeleton/helpers/source.rb,
lib/drakkon/skeleton/helpers/general.rb

Overview

General Helpers for Gem Class

Instance Method Summary collapse

Instance Method Details

#config_fileObject



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

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

#do_the_checkObject



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

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

  # Validate Config Structure
  read_config

  # Valid Structure - Name
  valid_structure?

  # Validate / Find Valid Templates
  load_templates

  # TODO: Some other system for dyanmic reloads?
  # if Hub.config[:skeletons].key?(name)
  #   LogBot.fatal('Skeleton', "Duplicate Skeleton already installed: #{name}")
  #   exit 0
  # end

  # I hate the other syntax
  :return
end

#load_templatesObject



55
56
57
58
59
# File 'lib/drakkon/skeleton/helpers/check.rb', line 55

def load_templates
  @templates = config[:templates].select do |template|
    valid_template?(template)
  end
end

#path?(path) ⇒ Boolean

Returns:

  • (Boolean)


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

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

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

    return dir if File.directory?(dir)

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

#promptObject



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

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

#read_configObject



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

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

#source?(source) ⇒ Boolean

Returns:

  • (Boolean)


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

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

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

#source_setupObject



5
6
7
8
9
10
11
12
13
14
# File 'lib/drakkon/skeleton/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/skeleton/helpers/source.rb', line 22

def sources
  [
    'local'
  ]
end

#valid_structure?Boolean

Returns:

  • (Boolean)


30
31
32
33
34
35
36
37
38
39
40
# File 'lib/drakkon/skeleton/helpers/check.rb', line 30

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

  @name = config[:name].to_sym

  # Validate Versioning
  valid_version?
end

#valid_template?(template_name) ⇒ Boolean

rubocop:disable Metrics/CyclomaticComplexity

Returns:

  • (Boolean)


62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/drakkon/skeleton/helpers/check.rb', line 62

def valid_template?(template_name)
  # TODO: Implement other sources
  template_config_file = case source
                         when :local
                           "#{data[:path]}/#{template_name}/skeleton.json"
                         end

  template_config = JSON.parse(File.read(template_config_file), { symbolize_names: true })

  case source
  when :local
    template_config[:path] = "#{data[:path]}/#{template_name}"
  end

  unless template_config.key?(:name)
    LogBot.fatal('Skeleton', "Invalid Template. No name found: #{template_name}")
    return false
  end

  unless template_config.key?(:description)
    LogBot.fatal('Skeleton', "Invalid Template. No description found: #{template_name}")
    return false
  end

  if template_config.key?(:variables) && template_config[:variables].is_a?(Array)
    # Preserve Nest cause line is nasty
    unless template_config[:variables].all? { |x| x.is_a? Hash }
      LogBot.fatal('Skeleton', "Invalid Template Variables: #{template_name}")
      return false
    end

    :do_something_else
  end

  unless template_config.key?(:files)
    LogBot.fatal('Skeleton', "Incomplete Template. Files Not Found: #{template_name}")
    return false
  end

  if template_config[:files].empty?
    LogBot.fatal('Skeleton', "Invalid Template. Files Empty: #{template_name}")
    return false
  end

  unless template_config[:files].all? { |x| x.is_a?(Hash) }
    LogBot.fatal('Skeleton', "Invalid Template Files. Should all be hashes: #{template_name}")
    LogBot.fatal('Skeleton', "Template Details: #{template_config[:files]}")
    return false
  end

  # TODO: Potentially Other Validation
  unless template_config[:files].all? do |file|
    # Root
    full_path = "#{template_config[:path]}/#{file[:source]}"

    if File.exist?(full_path)
      true
    else
      LogBot.fatal('Skeleton', "Invalid Template File #{file}. Source File Missing: #{template_name}")
      false
    end
  end

    LogBot.fatal('Skeleton', "Invalid Template File. Source Files Missing: #{template_name}")
    return false
  end

  # TODO: Individual Validate
  # unless files_exist?(template[:files])
  #   LogBot.warn('Skeleton', "Invalid Template: Not all files found: #{template_name}")
  #   return false
  # end

  true
end

#valid_version?Boolean

Returns:

  • (Boolean)


42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/drakkon/skeleton/helpers/check.rb', line 42

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

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