Class: LicenseScout::Dependency

Inherits:
Object
  • Object
show all
Defined in:
lib/license_scout/dependency.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, version, path, type) ⇒ Dependency

Returns a new instance of Dependency.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/license_scout/dependency.rb', line 31

def initialize(name, version, path, type)
  @name = name
  @version = version
  @path = path
  @type = type

  if path.nil?
    @license = LicenseScout::License.new
  elsif path =~ /^http/ || File.directory?(path)
    @license = LicenseScout::License.new(path)
  else
    raise LicenseScout::Exceptions::MissingSourceDirectory.new("Could not find the source for '#{name}' in the following directories:\n\t * #{path}")
  end

  fallbacks = LicenseScout::Config.fallbacks.send(type.to_sym).select { |f| f["name"] =~ uid_regexp }
  fallbacks.each do |fallback|
    license.add_license(fallback["license_id"], "license_scout fallback", fallback["license_file"], force: true)
  end
end

Instance Attribute Details

#licenseObject (readonly)

Returns the value of attribute license.



29
30
31
# File 'lib/license_scout/dependency.rb', line 29

def license
  @license
end

#nameObject (readonly)

Returns the value of attribute name.



21
22
23
# File 'lib/license_scout/dependency.rb', line 21

def name
  @name
end

#pathObject (readonly)

Returns the value of attribute path.



25
26
27
# File 'lib/license_scout/dependency.rb', line 25

def path
  @path
end

#typeObject (readonly)

Returns the value of attribute type.



27
28
29
# File 'lib/license_scout/dependency.rb', line 27

def type
  @type
end

#versionObject (readonly)

Returns the value of attribute version.



23
24
25
# File 'lib/license_scout/dependency.rb', line 23

def version
  @version
end

Instance Method Details

#<=>(other) ⇒ Object

Be able to sort dependencies by type, then name, then version



91
92
93
# File 'lib/license_scout/dependency.rb', line 91

def <=>(other)
  "#{type}#{name}#{version}" <=> "#{other.type}#{other.name}#{other.version}"
end

#add_license(license_id, source, contents_url = nil) ⇒ void

This method returns an undefined value.

Capture a license that was specified in metadata

Parameters:

  • license_id (String)

    The license as specified in the metadata file

  • source (String)

    Where we found the license info

  • contents_url (String) (defaults to: nil)

    Where we can find the contents of the license



72
73
74
75
# File 'lib/license_scout/dependency.rb', line 72

def add_license(license_id, source, contents_url = nil)
  LicenseScout::Log.debug("[#{type}] Adding #{license_id} license for #{name} from #{source}")
  license.add_license(license_id, source, contents_url, {})
end

#eql?(other) ⇒ Boolean

Returns Whether or not this object is equal to another one. Used for Set uniqueness.

Returns:

  • (Boolean)

    Whether or not this object is equal to another one. Used for Set uniqueness.



96
97
98
# File 'lib/license_scout/dependency.rb', line 96

def eql?(other)
  other.is_a?(self.class) && other.hash == hash
end

#exception_reasonObject



82
83
84
85
86
87
88
# File 'lib/license_scout/dependency.rb', line 82

def exception_reason
  if has_exception?
    exceptions.first.dig("reason")
  else
    nil
  end
end

#exceptionsObject



61
62
63
# File 'lib/license_scout/dependency.rb', line 61

def exceptions
  @exceptions ||= LicenseScout::Config.exceptions.send(type.to_sym).select { |e| e["name"] =~ uid_regexp }
end

#has_exception?Boolean

Determine if this dependency has an exception. Will match an exception for both the name and the name+version

Returns:

  • (Boolean)


78
79
80
# File 'lib/license_scout/dependency.rb', line 78

def has_exception?
  exceptions.any?
end

#hashInteger

Returns A hashcode that can be used to idenitfy this object. Used for Set uniqueness.

Returns:

  • (Integer)

    A hashcode that can be used to idenitfy this object. Used for Set uniqueness.



101
102
103
# File 'lib/license_scout/dependency.rb', line 101

def hash
  [type, name, version].hash
end

#uidString

Returns The UID for this dependency. Example: bundler (1.16.1).

Returns:

  • (String)

    The UID for this dependency. Example: bundler (1.16.1)



52
53
54
# File 'lib/license_scout/dependency.rb', line 52

def uid
  "#{name} (#{version})"
end

#uid_regexpRegexp

Returns The regular expression that can be used to identify this dependency.

Returns:

  • (Regexp)

    The regular expression that can be used to identify this dependency



57
58
59
# File 'lib/license_scout/dependency.rb', line 57

def uid_regexp
  Regexp.new("#{Regexp.escape(name)}(\s+\\(#{Regexp.escape(version)}\\))?")
end