require 'singleton' require 'json' require 'rubygems' class BrandsManager include Singleton def initialize brands_list_path = FilePath.brands_list @brands_list = JSON.parse(File.read(brands_list_path)) end def first_brand brands_list.first end def first_brand_key brands_list.first["key"] rescue nil end def brand_name(brand_key) brand_data(brand_key)["name"] end def brand_with_configurations(brand_key) configurations = BrandConfigurationsManager.new(brand_key).create { solaraVersion: Gem.loaded_specs['solara'].version.to_s, brand: brand_data(brand_key), configurations: configurations } end def brand_data(brand_key) brands_list.find { |brand| brand["key"] == brand_key } end def brands_list @brands_list["brands"] end def find(brand_key) brands = brands_list brands.find { |b| b["key"] == brand_key } end def brands_json @brands_list end def exists(brand_key) !brand_data(brand_key).nil? end def add_brand(brand_name, brand_key) brand = { 'key' => brand_key, 'name' => brand_name } existing_brand = brand_data(brand_key) if existing_brand update_brand(brands_list.index(existing_brand), brand) Solara.logger.debug("#{brand_name} Brand updated in the list.") else brands_list.push(brand) save_brands_list Solara.logger.debug("#{brand_name} added to the list.") end end def current_brand JSON.parse(File.read(FilePath.current_brand)) end def current_brand_content_changed(brand_key) unless is_current_brand(brand_key) return false end current_brand['content_changed'] == true end def set_current_brand_content_changed(brand_key, changed) unless is_current_brand(brand_key) return false end Solara.logger.debug("") brand = current_brand brand['content_changed'] = changed save_current_brand_date(brand) Solara.logger.debug("#{brand_key} changed saved to current_brand.json.") end def is_current_brand(brand_key) current_brand['key'] == brand_key end def save_current_brand(brand_key) brand = brand_data(brand_key) save_current_brand_date(brand) Solara.logger.debug("#{brand_key} saved as current brand.") end def save_current_brand_date(brand_data) current_brand = FilePath.current_brand # Create the file if it doesn't exist FileUtils.touch(current_brand) unless File.exist?(current_brand) File.open(current_brand, 'w') do |file| file.write(JSON.pretty_generate(brand_data)) end end def update_brand(index, new_brand) brands_list[index] = new_brand save_brands_list Solara.logger.debug("Brand updated.") end def remove_brand(index) brands_list.delete_at(index) save_brands_list Solara.logger.debug("Brand removed.") end def offboard(brand_key) index = brands_list.find_index { |brand| brand["key"] == brand_key } if index brand_dir = FilePath.brand(brand_key) remove_brand(index) FileUtils.rm_rf(brand_dir) save_brands_list Solara.logger.debug("Brand removed.") else Solara.logger.debug("Brand not found (#{brand_key}).") end end # Class method to access the singleton instance def self.instance @instance ||= new end private def save_brands_list brands_list_path = FilePath.brands_list File.open(brands_list_path, 'w') do |file| file.write(JSON.pretty_generate(@brands_list)) end end private_class_method :new end