Skip to content
Merged
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
19 changes: 19 additions & 0 deletions v2/ruby/lib/terminalwire/v2/rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,25 @@ def file_exist?
end
end

# Drop-in Rails terminal mixin — the v2 equivalent of v1's `Terminalwire::Thor`.
# `include Terminalwire::V2::Rails::Thor` in your Thor CLI and it:
# * streams I/O over the v2 wire (Terminalwire::V2::Server::Thor),
# * exposes the client `session` (the shell delegator), and
# * mixes in Rails route URL helpers (root_url, *_url, *_path) so commands like
# login/browser-open can build links. The per-connection host is set by the
# handler, so the *_url helpers resolve to the host the client connected on.
# The url_helpers go in `no_commands` so Thor doesn't register them as commands.
module Thor
def self.included(base)
base.include Terminalwire::V2::Server::Thor
base.class_eval do
no_commands do
include ::Rails.application.routes.url_helpers
end
end
end
end

# Returns a Rack endpoint that serves `cli` over both protocols. Pass `v1:`/`v2:`
# to override the handlers (tests, custom adapters); by default it builds the
# stock v1 `Terminalwire::Rails::Thor` and v2 `Terminalwire::V2::Server::Rack`.
Expand Down
7 changes: 7 additions & 0 deletions v2/ruby/lib/terminalwire/v2/server/thor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ def warn(*args) = args.each { |arg| shell.context.warn(arg.to_s) }
def gets = shell.context.gets
def getpass = shell.context.getpass
def context = shell.context
# The client-side resources, exposed on the CLI instance like v1 (so a Thor
# command can call `browser.launch(url)`, `file.read(path)`, `env("HOME")`).
def browser = shell.context.browser
def file = shell.context.file
def directory = shell.context.directory
def env(name) = shell.context.env(name)
def client = shell.context
end

class Shell < ::Thor::Shell::Basic
Expand Down
Loading