# frozen_string_literal: true require "json" # monkey patch String class with a run method class String def run(args = nil) if args.nil? command = self Makit::RUNNER.run(Makit::RUNNER::parse_command_request(command)) else command = self request = Makit::RUNNER.parse_args(command) if args.is_a?(Hash) args.each do |key, value| request.send("#{key}=", value) end end Makit::RUNNER.run(request) end end def cache_run() command = self Makit::RUNNER.cache_run(Makit::RUNNER::parse_command_request(command)) end def cache_run(timestamp = nil) if timestamp.nil? timestamp = Makit::GIT_FILE_INFOS.first.mtime #GIT_FILE_INFOS. Makit::Directory.get_newest_git_file_timestamp(Makit::Directories::PROJECT_ROOT) #timestamp = Makit::Timestamp.now end command = self Makit::RUNNER.cache_run(Makit::RUNNER::parse_command_request(command), timestamp) end def try(args = nil) if args.nil? command = self Makit::RUNNER.try(command) else command = self request = Makit::RUNNER.parse_args(command) if args.is_a?(Hash) args.each do |key, value| request.send("#{key}=", value) end end Makit::RUNNER.try(request) end end def cache_try() command = self Makit::RUNNER.cache_try(Makit::RUNNER::parse_command_request(command)) end def cache_try(timestamp = nil) if timestamp.nil? timestamp = Makit::GIT_FILE_INFOS.first.mtime #GIT_FILE_INFOS. Makit::Directory.get_newest_git_file_timestamp(Makit::Directories::PROJECT_ROOT) #timestamp = Makit::Timestamp.now end command = self Makit::RUNNER.cache_try(Makit::RUNNER::parse_command_request(command), timestamp) end # Read a value from a JSON file # key is a string with the key to read, e.g. "AzureAd.Authority" def get_json_value(key) json = File.read(self) data = JSON.parse(json) # key delimiter is '.' so we can access nested keys key.split(".").each do |k| data = data[k] end data end alias_method :retrieve, :get_json_value # Set a value in a JSON file # key is a string with the key to set, e.g. "AzureAd.Authority" def set_json_value(key, value) file = File.read(self) data = JSON.parse(file) keys = key.split(".") current = data # Traverse and create any missing keys keys[0..-2].each do |k| current[k] ||= {} # Create a new hash if the key doesn't exist current = current[k] end # Set the value for the final key current[keys[-1]] = value # Write the JSON back to the file File.write(self, JSON.pretty_generate(data)) end # Alias for set_json_value alias_method :assign, :set_json_value def strip_color_codes # Regular expression to remove ANSI color codes cleaned_content = self.gsub(/\e\[[\d;]+m/, "") cleaned_content end def to_lines(max_length = 80, indent_length = 5) if (self.length <= max_length) return self else indent = " " * indent_length words = self.split(" ") lines = [] line = "" words.each do |word| if ((line + word).length > max_length) lines << line line = indent + word else if (line.length == 0) line = word else line += " " + word end end end lines << line return lines.join("\n") end end end