Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
kjellberg committed Apr 5, 2023
1 parent 43506f5 commit e7464c7
Showing 1 changed file with 45 additions and 40 deletions.
85 changes: 45 additions & 40 deletions lib/monoz/services/run_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ def success?
exit_code == 0
end

def error?
exit_code == 1
def failed?
!!success?
end
end
end
Expand All @@ -33,23 +33,13 @@ def initialize(thor_instance)
@projects = nil
@errors = []
@warnings = []
end

def success?
!errors? && !warnings?
end

def errors?
@errors.any?
end

def warnings?
@warnings.any?
@command = []
end

def call(projects, *command)
raise ArgumentError, "Missing command" if command.empty?

@command = command
if projects.is_a?(Monoz::ProjectCollection)
@projects = projects.all
elsif projects.is_a?(Monoz::Project)
Expand All @@ -58,31 +48,7 @@ def call(projects, *command)
raise "Invalid projects"
end

@projects.each do |project|
if Monoz.tty?
say "[#{project.name}] ", [:blue, :bold], nil
say command.join(" ")
else
spinner = Monoz::Spinner.new(command.join(" "), prefix: project.name).start
end

response = run_commands_in_project(project, *command)

if response.success?
spinner&.success! unless Monoz.tty?
else
spinner&.error! unless Monoz.tty?
say response.output
say "" # line break
@errors << {
project: project.name,
command: command.join(" "),
exit_code: response.exit_code,
output: response.output
}
end
end

run_commands
say ""

if errors?
Expand All @@ -93,8 +59,47 @@ def call(projects, *command)
end
end

def success?
!errors? && !warnings?
end

def errors?
@errors.any?
end

def warnings?
@warnings.any?
end

private
def run_commands_in_project(project, *command)
def run_commands
@projects.each do |project|
if Monoz.tty?
say "[#{project.name}] ", [:blue, :bold], nil
say @command.join(" ")
else
spinner = Monoz::Spinner.new(@command.join(" "), prefix: project.name).start
end

response = run_in_project(project, *@command)

if response.success?
spinner&.success! unless Monoz.tty?
else
spinner&.error! unless Monoz.tty?
say response.output
say "" # line break
@errors << {
project: project.name,
command: @command.join(" "),
exit_code: response.exit_code,
output: response.output
}
end
end
end

def run_in_project(project, *command)
raise ArgumentError, "Invalid command" if command.empty?

output = ""
Expand Down

0 comments on commit e7464c7

Please sign in to comment.