module EacLauncher class Context include ::Eac::SimpleCache include ::EacRubyUtils::Console::Speaker DEFAULT_PROJECTS_ROOT = '.'.freeze DEFAULT_SETTINGS_FILE = ::File.join(ENV['HOME'], '.config', 'eac_launcher', 'settings.yml') DEFAULT_CACHE_ROOT = ::File.join(ENV['HOME'], '.cache', 'eac_launcher') class << self attr_writer :current def current @current ||= default end def default @default ||= Context.new end end attr_reader :root, :settings, :cache_root attr_accessor :publish_options def initialize(options = {}) @options = options.with_indifferent_access @root = ::EacLauncher::Paths::Logical.new(nil, build_option(:projects_root), '/') @settings = ::EacLauncher::Context::Settings.new(build_option(:settings_file)) @cache_root = build_option(:cache_root) @publish_options = { new: false, confirm: false, stereotype: nil } end def instance(name) instances.find { |i| i.name == name } end private def build_option(key) @options[key] || env_option(key) || default_option(key) end def env_option(key) ENV["EAC_LAUNCHER_#{key}".underscore.upcase] end def default_option(key) self.class.const_get("DEFAULT_#{key}".underscore.upcase) end def projects_uncached r = {} instances.each do |i| r[i.project_name] ||= [] r[i.project_name] << i end r.map { |name, instances| ::EacLauncher::Project.new(name, instances) } end def instances_uncached path_instances(root, nil) end def path_instances(path, parent_instance) on_rescued_path_instances(path) do |r| if path.project? parent_instance = ::EacLauncher::Instances::Base.instanciate(path, self, parent_instance) r << path if path.included? end r.concat(path.children.flat_map { |c| path_instances(c, parent_instance) }) end end def on_rescued_path_instances(path) r = [] begin yield(r) rescue StandardError => ex warn("#{path}: #{ex}") end r end end end