Class: ShopifyCLI::Command

Inherits:
CLI::Kit::BaseCommand
  • Object
show all
Extended by:
Feature::Set
Defined in:
lib/shopify_cli/command.rb,
lib/shopify_cli/command/sub_command.rb,
lib/shopify_cli/command/app_sub_command.rb,
lib/shopify_cli/command/project_command.rb

Defined Under Namespace

Classes: AppSubCommand, PrerequisiteTask, ProjectCommand, SubCommand, VersionRange

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Feature::Set

hidden?, hidden_feature

Constructor Details

#initialize(ctx = nil) ⇒ Command

Returns a new instance of Command.



167
168
169
170
171
# File 'lib/shopify_cli/command.rb', line 167

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

Class Attribute Details

.ctx=(value) ⇒ Object (writeonly)

Sets the attribute ctx

Parameters:

  • value

    the value to set the attribute ctx to.



19
20
21
# File 'lib/shopify_cli/command.rb', line 19

def ctx=(value)
  @ctx = value
end

.task_registryObject



148
149
150
# File 'lib/shopify_cli/command.rb', line 148

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

Instance Attribute Details

#ctx=(value) ⇒ Object (writeonly)

Sets the attribute ctx

Parameters:

  • value

    the value to set the attribute ctx to.



15
16
17
# File 'lib/shopify_cli/command.rb', line 15

def ctx=(value)
  @ctx = value
end

#optionsObject

Returns the value of attribute options.



16
17
18
# File 'lib/shopify_cli/command.rb', line 16

def options
  @options
end

Class Method Details

.call(args, command_name) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/shopify_cli/command.rb', line 21

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.drop(1), resolved_name, command_name)
  else
    cmd = new(@ctx)
    cmd.options.parse(@_options, args)
    return call_help(command_name) if cmd.options.help
    check_ruby_version
    check_node_version
    run_prerequisites
    cmd.call(args, command_name)
  end
rescue OptionParser::InvalidOption => error
  arg = error.args.first
  store_name = arg.match(/\A--(?<store_name>.*\.myshopify\.com)\z/)&.[](:store_name)
  if store_name && !arg.match?(/\A--(store|shop)=/)
    # Sometimes it may look like --invalidoption=https://storename.myshopify.com
    store_name = store_name.sub(%r{\A(.*=)?(https?://)?}, "")
    raise ShopifyCLI::Abort,
      @ctx.message("core.errors.option_parser.invalid_option_store_equals", arg, store_name)
  end
  raise ShopifyCLI::Abort, @ctx.message("core.errors.option_parser.invalid_option", arg)
rescue OptionParser::MissingArgument => error
  arg = error.args.first
  raise ShopifyCLI::Abort, @ctx.message("core.errors.option_parser.missing_argument", arg)
end

.call_help(*cmds) ⇒ Object



152
153
154
155
# File 'lib/shopify_cli/command.rb', line 152

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

.check_node_versionObject



109
110
111
112
113
114
115
116
117
# File 'lib/shopify_cli/command.rb', line 109

def check_node_version
  return unless @compatible_node_range

  check_version(
    Environment.node_version,
    range: @compatible_node_range,
    runtime: "Node"
  )
end

.check_ruby_versionObject



87
88
89
90
91
92
93
# File 'lib/shopify_cli/command.rb', line 87

def check_ruby_version
  check_version(
    Environment.ruby_version,
    range: @compatible_ruby_range,
    runtime: "Ruby"
  )
end

.check_version(version, range:, runtime:, context: Context.new) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/shopify_cli/command.rb', line 119

def check_version(version, range:, runtime:, context: Context.new)
  return if Environment.test? || Environment.run_as_subprocess?
  return if range.nil?

  version_without_pre_nor_build = Utilities.version_dropping_pre_and_build(version)
  is_higher_than_bottom = version_without_pre_nor_build >= Utilities.version_dropping_pre_and_build(range.from)
  is_lower_than_top = version_without_pre_nor_build < Utilities.version_dropping_pre_and_build(range.to)
  return if is_higher_than_bottom && is_lower_than_top

  context.warn("Your environment #{runtime} version, #{version},"\
    " is outside of the range supported by the CLI,"\
    " #{range.from}..<#{range.to},"\
    " and might cause incompatibility issues.")
rescue StandardError => error
  ExceptionReporter.report_error_silently(error)
end

.options(&block) ⇒ Object



52
53
54
55
56
57
58
59
# File 'lib/shopify_cli/command.rb', line 52

def options(&block)
  existing_options = @_options
  # We prevent new options calls to override existing blocks by nesting them.
  @_options = ->(parser, flags) {
    existing_options&.call(parser, flags)
    block.call(parser, flags)
  }
end

.prerequisite_task(*tasks_without_args, **tasks_with_args) ⇒ Object



136
137
138
139
140
# File 'lib/shopify_cli/command.rb', line 136

def prerequisite_task(*tasks_without_args, **tasks_with_args)
  @prerequisite_tasks ||= []
  @prerequisite_tasks += tasks_without_args.map { |t| PrerequisiteTask.new(t) }
  @prerequisite_tasks += tasks_with_args.map { |t, args| PrerequisiteTask.new(t, args) }
end

.recommend_default_node_rangeObject



102
103
104
105
106
107
# File 'lib/shopify_cli/command.rb', line 102

def recommend_default_node_range
  recommend_node(
    from: Constants::SupportedVersions::Node::FROM,
    to: Constants::SupportedVersions::Node::TO
  )
end

.recommend_default_ruby_rangeObject



80
81
82
83
84
85
# File 'lib/shopify_cli/command.rb', line 80

def recommend_default_ruby_range
  recommend_ruby(
    from: Constants::SupportedVersions::Ruby::FROM,
    to: Constants::SupportedVersions::Ruby::TO
  )
end

.recommend_node(from:, to:) ⇒ Object



95
96
97
98
99
100
# File 'lib/shopify_cli/command.rb', line 95

def recommend_node(from:, to:)
  @compatible_node_range = VersionRange.new(
    from: Semantic::Version.new(from),
    to: Semantic::Version.new(to)
  )
end

.recommend_ruby(from:, to:) ⇒ Object



73
74
75
76
77
78
# File 'lib/shopify_cli/command.rb', line 73

def recommend_ruby(from:, to:)
  @compatible_ruby_range = VersionRange.new(
    from: Semantic::Version.new(from),
    to: Semantic::Version.new(to)
  )
end

.run_prerequisitesObject



142
143
144
145
146
# File 'lib/shopify_cli/command.rb', line 142

def run_prerequisites
  (@prerequisite_tasks || []).each do |task|
    task_registry[task.name]&.call(@ctx, *task.args)
  end
end

.subcommand(const, cmd, path = nil) ⇒ Object



61
62
63
64
# File 'lib/shopify_cli/command.rb', line 61

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

.subcommand_registryObject



66
67
68
69
70
71
# File 'lib/shopify_cli/command.rb', line 66

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