Class: Licensed::Dependency

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

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.



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

def initialize(name:, version:, path:, search_root: nil, metadata: {}, errors: [])
  @name = name
  @version = version
  @metadata = 
  @errors = errors
  path = path.to_s
  @path = path

  # enforcing absolute paths makes life much easier when determining
  # an absolute file path in #notices
  if File.exist?(path) && !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

#pathObject (readonly)

Returns the value of attribute path.



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

def path
  @path
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)


53
54
55
# File 'lib/licensed/dependency.rb', line 53

def errors?
  errors.any?
end

#exist?Boolean

Returns whether the dependency exists locally

Returns:

  • (Boolean)


44
45
46
47
48
49
50
# File 'lib/licensed/dependency.rb', line 44

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
  File.exist?(path) || 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.



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

def license_contents
  files = matched_files.reject { |f| f == package_file }
                       .group_by(&:content)
                       .map { |content, sources| { "sources" => license_content_sources(sources), "text" => content } }

  files << generated_license_contents if files.empty?
  files.compact
end

#license_keyObject

Returns a string representing the dependencys license



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

def license_key
  return "none" unless license
  license.key
end

#notice_contentsObject

Returns legal notices found at the dependency path



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

def notice_contents
  Dir.glob(dir_path.join("*"))
     .grep(LEGAL_FILES_PATTERN)
     .select { |path| File.file?(path) }
     .sort # sorted by the path
     .map { |path| { "sources" => normalize_source_path(path), "text" => read_file_with_encoding_check(path) } }
     .select { |notice| notice["text"].length > 0 } # files with content only
end

#recordObject

Returns a record for this dependency including metadata and legal contents



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

def record
  @record ||= DependencyRecord.new(
    metadata: ,
    licenses: license_contents,
    notices: notice_contents
  )
end