lib/makit/command_runner.rb in makit-0.0.21 vs lib/makit/command_runner.rb in makit-0.0.22

- old
+ new

@@ -1,32 +1,30 @@ require "English" require "open3" require "socket" require "etc" require "logger" +require_relative "mp/command_request.mp" # This module provides classes for the Makit gem. module Makit # This class provide methods running commands. # class CommandRunner attr_accessor :show_output_on_success, :log_to_artifacts, :commands + attr_accessor :debug def initialize @show_output_on_success = false - @log_to_artifacts = true + @log_to_artifacts = false @commands = [] + @debug = false end def get_cache_filename(command) - # test if the command_request is a Makit::V1::CommandRequest - #if command_request.is_a? Makit::V1::CommandRequest || command_request.is_a? Makit::V1::Command - # also replacing any path delimiters with an underscore - int_hash = Digest::SHA256.hexdigest("{command.name}.#{command.arguments.join(" ")}") - # int_hash - #int_hash = command_request.to_hash - hash_string = "#{int_hash}"[0, 8] + int_hash = Digest::SHA256.hexdigest("#{command.name}.#{command.arguments.join(" ")}") + hash_string = "#{int_hash}" cache_filename = Makit::Directories::PROJECT_ARTIFACTS + "/commands/cache/#{hash_string}.pb" # create the directory if it does not already exist FileUtils.mkdir_p(File.dirname(cache_filename)) cache_filename @@ -35,35 +33,39 @@ # if there is a matching cached command result, that then the specified timestamp, # then return the cached result # otherwise run the command and save the result to a cache file # then return the result def cache_run(command_request, timestamp) - # combine the command name and arguments into a single string - # and use it to create a cache filename, making sure it is a valid filename, - # by replacing all characters that are not valid in a filename with an underscore - # also replacing any path delimiters with an underscore - int_hash = command_request.to_hash - hash_string = "#{int_hash}"[0, 8] + raise "Invalid command_request" unless command_request.is_a? Makit::V1::CommandRequest cache_filename = get_cache_filename(command_request) - - #Makit::Directories::PROJECT_ARTIFACTS + - # "/commands/#{hash_string}.pb" - #puts "cache_filename: #{cache_filename}" + if debug + puts " timestamp: #{Makit::Humanize.get_humanized_timestamp(timestamp)}" + puts " command.name: #{command_request.name}" + puts " command.arguments: #{command_request.arguments.join(" ")}" + puts " cache_filename: #{cache_filename}" + end #cache_filename = Makit::Directories::PROJECT_ARTIFACTS + "/commands/#{command_request.name}.#{command_request.arguments.join("_")}.#{timestamp.seconds}.pb" if File.exist?(cache_filename) + cache_timestamp = File.mtime(cache_filename) + if debug + puts " cache timestamp: #{Makit::Humanize.get_humanized_timestamp(cache_timestamp)}" + end #puts "cache file date: #{File.mtime(cache_filename)}" if (File.mtime(cache_filename) > timestamp) - puts " found cached command (newer than #{timestamp})".colorize(:grey) + + #puts " found cached command (newer than #{timestamp})".colorize(:grey) + #puts " cache_filename: #{cache_filename}" command = Makit::Serializer.open(cache_filename, Makit::V1::Command) - show_command(command) + show_cached_command(command) if command.exit_code != 0 abort "cached command failed: #{command.name} #{command.arguments.join(" ")}" end - return command #Makit::Serializer.open(cache_filename, Makit::V1::Command) + return command else - #puts "cache_filename exists, but is older than #{timestamp}" + puts " cache_filename exists, but is older than #{timestamp}" if debug + File.delete(cache_filename) end end command = run(command_request) # make sure the cache directory exists @@ -86,45 +88,42 @@ puts Makit::CommandRunner.get_command_summary(command) + " (#{command.duration.seconds} seconds)".colorize(:cyan) puts Makit::Humanize::indent_string(command.output, 2).colorize(:default) if show_output_on_success end end + def show_cached_command(command) + if command.exit_code != 0 + puts Makit::CommandRunner.get_command_summary(command) + " (exit code #{command.exit_code})".colorize(:default) + puts " directory: #{command.directory}\n" + puts " duration: #{command.duration.seconds} seconds\n" + puts Makit::Humanize::indent_string(command.output, 2) if command.output.length > 0 + puts Makit::Humanize::indent_string(command.error, 2) if command.error.length > 0 + #exit 1 if command.exit_on_error + else + puts Makit::CommandRunner.get_command_summary(command).strip_color_codes.colorize(:grey) + " (#{command.duration.seconds} seconds)".colorize(:grey) + puts Makit::Humanize::indent_string(command.output, 2).colorize(:default) if show_output_on_success + end + end + # Run a command and return a Makit::V1::Command. def run(command_request) raise "Invalid command_request" unless command_request.is_a? Makit::V1::CommandRequest command = execute(command_request) show_output = true exit_on_error = true log_to_artifacts(command) if @log_to_artifacts show_command(command) - #exit 1 if exit_on_error & command.exit_code != 1 - #if command.exit_code != 0 - # puts Makit::CommandRunner.get_command_summary(command) + " (exit code #{command.exit_code})".colorize(:default) - # puts " directory: #{command.directory}\n" - # puts " duration: #{command.duration.seconds} seconds\n" - # puts Makit::Humanize::indent_string(command.output, 2) if command.output.length > 0 - # puts Makit::Humanize::indent_string(command.error, 2) if command.error.length > 0 - # exit 1 if command_request.exit_on_error - #else - # puts Makit::CommandRunner.get_command_summary(command) + " (#{command.duration.seconds} seconds)".colorize(:cyan) - # puts Makit::Humanize::indent_string(command.output, 2).colorize(:default) if show_output_on_success - #end - + if command.exit_code != 0 + exit 1 if command_request.exit_on_error + end commands.push(command) command end def log_to_artifacts(command) - #dir = File.join(Makit::Directories::PROJECT_ARTIFACTS, "commands") - #FileUtils.mkdir_p(dir) unless Dir.exist?(dir) - #filename_friendly_timestamp = Time.now.strftime("%Y.%m.%d_%H%M%S") - #log_filename = File.join(dir, "#{filename_friendly_timestamp}.json") log_filename = get_cache_filename(command) - # serialize to protobuf json - json = command.to_json - pretty_json = JSON.pretty_generate(JSON.parse(json)) - File.write(log_filename, pretty_json) + Makit::Serializer.save_as(log_filename, command) end # Run a command and return a Makit::V1::Command. def try(args) request = parse_command_request(args)