Skip to content

Commit

Permalink
Merge pull request #1351 from mbj/refactor/cli
Browse files Browse the repository at this point in the history
Refactor CLI
  • Loading branch information
mbj authored Aug 8, 2022
2 parents d76ea19 + f7c8750 commit b274333
Show file tree
Hide file tree
Showing 9 changed files with 179 additions and 156 deletions.
75 changes: 40 additions & 35 deletions bin/mutant
Original file line number Diff line number Diff line change
Expand Up @@ -8,43 +8,48 @@ end

require 'mutant'

command = Mutant::CLI.parse(
Mutant::CLI.parse(
arguments: ARGV,
world: Mutant::WORLD
)
).either(
->(message) { Mutant::WORLD.stderr.puts(message); Kernel.exit(false) },
# rubocop:disable Metrics/BlockLength
lambda do |command|
status =
if command.zombie?
$stderr.puts('Running mutant zombified!')
Mutant::Zombifier.call(
namespace: :Zombie,
load_path: $LOAD_PATH,
kernel: Kernel,
pathname: Pathname,
require_highjack: Mutant::RequireHighjack
.public_method(:call)
.to_proc
.curry
.call(Kernel),
root_require: 'mutant',
includes: %w[
adamantium
anima
concord
equalizer
mprelude
mutant
unparser
variable
]
)

status =
if command.zombie?
$stderr.puts('Running mutant zombified!')
Mutant::Zombifier.call(
namespace: :Zombie,
load_path: $LOAD_PATH,
kernel: Kernel,
pathname: Pathname,
require_highjack: Mutant::RequireHighjack
.public_method(:call)
.to_proc
.curry
.call(Kernel),
root_require: 'mutant',
includes: %w[
adamantium
anima
concord
equalizer
mprelude
mutant
unparser
variable
]
)
Zombie::Mutant::CLI.parse(
arguments: ARGV,
world: Zombie::Mutant::WORLD
).from_right.call
else
command.call
end

Zombie::Mutant::CLI.parse(
arguments: ARGV,
world: Zombie::Mutant::WORLD
).call
else
command.call
Kernel.exit(status)
end

Kernel.exit(status)
# rubocop:enable Metrics/BlockLength
)
3 changes: 1 addition & 2 deletions lib/mutant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,7 @@ class Config
matcher: Matcher::Config::DEFAULT,
mutation: Mutation::Config::DEFAULT,
reporter: Reporter::CLI.build(WORLD.stdout),
requires: EMPTY_ARRAY,
zombie: false
requires: EMPTY_ARRAY
)
end # Config

Expand Down
6 changes: 2 additions & 4 deletions lib/mutant/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ module CLI
# Parse command
#
# @return [Command]
def self.parse(world:, **attributes)
Command::Root
.parse(world: world, **attributes)
.from_right { |message| Command::FailParse.new(world, message) }
def self.parse(arguments:, world:)
Command::Root.parse(arguments: arguments, world: world)
end
end # CLI
end # Mutant
46 changes: 24 additions & 22 deletions lib/mutant/cli/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ module Mutant
module CLI
# rubocop:disable Metrics/ClassLength
class Command
include AbstractType, Anima.new(:world, :main, :parent, :arguments)

include Equalizer.new(:parent, :arguments)
include AbstractType, Anima.new(:world, :main, :parent, :zombie)

OPTIONS = [].freeze
SUBCOMMANDS = [].freeze
Expand All @@ -18,21 +16,20 @@ class OptionParser < ::OptionParser
define_method(:add_officious) {}
end # OptionParser

class FailParse < self
include Concord.new(:world, :message)

def call
world.stderr.puts(message)
false
end
end

# Parse command
#
# @return [Command]
def self.parse(**attributes)
new(main: nil, parent: nil, **attributes).__send__(:parse)
#
# rubocop:disable Metrics/ParameterLists
def self.parse(arguments:, parent: nil, world:, zombie: false)
new(
main: nil,
parent: parent,
world: world,
zombie: zombie
).__send__(:parse, arguments)
end
# rubocop:enable Metrics/ParameterLists

# Command name
#
Expand Down Expand Up @@ -62,12 +59,7 @@ def full_name
[*parent&.full_name, self.class.command_name].join(' ')
end

# Test if command needs to be executed in zombie environment
#
# @return [Bool]
def zombie?
false
end
alias_method :zombie?, :zombie

abstract_method :action

Expand Down Expand Up @@ -116,7 +108,7 @@ def banner
end
end

def parse
def parse(arguments)
Either
.wrap_error(OptionParser::InvalidOption) { parser.order(arguments) }
.lmap(&method(:with_help))
Expand All @@ -129,6 +121,7 @@ def add_summary(parser)
parser.separator(nil)
end

# rubocop:disable Metrics/MethodLength
def add_global_options(parser)
parser.separator("mutant version: #{VERSION}")
parser.separator(nil)
Expand All @@ -142,6 +135,10 @@ def add_global_options(parser)
parser.on('--version', 'Print mutants version') do
capture_main { world.stdout.puts("mutant-#{VERSION}"); true }
end

parser.on('--zombie', 'Run mutant zombified') do
@zombie = true
end
end

def add_subcommands(parser)
Expand Down Expand Up @@ -178,7 +175,12 @@ def parse_subcommand(arguments)
Either::Left.new(with_help('Missing required subcommand!'))
else
find_command(command_name).bind do |command|
command.parse(**to_h, parent: self, arguments: arguments)
command.parse(
arguments: arguments,
parent: self,
world: world,
zombie: zombie
)
end
end
end
Expand Down
3 changes: 0 additions & 3 deletions lib/mutant/cli/command/environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,6 @@ def add_matcher(attribute, value)
# rubocop:disable Metrics/MethodLength
def add_environment_options(parser)
parser.separator('Environment:')
parser.on('--zombie', 'Run mutant zombified') do
set(zombie: true)
end
parser.on('-I', '--include DIRECTORY', 'Add DIRECTORY to $LOAD_PATH') do |directory|
add(:includes, directory)
end
Expand Down
7 changes: 0 additions & 7 deletions lib/mutant/cli/command/environment/run.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,6 @@ class Run < self
===============
MESSAGE

# Test if command needs to be executed in zombie environment
#
# @return [Bool]
def zombie?
@config.zombie
end

private

def action
Expand Down
8 changes: 3 additions & 5 deletions lib/mutant/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,10 @@ class Config
:matcher,
:mutation,
:reporter,
:requires,
:zombie
:requires
)

%i[fail_fast zombie].each do |name|
%i[fail_fast].each do |name|
define_method(:"#{name}?") { public_send(name) }
end

Expand Down Expand Up @@ -74,8 +73,7 @@ def merge(other)
jobs: other.jobs || jobs,
matcher: matcher.merge(other.matcher),
mutation: mutation.merge(other.mutation),
requires: requires + other.requires,
zombie: zombie || other.zombie
requires: requires + other.requires
)
end
# rubocop:enable Metrics/AbcSize
Expand Down
Loading

0 comments on commit b274333

Please sign in to comment.