class Licensed::Configuration
Constants
- DEFAULT_CONFIG_FILES
Attributes
apps[R]
An array of the applications in this licensed configuration.
Public Class Methods
load_from(path)
click to toggle source
Loads and returns a Licensed::Configuration
object from the given path. The path can be relative or absolute, and can point at a file or directory. If the path given is a directory, the directory will be searched for a ‘config.yml` file.
# File lib/licensed/configuration.rb, line 270 def self.load_from(path) config_path = Pathname.pwd.join(path) config_path = find_config(config_path) if config_path.directory? Configuration.new(parse_config(config_path)) end
new(options = {})
click to toggle source
# File lib/licensed/configuration.rb, line 276 def initialize(options = {}) @options = options apps = options.delete("apps") || [] apps << default_options.merge(options) if apps.empty? # apply a root setting to all app configurations so that it's available # when expanding app source paths apps.each { |app| app["root"] ||= options["root"] if options["root"] } apps = apps.flat_map { |app| self.class.expand_app_source_path(app) } @apps = apps.map { |app| AppConfiguration.new(app, options) } end
Private Class Methods
expand_app_source_path(app_config)
click to toggle source
# File lib/licensed/configuration.rb, line 295 def self.expand_app_source_path(app_config) # map a source_path configuration value to an array of non-empty values source_path_array = Array(app_config["source_path"]) .reject { |path| path.to_s.empty? } .compact app_root = AppConfiguration.root_for(app_config) return app_config.merge("source_path" => app_root) if source_path_array.empty? # check if the source path maps to an existing directory if source_path_array.length == 1 source_path = File.expand_path(source_path_array[0], app_root) return app_config.merge("source_path" => source_path) if Dir.exist?(source_path) end # try to expand the source path for glob patterns expanded_source_paths = source_path_array.reduce(Set.new) do |matched_paths, pattern| current_matched_paths = if pattern.start_with?("!") # if the pattern is an exclusion, remove all matching files # from the result matched_paths - Dir.glob(pattern[1..-1]) else # if the pattern is an inclusion, add all matching files # to the result matched_paths + Dir.glob(pattern) end current_matched_paths.select { |p| File.directory?(p) } end configs = expanded_source_paths.map { |path| app_config.merge("source_path" => path) } # if no directories are found for the source path, return the original config if configs.size == 0 app_config["source_path"] = app_root if app_config["source_path"].is_a?(Array) return app_config end # update configured values for name and cache_path for uniqueness. # this is only needed when values are explicitly set, AppConfiguration # will handle configurations that don't have these explicitly set configs.each do |config| dir_name = File.basename(config["source_path"]) config["name"] = "#{config["name"]}-#{dir_name}" if config["name"].is_a?(String) # if a cache_path is set and is not marked as shared, append the app name # to the end of the cache path to make a unique cache path for the app if config["cache_path"] && config["shared_cache"] != true config["cache_path"] = File.join(config["cache_path"], dir_name) end end configs end
expand_config_roots(config, config_path)
click to toggle source
Expand any roots specified in a configuration file based on the configuration files directory.
# File lib/licensed/configuration.rb, line 384 def self.expand_config_roots(config, config_path) if config["root"] == true config["root"] = File.dirname(config_path) elsif config["root"] config["root"] = File.expand_path(config["root"], File.dirname(config_path)) end if config["apps"]&.any? config["apps"].each { |app_config| expand_config_roots(app_config, config_path) } end end
find_config(directory)
click to toggle source
Find a default configuration file in the given directory. File preference is given by the order of elements in DEFAULT_CONFIG_FILES
Raises Licensed::Configuration::LoadError
if a file isn’t found
# File lib/licensed/configuration.rb, line 354 def self.find_config(directory) config_file = DEFAULT_CONFIG_FILES.map { |file| directory.join(file) } .find { |file| file.exist? } config_file || raise(LoadError, "Licensed configuration not found in #{directory}") end
parse_config(config_path)
click to toggle source
Parses the configuration given at ‘config_path` and returns the values as a Hash
Raises Licensed::Configuration::LoadError
if the file type isn’t known
# File lib/licensed/configuration.rb, line 365 def self.parse_config(config_path) return {} unless config_path.file? extension = config_path.extname.downcase.delete "." config = case extension when "json" JSON.parse(File.read(config_path)) when "yml", "yaml" YAML.load_file(config_path) else raise LoadError, "Unknown file type #{extension} for #{config_path}" end expand_config_roots(config, config_path) config end
Public Instance Methods
[](key)
click to toggle source
# File lib/licensed/configuration.rb, line 289 def [](key) @options&.fetch(key, nil) end
Private Instance Methods
default_options()
click to toggle source
# File lib/licensed/configuration.rb, line 396 def default_options # manually set a cache path without additional name { "cache_path" => AppConfiguration::DEFAULT_CACHE_PATH } end