Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.tags
TAGS
target/
*lt_client.log
*lt_client.log
13 changes: 13 additions & 0 deletions rb-src/handle_specs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,17 @@ def send_response!
client.send_response result.eval_id, message_name, result_hash
end
end

class SpecPlugin
def handle?(cmd,args)
res = (cmd == "editor.eval.ruby") && (args['name'] =~ /_spec\.rb$/)
!!res
end
def handle(id,cmd,args,client)
run = HandleSpecs::Run.new(:client => client, :eval_id => id, :args => args)
run.result.send_responses!
end

LtRuby::Plugin.register self
end
end
44 changes: 21 additions & 23 deletions rb-src/lt_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ def add(*args, &block)

logger = LOGGER_CLASS.new(LOGFILE)

load File.dirname(__FILE__) + "/plugin.rb"
load File.dirname(__FILE__) + "/handle_specs.rb"
load File.dirname(__FILE__) + "/project_file.rb"

logger.debug "Client started with command:"
logger.debug($0)
Expand All @@ -31,6 +33,7 @@ def add(*args, &block)
class LtClient < EM::Connection

include MethodSource::CodeHelpers
include LtRuby::PluginMod

attr_accessor :currentId, :eval_queue

Expand Down Expand Up @@ -59,14 +62,25 @@ def connection_completed
$stderr = LtPrinter.new(self)
self.eval_queue = ""

load_project_file!(FileUtils.pwd)
LtRuby::Plugin.setup_user_plugins!(ARGV[2])

invoke_plugin(:connection_completed)
end

def dispatch(id,cmd,args)
return if dispatch_to_plugin(id,cmd,args)
dispatch_built_in(id,cmd,args)
end

def load_project_file!(dir)
file = "#{dir}/.lighttable"
load(file) if FileTest.exist?(file)
rescue => exp
logger.error "Error loading project file #{file}: #{exp.message}"
def dispatch_built_in(id,cmd,args)
case cmd
when "editor.eval.ruby"
eval_ruby(id, args)
when "client.close"
logger.debug("Disconnecting")
close_connection
exit(0)
end
end

def receive_data(data)
Expand All @@ -84,18 +98,7 @@ def receive_data(data)

# Dispatch on cmd
if id && cmd
case cmd
when "editor.eval.ruby"
if args['name'] =~ /_spec\.rb$/
eval_spec(id,args)
else
eval_ruby(id, args)
end
when "client.close"
logger.debug("Disconnecting")
close_connection
exit(0)
end
dispatch(id,cmd,args)
else
logger.debug "Ignoring invalid input"
end
Expand Down Expand Up @@ -143,11 +146,6 @@ def run_shell(cmd)
`#{cmd} 2>&1`
end

def eval_spec(id,args)
run = HandleSpecs::Run.new(:client => self, :eval_id => id, :args => args)
run.result.send_responses!
end

def response_meta(request_meta)
result = request_meta || {}
result["start"] ||= 1
Expand Down
4 changes: 2 additions & 2 deletions rb-src/lt_client_runner.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env bash

# Usage
# lt_client_runner.sh <project root directory> <path_to_lt_client.rb> <lt_port> <lt_client_id>
# lt_client_runner.sh <project root directory> <path_to_lt_client.rb> <lt_port> <lt_client_id> <comma_plugins>


if [ -n "$LT_USE_RVM" ]
Expand All @@ -16,4 +16,4 @@ fi

cd $1

echo $2 $3 $4 | xargs ruby
echo $2 $3 $4 $5 | xargs ruby
54 changes: 54 additions & 0 deletions rb-src/plugin.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
module LtRuby
class Plugins
def plugins
@plugins ||= []
end
include Enumerable
def each(&b)
plugins.each(&b)
end
def register(plugin)
self.plugins << plugin.new
end

def invoke(method,*args)
plugins.each do |plugin|
plugin.send(method,*args) if plugin.respond_to?(method)
end
end
end

class Plugin
class << self
def plugins
@plugins ||= Plugins.new
end

def method_missing(sym,*args,&b)
plugins.send(sym,*args,&b)
end

def setup_user_plugins!(plugin_str)
plugin_str.split(",").each do |x|
logger.debug "loading plugin #{x}"
require x
end
end
end
end

module PluginMod
def invoke_plugin(method)
LtRuby::Plugin.invoke(method,:client => self)
end
def dispatch_to_plugin(id,cmd,args)
plugin = LtRuby::Plugin.plugins.find { |x| x.respond_to?("handle?") && x.handle?(cmd,args) }
if plugin
plugin.handle(id,cmd,args,self)
true
else
false
end
end
end
end
16 changes: 16 additions & 0 deletions rb-src/project_file.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module ProjectFile
class ProjectFilePlugin
def connection_completed(*args)
load_project_file! FileUtils.pwd
end

def load_project_file!(dir)
file = "#{dir}/.lighttable"
load(file) if FileTest.exist?(file)
rescue => exp
#logger.error "Error loading project file #{file}: #{exp.message}"
end

LtRuby::Plugin.register self
end
end
9 changes: 7 additions & 2 deletions rb-src/spec/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,14 @@ def eval_code(code, ops={})
client.eval_ruby ops[:id], 'code' => code
end

let(:spec_plugin) do
LtRuby::Plugin.plugins.find { |x| x.class == HandleSpecs::SpecPlugin }
end

def eval_spec(code, ops={})
ops[:id] ||= 1
client.eval_spec ops[:id], 'code' => code, 'name' => ops[:file], 'path' => (ops[:path]||ops[:file])
args = {'code' => code, 'name' => ops[:file], 'path' => (ops[:path]||ops[:file])}
spec_plugin.handle ops[:id], nil, args, client
end
end

Expand Down Expand Up @@ -101,7 +106,7 @@ def make_data(ops)
it 'receive spec file' do
data = make_data :code => "a = 42", :file => "main_spec.rb"
client.should_not_receive(:eval_ruby)
client.should_receive(:eval_spec)
spec_plugin.should_receive(:handle)
client.receive_data(data)
end
end
Loading