class String

def cache_run()

def cache_run()
  command = self
  Makit::RUNNER.cache_run(Makit::RUNNER::parse_command_request(command))
end

def cache_run(timestamp = nil)

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 cache_try()

def cache_try()
  command = self
  Makit::RUNNER.cache_try(Makit::RUNNER::parse_command_request(command))
end

def cache_try(timestamp = nil)

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

def get_json_value(key)

key is a string with the key to read, e.g. "AzureAd.Authority"
Read a value from a JSON file
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

def log(filename)

def log(filename)
  #puts "Logging to file: #{filename}"

  # does the filename have a directory?

  if filename.include?("/")
    # get the directory

    directory = File.dirname(filename)
    # create the directory

    #puts "Creating directory: #{directory}"

    FileUtils.mkdir_p(directory)
  end
  command = self
  cmd =  Makit::RUNNER.run(Makit::RUNNER::parse_command_request(command))
  # write the cmd output and error to the file

  #puts "Writing to file: #{filename}"

  File.write(filename, cmd.output + cmd.error)
  # display the summary of the command

  #puts "Command: #{command}"

  #puts "Output: #{cmd.output}"

  #puts "Error: #{cmd.error}"

  Makit::RUNNER.show_command(cmd)
end

def run(args = nil)

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 set_json_value(key, value)

key is a string with the key to set, e.g. "AzureAd.Authority"
Set a value in a JSON file
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

def show(args = nil)

def show(args = nil)
  if args.nil?
    command = self
    Makit::RUNNER.show(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.show(request)
  end
end

def strip_color_codes

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)

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

def try(args = nil)

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