Module: Drakkon::Run

Defined in:
lib/drakkon/run.rb,
lib/drakkon/run/tty.rb,
lib/drakkon/run/helpers.rb,
lib/drakkon/run/commands/log.rb,
lib/drakkon/run/commands/images.rb,
lib/drakkon/run/commands/restart.rb

Overview

Run Command for CLI

Defined Under Namespace

Modules: Commands

Class Method Summary collapse

Class Method Details

.args(raw = []) ⇒ Object



35
36
37
38
39
# File 'lib/drakkon/run/helpers.rb', line 35

def self.args(raw = [])
  @args ||= raw

  @args
end

.backObject

Raises:

  • (Interrupt)


91
92
93
94
# File 'lib/drakkon/run/tty.rb', line 91

def self.back
  # puts 'clear'
  raise Interrupt
end

.build_indexObject



9
10
11
12
13
# File 'lib/drakkon/run/helpers.rb', line 9

def self.build_index
  Commands.public_methods.select { |x| x.to_s[0..3] == 'cmd_' }.map do |cmd|
    cmd.to_s.gsub('cmd_', '')
  end
end

.cliObject

CLI Start



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/drakkon/run/tty.rb', line 22

def self.cli
  # Initial Log Follow
  Commands.send(:cmd_tail, [])

  value ||= '' # Empty Start

  loop do
    line = reader.read_line(readline_notch, value: value)
    value = '' # Remove Afterwards

    if reader.breaker
      value = line
      puts ''
      next
    end

    break if line =~ /^exit/i

    process
    run
  end
end

.contextObject

Helper for external access



33
34
35
36
37
# File 'lib/drakkon/run.rb', line 33

def self.context
  @context ||= Dir.pwd

  @context
end

.cursorObject



104
105
106
107
108
# File 'lib/drakkon/run/tty.rb', line 104

def self.cursor
  @cursor ||= TTY::Cursor

  @cursor
end

.dragonruby(value = nil) ⇒ Object



29
30
31
32
33
# File 'lib/drakkon/run/helpers.rb', line 29

def self.dragonruby(value = nil)
  @dragonruby ||= value

  @dragonruby
end

.force_fonts?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/drakkon/run/helpers.rb', line 53

def self.force_fonts?
  args.include?('fonts')
end

.force_images?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/drakkon/run/helpers.rb', line 45

def self.force_images?
  args.include?('images')
end

.force_manifest?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/drakkon/run/helpers.rb', line 49

def self.force_manifest?
  args.include?('manifest')
end

.go!(raw) ⇒ Object

General Run



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/run.rb', line 5

def self.go!(raw)
  Settings.ready?
  args(raw)

  # Save Current Directory before changing to Version Directory
  @context = Dir.pwd

  run_setup

  watcher!

  # Yes... Run.run!
  Dir.chdir(version_dir) do
    runtime
    cli
  end

  # Finish
rescue SystemExit, Interrupt
  LogBot.info('Run', 'Exiting')
  puts
  exit
ensure
  Process.kill('TERM', dragonruby&.pid) if dragonruby
  watcher&.kill
end

.indexObject



4
5
6
7
# File 'lib/drakkon/run/helpers.rb', line 4

def self.index
  @index ||= build_index
  @index
end

.log_fileObject



25
26
27
# File 'lib/drakkon/run/helpers.rb', line 25

def self.log_file
  "#{Hub.dir}/log.txt"
end

.logsObject



110
111
112
113
114
# File 'lib/drakkon/run/tty.rb', line 110

def self.logs
  @logs ||= []

  @logs
end

.processObject

Auto/Run Process - Populate and parse: @list



97
98
99
100
101
102
# File 'lib/drakkon/run/tty.rb', line 97

def self.process
  line = reader.line
  @list = Shellwords.split line.text
rescue StandardError => e
  puts "#{'Invalid Command'.pastel(:red)}: #{e.message.pastel(:green)}"
end

.promptObject



57
58
59
# File 'lib/drakkon/run/helpers.rb', line 57

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

.readerObject



45
46
47
# File 'lib/drakkon/run/tty.rb', line 45

def self.reader
  @reader ||= reader_setup
end

.reader_setupObject



49
50
51
52
53
54
55
56
57
58
59
60
61
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
# File 'lib/drakkon/run/tty.rb', line 49

def self.reader_setup
  reader = TTY::Reader.new(history_duplicates: false, interrupt: -> { back })

  # Blank?
  reader.add_to_history ''

  # Remove Line
  reader.on(:keyctrl_u) do |_event|
    reader.line.remove reader.line.text.size
  end

  # Navigate Word Left
  reader.on(:keyctrl_left) { reader.line.move_word_left }

  # Navigate Word Right
  reader.on(:keyctrl_right) { reader.line.move_word_right }

  # Navigate Beginning
  reader.on(:keyctrl_a) { reader.line.move_to_start }

  # Navigate End
  reader.on(:keyctrl_e) { reader.line.move_to_end }

  reader.on(:keyback_tab) { back }

  # TODO: Keytab?
  # reader.on(:keytab) do
  #   process
  #   auto
  # end

  reader.instance_variable_get(:@history)

  # DEBUG PRY
  reader.on(:keyctrl_p) do |event|
    binding.pry
    # rubocop:enable Lint/Debugger
  end

  reader
end

.readline_notchObject



15
16
17
# File 'lib/drakkon/run/helpers.rb', line 15

def self.readline_notch
  "#{'drakkon'.pastel(:bright_black)} ยป "
end

.restartObject



73
74
75
76
# File 'lib/drakkon/run.rb', line 73

def self.restart
  Process.kill('TERM', dragonruby&.pid)
  runtime
end

.runObject

Command Processing



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/drakkon/run/tty.rb', line 5

def self.run
  return true if @list.empty?

  cmd = @list.shift
  if index.include? cmd
    Commands.send(:"cmd_#{cmd}", @list)
  else
    puts "Unknown Command: '#{cmd.pastel(:red)}'"
    puts
    puts "Available Commands: #{index.join(', ').pastel(:green)}"
  end
rescue StandardError => e
  LogBot.fatal('CLI Run', e.message)
  puts e.backtrace[0..4].join("\n").pastel(:red)
end

.run_cmdObject

Exec skips the weird shell stuff



20
21
22
23
# File 'lib/drakkon/run/helpers.rb', line 20

def self.run_cmd
  "PATH=#{version_dir}:$PATH exec dragonruby #{@context}"
  # "PATH=#{version_dir}:$PATH exec dragonruby #{@context} > #{log_file}"
end

.run_setupObject



62
63
64
65
66
67
# File 'lib/drakkon/run.rb', line 62

def self.run_setup
  Images::Index.run!(force: force_images?) if Settings.image_index?
  Manifest.run!(force: force_manifest?) if Settings.manifest?
  Fonts::Index.run!(force: force_fonts?) if Settings.font_index?
  Gems::Bundle.build!(args, @context)
end

.runtimeObject



69
70
71
# File 'lib/drakkon/run.rb', line 69

def self.runtime
  @dragonruby = IO.popen(run_cmd)
end

.version_dirObject



41
42
43
# File 'lib/drakkon/run/helpers.rb', line 41

def self.version_dir
  "#{Hub.dir}/#{Settings.version}"
end

.watchObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/drakkon/run.rb', line 47

def self.watch
  # ======================================
  # Watch for Updates
  # ======================================
  # Force the bundle
  files = Gems::Bundle.collect.values.flatten.map(&:path)
  files.push "#{Run.context}/sprites/" if Settings.image_index?

  Filewatcher.new(files, every: true).watch do |changes|
    sleep 1 # Sleep to prevent exceptions?
    Images::Index.run!(force: true, dir: Run.context) if changes.keys.find { |x| x.include? 'sprites/' }
    Gems::Bundle.build!(['bundle'], @context)
  end
end

.watcherObject



43
44
45
# File 'lib/drakkon/run.rb', line 43

def self.watcher
  @watcher
end

.watcher!Object



39
40
41
# File 'lib/drakkon/run.rb', line 39

def self.watcher!
  @watcher = Thread.new { watch }
end