Class: Licensed::Dependency

Inherits:
Licensee::Projects::FSProject
  • Object
show all
Defined in:
lib/licensed/dependency.rb

Direct Known Subclasses

Sources::Manifest::Dependency

Constant Summary collapse

/(AUTHORS|NOTICE|LEGAL)(?:\..*)?\z/i

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, version:, path:, search_root: nil, metadata: {}, errors: []) ⇒ Dependency

Create a new project dependency

name - unique dependency name version - dependency version path - absolute file path to the dependency, to find license contents search_root - (optional) the root location to search for dependency license contents metadata - (optional) additional dependency data to cache errors - (optional) errors encountered when evaluating dependency

Returns a new dependency object. Dependency metadata and license contents are available if no errors are set on the dependency.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/licensed/dependency.rb', line 23

def initialize(name:, version:, path:, search_root: nil, metadata: {}, errors: [])
  # check the path for default errors if no other errors
  # were found when loading the dependency
  if errors.empty? && path.to_s.empty?
    errors.push("dependency path not found")
  end

  @name = name
  @version = version
  @metadata = 
  @errors = errors

  # if there are any errors, don't evaluate any dependency contents
  return if errors.any?

  # enforcing absolute paths makes life much easier when determining
  # an absolute file path in #notices
  if !Pathname.new(path).absolute?
    # this is an internal error related to source implementation and
    # should be raised, not stored to be handled by reporters
    raise ArgumentError, "dependency path #{path} must be absolute"
  end

  super(path, search_root: search_root, detect_readme: true, detect_packages: true)
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



10
11
12
# File 'lib/licensed/dependency.rb', line 10

def errors
  @errors
end

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/licensed/dependency.rb', line 8

def name
  @name
end

#versionObject (readonly)

Returns the value of attribute version.



9
10
11
# File 'lib/licensed/dependency.rb', line 9

def version
  @version
end

Instance Method Details

#errors?Boolean

Returns true if the dependency has any errors, false otherwise

Returns:

  • (Boolean)


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

def errors?
  errors.any?
end

#exist?Boolean

Returns whether the dependency exists locally

Returns:

  • (Boolean)


50
51
52
53
54
55
56
# File 'lib/licensed/dependency.rb', line 50

def exist?
  # some types of dependencies won't necessarily have a path that exists,
  # but they can still find license contents between the given path and
  # the search root
  # @root is defined
  path.exist? || File.exist?(@root)
end

#license_contentsObject

Returns the license text content from all matched sources except the package file, which doesn’t contain license text.



87
88
89
90
91
92
# File 'lib/licensed/dependency.rb', line 87

def license_contents
  return [] if errors?
  matched_files.reject { |f| f == package_file }
               .group_by(&:content)
               .map { |content, files| { "sources" => content_sources(files), "text" => content } }
end

#license_keyObject

Returns a string representing the dependencys license



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

def license_key
  return "none" if errors? || !license
  license.key
end

#notice_contentsObject

Returns legal notices found at the dependency path



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

def notice_contents
  return [] if errors?
  notice_files.sort # sorted by the path
              .map { |file| { "sources" => content_sources(file), "text" => File.read(file).rstrip } }
              .select { |text| text.length > 0 } # files with content only
end

#notice_filesObject

Returns an array of file paths used to locate legal notices



103
104
105
106
107
108
109
# File 'lib/licensed/dependency.rb', line 103

def notice_files
  return [] if errors?

  Dir.glob(dir_path.join("*"))
     .grep(LEGAL_FILES_PATTERN)
     .select { |path| File.file?(path) }
end

#pathObject

Returns the location of this dependency on the local disk



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

def path
  # exposes the private method Licensee::Projects::FSProject#dir_path
  dir_path
end

#recordObject

Returns a record for this dependency including metadata and legal contents



70
71
72
73
74
75
76
77
# File 'lib/licensed/dependency.rb', line 70

def record
  return nil if errors?
  @record ||= DependencyRecord.new(
    metadata: ,
    licenses: license_contents,
    notices: notice_contents
  )
end