Module: Drakkon::Run

Extended by:
PlatformCompat
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/utils.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

Methods included from PlatformCompat

build_env, path_divider, run_env, windows?

Class Method Details

.args(raw = []) ⇒ Object



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

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



11
12
13
14
15
# File 'lib/drakkon/run/helpers.rb', line 11

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



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

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

No longer used def self.log_file

"#{Hub.dir}/log.txt"

end



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

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

  @dragonruby
end

.force_fonts?Boolean

Returns:

  • (Boolean)


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

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

.force_images?Boolean

Returns:

  • (Boolean)


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

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

.force_manifest?Boolean

Returns:

  • (Boolean)


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

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

.force_sounds?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/drakkon/run/helpers.rb', line 60

def self.force_sounds?
  args.include?('sounds')
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
31
32
# File 'lib/drakkon/run.rb', line 5

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

  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



6
7
8
9
# File 'lib/drakkon/run/helpers.rb', line 6

def self.index
  @index ||= build_index
  @index
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



64
65
66
# File 'lib/drakkon/run/helpers.rb', line 64

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



17
18
19
# File 'lib/drakkon/run/helpers.rb', line 17

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

.restartObject



76
77
78
79
# File 'lib/drakkon/run.rb', line 76

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



22
23
24
25
# File 'lib/drakkon/run/helpers.rb', line 22

def self.run_cmd
  ['./dragonruby', @context, *args]
  # "PATH=#{version_dir}:$PATH exec dragonruby #{@context} > #{log_file}"
end

.run_setupObject



64
65
66
67
68
69
70
# File 'lib/drakkon/run.rb', line 64

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?
  Sounds::Index.run!(force: force_sounds?) if Settings.sound_index?
  Gems::Bundle.build!(args, @context)
end

.runtimeObject



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

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

.version_dirObject



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

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

.watchObject



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

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



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

def self.watcher
  @watcher
end

.watcher!Object



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

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