Class: Licensed::Configuration

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Configuration

Returns a new instance of Configuration.



276
277
278
279
280
281
282
283
284
285
286
287
# 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

Instance Attribute Details

#appsObject (readonly)

An array of the applications in this licensed configuration.



264
265
266
# File 'lib/licensed/configuration.rb', line 264

def apps
  @apps
end

Class Method Details

.expand_app_source_path(app_config) ⇒ Object



295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
# 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) ⇒ Object

Expand any roots specified in a configuration file based on the configuration files directory.



384
385
386
387
388
389
390
391
392
393
394
# 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) ⇒ 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



354
355
356
357
358
359
# 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

.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.



270
271
272
273
274
# 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

.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



365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
# 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

Instance Method Details

#[](key) ⇒ Object



289
290
291
# File 'lib/licensed/configuration.rb', line 289

def [](key)
  @options&.fetch(key, nil)
end