Class: Licensed::AppConfiguration

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

Constant Summary collapse

DEFAULT_CACHE_PATH =
".licenses".freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of AppConfiguration.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/licensed/configuration.rb', line 16

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

  # update order:
  # 1. anything inherited from root config
  # 2. explicitly configured app settings
  update(inherited_options)
  update(options)
  verify_arg "source_path"

  self["sources"] ||= {}
  self["reviewed"] ||= {}
  self["ignored"] ||= {}
  self["allowed"] ||= []
  self["root"] = AppConfiguration.root_for(self)
  # defaults to the directory name of the source path if not set
  self["name"] ||= File.basename(self["source_path"])
  # setting the cache path might need a valid app name
  self["cache_path"] = detect_cache_path(options, inherited_options)
end

Class Method Details

.root_for(configuration) ⇒ Object

Returns the root for a configuration in following order of precendence:

  1. explicitly configured “root” property

  2. a found git repository root

  3. the current directory



12
13
14
# File 'lib/licensed/configuration.rb', line 12

def self.root_for(configuration)
  configuration["root"] || Licensed::Git.repository_root || Dir.pwd
end

Instance Method Details

#allow(license) ⇒ Object

Set a license as explicitly allowed



98
99
100
# File 'lib/licensed/configuration.rb', line 98

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

#allowed?(license) ⇒ Boolean

Is the license of the dependency allowed?

Returns:

  • (Boolean)


83
84
85
# File 'lib/licensed/configuration.rb', line 83

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

#cache_pathObject

Returns the path to the app cache directory as a Pathname



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

def cache_path
  root.join(self["cache_path"])
end

#enabled?(source_type) ⇒ Boolean

Returns whether a source type is enabled

Returns:

  • (Boolean)


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

def enabled?(source_type)
  # the default is false if any sources are set to true, true otherwise
  default = !self["sources"].any? { |_, enabled| enabled }
  self["sources"].fetch(source_type, default)
end

#ignore(dependency) ⇒ Object

Ignore a dependency



88
89
90
# File 'lib/licensed/configuration.rb', line 88

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

#ignored?(dependency) ⇒ Boolean

Is the given dependency ignored?

Returns:

  • (Boolean)


76
77
78
79
80
# File 'lib/licensed/configuration.rb', line 76

def ignored?(dependency)
  Array(self["ignored"][dependency["type"]]).any? do |pattern|
    File.fnmatch?(pattern, dependency["name"], File::FNM_PATHNAME | File::FNM_CASEFOLD)
  end
end

#pwdObject



52
53
54
# File 'lib/licensed/configuration.rb', line 52

def pwd
  Pathname.pwd
end

#review(dependency) ⇒ Object

Set a dependency as reviewed



93
94
95
# File 'lib/licensed/configuration.rb', line 93

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

#reviewed?(dependency) ⇒ Boolean

Is the given dependency reviewed?

Returns:

  • (Boolean)


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

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

#rootObject

Returns the path to the workspace root as a Pathname.



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

def root
  @root ||= Pathname.new(self["root"])
end

#source_pathObject

Returns the path to the app source directory as a Pathname



48
49
50
# File 'lib/licensed/configuration.rb', line 48

def source_path
  root.join(self["source_path"])
end

#sourcesObject

Returns an array of enabled app sources



57
58
59
60
61
# File 'lib/licensed/configuration.rb', line 57

def sources
  @sources ||= Licensed::Sources::Source.sources
                                        .select { |source_class| enabled?(source_class.type) }
                                        .map { |source_class| source_class.new(self) }
end