Skip to content

Commit

Permalink
Add text mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Nu-hin committed Dec 25, 2024
1 parent 8e6e858 commit 9cccb8e
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 18 deletions.
4 changes: 4 additions & 0 deletions lib/remote_ruby/connection_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ def initialize(**args); end
def open(_code)
raise NotImplementedError
end

def connection_name
"#{self.class.name} "
end
end
end

Expand Down
5 changes: 4 additions & 1 deletion lib/remote_ruby/connection_adapter/cache_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ module RemoteRuby
# An adapter which takes stdout and stderr from files and ignores
# all stdin. Only used to read from cache.
class CacheAdapter < ConnectionAdapter
def initialize(cache_path:)
attr_reader :connection_name

def initialize(cache_path:, connection_name:)
super
@cache_path = cache_path
@connection_name = connection_name
end

def open(_code)
Expand Down
4 changes: 4 additions & 0 deletions lib/remote_ruby/connection_adapter/caching_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ def with_result_stream
end
end

def connection_name
adapter.connection_name
end

private

attr_reader :cache_path, :adapter
Expand Down
6 changes: 5 additions & 1 deletion lib/remote_ruby/connection_adapter/ssh_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def open(code)

t = Thread.new do
ssh.open_channel do |channel|
channel.exec("cd #{working_dir} && ruby #{fname}") do |ch, success|
channel.exec("cd '#{working_dir}' && ruby #{fname}") do |ch, success|
raise 'Could not execute command' unless success

ssh.listen_to(stdin_r) do |io|
Expand Down Expand Up @@ -60,6 +60,10 @@ def with_result_stream
yield StringIO.new(@result)
end

def connection_name
"#{user}@#{host}:#{working_dir || '~'}> "
end

def with_temp_file(code, ssh)
fname = ''
code_channel = ssh.open_channel do |channel|
Expand Down
11 changes: 9 additions & 2 deletions lib/remote_ruby/connection_adapter/text_mode_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,18 @@

module RemoteRuby
class TextModeAdapter < ConnectionAdapter
DEFAULT_SETTINGS = {
stdout_mode: { color: :green, mode: :italic },
stderr_mode: { color: :red, mode: :italic },
cache_mode: { color: :blue, mode: :bold },
cache_prefix: '[C] '
}

attr_reader :adapter, :stdout_prefix, :stderr_prefix, :cache_prefix, :cache_used, :stdout_mode, :stderr_mode,
:cache_mode

def initialize(
adapter:,
adapter,
stdout_prefix:,
stderr_prefix:,
cache_prefix:,
Expand All @@ -16,7 +23,7 @@ def initialize(
stderr_mode:,
cache_mode:
)
super
super()
@adapter = adapter
@stdout_prefix = stdout_prefix
@stderr_prefix = stderr_prefix
Expand Down
44 changes: 32 additions & 12 deletions lib/remote_ruby/execution_context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def initialize(**params)
@out_stream = params.delete(:out_stream) || $stdout
@err_stream = params.delete(:err_stream) || $stderr
@adapter_klass = params.delete(:adapter) || ::RemoteRuby::SSHAdapter
@text_mode = params.delete(:text_mode) || nil
@params = params

FileUtils.mkdir_p(@cache_dir)
Expand All @@ -44,7 +45,7 @@ def execute(locals = nil, &block)
private

attr_reader :params, :adapter_klass, :use_cache, :save_cache, :cache_dir,
:in_stream, :out_stream, :err_stream, :flavours
:in_stream, :out_stream, :err_stream, :flavours, :text_mode

def assign_locals(local_names, values, block)
local_names.each do |local|
Expand Down Expand Up @@ -107,20 +108,39 @@ def execute_code(ruby_code, client_locals = {})
end

def adapter(code_hash)
actual_adapter = adapter_klass.new(**params)

if use_cache && cache_exists?(code_hash)
cache_adapter(code_hash)
elsif save_cache
caching_adapter(actual_adapter, code_hash)
else
actual_adapter
end
res = adapter_klass.new(**params)

cache_mode = use_cache && cache_exists?(code_hash)

res = if cache_mode
cache_adapter(code_hash, res.connection_name)
elsif save_cache
caching_adapter(res, code_hash)
else
res
end

wrap_text_mode(res, cache_mode)
end

def cache_adapter(code_hash)
def wrap_text_mode(ad, cache_mode)
return ad unless text_mode

params = ::RemoteRuby::TextModeAdapter::DEFAULT_SETTINGS.merge(
stdout_prefix: ad.connection_name,
stderr_prefix: ad.connection_name,
cache_used: cache_mode
)

params = params.merge(text_mode) if text_mode.is_a? Hash

::RemoteRuby::TextModeAdapter.new(ad, **params)
end

def cache_adapter(code_hash, connection_name)
::RemoteRuby::CacheAdapter.new(
cache_path: cache_path(code_hash)
cache_path: cache_path(code_hash),
connection_name: connection_name
)
end

Expand Down
3 changes: 2 additions & 1 deletion spec/remote_ruby/connection_adapter/cache_adapter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
describe RemoteRuby::CacheAdapter do
subject(:adapter) do
described_class.new(
cache_path: cache_path
cache_path: cache_path,
connection_name: 'conn'
)
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
let(:result) { '' }
subject(:adapter) do
described_class.new(
adapter: base_adapter,
base_adapter,
stdout_prefix: stdout_prefix,
stderr_prefix: stderr_prefix,
cache_prefix: cache_prefix,
Expand Down

0 comments on commit 9cccb8e

Please sign in to comment.