Dir[File.expand_path('../scripts/*.rb', __dir__)].each { |file| require_relative file } require 'json' class AliasGenerator def initialize @output_file = FilePath.solara_aliases_sh FileManager.create_file_if_not_exist(@output_file) @readme_file = FilePath.solara_aliases_readme FileManager.create_file_if_not_exist(@readme_file) @json_file = FilePath.solara_aliases_json FileManager.create_file_if_not_exist(@json_file) @brand_aliases = {} @common_aliases = [] end def add_brand_aliases(brands) alias_templates = [ "solara_export_{brand_key}='solara export --brand_keys {brand_key}'", "solara_offboard_{brand_key}='solara offboard -k {brand_key}'", "solara_switch_{brand_key}='solara switch -k {brand_key}'", "solara_doctor_{brand_key}='solara doctor -k {brand_key}'", "solara_dashboard_{brand_key}='solara dashboard -k {brand_key}'" ] brands.each do |app| brand_key = app['key'] @brand_aliases[brand_key] = [] alias_templates.each do |template| alias_line = template.gsub('{brand_key}', brand_key) @brand_aliases[brand_key] << "alias #{alias_line}" end end end def add_common_aliases(common_aliases) @common_aliases = common_aliases.map { |alias_line| "alias #{alias_line}" } end def generate_shell_file existing_content = File.exist?(@output_file) ? File.read(@output_file) : "" new_content = [] new_content << "#!/bin/bash" if existing_content.empty? common_aliases_added = false @common_aliases.each do |alias_line| unless existing_content.include?(alias_line) new_content << "# Common Aliases" unless common_aliases_added common_aliases_added = true new_content << alias_line end end brand_aliases_added = false @brand_aliases.each do |brand_name, brand_aliases| brand_aliases_for_this_brand_added = false brand_aliases.each do |alias_line| unless existing_content.include?(alias_line) unless brand_aliases_added brand_aliases_added = true end unless brand_aliases_for_this_brand_added new_content << "" # Add a new line before each brand name new_content << "# #{brand_name}" brand_aliases_for_this_brand_added = true end new_content << alias_line end end end new_content.reject!(&:empty?) if new_content.size == 1 if new_content.any? && !(new_content.size == 1 && new_content.first == "#!/bin/bash") File.open(@output_file, 'a') do |file| file.puts new_content end Solara.logger.debug("Aliases have been appended to #{@output_file}") else Solara.logger.debug("No new aliases to add to #{@output_file}") end end def generate_readme File.open(@readme_file, 'w') do |file| file.puts "# Aliases" file.puts file.puts "This document provides an overview of all available aliases." file.puts if @common_aliases.any? file.puts "## Common Aliases" file.puts @common_aliases.each do |alias_line| alias_name = alias_line.split('=').first.strip.gsub('alias ', '') file.puts "- `#{alias_name}`" end end file.puts file.puts @brand_aliases.each do |brand_name, brand_aliases| file.puts "## #{brand_name}" file.puts brand_aliases.each do |alias_line| alias_name = alias_line.split('=').first.strip.gsub('alias ', '') file.puts "- `#{alias_name}`" end end end Solara.logger.debug("README.md has been generated in #{@readme_file}") end def save_aliases_to_json json_data = { "common_aliases" => @common_aliases, "brand_aliases" => @brand_aliases } File.open(@json_file, 'w') do |file| file.puts JSON.pretty_generate(json_data) end Solara.logger.debug("Aliases have been saved in JSON format in #{@json_file}") end end