Skip to content
This repository has been archived by the owner on Jun 1, 2023. It is now read-only.

Commit

Permalink
Merge pull request #35 from Shopify/prereq-refactor
Browse files Browse the repository at this point in the history
more elegant solution for prerequisite tasks
  • Loading branch information
tylerball authored Apr 12, 2019
2 parents 16000de + a7645e5 commit c5d621b
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 25 deletions.
3 changes: 0 additions & 3 deletions lib/shopify-cli/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ def prerequisite_tasks

def initialize(ctx = nil)
@ctx = ctx || ShopifyCli::Context.new
self.class.prerequisite_tasks.each do |_, task|
task.call(ctx)
end
end
end
end
22 changes: 22 additions & 0 deletions lib/shopify-cli/command_registry.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require 'shopify_cli'

module ShopifyCli
class CommandRegistry < CLI::Kit::CommandRegistry
def initialize(default:, contextual_resolver: nil, task_registry: nil, ctx: nil)
@ctx = ctx || ShopifyCli::Context.new
@task_registry = task_registry || ShopifyCli::Tasks::Registry.new
super(default: default, contextual_resolver: contextual_resolver)
end

private

def resolve_command(name)
resolve_prerequisite(name)
super
end

def resolve_prerequisite(name)
@task_registry[name].call(@ctx)
end
end
end
9 changes: 4 additions & 5 deletions lib/shopify-cli/commands.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@

module ShopifyCli
module Commands
class CommandRegistry < CLI::Kit::CommandRegistry
end

Registry = CommandRegistry.new(
Registry = ShopifyCli::CommandRegistry.new(
default: 'help',
contextual_resolver: nil
contextual_resolver: nil,
task_registry: ShopifyCli::Tasks::Registry
)

def self.register(const, cmd, path)
Expand All @@ -20,6 +18,7 @@ def self.register(const, cmd, path)
register :LoadDev, 'load-dev', 'shopify-cli/commands/load_dev'
register :LoadSystem, 'load-system', 'shopify-cli/commands/load_system'
register :Server, 'server', 'shopify-cli/commands/server'
register :Tunnel, 'tunnel', 'shopify-cli/commands/tunnel'
register :Update, 'update', 'shopify-cli/commands/update'
end
end
22 changes: 22 additions & 0 deletions lib/shopify-cli/commands/tunnel.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

require 'shopify_cli'

module ShopifyCli
module Commands
class Tunnel < ShopifyCli::Command
# subcommands :start, :stop

def call(_args, _name)
puts CLI::UI.fmt(self.class.help)
end

def self.help
<<~HELP
Start and manage an http tunnel.
Usage: {{command:#{ShopifyCli::TOOL_NAME} tunnel start|stop}}
HELP
end
end
end
end
3 changes: 3 additions & 0 deletions lib/shopify_cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@

# Defines register commands for init scripts
require 'shopify-cli/register'
require 'shopify-cli/tasks'
require 'shopify-cli/context'
require 'shopify-cli/command_registry'
require 'shopify-cli/commands'

# Enable stdout routing. At this point all calls to STDOUT (and STDERR) will go through this class.
Expand Down
17 changes: 1 addition & 16 deletions test/commands/command_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,13 @@ class CommandTest < MiniTest::Test
include TestHelpers::Context

def test_non_existant
command = ShopifyCli::Commands::Help.new
command = ShopifyCli::Commands::Help.new(@context)
io = capture_io do
command.call(%w(foobar), nil)
end

assert_match(/Available commands/, io.join)
end

class FakeCommand < ShopifyCli::Command
prerequisite_task :tunnel

def call(_args, _name)
@ctx.puts('command!')
end
end

def test_prerequisite_task
@context.expects(:puts).with('success!')
@context.expects(:puts).with('command!')
command = FakeCommand.new(@context)
command.call([], nil)
end
end
end
end
29 changes: 29 additions & 0 deletions test/shopify-cli/command_registry_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module ShopifyCli
class CommandRegistryTest < MiniTest::Test
include TestHelpers::Context
include TestHelpers::FakeTask

class FakeCommand < ShopifyCli::Command
prerequisite_task :fake_task
attr_accessor :ctx

def call(_args, _name)
@ctx.puts('command!')
end
end

def test_prerequisite_task
reg = ShopifyCli::CommandRegistry.new(
default: nil,
task_registry: @registry,
ctx: @context
)
reg.add(FakeCommand, :fake)
@context.expects(:puts).with('success!')
@context.expects(:puts).with('command!')
klass, _ = reg.lookup_command(:fake)
cmd = klass.new(@context)
cmd.call([], nil)
end
end
end
3 changes: 2 additions & 1 deletion test/test_helpers.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# frozen_string_literal: true
module TestHelpers
autoload :FakeFS, 'test_helpers/fake_fs'
autoload :Constants, 'test_helpers/constants'
autoload :Context, 'test_helpers/context'
autoload :FakeTask, 'test_helpers/fake_task'
autoload :FakeContext, 'test_helpers/fake_context'
autoload :FakeFS, 'test_helpers/fake_fs'
end
20 changes: 20 additions & 0 deletions test/test_helpers/fake_task.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module TestHelpers
module FakeTask
class FakeTask < ShopifyCli::Task
def call(ctx)
ctx.puts('success!')
end
end

def setup
@registry = ShopifyCli::Tasks::TaskRegistry.new
@registry.add(FakeTask, :fake)
super
end

def teardown
@registry = nil
super
end
end
end

0 comments on commit c5d621b

Please sign in to comment.