Class: Licensed::AppConfiguration

Inherits:
Hash
  • Object
show all
Defined in:
lib/licensed/configuration.rb

Direct Known Subclasses

Configuration

Constant Summary collapse

DEFAULT_CACHE_PATH =
".licenses".freeze
DEFAULT_CONFIG_FILES =
[
  ".licensed.yml".freeze,
  ".licensed.yaml".freeze,
  ".licensed.json".freeze
].freeze

Instance Method Summary collapse

Constructor Details

#initialize(options = {}, inherited_options = {}) ⇒ AppConfiguration

Returns a new instance of AppConfiguration.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/licensed/configuration.rb', line 13

def initialize(options = {}, inherited_options = {})
  super()

  # update order:
  # 1. anything inherited from root config
  # 2. app defaults
  # 3. explicitly configured app settings
  update(inherited_options)
  update(defaults_for(options, inherited_options))
  update(options)

  self["sources"] ||= {}
  self["reviewed"] ||= {}
  self["ignored"] ||= {}
  self["allowed"] ||= []

  verify_arg "source_path"
  verify_arg "cache_path"
end

Instance Method Details

#allow(license) ⇒ Object

Set a license as explicitly allowed



90
91
92
# File 'lib/licensed/configuration.rb', line 90

def allow(license)
  self["allowed"] << license
end

#allowed?(dependency) ⇒ Boolean

Is the license of the dependency allowed?

Returns:

  • (Boolean)


75
76
77
# File 'lib/licensed/configuration.rb', line 75

def allowed?(dependency)
  Array(self["allowed"]).include?(dependency["license"])
end

#cache_pathObject

Returns the path to the app cache directory as a Pathname



34
35
36
# File 'lib/licensed/configuration.rb', line 34

def cache_path
  Licensed::Git.repository_root.join(self["cache_path"])
end

#defaults_for(options, inherited_options) ⇒ Object



94
95
96
97
98
99
100
101
# File 'lib/licensed/configuration.rb', line 94

def defaults_for(options, inherited_options)
  name = options["name"] || File.basename(options["source_path"])
  cache_path = inherited_options["cache_path"] || DEFAULT_CACHE_PATH
  {
    "name" => name,
    "cache_path" => File.join(cache_path, name)
  }
end

#enabled?(source_type) ⇒ Boolean

Returns whether a source type is enabled

Returns:

  • (Boolean)


60
61
62
# File 'lib/licensed/configuration.rb', line 60

def enabled?(source_type)
  self["sources"].fetch(source_type, true)
end

#ignore(dependency) ⇒ Object

Ignore a dependency



80
81
82
# File 'lib/licensed/configuration.rb', line 80

def ignore(dependency)
  (self["ignored"][dependency["type"]] ||= []) << dependency["name"]
end

#ignored?(dependency) ⇒ Boolean

Is the given dependency ignored?

Returns:

  • (Boolean)


70
71
72
# File 'lib/licensed/configuration.rb', line 70

def ignored?(dependency)
  Array(self["ignored"][dependency["type"]]).include?(dependency["name"])
end

#pwdObject



43
44
45
# File 'lib/licensed/configuration.rb', line 43

def pwd
  Pathname.pwd
end

#review(dependency) ⇒ Object

Set a dependency as reviewed



85
86
87
# File 'lib/licensed/configuration.rb', line 85

def review(dependency)
  (self["reviewed"][dependency["type"]] ||= []) << dependency["name"]
end

#reviewed?(dependency) ⇒ Boolean

Is the given dependency reviewed?

Returns:

  • (Boolean)


65
66
67
# File 'lib/licensed/configuration.rb', line 65

def reviewed?(dependency)
  Array(self["reviewed"][dependency["type"]]).include?(dependency["name"])
end

#source_pathObject

Returns the path to the app source directory as a Pathname



39
40
41
# File 'lib/licensed/configuration.rb', line 39

def source_path
  Licensed::Git.repository_root.join(self["source_path"])
end

#sourcesObject

Returns an array of enabled app sources



48
49
50
51
52
53
54
55
56
57
# File 'lib/licensed/configuration.rb', line 48

def sources
  @sources ||= [
    Source::Bundler.new(self),
    Source::Bower.new(self),
    Source::Cabal.new(self),
    Source::Go.new(self),
    Source::Manifest.new(self),
    Source::NPM.new(self)
  ].select(&:enabled?)
end

#verify_arg(property) ⇒ Object



103
104
105
106
107
# File 'lib/licensed/configuration.rb', line 103

def verify_arg(property)
  return if self[property]
  raise Licensed::Configuration::LoadError,
    "App #{self["name"]} is missing required property #{property}"
end