Class: Live

Inherits:
Object
  • Object
show all
Defined in:
lib/motion/project/live.rb

Instance Method Summary collapse

Constructor Details

#initializeLive

Returns a new instance of Live.



32
33
34
# File 'lib/motion/project/live.rb', line 32

def initialize
  @buffered_output = ''
end

Instance Method Details

#adieu(string) ⇒ Object



156
157
158
159
160
# File 'lib/motion/project/live.rb', line 156

def  adieu(string)
  puts
  puts string.yellow
  puts
end

#colorize_console_output(string) ⇒ Object



142
143
144
145
# File 'lib/motion/project/live.rb', line 142

def colorize_console_output(string)
  color = string.include?('Error') ? :yellow : :green
  string.gsub(/\n*(=> .*)/,"\n" + '\1'.send(color))
end

#console_attachObject



129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/motion/project/live.rb', line 129

def console_attach
  Thread.new do
    loop do
      buffer = ''
      @stdout.readpartial(4096, buffer)
      @buffered_output << buffer
      print colorize_console_output(buffer)
      STDOUT.flush
    end
  end
  wait_console
end

#console_put(input) ⇒ Object



110
111
112
113
114
115
# File 'lib/motion/project/live.rb', line 110

def console_put(input)
  wait_console
  @stdin.puts input
  @buffered_output = ''
  wait_console
end

#console_selfObject

Interactions with the ruby motion REPL



105
106
107
108
# File 'lib/motion/project/live.rb', line 105

def console_self
  wait_console
  @buffered_output[/\(.*\)>/] + ' '
end

#create_scratch_pad_if_neededObject

Helpers



87
88
89
# File 'lib/motion/project/live.rb', line 87

def create_scratch_pad_if_needed
  FileUtils.touch(scratchpad_file) unless File.exist?(scratchpad_file)
end

#dirsObject



99
100
101
# File 'lib/motion/project/live.rb', line 99

def dirs
  Dir.pwd
end

#find_changed_linesObject



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/motion/project/live.rb', line 73

def find_changed_lines
  new = scratch_pad
  if @old_code && !new.include?('#nodiff')
    delta = new.split("\n") - @old_code.split("\n")
  else
    delta = new.split("\n")
  end
  @old_code = new
  # reject comments
  delta.reject{ |l| l =~ /^\s*$/ || l =~ /^\s*#/ }
end

#runObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/motion/project/live.rb', line 36

def run
  create_scratch_pad_if_needed
  # TODO: running rake inside rake is very lame,
  # need to do something about it.
  PTY.spawn("rake", 'simulator') do |stdout, stdin, pid|
    @stdout, @stdin = stdout, stdin
    console_attach
    welcome "Welcome to liveconding"
    print console_self
    start_watching
  end
rescue PTY::ChildExited
  adieu "[LIVE] Terminating as the simulator quitted"
# TODO: need to trap interrupt to exit gracefully.
# rescue Interrupt
#   adieu "Bye Bye"
end

#scratch_padObject



91
92
93
# File 'lib/motion/project/live.rb', line 91

def scratch_pad
  File.read(scratchpad_file)
end

#scratchpad_fileObject



95
96
97
# File 'lib/motion/project/live.rb', line 95

def scratchpad_file
  'LiveScratchpad.rb'
end

#send_changesObject



62
63
64
65
66
67
68
69
70
71
# File 'lib/motion/project/live.rb', line 62

def send_changes
  code = find_changed_lines
  return if code.empty?
  puts "\n\n#{code.join("\n")}\n".magenta
  print console_self
  code.each { |l| console_put l }
  wait_console_result
  puts "\n\n Changes processed \n\n".reversed
  print console_self
end

#start_watchingObject



54
55
56
57
58
59
60
# File 'lib/motion/project/live.rb', line 54

def start_watching
  fsevent = FSEvent.new
  fsevent.watch Dir.pwd do |dirs|
    send_changes
  end
  fsevent.run
end

#wait_consoleObject



117
118
119
120
121
# File 'lib/motion/project/live.rb', line 117

def wait_console
  until @buffered_output =~ /\(.*\)[>,?]/
    # Wait
  end
end

#wait_console_resultObject



123
124
125
126
127
# File 'lib/motion/project/live.rb', line 123

def wait_console_result
  until @buffered_output =~ /=> .*/
    # Wait
  end
end

#welcome(string) ⇒ Object

Custom output



149
150
151
152
153
154
# File 'lib/motion/project/live.rb', line 149

def welcome(string)
  puts
  puts
  puts "-> #{string}".green
  puts
end