Class: Licensed::Configuration
- Inherits:
-
Object
- Object
- Licensed::Configuration
- Defined in:
- lib/licensed/configuration.rb
Defined Under Namespace
Classes: LoadError
Constant Summary collapse
- DEFAULT_CONFIG_FILES =
[ ".licensed.yml".freeze, ".licensed.yaml".freeze, ".licensed.json".freeze ].freeze
Instance Attribute Summary collapse
-
#apps ⇒ Object
readonly
An array of the applications in this licensed configuration.
Class Method Summary collapse
- .expand_app_source_path(app_config) ⇒ Object
-
.expand_config_roots(config, config_path) ⇒ Object
Expand any roots specified in a configuration file based on the configuration files directory.
-
.find_config(directory) ⇒ Object
Find a default configuration file in the given directory.
-
.load_from(path) ⇒ Object
Loads and returns a Licensed::Configuration object from the given path.
-
.parse_config(config_path) ⇒ Object
Parses the configuration given at ‘config_path` and returns the values as a Hash.
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Configuration
constructor
A new instance of Configuration.
Constructor Details
#initialize(options = {}) ⇒ Configuration
Returns a new instance of Configuration.
216 217 218 219 220 221 222 223 224 225 226 |
# File 'lib/licensed/configuration.rb', line 216 def initialize( = {}) apps = .delete("apps") || [] apps << .merge() 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"] ||= ["root"] if ["root"] } apps = apps.flat_map { |app| self.class.(app) } @apps = apps.map { |app| AppConfiguration.new(app, ) } end |
Instance Attribute Details
#apps ⇒ Object (readonly)
An array of the applications in this licensed configuration.
204 205 206 |
# File 'lib/licensed/configuration.rb', line 204 def apps @apps end |
Class Method Details
.expand_app_source_path(app_config) ⇒ Object
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 |
# File 'lib/licensed/configuration.rb', line 230 def self.(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.(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 = 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 = .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) ⇒ Object
Expand any roots specified in a configuration file based on the configuration files directory.
318 319 320 321 322 323 324 325 326 327 328 |
# File 'lib/licensed/configuration.rb', line 318 def self.(config, config_path) if config["root"] == true config["root"] = File.dirname(config_path) elsif config["root"] config["root"] = File.(config["root"], File.dirname(config_path)) end if config["apps"]&.any? config["apps"].each { |app_config| (app_config, config_path) } end end |
.find_config(directory) ⇒ Object
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
288 289 290 291 292 293 |
# File 'lib/licensed/configuration.rb', line 288 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 |
.load_from(path) ⇒ Object
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.
210 211 212 213 214 |
# File 'lib/licensed/configuration.rb', line 210 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 |
.parse_config(config_path) ⇒ Object
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
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 |
# File 'lib/licensed/configuration.rb', line 299 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 (config, config_path) config end |