Class: Licensed::Dependency

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

Constant Summary collapse

/#{File::SEPARATOR}(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.



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

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

  # 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

#additional_termsObject (readonly)

Returns the value of attribute additional_terms.



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

def additional_terms
  @additional_terms
end

#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)


55
56
57
# File 'lib/licensed/dependency.rb', line 55

def errors?
  errors.any?
end

#exist?Boolean

Returns whether the dependency exists locally

Returns:

  • (Boolean)


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

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.



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

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



69
70
71
72
# File 'lib/licensed/dependency.rb', line 69

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

#metadataObject

Returns a hash of basic metadata about the dependency - name, version, type, etc



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

def 
  {
    # can be overriden by values in @metadata
    "name" => name,
    "version" => version
  }.merge(
    @metadata
  )
end

#notice_contentsObject

Returns legal notices found at the dependency path



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

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

#project_filesObject

Override the behavior of Licensee::Projects::FSProject#project_files to include additional license terms



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

def project_files
  super + additional_license_terms_files
end

#recordObject

Returns a record for this dependency including metadata and legal contents



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

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