lib/makit/command_runner.rb in makit-0.0.2 vs lib/makit/command_runner.rb in makit-0.0.3

- old
+ new

@@ -11,48 +11,60 @@ class CommandRunner attr_accessor :show_output_on_success, :log_to_artifacts, :commands def initialize @show_output_on_success = false - @log_to_artifacts = false + @log_to_artifacts = true @commands = [] 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] + 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 + end # 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] cache_filename = Makit::Directories::PROJECT_ARTIFACTS + - "/commands/#{command_request.to_hash}.pb" + "/commands/#{hash_string}.pb" puts "cache_filename: #{cache_filename}" - #cache_filename = Makit::Directories::PROJECT_ARTIFACTS + "/commands/#{command_request.name}.#{command_request.arguments.join("_")}.#{timestamp.seconds}.pb" - if File.exist?(cache_filename) + if File.exist?(cache_filename) puts "cache file date: #{File.mtime(cache_filename)}" - if(File.mtime(cache_filename) > timestamp) + if (File.mtime(cache_filename) > timestamp) puts "cache_filename exists and is newer than #{timestamp}" return Makit::Serializer.open(cache_filename, Makit::V1::Command) else puts "cache_filename exists, but is older than #{timestamp}" end end - - - command = run(command_request) - # make sure the cache directory exists - FileUtils.mkdir_p(File.dirname(cache_filename)) - puts "saving command to cache_filename" - Makit::Serializer.save_as(cache_filename, command) - command - + command = run(command_request) + # make sure the cache directory exists + FileUtils.mkdir_p(File.dirname(cache_filename)) + puts "saving command to cache_filename" + Makit::Serializer.save_as(cache_filename, command) + command 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 @@ -76,14 +88,15 @@ 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") + #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) end @@ -267,8 +280,39 @@ if summary.length > 80 summary = summary.to_lines(80, command.name.length + 3) end summary + end + + def log_rake_commands + dir = File.join(Makit::Directories::PROJECT_ARTIFACTS, "commands") + FileUtils.mkdir_p(dir) unless Dir.exist?(dir) + + # open a text file to write to + File.open(File.join(dir, "rake.commands.txt"), "w") do |file| + #rake_commands = commands.select { |command| command.name == "rake" } + commands.each do |command| + file.puts " " + Makit::CommandRunner.get_command_summary(command).strip_color_codes + file.puts " start time: #{command.started_at}" + file.puts command.output.strip_color_codes unless command.output.strip_color_codes.strip.length == 0 + file.puts command.error.strip_color_codes unless command.error.strip_color_codes.strip.length == 0 + file.puts " " + end + end + end + def log_slowest_commands + dir = File.join(Makit::Directories::PROJECT_ARTIFACTS, "commands") + FileUtils.mkdir_p(dir) unless Dir.exist?(dir) + + # open a text file to write to + File.open(File.join(dir, "slow.commands.txt"), "w") do |file| + Makit::RUNNER.commands.sort_by { |command| (command.duration.seconds + (command.duration.nanos / 1_000_000_000.0)) }.reverse.first(5).each do |command| + # Convert to float representation + duration_in_float = command.duration.seconds + (command.duration.nanos / 1_000_000_000.0) + #puts " #{command.name} took #{duration_in_float} seconds" + file.puts " " + Makit::CommandRunner.get_command_summary(command).strip_color_codes + " (#{command.duration.seconds} seconds)" + end + end end end # class CommandRunner end # module Makit