This repository has been archived by the owner on Jun 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 205
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from Shopify/prereq-refactor
more elegant solution for prerequisite tasks
- Loading branch information
Showing
9 changed files
with
103 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |