Sha256: dd9a990c9d1386cd4b6a5a53ada0b1980bf46f805591763abdf52cd7bfecf632

Contents?: true

Size: 1.71 KB

Versions: 11

Compression:

Stored size: 1.71 KB

Contents

# frozen_string_literal: true
require 'shopify_cli'

module ShopifyCli
  class Command < CLI::Kit::BaseCommand
    extend Feature::Set

    attr_writer :ctx
    attr_accessor :options

    class << self
      attr_writer :ctx, :task_registry

      def call(args, command_name)
        subcommand, resolved_name = subcommand_registry.lookup_command(args.first)
        if subcommand
          subcommand.ctx = @ctx
          subcommand.task_registry = @task_registry
          subcommand.call(args, resolved_name, command_name)
        else
          cmd = new(@ctx)
          cmd.options.parse(@_options, args)
          return call_help(command_name) if cmd.options.help
          run_prerequisites
          cmd.call(args, command_name)
        end
      end

      def options(&block)
        @_options = block
      end

      def subcommand(const, cmd, path = nil)
        autoload(const, path) if path
        subcommand_registry.add(->() { const_get(const) }, cmd.to_s)
      end

      def subcommand_registry
        @subcommand_registry ||= CLI::Kit::CommandRegistry.new(
          default: nil,
          contextual_resolver: nil,
        )
      end

      def prerequisite_task(*tasks)
        @prerequisite_tasks ||= []
        @prerequisite_tasks += tasks
      end

      def run_prerequisites
        (@prerequisite_tasks || []).each { |task| task_registry[task]&.call(@ctx) }
      end

      def task_registry
        @task_registry || ShopifyCli::Tasks::Registry
      end

      def call_help(*cmds)
        help = Commands::Help.new(@ctx)
        help.call(cmds, nil)
      end
    end

    def initialize(ctx = nil)
      super()
      @ctx = ctx || ShopifyCli::Context.new
      self.options = Options.new
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
shopify-cli-1.6.0 lib/shopify-cli/command.rb
shopify-cli-1.5.0 lib/shopify-cli/command.rb
shopify-cli-1.4.1 lib/shopify-cli/command.rb
shopify-cli-1.4.0 lib/shopify-cli/command.rb
shopify-cli-1.3.1 lib/shopify-cli/command.rb
shopify-cli-1.3.0 lib/shopify-cli/command.rb
shopify-cli-1.2.0 lib/shopify-cli/command.rb
shopify-cli-1.1.2 lib/shopify-cli/command.rb
shopify-cli-1.1.1 lib/shopify-cli/command.rb
shopify-cli-1.1.0 lib/shopify-cli/command.rb
shopify-cli-1.0.5 lib/shopify-cli/command.rb