Skip to content

Commit

Permalink
Continue the work on the new Guard::API
Browse files Browse the repository at this point in the history
Signed-off-by: Rémy Coutable <remy@rymai.me>
  • Loading branch information
rymai committed Mar 3, 2018
1 parent add7310 commit 672ed89
Show file tree
Hide file tree
Showing 17 changed files with 123 additions and 185 deletions.
37 changes: 15 additions & 22 deletions lib/guard/api.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
require "guard"
# frozen_string_literal: true

require "guard/internals/groups"

module Guard
module API
TEMPLATE_FORMAT = "%s/lib/guard/%s/templates/Guardfile"
TEMPLATE_FORMAT = "%s/lib/guard/%s/templates/Guardfile".freeze

require "guard/ui"

Expand Down Expand Up @@ -63,7 +64,7 @@ def reset_callbacks!
# @return [String]
#
def non_namespaced_classname
to_s.sub("Guard::", "")
to_s.sub(/\AGuard::/, "").sub(/::Plugin\z/, "")
end

# Returns the non-namespaced name of the plugin
Expand Down Expand Up @@ -120,13 +121,13 @@ def template(plugin_location)
#
def hook(event, *args)
hook_name = if event.is_a? Symbol
calling_method = caller[0][/`([^']*)'/, 1]
"#{ calling_method }_#{ event }"
calling_method = caller(1..1).first[/`([^']*)'/, 1]
"#{calling_method}_#{event}"
else
event
end

UI.debug "Hook :#{ hook_name } executed for #{ self.class }"
UI.debug "Hook :#{hook_name} executed for #{self.class}"

self.class.notify(self, hook_name.to_sym, *args)
end
Expand All @@ -137,17 +138,15 @@ def hook(event, *args)
# @raise [:task_has_failed] when start has failed
# @return [Object] the task result
#
def start
end
def start; end

# Called when `stop|quit|exit|s|q|e + enter` is pressed (when Guard
# quits).
#
# @raise [:task_has_failed] when stop has failed
# @return [Object] the task result
#
def stop
end
def stop; end

# Called when `reload|r|z + enter` is pressed.
# This method should be mainly used for "reload" (really!) actions like
Expand All @@ -156,8 +155,7 @@ def stop
# @raise [:task_has_failed] when reload has failed
# @return [Object] the task result
#
def reload
end
def reload; end

# Called when just `enter` is pressed
# This method should be principally used for long action like running all
Expand All @@ -166,44 +164,39 @@ def reload
# @raise [:task_has_failed] when run_all has failed
# @return [Object] the task result
#
def run_all
end
def run_all; end

# Default behaviour on file(s) changes that the Guard plugin watches.
#
# @param [Array<String>] paths the changes files or paths
# @raise [:task_has_failed] when run_on_changes has failed
# @return [Object] the task result
#
def run_on_changes(paths)
end
def run_on_changes(paths); end

# Called on file(s) additions that the Guard plugin watches.
#
# @param [Array<String>] paths the changes files or paths
# @raise [:task_has_failed] when run_on_additions has failed
# @return [Object] the task result
#
def run_on_additions(paths)
end
def run_on_additions(paths); end

# Called on file(s) modifications that the Guard plugin watches.
#
# @param [Array<String>] paths the changes files or paths
# @raise [:task_has_failed] when run_on_modifications has failed
# @return [Object] the task result
#
def run_on_modifications(paths)
end
def run_on_modifications(paths); end

# Called on file(s) removals that the Guard plugin watches.
#
# @param [Array<String>] paths the changes files or paths
# @raise [:task_has_failed] when run_on_removals has failed
# @return [Object] the task result
#
def run_on_removals(paths)
end
def run_on_removals(paths); end

# Returns the plugin's name (without "guard-").
#
Expand Down
4 changes: 2 additions & 2 deletions lib/guard/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class CLI < Thor
method_option :latency,
type: :numeric,
aliases: "-l",
banner: 'Overwrite Listen\'s default latency'
banner: "Overwrite Listen's default latency"

method_option :force_polling,
type: :boolean,
Expand All @@ -87,7 +87,7 @@ class CLI < Thor
method_option :wait_for_delay,
type: :numeric,
aliases: "-y",
banner: 'Overwrite Listen\'s default wait_for_delay'
banner: "Overwrite Listen's default wait_for_delay"

method_option :listen_on,
type: :string,
Expand Down
2 changes: 0 additions & 2 deletions lib/guard/dsl.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
require "guard/guardfile/evaluator"
require "guard/interactor"
require "guard/notifier"
require "guard/ui"
require "guard/watcher"

require "guard/deprecated/dsl" unless Guard::Config.new.strict?
require "guard"

module Guard
# The Dsl class provides the methods that are used in each `Guardfile` to
Expand Down
1 change: 1 addition & 0 deletions lib/guard/interactor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def initialize(no_interaction = false)

job_klass = interactive? ? Jobs::PryWrapper : Jobs::Sleep
@idle_job = job_klass.new(self.class.options)
@enabled = nil
end

def interactive?
Expand Down
2 changes: 0 additions & 2 deletions lib/guard/internals/scope.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
require "guard"

module Guard
# @private api
module Internals
Expand Down
2 changes: 1 addition & 1 deletion lib/guard/plugin_util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def initialize(name)
# Guard 2.0, please head over to:
# https://github.com/guard/guard/wiki/Upgrading-to-Guard-2.0
#
def initialize_plugin(options)
def initialize_plugin(options = {})
klass = plugin_class
klass = klass.const_get("Plugin") if klass.const_defined?("Plugin")

Expand Down
18 changes: 9 additions & 9 deletions spec/lib/guard/commands/all_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@

let(:state) { instance_double("Guard::Internals::State") }
let(:session) { instance_double("Guard::Internals::Session") }

class FakePry < Pry::Command
def self.output
let(:fake_pry_class) do
Class.new(Pry::Command) do
def self.output; end
end
end

Expand All @@ -25,9 +25,9 @@ def self.output
allow(state).to receive(:session).and_return(session)
allow(Guard).to receive(:state).and_return(state)

allow(FakePry).to receive(:output).and_return(output)
allow(fake_pry_class).to receive(:output).and_return(output)
allow(Pry::Commands).to receive(:create_command).with("all") do |&block|
FakePry.instance_eval(&block)
fake_pry_class.instance_eval(&block)
end

described_class.import
Expand All @@ -41,7 +41,7 @@ def self.output
expect(Guard).to receive(:async_queue_add).
with([:guard_run_all, groups: [], plugins: []])

FakePry.process
fake_pry_class.process
end
end

Expand All @@ -53,7 +53,7 @@ def self.output
expect(Guard).to receive(:async_queue_add).
with([:guard_run_all, groups: [foo_group], plugins: []])

FakePry.process("foo")
fake_pry_class.process("foo")
end
end

Expand All @@ -65,7 +65,7 @@ def self.output
expect(Guard).to receive(:async_queue_add).
with([:guard_run_all, plugins: [bar_guard], groups: []])

FakePry.process("bar")
fake_pry_class.process("bar")
end
end

Expand All @@ -77,7 +77,7 @@ def self.output
expect(output).to receive(:puts).with("Unknown scopes: baz")
expect(Guard).to_not receive(:async_queue_add)

FakePry.process("baz")
fake_pry_class.process("baz")
end
end
end
16 changes: 8 additions & 8 deletions spec/lib/guard/commands/change_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@

RSpec.describe Guard::Commands::Change do
let(:output) { instance_double(Pry::Output) }

class FakePry < Pry::Command
def self.output
let(:fake_pry_class) do
Class.new(Pry::Command) do
def self.output; end
end
end

before do
allow(FakePry).to receive(:output).and_return(output)
allow(fake_pry_class).to receive(:output).and_return(output)
allow(Pry::Commands).to receive(:create_command).with("change") do |&block|
FakePry.instance_eval(&block)
fake_pry_class.instance_eval(&block)
end

described_class.import
Expand All @@ -22,7 +22,7 @@ def self.output
expect(::Guard).to receive(:async_queue_add).
with(modified: ["foo"], added: [], removed: [])

FakePry.process("foo")
fake_pry_class.process("foo")
end
end

Expand All @@ -31,7 +31,7 @@ def self.output
expect(::Guard).to receive(:async_queue_add).
with(modified: %w(foo bar baz), added: [], removed: [])

FakePry.process("foo", "bar", "baz")
fake_pry_class.process("foo", "bar", "baz")
end
end

Expand All @@ -40,7 +40,7 @@ def self.output
expect(::Guard).to_not receive(:async_queue_add)
expect(output).to receive(:puts).with("Please specify a file.")

FakePry.process
fake_pry_class.process
end
end
end
13 changes: 7 additions & 6 deletions spec/lib/guard/commands/notification_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,24 @@

RSpec.describe Guard::Commands::Notification do
let(:output) { instance_double(Pry::Output) }

class FakePry < Pry::Command
def self.output; end
let(:fake_pry_class) do
Class.new(Pry::Command) do
def self.output; end
end
end

before do
allow(FakePry).to receive(:output).and_return(output)
allow(fake_pry_class).to receive(:output).and_return(output)
allow(Pry::Commands).to receive(:create_command).
with("notification") do |&block|
FakePry.instance_eval(&block)
fake_pry_class.instance_eval(&block)
end

described_class.import
end

it "toggles the Guard notifier" do
expect(::Guard::Notifier).to receive(:toggle)
FakePry.process
fake_pry_class.process
end
end
11 changes: 6 additions & 5 deletions spec/lib/guard/commands/pause_spec.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
require "guard/commands/pause"

RSpec.describe Guard::Commands::Pause do
class FakePry < Pry::Command
def self.output
let(:fake_pry_class) do
Class.new(Pry::Command) do
def self.output; end
end
end

before do
allow(FakePry).to receive(:output).and_return(output)
allow(fake_pry_class).to receive(:output).and_return(output)
allow(Pry::Commands).to receive(:create_command).with("pause") do |&block|
FakePry.instance_eval(&block)
fake_pry_class.instance_eval(&block)
end

described_class.import
end

it "tells Guard to pause" do
expect(::Guard).to receive(:async_queue_add).with([:guard_pause])
FakePry.process
fake_pry_class.process
end
end

0 comments on commit 672ed89

Please sign in to comment.