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
48
# 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_error = path_error(path, search_root)
    errors.push(path_error) if path_error
  end

  @name = name
  @version = version
   = 
  @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)


51
52
53
# File 'lib/licensed/dependency.rb', line 51

def errors?
  errors.any?
end

#license_contentsObject

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



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

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



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

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

#notice_contentsObject

Returns legal notices found at the dependency path



81
82
83
84
85
86
# File 'lib/licensed/dependency.rb', line 81

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



89
90
91
92
93
94
95
# File 'lib/licensed/dependency.rb', line 89

def notice_files
  return [] if errors?

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

#recordObject

Returns a record for this dependency including metadata and legal contents



56
57
58
59
60
61
62
63
# File 'lib/licensed/dependency.rb', line 56

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