# File lib/guard/jobs/pry_wrapper.rb, line 63 def initialize(options) @mutex = Mutex.new @thread = nil @terminal_settings = TerminalSettings.new _setup(options) end
# File lib/guard/jobs/pry_wrapper.rb, line 84 def background _kill_pry end
# File lib/guard/jobs/pry_wrapper.rb, line 71 def foreground UI.debug "Start interactor" @terminal_settings.save _switch_to_pry # TODO: rename :stopped to continue _killed? ? :stopped : :exit ensure UI.reset_line UI.debug "Interactor was stopped or killed" @terminal_settings.restore end
# File lib/guard/jobs/pry_wrapper.rb, line 88 def handle_interrupt thread = @thread fail Interrupt unless thread thread.raise Interrupt end
Add Pry hooks:
Load `~/.guardrc` within each new Pry session.
Load project's `.guardrc` within each new Pry session.
Restore prompt after each evaluation.
# File lib/guard/jobs/pry_wrapper.rb, line 153 def _add_hooks(options) _add_load_guard_rc_hook(Pathname(options[:guard_rc] || GUARD_RC)) _add_load_project_guard_rc_hook(Pathname.pwd + ".guardrc") _add_restore_visibility_hook if @terminal_settings.configurable? end
Add a `when_started` hook that loads a global .guardrc if it exists.
# File lib/guard/jobs/pry_wrapper.rb, line 161 def _add_load_guard_rc_hook(guard_rc) Pry.config.hooks.add_hook :when_started, :load_guard_rc do guard_rc.expand_path.tap { |p| load p if p.exist? } end end
Add a `when_started` hook that loads a project .guardrc if it exists.
# File lib/guard/jobs/pry_wrapper.rb, line 169 def _add_load_project_guard_rc_hook(guard_rc) Pry.config.hooks.add_hook :when_started, :load_project_guard_rc do load guard_rc if guard_rc.exist? end end
Add a `after_eval` hook that restores visibility after a command is eval.
# File lib/guard/jobs/pry_wrapper.rb, line 177 def _add_restore_visibility_hook Pry.config.hooks.add_hook :after_eval, :restore_visibility do @terminal_settings.echo end end
# File lib/guard/jobs/pry_wrapper.rb, line 288 def _clip_name(target) Pry.view_clip(target) end
Configures the pry prompt to see `guard` instead of `pry`.
# File lib/guard/jobs/pry_wrapper.rb, line 262 def _configure_prompt Pry.config.prompt = [_prompt(">"), _prompt("*")] end
Creates command aliases for the commands: `help`, `reload`, `change`, `scope`, `notification`, `pause`, `exit` and `quit`, which will be the first letter of the command.
# File lib/guard/jobs/pry_wrapper.rb, line 215 def _create_command_aliases SHORTCUTS.each do |command, shortcut| Pry.commands.alias_command shortcut, command.to_s end end
Create a shorthand command to run the `:run_all` action on a specific Guard group. For example, when you have a group `frontend`, then a command `frontend` is created that runs `all frontend`.
# File lib/guard/jobs/pry_wrapper.rb, line 244 def _create_group_commands Guard.state.session.groups.all.each do |group| next if group.name == :default cmd = "Run all #{ group.title }" Pry.commands.create_command group.name.to_s, cmd do group "Guard" def process Pry.run_command "all #{ match }" end end end end
Create a shorthand command to run the `:run_all` action on a specific Guard plugin. For example, when guard-rspec is available, then a command `rspec` is created that runs `all rspec`.
# File lib/guard/jobs/pry_wrapper.rb, line 226 def _create_guard_commands Guard.state.session.plugins.all.each do |guard_plugin| cmd = "Run all #{ guard_plugin.title }" Pry.commands.create_command guard_plugin.name, cmd do group "Guard" def process Pry.run_command "all #{ match }" end end end end
Creates a command that triggers the `:run_all` action when the command is empty (just pressing enter on the beginning of a line).
# File lib/guard/jobs/pry_wrapper.rb, line 205 def _create_run_all_command Pry.commands.block_command(/^$/, "Hit enter to run all") do Pry.run_command "all" end end
# File lib/guard/jobs/pry_wrapper.rb, line 118 def _kill_pry @mutex.synchronize do unless @thread.nil? @thread.kill @thread = nil # set to nil so we know we were killed end end end
# File lib/guard/jobs/pry_wrapper.rb, line 112 def _killed? th = nil @mutex.synchronize { th = @thread } th.nil? end
Returns a proc that will return itself a string ending with the given `ending_char` when called.
# File lib/guard/jobs/pry_wrapper.rb, line 277 def _prompt(ending_char) proc do |target_self, nest_level, pry| history = pry.input_array.size process = Guard.listener.paused? ? "pause" : "guard" level = ":#{ nest_level }" unless nest_level.zero? "[#{ history }] #{ _scope_for_prompt }#{ process }" "(#{ _clip_name(target_self) })#{ level }#{ ending_char } " end end
Replaces reset defined inside of Pry with a reset that instead restarts guard.
# File lib/guard/jobs/pry_wrapper.rb, line 194 def _replace_reset_command Pry.commands.command "reset", "Reset the Guard to a clean state." do output.puts "Guard reset." exec "guard" end end
Returns the plugins scope, or the groups scope ready for display in the prompt.
# File lib/guard/jobs/pry_wrapper.rb, line 269 def _scope_for_prompt titles = Guard.state.scope.titles.join(",") titles == "all" ? "" : titles + " " end
# File lib/guard/jobs/pry_wrapper.rb, line 127 def _setup(options) Pry.config.should_load_rc = false Pry.config.should_load_local_rc = false history_file_path = options[:history_file] || HISTORY_FILE Pry.config.history.file = File.expand_path(history_file_path) _add_hooks(options) Commands::All.import Commands::Change.import Commands::Notification.import Commands::Pause.import Commands::Reload.import Commands::Show.import Commands::Scope.import _setup_commands _configure_prompt end
# File lib/guard/jobs/pry_wrapper.rb, line 183 def _setup_commands _replace_reset_command _create_run_all_command _create_command_aliases _create_guard_commands _create_group_commands end
# File lib/guard/jobs/pry_wrapper.rb, line 98 def _switch_to_pry th = nil @mutex.synchronize do unless @thread @thread = Thread.new { Pry.start } @thread.join(0.5) # give pry a chance to start th = @thread end end # check for nill, because it might've been killed between the mutex and # now th.join unless th.nil? end
# File lib/guard/jobs/pry_wrapper.rb, line 232 def process Pry.run_command "all #{ match }" end